SQLite Android Bindings
Check-in [6f830dd323]
Not logged in

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

Overview
Comment:Update this project to version 3.24.0.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6f830dd323380f4e9970afea023d530b58b09304
User & Date: dan 2018-06-04 19:55:35
Context
2018-09-04
17:24
Upgrade gradle version to 3.1.4. check-in: 193bea893f user: dan tags: trunk
2018-06-04
19:55
Update this project to version 3.24.0. check-in: 6f830dd323 user: dan tags: trunk
2018-04-10
18:04
Update this project to SQLite version 3.23.1. check-in: 1064adb2bb user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.23.1.  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.1"
  1151         -#define SQLITE_VERSION_NUMBER 3023001
  1152         -#define SQLITE_SOURCE_ID      "2018-04-10 17:39:29 4bb2294022060e61de7da5c227a69ccd846ba330e31626ebcd59a94efd148b3b"
         1153  +#define SQLITE_VERSION        "3.24.0"
         1154  +#define SQLITE_VERSION_NUMBER 3024000
         1155  +#define SQLITE_SOURCE_ID      "2018-06-04 19:24:41 c7ee0833225bfd8c5ec2f9bf62b97c4e04d03bd9566366d5221ac8fb199a87ca"
  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
................................................................................
  6512   6550   ** using [sqlite3_free].
  6513   6551   ** Hence, if this variable is modified directly, either it should be
  6514   6552   ** made NULL or made to point to memory obtained from [sqlite3_malloc]
  6515   6553   ** or else the use of the [data_store_directory pragma] should be avoided.
  6516   6554   */
  6517   6555   SQLITE_API char *sqlite3_data_directory;
  6518   6556   
         6557  +/*
         6558  +** CAPI3REF: Win32 Specific Interface
         6559  +**
         6560  +** These interfaces are available only on Windows.  The
         6561  +** [sqlite3_win32_set_directory] interface is used to set the value associated
         6562  +** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
         6563  +** zValue, depending on the value of the type parameter.  The zValue parameter
         6564  +** should be NULL to cause the previous value to be freed via [sqlite3_free];
         6565  +** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
         6566  +** prior to being used.  The [sqlite3_win32_set_directory] interface returns
         6567  +** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
         6568  +** or [SQLITE_NOMEM] if memory could not be allocated.  The value of the
         6569  +** [sqlite3_data_directory] variable is intended to act as a replacement for
         6570  +** the current directory on the sub-platforms of Win32 where that concept is
         6571  +** not present, e.g. WinRT and UWP.  The [sqlite3_win32_set_directory8] and
         6572  +** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
         6573  +** sqlite3_win32_set_directory interface except the string parameter must be
         6574  +** UTF-8 or UTF-16, respectively.
         6575  +*/
         6576  +SQLITE_API int sqlite3_win32_set_directory(
         6577  +  unsigned long type, /* Identifier for directory being set or reset */
         6578  +  void *zValue        /* New value for directory being set or reset */
         6579  +);
         6580  +SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
         6581  +SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
         6582  +
         6583  +/*
         6584  +** CAPI3REF: Win32 Directory Types
         6585  +**
         6586  +** These macros are only available on Windows.  They define the allowed values
         6587  +** for the type argument to the [sqlite3_win32_set_directory] interface.
         6588  +*/
         6589  +#define SQLITE_WIN32_DATA_DIRECTORY_TYPE  1
         6590  +#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE  2
         6591  +
  6519   6592   /*
  6520   6593   ** CAPI3REF: Test For Auto-Commit Mode
  6521   6594   ** KEYWORDS: {autocommit mode}
  6522   6595   ** METHOD: sqlite3
  6523   6596   **
  6524   6597   ** ^The sqlite3_get_autocommit() interface returns non-zero or
  6525   6598   ** zero if the given database connection is or is not in autocommit mode,
................................................................................
  7244   7317     int idxFlags;              /* Mask of SQLITE_INDEX_SCAN_* flags */
  7245   7318     /* Fields below are only available in SQLite 3.10.0 and later */
  7246   7319     sqlite3_uint64 colUsed;    /* Input: Mask of columns used by statement */
  7247   7320   };
  7248   7321   
  7249   7322   /*
  7250   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.
  7251   7328   */
  7252   7329   #define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
  7253   7330   
  7254   7331   /*
  7255   7332   ** CAPI3REF: Virtual Table Constraint Operator Codes
  7256   7333   **
  7257   7334   ** These macros defined the allowed values for the
................................................................................
  8019   8096   #define SQLITE_TESTCTRL_FAULT_INSTALL            9
  8020   8097   #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
  8021   8098   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  8022   8099   #define SQLITE_TESTCTRL_ASSERT                  12
  8023   8100   #define SQLITE_TESTCTRL_ALWAYS                  13
  8024   8101   #define SQLITE_TESTCTRL_RESERVE                 14
  8025   8102   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  8026         -#define SQLITE_TESTCTRL_ISKEYWORD               16
         8103  +#define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
  8027   8104   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
  8028   8105   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  8029   8106   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
  8030   8107   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  8031   8108   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  8032   8109   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  8033   8110   #define SQLITE_TESTCTRL_BYTEORDER               22
  8034   8111   #define SQLITE_TESTCTRL_ISINIT                  23
  8035   8112   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  8036   8113   #define SQLITE_TESTCTRL_IMPOSTER                25
  8037   8114   #define SQLITE_TESTCTRL_PARSER_COVERAGE         26
  8038   8115   #define SQLITE_TESTCTRL_LAST                    26  /* Largest TESTCTRL */
  8039   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  +
  8040   8300   /*
  8041   8301   ** CAPI3REF: SQLite Runtime Status
  8042   8302   **
  8043   8303   ** ^These interfaces are used to retrieve runtime status information
  8044   8304   ** about the performance of SQLite, and optionally to reset various
  8045   8305   ** highwater marks.  ^The first argument is an integer code for
  8046   8306   ** the specific parameter to measure.  ^(Recognized integer codes
................................................................................
  9302   9562   /*
  9303   9563   ** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
  9304   9564   **
  9305   9565   ** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
  9306   9566   ** method of a [virtual table], then it returns true if and only if the
  9307   9567   ** column is being fetched as part of an UPDATE operation during which the
  9308   9568   ** column value will not change.  Applications might use this to substitute
  9309         -** a lighter-weight value to return that the corresponding [xUpdate] method
  9310         -** 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.
  9311   9571   **
  9312   9572   ** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
  9313         -** 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
  9314   9574   ** method can optionally return without setting a result, without calling
  9315   9575   ** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
  9316   9576   ** In that case, [sqlite3_value_nochange(X)] will return true for the
  9317   9577   ** same column in the [xUpdate] method.
  9318   9578   */
  9319   9579   SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
  9320   9580   
................................................................................
  9801  10061   ** to the contiguous memory representation of the database that SQLite
  9802  10062   ** is currently using for that database, or NULL if the no such contiguous
  9803  10063   ** memory representation of the database exists.  A contiguous memory
  9804  10064   ** representation of the database will usually only exist if there has
  9805  10065   ** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
  9806  10066   ** values of D and S.
  9807  10067   ** The size of the database is written into *P even if the 
  9808         -** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
        10068  +** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
  9809  10069   ** of the database exists.
  9810  10070   **
  9811  10071   ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
  9812  10072   ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
  9813  10073   ** allocation error occurs.
  9814  10074   **
  9815  10075   ** This interface is only available if SQLite is compiled with the
................................................................................
 12937  13197   #define TK_GT                              54
 12938  13198   #define TK_LE                              55
 12939  13199   #define TK_LT                              56
 12940  13200   #define TK_GE                              57
 12941  13201   #define TK_ESCAPE                          58
 12942  13202   #define TK_ID                              59
 12943  13203   #define TK_COLUMNKW                        60
 12944         -#define TK_FOR                             61
 12945         -#define TK_IGNORE                          62
 12946         -#define TK_INITIALLY                       63
 12947         -#define TK_INSTEAD                         64
 12948         -#define TK_NO                              65
 12949         -#define TK_KEY                             66
 12950         -#define TK_OF                              67
 12951         -#define TK_OFFSET                          68
 12952         -#define TK_PRAGMA                          69
 12953         -#define TK_RAISE                           70
 12954         -#define TK_RECURSIVE                       71
 12955         -#define TK_REPLACE                         72
 12956         -#define TK_RESTRICT                        73
 12957         -#define TK_ROW                             74
 12958         -#define TK_TRIGGER                         75
 12959         -#define TK_VACUUM                          76
 12960         -#define TK_VIEW                            77
 12961         -#define TK_VIRTUAL                         78
 12962         -#define TK_WITH                            79
 12963         -#define TK_REINDEX                         80
 12964         -#define TK_RENAME                          81
 12965         -#define TK_CTIME_KW                        82
 12966         -#define TK_ANY                             83
 12967         -#define TK_BITAND                          84
 12968         -#define TK_BITOR                           85
 12969         -#define TK_LSHIFT                          86
 12970         -#define TK_RSHIFT                          87
 12971         -#define TK_PLUS                            88
 12972         -#define TK_MINUS                           89
 12973         -#define TK_STAR                            90
 12974         -#define TK_SLASH                           91
 12975         -#define TK_REM                             92
 12976         -#define TK_CONCAT                          93
 12977         -#define TK_COLLATE                         94
 12978         -#define TK_BITNOT                          95
 12979         -#define TK_INDEXED                         96
 12980         -#define TK_STRING                          97
 12981         -#define TK_JOIN_KW                         98
 12982         -#define TK_CONSTRAINT                      99
 12983         -#define TK_DEFAULT                        100
 12984         -#define TK_NULL                           101
 12985         -#define TK_PRIMARY                        102
 12986         -#define TK_UNIQUE                         103
 12987         -#define TK_CHECK                          104
 12988         -#define TK_REFERENCES                     105
 12989         -#define TK_AUTOINCR                       106
 12990         -#define TK_ON                             107
 12991         -#define TK_INSERT                         108
 12992         -#define TK_DELETE                         109
 12993         -#define TK_UPDATE                         110
 12994         -#define TK_SET                            111
 12995         -#define TK_DEFERRABLE                     112
 12996         -#define TK_FOREIGN                        113
 12997         -#define TK_DROP                           114
 12998         -#define TK_UNION                          115
 12999         -#define TK_ALL                            116
 13000         -#define TK_EXCEPT                         117
 13001         -#define TK_INTERSECT                      118
 13002         -#define TK_SELECT                         119
 13003         -#define TK_VALUES                         120
 13004         -#define TK_DISTINCT                       121
 13005         -#define TK_DOT                            122
 13006         -#define TK_FROM                           123
 13007         -#define TK_JOIN                           124
 13008         -#define TK_USING                          125
 13009         -#define TK_ORDER                          126
 13010         -#define TK_GROUP                          127
 13011         -#define TK_HAVING                         128
 13012         -#define TK_LIMIT                          129
 13013         -#define TK_WHERE                          130
 13014         -#define TK_INTO                           131
 13015         -#define TK_FLOAT                          132
 13016         -#define TK_BLOB                           133
 13017         -#define TK_INTEGER                        134
 13018         -#define TK_VARIABLE                       135
 13019         -#define TK_CASE                           136
 13020         -#define TK_WHEN                           137
 13021         -#define TK_THEN                           138
 13022         -#define TK_ELSE                           139
 13023         -#define TK_INDEX                          140
 13024         -#define TK_ALTER                          141
 13025         -#define TK_ADD                            142
 13026         -#define TK_TRUEFALSE                      143
 13027         -#define TK_ISNOT                          144
 13028         -#define TK_FUNCTION                       145
 13029         -#define TK_COLUMN                         146
 13030         -#define TK_AGG_FUNCTION                   147
 13031         -#define TK_AGG_COLUMN                     148
 13032         -#define TK_UMINUS                         149
 13033         -#define TK_UPLUS                          150
 13034         -#define TK_TRUTH                          151
 13035         -#define TK_REGISTER                       152
 13036         -#define TK_VECTOR                         153
 13037         -#define TK_SELECT_COLUMN                  154
 13038         -#define TK_IF_NULL_ROW                    155
 13039         -#define TK_ASTERISK                       156
 13040         -#define TK_SPAN                           157
 13041         -#define TK_END_OF_FILE                    158
 13042         -#define TK_UNCLOSED_STRING                159
 13043         -#define TK_SPACE                          160
 13044         -#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
 13045  13307   
 13046  13308   /* The token codes above must all fit in 8 bits */
 13047  13309   #define TKFLG_MASK           0xff  
 13048  13310   
 13049  13311   /* Flags that can be added to a token code when it is not
 13050  13312   ** being stored in a u8: */
 13051  13313   #define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
................................................................................
 13157  13419   ** The default value of "20" was choosen to minimize the run-time of the
 13158  13420   ** speedtest1 test program with options: --shrink-memory --reprepare
 13159  13421   */
 13160  13422   #ifndef SQLITE_DEFAULT_PCACHE_INITSZ
 13161  13423   # define SQLITE_DEFAULT_PCACHE_INITSZ 20
 13162  13424   #endif
 13163  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  +
 13164  13433   /*
 13165  13434   ** The compile-time options SQLITE_MMAP_READWRITE and 
 13166  13435   ** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
 13167  13436   ** You must choose one or the other (or neither) but not both.
 13168  13437   */
 13169  13438   #if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
 13170  13439   #error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
................................................................................
 13606  13875   typedef struct PrintfArguments PrintfArguments;
 13607  13876   typedef struct RowSet RowSet;
 13608  13877   typedef struct Savepoint Savepoint;
 13609  13878   typedef struct Select Select;
 13610  13879   typedef struct SQLiteThread SQLiteThread;
 13611  13880   typedef struct SelectDest SelectDest;
 13612  13881   typedef struct SrcList SrcList;
 13613         -typedef struct StrAccum StrAccum;
        13882  +typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
 13614  13883   typedef struct Table Table;
 13615  13884   typedef struct TableLock TableLock;
 13616  13885   typedef struct Token Token;
 13617  13886   typedef struct TreeView TreeView;
 13618  13887   typedef struct Trigger Trigger;
 13619  13888   typedef struct TriggerPrg TriggerPrg;
 13620  13889   typedef struct TriggerStep TriggerStep;
 13621  13890   typedef struct UnpackedRecord UnpackedRecord;
        13891  +typedef struct Upsert Upsert;
 13622  13892   typedef struct VTable VTable;
 13623  13893   typedef struct VtabCtx VtabCtx;
 13624  13894   typedef struct Walker Walker;
 13625  13895   typedef struct WhereInfo WhereInfo;
 13626  13896   typedef struct With With;
 13627  13897   
 13628  13898   /* A VList object records a mapping between parameters/variables/wildcards
................................................................................
 13897  14167   #define BTREE_AUXDELETE    0x04  /* not the primary delete operation */
 13898  14168   #define BTREE_APPEND       0x08  /* Insert is likely an append */
 13899  14169   
 13900  14170   /* An instance of the BtreePayload object describes the content of a single
 13901  14171   ** entry in either an index or table btree.
 13902  14172   **
 13903  14173   ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
 13904         -** an arbitrary key and no data.  These btrees have pKey,nKey set to their
 13905         -** 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.
 13906  14178   **
 13907  14179   ** Table btrees (used for rowid tables) contain an integer rowid used as
 13908  14180   ** the key and passed in the nKey field.  The pKey field is zero.  
 13909  14181   ** pData,nData hold the content of the new entry.  nZero extra zero bytes
 13910  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
 13911  14196   **
 13912  14197   ** This object is used to pass information into sqlite3BtreeInsert().  The
 13913  14198   ** same information used to be passed as five separate parameters.  But placing
 13914  14199   ** the information into this object helps to keep the interface more 
 13915  14200   ** organized and understandable, and it also helps the resulting code to
 13916  14201   ** run a little faster by using fewer registers for parameter passing.
 13917  14202   */
 13918  14203   struct BtreePayload {
 13919  14204     const void *pKey;       /* Key content for indexes.  NULL for tables */
 13920  14205     sqlite3_int64 nKey;     /* Size of pKey for indexes.  PRIMARY KEY for tabs */
 13921         -  const void *pData;      /* Data for tables.  NULL for indexes */
        14206  +  const void *pData;      /* Data for tables. */
 13922  14207     sqlite3_value *aMem;    /* First of nMem value in the unpacked pKey */
 13923  14208     u16 nMem;               /* Number of aMem[] value.  Might be zero */
 13924  14209     int nData;              /* Size of pData.  0 if none. */
 13925  14210     int nZero;              /* Extra zero data appended after pData,nData */
 13926  14211   };
 13927  14212   
 13928  14213   SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
................................................................................
 14272  14557   #define OP_IntCopy        77 /* synopsis: r[P2]=r[P1]                      */
 14273  14558   #define OP_ResultRow      78 /* synopsis: output=r[P1@P2]                  */
 14274  14559   #define OP_CollSeq        79
 14275  14560   #define OP_AddImm         80 /* synopsis: r[P1]=r[P1]+P2                   */
 14276  14561   #define OP_RealAffinity   81
 14277  14562   #define OP_Cast           82 /* synopsis: affinity(r[P1])                  */
 14278  14563   #define OP_Permutation    83
 14279         -#define OP_BitAnd         84 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 14280         -#define OP_BitOr          85 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 14281         -#define OP_ShiftLeft      86 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 14282         -#define OP_ShiftRight     87 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 14283         -#define OP_Add            88 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 14284         -#define OP_Subtract       89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 14285         -#define OP_Multiply       90 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 14286         -#define OP_Divide         91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 14287         -#define OP_Remainder      92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 14288         -#define OP_Concat         93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 14289         -#define OP_Compare        94 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 14290         -#define OP_BitNot         95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 14291         -#define OP_IsTrue         96 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 14292         -#define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 14293         -#define OP_Offset         98 /* synopsis: r[P3] = sqlite_offset(P1)        */
 14294         -#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'    */
 14295  14580   #define OP_Affinity      100 /* synopsis: affinity(r[P1@P2])               */
 14296  14581   #define OP_MakeRecord    101 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 14297  14582   #define OP_Count         102 /* synopsis: r[P2]=count()                    */
 14298  14583   #define OP_ReadCookie    103
 14299  14584   #define OP_SetCookie     104
 14300  14585   #define OP_ReopenIdx     105 /* synopsis: root=P2 iDb=P3                   */
 14301  14586   #define OP_OpenRead      106 /* synopsis: root=P2 iDb=P3                   */
................................................................................
 14320  14605   #define OP_Rowid         125 /* synopsis: r[P2]=rowid                      */
 14321  14606   #define OP_NullRow       126
 14322  14607   #define OP_SeekEnd       127
 14323  14608   #define OP_SorterInsert  128 /* synopsis: key=r[P2]                        */
 14324  14609   #define OP_IdxInsert     129 /* synopsis: key=r[P2]                        */
 14325  14610   #define OP_IdxDelete     130 /* synopsis: key=r[P2@P3]                     */
 14326  14611   #define OP_DeferredSeek  131 /* synopsis: Move P3 to P1.rowid if needed    */
 14327         -#define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 14328         -#define OP_IdxRowid      133 /* synopsis: r[P2]=rowid                      */
 14329         -#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       */
 14330  14615   #define OP_Clear         135
 14331  14616   #define OP_ResetSorter   136
 14332  14617   #define OP_CreateBtree   137 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 14333  14618   #define OP_SqlExec       138
 14334  14619   #define OP_ParseSchema   139
 14335  14620   #define OP_LoadAnalysis  140
 14336  14621   #define OP_DropTable     141
................................................................................
 14359  14644   #define OP_Function0     164 /* synopsis: r[P3]=func(r[P2@P5])             */
 14360  14645   #define OP_PureFunc      165
 14361  14646   #define OP_Function      166 /* synopsis: r[P3]=func(r[P2@P5])             */
 14362  14647   #define OP_Trace         167
 14363  14648   #define OP_CursorHint    168
 14364  14649   #define OP_Noop          169
 14365  14650   #define OP_Explain       170
        14651  +#define OP_Abortable     171
 14366  14652   
 14367  14653   /* Properties such as "out2" or "jump" that are specified in
 14368  14654   ** comments following the "case" for each opcode in the vdbe.c
 14369  14655   ** are encoded into bitvectors as follows:
 14370  14656   */
 14371  14657   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 14372  14658   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 14381  14667   /*  24 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
 14382  14668   /*  32 */ 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
 14383  14669   /*  40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
 14384  14670   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 14385  14671   /*  56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
 14386  14672   /*  64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
 14387  14673   /*  72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 14388         -/*  80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
 14389         -/*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 14390         -/*  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,\
 14391  14677   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14392  14678   /* 112 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 14393  14679   /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,\
 14394  14680   /* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
 14395  14681   /* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14396  14682   /* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
 14397  14683   /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14398  14684   /* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14399         -/* 168 */ 0x00, 0x00, 0x00,}
        14685  +/* 168 */ 0x00, 0x00, 0x00, 0x00,}
 14400  14686   
 14401  14687   /* The sqlite3P2Values() routine is able to run faster if it knows
 14402  14688   ** the value of the largest JUMP opcode.  The smaller the maximum
 14403  14689   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 14404  14690   ** generated this include file strives to group all JUMP opcodes
 14405  14691   ** together near the beginning of the list.
 14406  14692   */
................................................................................
 14434  14720   #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
 14435  14721   SQLITE_PRIVATE   void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
 14436  14722   SQLITE_PRIVATE   void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
 14437  14723   #else
 14438  14724   # define sqlite3VdbeVerifyNoMallocRequired(A,B)
 14439  14725   # define sqlite3VdbeVerifyNoResultRow(A)
 14440  14726   #endif
 14441         -SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
        14727  +#if defined(SQLITE_DEBUG)
        14728  +SQLITE_PRIVATE   void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
        14729  +#else
        14730  +# define sqlite3VdbeVerifyAbortable(A,B)
        14731  +#endif
        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
 14442  14745   SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
 14443  14746   SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
 14444  14747   SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
 14445  14748   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
 14446  14749   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
 14447  14750   SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
 14448  14751   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
................................................................................
 14457  14760   SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
 14458  14761   SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
 14459  14762   SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
 14460  14763   SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
 14461  14764   SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
 14462  14765   SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
 14463  14766   SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
        14767  +#ifdef SQLITE_COVERAGE_TEST
        14768  +SQLITE_PRIVATE   int sqlite3VdbeLabelHasBeenResolved(Vdbe*,int);
        14769  +#endif
 14464  14770   SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
 14465  14771   #ifdef SQLITE_DEBUG
 14466  14772   SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
 14467  14773   #endif
 14468  14774   SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
 14469  14775   SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
 14470  14776   SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
................................................................................
 15591  15897     u8 bBenignMalloc;             /* Do not require OOMs if true */
 15592  15898     u8 dfltLockMode;              /* Default locking-mode for attached dbs */
 15593  15899     signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
 15594  15900     u8 suppressErr;               /* Do not issue error messages if true */
 15595  15901     u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
 15596  15902     u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
 15597  15903     u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
 15598         -  u8 skipBtreeMutex;            /* True if no shared-cache backends */
        15904  +  u8 noSharedCache;             /* True if no shared-cache backends */
 15599  15905     u8 nSqlExec;                  /* Number of pending OP_SqlExec opcodes */
 15600  15906     int nextPagesize;             /* Pagesize after VACUUM if >0 */
 15601  15907     u32 magic;                    /* Magic number for detect library misuse */
 15602  15908     int nChange;                  /* Value returned by sqlite3_changes() */
 15603  15909     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 15604  15910     int aLimit[SQLITE_N_LIMIT];   /* Limits */
 15605  15911     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
................................................................................
 15735  16041   #define SQLITE_EnableTrigger  0x00040000  /* True to enable triggers */
 15736  16042   #define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
 15737  16043   #define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
 15738  16044   #define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
 15739  16045   #define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
 15740  16046   #define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
 15741  16047   #define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
        16048  +#define SQLITE_ResetDatabase  0x02000000  /* Reset the database */
 15742  16049   
 15743  16050   /* Flags used only if debugging */
 15744  16051   #ifdef SQLITE_DEBUG
 15745  16052   #define SQLITE_SqlTrace       0x08000000  /* Debug print SQL as it executes */
 15746  16053   #define SQLITE_VdbeListing    0x10000000  /* Debug listings of VDBE programs */
 15747  16054   #define SQLITE_VdbeTrace      0x20000000  /* True to trace VDBE execution */
 15748  16055   #define SQLITE_VdbeAddopTrace 0x40000000  /* Trace sqlite3VdbeAddOp() calls */
................................................................................
 15751  16058   
 15752  16059   /*
 15753  16060   ** Allowed values for sqlite3.mDbFlags
 15754  16061   */
 15755  16062   #define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
 15756  16063   #define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
 15757  16064   #define DBFLAG_Vacuum         0x0004  /* Currently in a VACUUM */
        16065  +#define DBFLAG_SchemaKnownOk  0x0008  /* Schema is known to be valid */
 15758  16066   
 15759  16067   /*
 15760  16068   ** Bits of the sqlite3.dbOptFlags field that are used by the
 15761  16069   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 15762  16070   ** selectively disable various optimizations.
 15763  16071   */
 15764  16072   #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
................................................................................
 15996  16304   
 15997  16305   /* Allowed values for Column.colFlags:
 15998  16306   */
 15999  16307   #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
 16000  16308   #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 16001  16309   #define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
 16002  16310   #define COLFLAG_UNIQUE   0x0008    /* Column def contains "UNIQUE" or "PK" */
        16311  +#define COLFLAG_SORTERREF 0x0010   /* Use sorter-refs with this column */
 16003  16312   
 16004  16313   /*
 16005  16314   ** A "Collating Sequence" is defined by an instance of the following
 16006  16315   ** structure. Conceptually, a collating sequence consists of a name and
 16007  16316   ** a comparison routine that defines the order of that sequence.
 16008  16317   **
 16009  16318   ** If CollSeq.xCmp is NULL, it means that the
................................................................................
 16283  16592   */
 16284  16593   #define OE_None     0   /* There is no constraint to check */
 16285  16594   #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
 16286  16595   #define OE_Abort    2   /* Back out changes but do no rollback transaction */
 16287  16596   #define OE_Fail     3   /* Stop the operation but leave all prior changes */
 16288  16597   #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
 16289  16598   #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
 16290         -
 16291         -#define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
 16292         -#define OE_SetNull  7   /* Set the foreign key value to NULL */
 16293         -#define OE_SetDflt  8   /* Set the foreign key value to its default */
 16294         -#define OE_Cascade  9   /* Cascade the changes */
 16295         -
 16296         -#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 */
 16297  16605   
 16298  16606   
 16299  16607   /*
 16300  16608   ** An instance of the following structure is passed as the first
 16301  16609   ** argument to sqlite3VdbeKeyCompare and is used to control the
 16302  16610   ** comparison of the two index keys.
 16303  16611   **
................................................................................
 16736  17044       Expr *pExpr;            /* The parse tree for this expression */
 16737  17045       char *zName;            /* Token associated with this expression */
 16738  17046       char *zSpan;            /* Original text of the expression */
 16739  17047       u8 sortOrder;           /* 1 for DESC or 0 for ASC */
 16740  17048       unsigned done :1;       /* A flag to indicate when processing is finished */
 16741  17049       unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
 16742  17050       unsigned reusable :1;   /* Constant expression is reusable */
        17051  +    unsigned bSorterRef :1; /* Defer evaluation until after sorting */
 16743  17052       union {
 16744  17053         struct {
 16745  17054           u16 iOrderByCol;      /* For ORDER BY, column number in result set */
 16746  17055           u16 iAlias;           /* Index into Parse.aAlias[] for zName */
 16747  17056         } x;
 16748  17057         int iConstExprReg;      /* Register in which Expr value is cached */
 16749  17058       } u;
................................................................................
 16835  17144         unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
 16836  17145         unsigned isIndexedBy :1;   /* True if there is an INDEXED BY clause */
 16837  17146         unsigned isTabFunc :1;     /* True if table-valued-function syntax */
 16838  17147         unsigned isCorrelated :1;  /* True if sub-query is correlated */
 16839  17148         unsigned viaCoroutine :1;  /* Implemented as a co-routine */
 16840  17149         unsigned isRecursive :1;   /* True for recursive reference in WITH */
 16841  17150       } fg;
 16842         -#ifndef SQLITE_OMIT_EXPLAIN
 16843         -    u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
 16844         -#endif
 16845  17151       int iCursor;      /* The VDBE cursor number used to access this table */
 16846  17152       Expr *pOn;        /* The ON clause of a join */
 16847  17153       IdList *pUsing;   /* The USING clause of a join */
 16848  17154       Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
 16849  17155       union {
 16850  17156         char *zIndexedBy;    /* Identifier from "INDEXED BY <zIndex>" clause */
 16851  17157         ExprList *pFuncArg;  /* Arguments to table-valued-function */
................................................................................
 16919  17225   ** NameContext in the parent query.  Thus the process of scanning the
 16920  17226   ** NameContext list corresponds to searching through successively outer
 16921  17227   ** subqueries looking for a match.
 16922  17228   */
 16923  17229   struct NameContext {
 16924  17230     Parse *pParse;       /* The parser */
 16925  17231     SrcList *pSrcList;   /* One or more tables used to resolve names */
 16926         -  ExprList *pEList;    /* Optional list of result-set columns */
 16927         -  AggInfo *pAggInfo;   /* Information about aggregates at this level */
        17232  +  union {
        17233  +    ExprList *pEList;    /* Optional list of result-set columns */
        17234  +    AggInfo *pAggInfo;   /* Information about aggregates at this level */
        17235  +    Upsert *pUpsert;     /* ON CONFLICT clause information from an upsert */
        17236  +  } uNC;
 16928  17237     NameContext *pNext;  /* Next outer name context.  NULL for outermost */
 16929  17238     int nRef;            /* Number of names resolved by this context */
 16930  17239     int nErr;            /* Number of errors encountered while resolving names */
 16931  17240     u16 ncFlags;         /* Zero or more NC_* flags defined below */
 16932  17241   };
 16933  17242   
 16934  17243   /*
................................................................................
 16942  17251   #define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
 16943  17252   #define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
 16944  17253   #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
 16945  17254   #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
 16946  17255   #define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
 16947  17256   #define NC_IdxExpr   0x0020  /* True if resolving columns of CREATE INDEX */
 16948  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 */
 16949  17261   #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
 16950  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  +};
 16951  17293   
 16952  17294   /*
 16953  17295   ** An instance of the following structure contains all information
 16954  17296   ** needed to generate code for a single SELECT statement.
 16955  17297   **
 16956         -** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
 16957         -** If there is a LIMIT clause, the parser sets nLimit to the value of the
 16958         -** limit and nOffset to the value of the offset (or 0 if there is not
 16959         -** offset).  But later on, nLimit and nOffset become the memory locations
 16960         -** 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.
 16961  17300   **
 16962  17301   ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
 16963  17302   ** These addresses must be stored so that we can go back and fill in
 16964  17303   ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
 16965  17304   ** the number of columns in P2 can be computed at the same time
 16966  17305   ** as the OP_OpenEphm instruction is coded because not
 16967  17306   ** enough information about the compound query is known at that point.
................................................................................
 17013  17352   #define SF_NestedFrom     0x00800  /* Part of a parenthesized FROM clause */
 17014  17353   #define SF_MinMaxAgg      0x01000  /* Aggregate containing min() or max() */
 17015  17354   #define SF_Recursive      0x02000  /* The recursive part of a recursive CTE */
 17016  17355   #define SF_FixedLimit     0x04000  /* nSelectRow set by a constant LIMIT */
 17017  17356   #define SF_MaybeConvert   0x08000  /* Need convertCompoundSelectToSubquery() */
 17018  17357   #define SF_Converted      0x10000  /* By convertCompoundSelectToSubquery() */
 17019  17358   #define SF_IncludeHidden  0x20000  /* Include hidden columns in output */
 17020         -#define SF_ComplexResult  0x40000  /* Result set contains subquery or function */
 17021         -
        17359  +#define SF_ComplexResult  0x40000  /* Result contains subquery or function */
 17022  17360   
 17023  17361   /*
 17024  17362   ** The results of a SELECT can be distributed in several ways, as defined
 17025  17363   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
 17026  17364   ** Type".
 17027  17365   **
 17028  17366   **     SRT_Union       Store results as a key in a temporary index
................................................................................
 17284  17622     u8 explain;               /* True if the EXPLAIN flag is found on the query */
 17285  17623   #ifndef SQLITE_OMIT_VIRTUALTABLE
 17286  17624     u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
 17287  17625     int nVtabLock;            /* Number of virtual tables to lock */
 17288  17626   #endif
 17289  17627     int nHeight;              /* Expression tree height of current sub-select */
 17290  17628   #ifndef SQLITE_OMIT_EXPLAIN
 17291         -  int iSelectId;            /* ID of current select for EXPLAIN output */
 17292         -  int iNextSelectId;        /* Next available select ID for EXPLAIN output */
        17629  +  int addrExplain;          /* Address of current OP_Explain opcode */
 17293  17630   #endif
 17294  17631     VList *pVList;            /* Mapping between variable names and numbers */
 17295  17632     Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
 17296  17633     const char *zTail;        /* All SQL text past the last semicolon parsed */
 17297  17634     Table *pNewTable;         /* A table being constructed by CREATE TABLE */
 17298  17635     Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
 17299  17636     const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
................................................................................
 17444  17781   struct TriggerStep {
 17445  17782     u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
 17446  17783     u8 orconf;           /* OE_Rollback etc. */
 17447  17784     Trigger *pTrig;      /* The trigger that this step is a part of */
 17448  17785     Select *pSelect;     /* SELECT statement or RHS of INSERT INTO SELECT ... */
 17449  17786     char *zTarget;       /* Target table for DELETE, UPDATE, INSERT */
 17450  17787     Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
 17451         -  ExprList *pExprList; /* SET clause for UPDATE. */
        17788  +  ExprList *pExprList; /* SET clause for UPDATE */
 17452  17789     IdList *pIdList;     /* Column names for INSERT */
        17790  +  Upsert *pUpsert;     /* Upsert clauses on an INSERT */
 17453  17791     char *zSpan;         /* Original SQL text of this command */
 17454  17792     TriggerStep *pNext;  /* Next in the link-list */
 17455  17793     TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
 17456  17794   };
 17457  17795   
 17458  17796   /*
 17459  17797   ** The following structure contains information used by the sqliteFix...
................................................................................
 17470  17808     const Token *pName; /* Name of the container - used for error messages */
 17471  17809   };
 17472  17810   
 17473  17811   /*
 17474  17812   ** An objected used to accumulate the text of a string where we
 17475  17813   ** do not necessarily know how big the string will be in the end.
 17476  17814   */
 17477         -struct StrAccum {
        17815  +struct sqlite3_str {
 17478  17816     sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
 17479  17817     char *zText;         /* The string collected so far */
 17480  17818     u32  nAlloc;         /* Amount of space allocated in zText */
 17481  17819     u32  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
 17482  17820     u32  nChar;          /* Length of the string so far */
 17483         -  u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
        17821  +  u8   accError;       /* SQLITE_NOMEM or SQLITE_TOOBIG */
 17484  17822     u8   printfFlags;    /* SQLITE_PRINTF flags below */
 17485  17823   };
 17486         -#define STRACCUM_NOMEM   1
 17487         -#define STRACCUM_TOOBIG  2
 17488  17824   #define SQLITE_PRINTF_INTERNAL 0x01  /* Internal-use-only converters allowed */
 17489  17825   #define SQLITE_PRINTF_SQLFUNC  0x02  /* SQL function arguments to VXPrintf */
 17490  17826   #define SQLITE_PRINTF_MALLOCED 0x04  /* True if xText is allocated space */
 17491  17827   
 17492  17828   #define isMalloced(X)  (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
 17493  17829   
 17494  17830   
................................................................................
 17557  17893     void *pVdbeBranchArg;                                     /* 1st argument */
 17558  17894   #endif
 17559  17895   #ifndef SQLITE_UNTESTABLE
 17560  17896     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
 17561  17897   #endif
 17562  17898     int bLocaltimeFault;              /* True to fail localtime() calls */
 17563  17899     int iOnceResetThreshold;          /* When to reset OP_Once counters */
        17900  +  u32 szSorterRef;                  /* Min size in bytes to use sorter-refs */
 17564  17901   };
 17565  17902   
 17566  17903   /*
 17567  17904   ** This macro is used inside of assert() statements to indicate that
 17568  17905   ** the assert is only valid on a well-formed database.  Instead of:
 17569  17906   **
 17570  17907   **     assert( X );
................................................................................
 17848  18185   */
 17849  18186   struct PrintfArguments {
 17850  18187     int nArg;                /* Total number of arguments */
 17851  18188     int nUsed;               /* Number of arguments used so far */
 17852  18189     sqlite3_value **apArg;   /* The argument values */
 17853  18190   };
 17854  18191   
 17855         -SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, const char*, va_list);
 17856         -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
 17857  18192   SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
 17858  18193   SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
 17859  18194   #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
 17860  18195   SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
 17861  18196   #endif
 17862  18197   #if defined(SQLITE_TEST)
 17863  18198   SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
................................................................................
 17977  18312   #ifndef SQLITE_OMIT_AUTOINCREMENT
 17978  18313   SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
 17979  18314   SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
 17980  18315   #else
 17981  18316   # define sqlite3AutoincrementBegin(X)
 17982  18317   # define sqlite3AutoincrementEnd(X)
 17983  18318   #endif
 17984         -SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
        18319  +SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
 17985  18320   SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
 17986  18321   SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
 17987  18322   SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
 17988  18323   SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
 17989  18324   SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
 17990  18325   SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
 17991  18326                                         Token*, Select*, Expr*, IdList*);
................................................................................
 18007  18342   SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 18008  18343   SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
 18009  18344   SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
 18010  18345   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 18011  18346   SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
 18012  18347   #endif
 18013  18348   SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
 18014         -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*);
 18015  18351   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 18016  18352   SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 18017  18353   SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
 18018  18354   SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
 18019  18355   SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
 18020  18356   SQLITE_PRIVATE int sqlite3WhereOrderedInnerLoop(WhereInfo*);
 18021  18357   SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
................................................................................
 18100  18436   SQLITE_PRIVATE int sqlite3IsRowid(const char*);
 18101  18437   SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 18102  18438       Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
 18103  18439   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
 18104  18440   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
 18105  18441   SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
 18106  18442   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 18107         -                                     u8,u8,int,int*,int*);
        18443  +                                     u8,u8,int,int*,int*,Upsert*);
 18108  18444   #ifdef SQLITE_ENABLE_NULL_TRIM
 18109  18445   SQLITE_PRIVATE   void sqlite3SetMakeRecordP5(Vdbe*,Table*);
 18110  18446   #else
 18111  18447   # define sqlite3SetMakeRecordP5(A,B)
 18112  18448   #endif
 18113  18449   SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
 18114  18450   SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
................................................................................
 18153  18489                               int, int, int);
 18154  18490   SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
 18155  18491     void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
 18156  18492   SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
 18157  18493   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
 18158  18494                                           const char*,const char*);
 18159  18495   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
 18160         -                                        Select*,u8,const char*,const char*);
        18496  +                                        Select*,u8,Upsert*,
        18497  +                                        const char*,const char*);
 18161  18498   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8,
 18162  18499                                           const char*,const char*);
 18163  18500   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*,
 18164  18501                                           const char*,const char*);
 18165  18502   SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
 18166  18503   SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
 18167  18504   SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
................................................................................
 18339  18676   SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
 18340  18677   SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
 18341  18678   SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 18342  18679   SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 18343  18680   SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 18344  18681   SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 18345  18682   SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
 18346         -SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
        18683  +SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*);
 18347  18684   SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
 18348  18685   SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
 18349  18686   SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
 18350  18687   SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 18351  18688   SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 18352  18689   SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 18353  18690   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
................................................................................
 18364  18701   SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
 18365  18702   #endif
 18366  18703   SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
 18367  18704     void (*)(sqlite3_context*,int,sqlite3_value **),
 18368  18705     void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
 18369  18706     FuncDestructor *pDestructor
 18370  18707   );
        18708  +SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
 18371  18709   SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
 18372  18710   SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
 18373  18711   SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
 18374  18712   SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
 18375  18713   
 18376  18714   SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
 18377         -SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
 18378         -SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
 18379         -SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char);
 18380  18715   SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
 18381         -SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
 18382  18716   SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
 18383  18717   SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
 18384  18718   
 18385  18719   SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
 18386  18720   SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
 18387  18721   
 18388  18722   #ifndef SQLITE_OMIT_SUBQUERY
................................................................................
 18401  18735   SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
 18402  18736   #endif
 18403  18737   
 18404  18738   /*
 18405  18739   ** The interface to the LEMON-generated parser
 18406  18740   */
 18407  18741   #ifndef SQLITE_AMALGAMATION
 18408         -SQLITE_PRIVATE   void *sqlite3ParserAlloc(void*(*)(u64));
        18742  +SQLITE_PRIVATE   void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
 18409  18743   SQLITE_PRIVATE   void sqlite3ParserFree(void*, void(*)(void*));
 18410  18744   #endif
 18411         -SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
        18745  +SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
 18412  18746   #ifdef YYTRACKMAXSTACKDEPTH
 18413  18747   SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
 18414  18748   #endif
 18415  18749   
 18416  18750   SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
 18417  18751   #ifndef SQLITE_OMIT_LOAD_EXTENSION
 18418  18752   SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
................................................................................
 18470  18804   SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
 18471  18805   SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
 18472  18806   SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
 18473  18807   SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
 18474  18808   SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
 18475  18809   SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
 18476  18810   SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 18477         -SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
 18478  18811   SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
 18479  18812   SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
 18480  18813   SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 18481  18814   SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
 18482  18815   SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 18483  18816   SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 18484  18817   SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
................................................................................
 18492  18825   SQLITE_PRIVATE   With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
 18493  18826   SQLITE_PRIVATE   void sqlite3WithDelete(sqlite3*,With*);
 18494  18827   SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
 18495  18828   #else
 18496  18829   #define sqlite3WithPush(x,y,z)
 18497  18830   #define sqlite3WithDelete(x,y)
 18498  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  +
 18499  18844   
 18500  18845   /* Declarations for functions in fkey.c. All of these are replaced by
 18501  18846   ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
 18502  18847   ** key functionality is available. If OMIT_TRIGGER is defined but
 18503  18848   ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
 18504  18849   ** this case foreign keys are parsed, but no other functionality is
 18505  18850   ** provided (enforcement of FK constraints requires the triggers sub-system).
................................................................................
 18924  19269      0,                         /* xVdbeBranch */
 18925  19270      0,                         /* pVbeBranchArg */
 18926  19271   #endif
 18927  19272   #ifndef SQLITE_UNTESTABLE
 18928  19273      0,                         /* xTestCallback */
 18929  19274   #endif
 18930  19275      0,                         /* bLocaltimeFault */
 18931         -   0x7ffffffe                 /* iOnceResetThreshold */
        19276  +   0x7ffffffe,                /* iOnceResetThreshold */
        19277  +   SQLITE_DEFAULT_SORTERREF_SIZE   /* szSorterRef */
 18932  19278   };
 18933  19279   
 18934  19280   /*
 18935  19281   ** Hash table for global functions - functions common to all
 18936  19282   ** database connections.  After initialization, this table is
 18937  19283   ** read-only.
 18938  19284   */
................................................................................
 19387  19733     VList *pVList;          /* Name of variables */
 19388  19734   #ifndef SQLITE_OMIT_TRACE
 19389  19735     i64 startTime;          /* Time when query started - used for profiling */
 19390  19736   #endif
 19391  19737     int nOp;                /* Number of instructions in the program */
 19392  19738   #ifdef SQLITE_DEBUG
 19393  19739     int rcApp;              /* errcode set by sqlite3_result_error_code() */
        19740  +  u32 nWrite;             /* Number of write operations that have occurred */
 19394  19741   #endif
 19395  19742     u16 nResColumn;         /* Number of columns in one row of the result set */
 19396  19743     u8 errorAction;         /* Recovery action to do in case of an error */
 19397  19744     u8 minWriteFileFormat;  /* Minimum file format for writable database files */
 19398  19745     u8 prepFlags;           /* SQLITE_PREPARE_* flags */
 19399  19746     bft expired:1;          /* True if the VM needs to be recompiled */
 19400  19747     bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
................................................................................
 19521  19868   SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
 19522  19869   SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
 19523  19870   SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
 19524  19871   SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
 19525  19872   SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
 19526  19873   SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
 19527  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
 19528  19883   
 19529  19884   #if !defined(SQLITE_OMIT_SHARED_CACHE) 
 19530  19885   SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
 19531  19886   #else
 19532  19887   # define sqlite3VdbeEnter(X)
 19533  19888   #endif
 19534  19889   
................................................................................
 24214  24569     assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
 24215  24570     return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
 24216  24571   }
 24217  24572   #endif
 24218  24573   
 24219  24574   #endif /* !defined(SQLITE_MUTEX_OMIT) */
 24220  24575   
 24221         -
 24222  24576   /************** End of mutex.c ***********************************************/
 24223  24577   /************** Begin file mutex_noop.c **************************************/
 24224  24578   /*
 24225  24579   ** 2008 October 07
 24226  24580   **
 24227  24581   ** The author disclaims copyright to this source code.  In place of
 24228  24582   ** a legal notice, here is a blessing:
................................................................................
 26378  26732   }
 26379  26733   #endif /* SQLITE_OMIT_FLOATING_POINT */
 26380  26734   
 26381  26735   /*
 26382  26736   ** Set the StrAccum object to an error mode.
 26383  26737   */
 26384  26738   static void setStrAccumError(StrAccum *p, u8 eError){
 26385         -  assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG );
        26739  +  assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
 26386  26740     p->accError = eError;
 26387  26741     p->nAlloc = 0;
 26388  26742   }
 26389  26743   
 26390  26744   /*
 26391  26745   ** Extra argument values from a PrintfArguments object
 26392  26746   */
................................................................................
 26412  26766   # define SQLITE_PRINT_BUF_SIZE 70
 26413  26767   #endif
 26414  26768   #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
 26415  26769   
 26416  26770   /*
 26417  26771   ** Render a string given by "fmt" into the StrAccum object.
 26418  26772   */
 26419         -SQLITE_PRIVATE void sqlite3VXPrintf(
 26420         -  StrAccum *pAccum,          /* Accumulate results here */
        26773  +SQLITE_API void sqlite3_str_vappendf(
        26774  +  sqlite3_str *pAccum,       /* Accumulate results here */
 26421  26775     const char *fmt,           /* Format string */
 26422  26776     va_list ap                 /* arguments */
 26423  26777   ){
 26424  26778     int c;                     /* Next character in the format string */
 26425  26779     char *bufpt;               /* Pointer to the conversion buffer */
 26426  26780     int precision;             /* Precision of the current field */
 26427  26781     int length;                /* Length of the field */
................................................................................
 26470  26824       if( c!='%' ){
 26471  26825         bufpt = (char *)fmt;
 26472  26826   #if HAVE_STRCHRNUL
 26473  26827         fmt = strchrnul(fmt, '%');
 26474  26828   #else
 26475  26829         do{ fmt++; }while( *fmt && *fmt != '%' );
 26476  26830   #endif
 26477         -      sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
        26831  +      sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
 26478  26832         if( *fmt==0 ) break;
 26479  26833       }
 26480  26834       if( (c=(*++fmt))==0 ){
 26481         -      sqlite3StrAccumAppend(pAccum, "%", 1);
        26835  +      sqlite3_str_append(pAccum, "%", 1);
 26482  26836         break;
 26483  26837       }
 26484  26838       /* Find out what flags are present */
 26485  26839       flag_leftjustify = flag_prefix = cThousand =
 26486  26840        flag_alternateform = flag_altform2 = flag_zeropad = 0;
 26487  26841       done = 0;
 26488  26842       do{
................................................................................
 26652  27006           if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
 26653  27007             nOut = etBUFSIZE;
 26654  27008             zOut = buf;
 26655  27009           }else{
 26656  27010             u64 n = (u64)precision + 10 + precision/3;
 26657  27011             zOut = zExtra = sqlite3Malloc( n );
 26658  27012             if( zOut==0 ){
 26659         -            setStrAccumError(pAccum, STRACCUM_NOMEM);
        27013  +            setStrAccumError(pAccum, SQLITE_NOMEM);
 26660  27014               return;
 26661  27015             }
 26662  27016             nOut = (int)n;
 26663  27017           }
 26664  27018           bufpt = &zOut[nOut-1];
 26665  27019           if( xtype==etORDINAL ){
 26666  27020             static const char zOrd[] = "thstndrd";
................................................................................
 26777  27131           }else{
 26778  27132             e2 = exp;
 26779  27133           }
 26780  27134           if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
 26781  27135             bufpt = zExtra 
 26782  27136                 = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
 26783  27137             if( bufpt==0 ){
 26784         -            setStrAccumError(pAccum, STRACCUM_NOMEM);
        27138  +            setStrAccumError(pAccum, SQLITE_NOMEM);
 26785  27139               return;
 26786  27140             }
 26787  27141           }
 26788  27142           zOut = bufpt;
 26789  27143           nsd = 16 + flag_altform2*10;
 26790  27144           flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
 26791  27145           /* The sign in front of the number */
................................................................................
 26909  27263               buf[3] = 0x80 + (u8)(ch & 0x3f);
 26910  27264               length = 4;
 26911  27265             }
 26912  27266           }
 26913  27267           if( precision>1 ){
 26914  27268             width -= precision-1;
 26915  27269             if( width>1 && !flag_leftjustify ){
 26916         -            sqlite3AppendChar(pAccum, width-1, ' ');
        27270  +            sqlite3_str_appendchar(pAccum, width-1, ' ');
 26917  27271               width = 0;
 26918  27272             }
 26919  27273             while( precision-- > 1 ){
 26920         -            sqlite3StrAccumAppend(pAccum, buf, length);
        27274  +            sqlite3_str_append(pAccum, buf, length);
 26921  27275             }
 26922  27276           }
 26923  27277           bufpt = buf;
 26924  27278           flag_altform2 = 1;
 26925  27279           goto adjust_width_for_utf8;
 26926  27280         case etSTRING:
 26927  27281         case etDYNSTRING:
................................................................................
 26999  27353             }
 27000  27354           }
 27001  27355           needQuote = !isnull && xtype==etSQLESCAPE2;
 27002  27356           n += i + 3;
 27003  27357           if( n>etBUFSIZE ){
 27004  27358             bufpt = zExtra = sqlite3Malloc( n );
 27005  27359             if( bufpt==0 ){
 27006         -            setStrAccumError(pAccum, STRACCUM_NOMEM);
        27360  +            setStrAccumError(pAccum, SQLITE_NOMEM);
 27007  27361               return;
 27008  27362             }
 27009  27363           }else{
 27010  27364             bufpt = buf;
 27011  27365           }
 27012  27366           j = 0;
 27013  27367           if( needQuote ) bufpt[j++] = q;
................................................................................
 27023  27377         }
 27024  27378         case etTOKEN: {
 27025  27379           Token *pToken;
 27026  27380           if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
 27027  27381           pToken = va_arg(ap, Token*);
 27028  27382           assert( bArgList==0 );
 27029  27383           if( pToken && pToken->n ){
 27030         -          sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
        27384  +          sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
 27031  27385           }
 27032  27386           length = width = 0;
 27033  27387           break;
 27034  27388         }
 27035  27389         case etSRCLIST: {
 27036  27390           SrcList *pSrc;
 27037  27391           int k;
................................................................................
 27039  27393           if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
 27040  27394           pSrc = va_arg(ap, SrcList*);
 27041  27395           k = va_arg(ap, int);
 27042  27396           pItem = &pSrc->a[k];
 27043  27397           assert( bArgList==0 );
 27044  27398           assert( k>=0 && k<pSrc->nSrc );
 27045  27399           if( pItem->zDatabase ){
 27046         -          sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
 27047         -          sqlite3StrAccumAppend(pAccum, ".", 1);
        27400  +          sqlite3_str_appendall(pAccum, pItem->zDatabase);
        27401  +          sqlite3_str_append(pAccum, ".", 1);
 27048  27402           }
 27049         -        sqlite3StrAccumAppendAll(pAccum, pItem->zName);
        27403  +        sqlite3_str_appendall(pAccum, pItem->zName);
 27050  27404           length = width = 0;
 27051  27405           break;
 27052  27406         }
 27053  27407         default: {
 27054  27408           assert( xtype==etINVALID );
 27055  27409           return;
 27056  27410         }
................................................................................
 27061  27415       ** the output.  Both length and width are in bytes, not characters,
 27062  27416       ** at this point.  If the "!" flag was present on string conversions
 27063  27417       ** indicating that width and precision should be expressed in characters,
 27064  27418       ** then the values have been translated prior to reaching this point.
 27065  27419       */
 27066  27420       width -= length;
 27067  27421       if( width>0 ){
 27068         -      if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 27069         -      sqlite3StrAccumAppend(pAccum, bufpt, length);
 27070         -      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, ' ');
 27071  27425       }else{
 27072         -      sqlite3StrAccumAppend(pAccum, bufpt, length);
        27426  +      sqlite3_str_append(pAccum, bufpt, length);
 27073  27427       }
 27074  27428   
 27075  27429       if( zExtra ){
 27076  27430         sqlite3DbFree(pAccum->db, zExtra);
 27077  27431         zExtra = 0;
 27078  27432       }
 27079  27433     }/* End for loop over the format string */
................................................................................
 27086  27440   ** Return the number of bytes of text that StrAccum is able to accept
 27087  27441   ** after the attempted enlargement.  The value returned might be zero.
 27088  27442   */
 27089  27443   static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
 27090  27444     char *zNew;
 27091  27445     assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
 27092  27446     if( p->accError ){
 27093         -    testcase(p->accError==STRACCUM_TOOBIG);
 27094         -    testcase(p->accError==STRACCUM_NOMEM);
        27447  +    testcase(p->accError==SQLITE_TOOBIG);
        27448  +    testcase(p->accError==SQLITE_NOMEM);
 27095  27449       return 0;
 27096  27450     }
 27097  27451     if( p->mxAlloc==0 ){
 27098  27452       N = p->nAlloc - p->nChar - 1;
 27099         -    setStrAccumError(p, STRACCUM_TOOBIG);
        27453  +    setStrAccumError(p, SQLITE_TOOBIG);
 27100  27454       return N;
 27101  27455     }else{
 27102  27456       char *zOld = isMalloced(p) ? p->zText : 0;
 27103  27457       i64 szNew = p->nChar;
 27104  27458       szNew += N + 1;
 27105  27459       if( szNew+p->nChar<=p->mxAlloc ){
 27106  27460         /* Force exponential buffer size growth as long as it does not overflow,
 27107  27461         ** to avoid having to call this routine too often */
 27108  27462         szNew += p->nChar;
 27109  27463       }
 27110  27464       if( szNew > p->mxAlloc ){
 27111         -      sqlite3StrAccumReset(p);
 27112         -      setStrAccumError(p, STRACCUM_TOOBIG);
        27465  +      sqlite3_str_reset(p);
        27466  +      setStrAccumError(p, SQLITE_TOOBIG);
 27113  27467         return 0;
 27114  27468       }else{
 27115  27469         p->nAlloc = (int)szNew;
 27116  27470       }
 27117  27471       if( p->db ){
 27118  27472         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
 27119  27473       }else{
................................................................................
 27122  27476       if( zNew ){
 27123  27477         assert( p->zText!=0 || p->nChar==0 );
 27124  27478         if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
 27125  27479         p->zText = zNew;
 27126  27480         p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
 27127  27481         p->printfFlags |= SQLITE_PRINTF_MALLOCED;
 27128  27482       }else{
 27129         -      sqlite3StrAccumReset(p);
 27130         -      setStrAccumError(p, STRACCUM_NOMEM);
        27483  +      sqlite3_str_reset(p);
        27484  +      setStrAccumError(p, SQLITE_NOMEM);
 27131  27485         return 0;
 27132  27486       }
 27133  27487     }
 27134  27488     return N;
 27135  27489   }
 27136  27490   
 27137  27491   /*
 27138  27492   ** Append N copies of character c to the given string buffer.
 27139  27493   */
 27140         -SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
        27494  +SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
 27141  27495     testcase( p->nChar + (i64)N > 0x7fffffff );
 27142  27496     if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
 27143  27497       return;
 27144  27498     }
 27145  27499     while( (N--)>0 ) p->zText[p->nChar++] = c;
 27146  27500   }
 27147  27501   
 27148  27502   /*
 27149  27503   ** The StrAccum "p" is not large enough to accept N new bytes of z[].
 27150  27504   ** So enlarge if first, then do the append.
 27151  27505   **
 27152         -** 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
 27153  27507   ** work (enlarging the buffer) using tail recursion, so that the
 27154         -** sqlite3StrAccumAppend() routine can use fast calling semantics.
        27508  +** sqlite3_str_append() routine can use fast calling semantics.
 27155  27509   */
 27156  27510   static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
 27157  27511     N = sqlite3StrAccumEnlarge(p, N);
 27158  27512     if( N>0 ){
 27159  27513       memcpy(&p->zText[p->nChar], z, N);
 27160  27514       p->nChar += N;
 27161  27515     }
 27162  27516   }
 27163  27517   
 27164  27518   /*
 27165  27519   ** Append N bytes of text from z to the StrAccum object.  Increase the
 27166  27520   ** size of the memory allocation for StrAccum if necessary.
 27167  27521   */
 27168         -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){
 27169  27523     assert( z!=0 || N==0 );
 27170  27524     assert( p->zText!=0 || p->nChar==0 || p->accError );
 27171  27525     assert( N>=0 );
 27172  27526     assert( p->accError==0 || p->nAlloc==0 );
 27173  27527     if( p->nChar+N >= p->nAlloc ){
 27174  27528       enlargeAndAppend(p,z,N);
 27175  27529     }else if( N ){
................................................................................
 27178  27532       memcpy(&p->zText[p->nChar-N], z, N);
 27179  27533     }
 27180  27534   }
 27181  27535   
 27182  27536   /*
 27183  27537   ** Append the complete text of zero-terminated string z[] to the p string.
 27184  27538   */
 27185         -SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
 27186         -  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));
 27187  27541   }
 27188  27542   
 27189  27543   
 27190  27544   /*
 27191  27545   ** Finish off a string by making sure it is zero-terminated.
 27192  27546   ** Return a pointer to the resulting string.  Return a NULL
 27193  27547   ** pointer if any kind of error was encountered.
................................................................................
 27196  27550     char *zText;
 27197  27551     assert( p->mxAlloc>0 && !isMalloced(p) );
 27198  27552     zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
 27199  27553     if( zText ){
 27200  27554       memcpy(zText, p->zText, p->nChar+1);
 27201  27555       p->printfFlags |= SQLITE_PRINTF_MALLOCED;
 27202  27556     }else{
 27203         -    setStrAccumError(p, STRACCUM_NOMEM);
        27557  +    setStrAccumError(p, SQLITE_NOMEM);
 27204  27558     }
 27205  27559     p->zText = zText;
 27206  27560     return zText;
 27207  27561   }
 27208  27562   SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
 27209  27563     if( p->zText ){
 27210  27564       p->zText[p->nChar] = 0;
 27211  27565       if( p->mxAlloc>0 && !isMalloced(p) ){
 27212  27566         return strAccumFinishRealloc(p);
 27213  27567       }
 27214  27568     }
 27215  27569     return p->zText;
 27216  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, 0
        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  +}
 27217  27611   
 27218  27612   /*
 27219  27613   ** Reset an StrAccum string.  Reclaim all malloced memory.
 27220  27614   */
 27221         -SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
        27615  +SQLITE_API void sqlite3_str_reset(StrAccum *p){
 27222  27616     if( isMalloced(p) ){
 27223  27617       sqlite3DbFree(p->db, p->zText);
 27224  27618       p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
 27225  27619     }
        27620  +  p->nAlloc = 0;
        27621  +  p->nChar = 0;
 27226  27622     p->zText = 0;
 27227  27623   }
 27228  27624   
 27229  27625   /*
 27230  27626   ** Initialize a string accumulator.
 27231  27627   **
 27232  27628   ** p:     The accumulator to be initialized.
................................................................................
 27245  27641     p->db = db;
 27246  27642     p->nAlloc = n;
 27247  27643     p->mxAlloc = mx;
 27248  27644     p->nChar = 0;
 27249  27645     p->accError = 0;
 27250  27646     p->printfFlags = 0;
 27251  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  +}
 27252  27660   
 27253  27661   /*
 27254  27662   ** Print into memory obtained from sqliteMalloc().  Use the internal
 27255  27663   ** %-conversion extensions.
 27256  27664   */
 27257  27665   SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
 27258  27666     char *z;
 27259  27667     char zBase[SQLITE_PRINT_BUF_SIZE];
 27260  27668     StrAccum acc;
 27261  27669     assert( db!=0 );
 27262  27670     sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
 27263  27671                         db->aLimit[SQLITE_LIMIT_LENGTH]);
 27264  27672     acc.printfFlags = SQLITE_PRINTF_INTERNAL;
 27265         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27673  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27266  27674     z = sqlite3StrAccumFinish(&acc);
 27267         -  if( acc.accError==STRACCUM_NOMEM ){
        27675  +  if( acc.accError==SQLITE_NOMEM ){
 27268  27676       sqlite3OomFault(db);
 27269  27677     }
 27270  27678     return z;
 27271  27679   }
 27272  27680   
 27273  27681   /*
 27274  27682   ** Print into memory obtained from sqliteMalloc().  Use the internal
................................................................................
 27298  27706       return 0;
 27299  27707     }
 27300  27708   #endif
 27301  27709   #ifndef SQLITE_OMIT_AUTOINIT
 27302  27710     if( sqlite3_initialize() ) return 0;
 27303  27711   #endif
 27304  27712     sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
 27305         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27713  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27306  27714     z = sqlite3StrAccumFinish(&acc);
 27307  27715     return z;
 27308  27716   }
 27309  27717   
 27310  27718   /*
 27311  27719   ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
 27312  27720   ** %-conversion extensions.
................................................................................
 27343  27751     if( zBuf==0 || zFormat==0 ) {
 27344  27752       (void)SQLITE_MISUSE_BKPT;
 27345  27753       if( zBuf ) zBuf[0] = 0;
 27346  27754       return zBuf;
 27347  27755     }
 27348  27756   #endif
 27349  27757     sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
 27350         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27758  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27351  27759     zBuf[acc.nChar] = 0;
 27352  27760     return zBuf;
 27353  27761   }
 27354  27762   SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
 27355  27763     char *z;
 27356  27764     va_list ap;
 27357  27765     va_start(ap,zFormat);
................................................................................
 27365  27773   ** We house it in a separate routine from sqlite3_log() to avoid using
 27366  27774   ** stack space on small-stack systems when logging is disabled.
 27367  27775   **
 27368  27776   ** sqlite3_log() must render into a static buffer.  It cannot dynamically
 27369  27777   ** allocate memory because it might be called while the memory allocator
 27370  27778   ** mutex is held.
 27371  27779   **
 27372         -** sqlite3VXPrintf() might ask for *temporary* memory allocations for
        27780  +** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
 27373  27781   ** certain format characters (%q) or for very large precisions or widths.
 27374  27782   ** Care must be taken that any sqlite3_log() calls that occur while the
 27375  27783   ** memory mutex is held do not use these mechanisms.
 27376  27784   */
 27377  27785   static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
 27378  27786     StrAccum acc;                          /* String accumulator */
 27379  27787     char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
 27380  27788   
 27381  27789     sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
 27382         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27790  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27383  27791     sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
 27384  27792                              sqlite3StrAccumFinish(&acc));
 27385  27793   }
 27386  27794   
 27387  27795   /*
 27388  27796   ** Format and write a message to the log if logging is enabled.
 27389  27797   */
................................................................................
 27404  27812   */
 27405  27813   SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
 27406  27814     va_list ap;
 27407  27815     StrAccum acc;
 27408  27816     char zBuf[500];
 27409  27817     sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
 27410  27818     va_start(ap,zFormat);
 27411         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27819  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27412  27820     va_end(ap);
 27413  27821     sqlite3StrAccumFinish(&acc);
 27414  27822   #ifdef SQLITE_OS_TRACE_PROC
 27415  27823     {
 27416  27824       extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
 27417  27825       SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
 27418  27826     }
................................................................................
 27421  27829     fflush(stdout);
 27422  27830   #endif
 27423  27831   }
 27424  27832   #endif
 27425  27833   
 27426  27834   
 27427  27835   /*
 27428         -** variable-argument wrapper around sqlite3VXPrintf().  The bFlags argument
        27836  +** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
 27429  27837   ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
 27430  27838   */
 27431         -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
        27839  +SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
 27432  27840     va_list ap;
 27433  27841     va_start(ap,zFormat);
 27434         -  sqlite3VXPrintf(p, zFormat, ap);
        27842  +  sqlite3_str_vappendf(p, zFormat, ap);
 27435  27843     va_end(ap);
 27436  27844   }
 27437  27845   
 27438  27846   /************** End of printf.c **********************************************/
 27439  27847   /************** Begin file treeview.c ****************************************/
 27440  27848   /*
 27441  27849   ** 2015-06-08
................................................................................
 27493  27901     va_list ap;
 27494  27902     int i;
 27495  27903     StrAccum acc;
 27496  27904     char zBuf[500];
 27497  27905     sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
 27498  27906     if( p ){
 27499  27907       for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
 27500         -      sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
        27908  +      sqlite3_str_append(&acc, p->bLine[i] ? "|   " : "    ", 4);
 27501  27909       }
 27502         -    sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
        27910  +    sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
 27503  27911     }
 27504         -  va_start(ap, zFormat);
 27505         -  sqlite3VXPrintf(&acc, zFormat, ap);
 27506         -  va_end(ap);
 27507         -  assert( acc.nChar>0 );
 27508         -  if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
        27912  +  if( zFormat!=0 ){
        27913  +    va_start(ap, zFormat);
        27914  +    sqlite3_str_vappendf(&acc, zFormat, ap);
        27915  +    va_end(ap);
        27916  +    assert( acc.nChar>0 );
        27917  +    sqlite3_str_append(&acc, "\n", 1);
        27918  +  }
 27509  27919     sqlite3StrAccumFinish(&acc);
 27510  27920     fprintf(stdout,"%s", zBuf);
 27511  27921     fflush(stdout);
 27512  27922   }
 27513  27923   
 27514  27924   /*
 27515  27925   ** Shorthand for starting a new tree item that consists of a single label
................................................................................
 27534  27944     if( pWith->nCte>0 ){
 27535  27945       pView = sqlite3TreeViewPush(pView, 1);
 27536  27946       for(i=0; i<pWith->nCte; i++){
 27537  27947         StrAccum x;
 27538  27948         char zLine[1000];
 27539  27949         const struct Cte *pCte = &pWith->a[i];
 27540  27950         sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
 27541         -      sqlite3XPrintf(&x, "%s", pCte->zName);
        27951  +      sqlite3_str_appendf(&x, "%s", pCte->zName);
 27542  27952         if( pCte->pCols && pCte->pCols->nExpr>0 ){
 27543  27953           char cSep = '(';
 27544  27954           int j;
 27545  27955           for(j=0; j<pCte->pCols->nExpr; j++){
 27546         -          sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
        27956  +          sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
 27547  27957             cSep = ',';
 27548  27958           }
 27549         -        sqlite3XPrintf(&x, ")");
        27959  +        sqlite3_str_appendf(&x, ")");
 27550  27960         }
 27551         -      sqlite3XPrintf(&x, " AS");
        27961  +      sqlite3_str_appendf(&x, " AS");
 27552  27962         sqlite3StrAccumFinish(&x);
 27553  27963         sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
 27554  27964         sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
 27555  27965         sqlite3TreeViewPop(pView);
 27556  27966       }
 27557  27967       sqlite3TreeViewPop(pView);
 27558  27968     }
................................................................................
 27609  28019         pView = sqlite3TreeViewPush(pView, (n--)>0);
 27610  28020         sqlite3TreeViewLine(pView, "FROM");
 27611  28021         for(i=0; i<p->pSrc->nSrc; i++){
 27612  28022           struct SrcList_item *pItem = &p->pSrc->a[i];
 27613  28023           StrAccum x;
 27614  28024           char zLine[100];
 27615  28025           sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
 27616         -        sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor);
        28026  +        sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
 27617  28027           if( pItem->zDatabase ){
 27618         -          sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
        28028  +          sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
 27619  28029           }else if( pItem->zName ){
 27620         -          sqlite3XPrintf(&x, " %s", pItem->zName);
        28030  +          sqlite3_str_appendf(&x, " %s", pItem->zName);
 27621  28031           }
 27622  28032           if( pItem->pTab ){
 27623         -          sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName);
        28033  +          sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName);
 27624  28034           }
 27625  28035           if( pItem->zAlias ){
 27626         -          sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias);
        28036  +          sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
 27627  28037           }
 27628  28038           if( pItem->fg.jointype & JT_LEFT ){
 27629         -          sqlite3XPrintf(&x, " LEFT-JOIN");
        28039  +          sqlite3_str_appendf(&x, " LEFT-JOIN");
 27630  28040           }
 27631  28041           sqlite3StrAccumFinish(&x);
 27632  28042           sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
 27633  28043           if( pItem->pSelect ){
 27634  28044             sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
 27635  28045           }
 27636  28046           if( pItem->fg.isTabFunc ){
................................................................................
 27971  28381       sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
 27972  28382     }else{
 27973  28383       int i;
 27974  28384       sqlite3TreeViewLine(pView, "%s", zLabel);
 27975  28385       for(i=0; i<pList->nExpr; i++){
 27976  28386         int j = pList->a[i].u.x.iOrderByCol;
 27977  28387         char *zName = pList->a[i].zName;
        28388  +      int moreToFollow = i<pList->nExpr - 1;
 27978  28389         if( j || zName ){
 27979         -        sqlite3TreeViewPush(pView, 0);
        28390  +        sqlite3TreeViewPush(pView, moreToFollow);
        28391  +        moreToFollow = 0;
        28392  +        sqlite3TreeViewLine(pView, 0);
        28393  +        if( zName ){
        28394  +          fprintf(stdout, "AS %s ", zName);
        28395  +        }
        28396  +        if( j ){
        28397  +          fprintf(stdout, "iOrderByCol=%d", j);
        28398  +        }
        28399  +        fprintf(stdout, "\n");
        28400  +        fflush(stdout);
 27980  28401         }
 27981         -      if( zName ){
 27982         -        sqlite3TreeViewLine(pView, "AS %s", zName);
 27983         -      }
 27984         -      if( j ){
 27985         -        sqlite3TreeViewLine(pView, "iOrderByCol=%d", j);
 27986         -      }
 27987         -      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
        28402  +      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
 27988  28403         if( j || zName ){
 27989  28404           sqlite3TreeViewPop(pView);
 27990  28405         }
 27991  28406       }
 27992  28407     }
 27993  28408   }
 27994  28409   SQLITE_PRIVATE void sqlite3TreeViewExprList(
................................................................................
 30946  31361       /*  77 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
 30947  31362       /*  78 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
 30948  31363       /*  79 */ "CollSeq"          OpHelp(""),
 30949  31364       /*  80 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
 30950  31365       /*  81 */ "RealAffinity"     OpHelp(""),
 30951  31366       /*  82 */ "Cast"             OpHelp("affinity(r[P1])"),
 30952  31367       /*  83 */ "Permutation"      OpHelp(""),
 30953         -    /*  84 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 30954         -    /*  85 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 30955         -    /*  86 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 30956         -    /*  87 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 30957         -    /*  88 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 30958         -    /*  89 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 30959         -    /*  90 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 30960         -    /*  91 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 30961         -    /*  92 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 30962         -    /*  93 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 30963         -    /*  94 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 30964         -    /*  95 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 30965         -    /*  96 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
 30966         -    /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 30967         -    /*  98 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
 30968         -    /*  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'"),
 30969  31384       /* 100 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 30970  31385       /* 101 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 30971  31386       /* 102 */ "Count"            OpHelp("r[P2]=count()"),
 30972  31387       /* 103 */ "ReadCookie"       OpHelp(""),
 30973  31388       /* 104 */ "SetCookie"        OpHelp(""),
 30974  31389       /* 105 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 30975  31390       /* 106 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
................................................................................
 30994  31409       /* 125 */ "Rowid"            OpHelp("r[P2]=rowid"),
 30995  31410       /* 126 */ "NullRow"          OpHelp(""),
 30996  31411       /* 127 */ "SeekEnd"          OpHelp(""),
 30997  31412       /* 128 */ "SorterInsert"     OpHelp("key=r[P2]"),
 30998  31413       /* 129 */ "IdxInsert"        OpHelp("key=r[P2]"),
 30999  31414       /* 130 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 31000  31415       /* 131 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 31001         -    /* 132 */ "Real"             OpHelp("r[P2]=P4"),
 31002         -    /* 133 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 31003         -    /* 134 */ "Destroy"          OpHelp(""),
        31416  +    /* 132 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        31417  +    /* 133 */ "Destroy"          OpHelp(""),
        31418  +    /* 134 */ "Real"             OpHelp("r[P2]=P4"),
 31004  31419       /* 135 */ "Clear"            OpHelp(""),
 31005  31420       /* 136 */ "ResetSorter"      OpHelp(""),
 31006  31421       /* 137 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 31007  31422       /* 138 */ "SqlExec"          OpHelp(""),
 31008  31423       /* 139 */ "ParseSchema"      OpHelp(""),
 31009  31424       /* 140 */ "LoadAnalysis"     OpHelp(""),
 31010  31425       /* 141 */ "DropTable"        OpHelp(""),
................................................................................
 31033  31448       /* 164 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 31034  31449       /* 165 */ "PureFunc"         OpHelp(""),
 31035  31450       /* 166 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 31036  31451       /* 167 */ "Trace"            OpHelp(""),
 31037  31452       /* 168 */ "CursorHint"       OpHelp(""),
 31038  31453       /* 169 */ "Noop"             OpHelp(""),
 31039  31454       /* 170 */ "Explain"          OpHelp(""),
        31455  +    /* 171 */ "Abortable"        OpHelp(""),
 31040  31456     };
 31041  31457     return azName[i];
 31042  31458   }
 31043  31459   #endif
 31044  31460   
 31045  31461   /************** End of opcodes.c *********************************************/
 31046  31462   /************** Begin file os_unix.c *****************************************/
................................................................................
 35019  35435         /* The code below is handling the return value of osFallocate() 
 35020  35436         ** correctly. posix_fallocate() is defined to "returns zero on success, 
 35021  35437         ** or an error number on  failure". See the manpage for details. */
 35022  35438         int err;
 35023  35439         do{
 35024  35440           err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
 35025  35441         }while( err==EINTR );
 35026         -      if( err ) return SQLITE_IOERR_WRITE;
        35442  +      if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE;
 35027  35443   #else
 35028  35444         /* If the OS does not have posix_fallocate(), fake it. Write a 
 35029  35445         ** single byte to the last byte in each block that falls entirely
 35030  35446         ** within the extended region. Then, if required, a single byte
 35031  35447         ** at offset (nSize-1), to set the size of the file correctly.
 35032  35448         ** This is a similar technique to that used by glibc on systems
 35033  35449         ** that do not have a real fallocate() call.
................................................................................
 39544  39960   /*
 39545  39961    * The size of the buffer used by sqlite3_win32_write_debug().
 39546  39962    */
 39547  39963   #ifndef SQLITE_WIN32_DBG_BUF_SIZE
 39548  39964   #  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
 39549  39965   #endif
 39550  39966   
 39551         -/*
 39552         - * The value used with sqlite3_win32_set_directory() to specify that
 39553         - * the data directory should be changed.
 39554         - */
 39555         -#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
 39556         -#  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
 39557         -#endif
 39558         -
 39559         -/*
 39560         - * The value used with sqlite3_win32_set_directory() to specify that
 39561         - * the temporary directory should be changed.
 39562         - */
 39563         -#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
 39564         -#  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
 39565         -#endif
 39566         -
 39567  39967   /*
 39568  39968    * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
 39569  39969    * various Win32 API heap functions instead of our own.
 39570  39970    */
 39571  39971   #ifdef SQLITE_WIN32_MALLOC
 39572  39972   
 39573  39973   /*
................................................................................
 41156  41556   #ifndef SQLITE_OMIT_AUTOINIT
 41157  41557     if( sqlite3_initialize() ) return 0;
 41158  41558   #endif
 41159  41559     return winUtf8ToMbcs(zText, useAnsi);
 41160  41560   }
 41161  41561   
 41162  41562   /*
 41163         -** This function sets the data directory or the temporary directory based on
 41164         -** the provided arguments.  The type argument must be 1 in order to set the
 41165         -** data directory or 2 in order to set the temporary directory.  The zValue
 41166         -** argument is the name of the directory to use.  The return value will be
 41167         -** SQLITE_OK if successful.
        41563  +** This function is the same as sqlite3_win32_set_directory (below); however,
        41564  +** it accepts a UTF-8 string.
 41168  41565   */
 41169         -SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
        41566  +SQLITE_API int sqlite3_win32_set_directory8(
        41567  +  unsigned long type, /* Identifier for directory being set or reset */
        41568  +  const char *zValue  /* New value for directory being set or reset */
        41569  +){
 41170  41570     char **ppDirectory = 0;
 41171  41571   #ifndef SQLITE_OMIT_AUTOINIT
 41172  41572     int rc = sqlite3_initialize();
 41173  41573     if( rc ) return rc;
 41174  41574   #endif
 41175  41575     if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
 41176  41576       ppDirectory = &sqlite3_data_directory;
................................................................................
 41178  41578       ppDirectory = &sqlite3_temp_directory;
 41179  41579     }
 41180  41580     assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
 41181  41581             || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
 41182  41582     );
 41183  41583     assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
 41184  41584     if( ppDirectory ){
 41185         -    char *zValueUtf8 = 0;
        41585  +    char *zCopy = 0;
 41186  41586       if( zValue && zValue[0] ){
 41187         -      zValueUtf8 = winUnicodeToUtf8(zValue);
 41188         -      if ( zValueUtf8==0 ){
        41587  +      zCopy = sqlite3_mprintf("%s", zValue);
        41588  +      if ( zCopy==0 ){
 41189  41589           return SQLITE_NOMEM_BKPT;
 41190  41590         }
 41191  41591       }
 41192  41592       sqlite3_free(*ppDirectory);
 41193         -    *ppDirectory = zValueUtf8;
        41593  +    *ppDirectory = zCopy;
 41194  41594       return SQLITE_OK;
 41195  41595     }
 41196  41596     return SQLITE_ERROR;
 41197  41597   }
        41598  +
        41599  +/*
        41600  +** This function is the same as sqlite3_win32_set_directory (below); however,
        41601  +** it accepts a UTF-16 string.
        41602  +*/
        41603  +SQLITE_API int sqlite3_win32_set_directory16(
        41604  +  unsigned long type, /* Identifier for directory being set or reset */
        41605  +  const void *zValue  /* New value for directory being set or reset */
        41606  +){
        41607  +  int rc;
        41608  +  char *zUtf8 = 0;
        41609  +  if( zValue ){
        41610  +    zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
        41611  +    if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
        41612  +  }
        41613  +  rc = sqlite3_win32_set_directory8(type, zUtf8);
        41614  +  if( zUtf8 ) sqlite3_free(zUtf8);
        41615  +  return rc;
        41616  +}
        41617  +
        41618  +/*
        41619  +** This function sets the data directory or the temporary directory based on
        41620  +** the provided arguments.  The type argument must be 1 in order to set the
        41621  +** data directory or 2 in order to set the temporary directory.  The zValue
        41622  +** argument is the name of the directory to use.  The return value will be
        41623  +** SQLITE_OK if successful.
        41624  +*/
        41625  +SQLITE_API int sqlite3_win32_set_directory(
        41626  +  unsigned long type, /* Identifier for directory being set or reset */
        41627  +  void *zValue        /* New value for directory being set or reset */
        41628  +){
        41629  +  return sqlite3_win32_set_directory16(type, zValue);
        41630  +}
 41198  41631   
 41199  41632   /*
 41200  41633   ** The return value of winGetLastErrorMsg
 41201  41634   ** is zero if the error message fits in the buffer, or non-zero
 41202  41635   ** otherwise (if the message was truncated).
 41203  41636   */
 41204  41637   static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
................................................................................
 61478  61911     for(i=0; i<db->nDb; i++){
 61479  61912       p = db->aDb[i].pBt;
 61480  61913       if( p && p->sharable ){
 61481  61914         sqlite3BtreeEnter(p);
 61482  61915         skipOk = 0;
 61483  61916       }
 61484  61917     }
 61485         -  db->skipBtreeMutex = skipOk;
        61918  +  db->noSharedCache = skipOk;
 61486  61919   }
 61487  61920   SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 61488         -  if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
        61921  +  if( db->noSharedCache==0 ) btreeEnterAll(db);
 61489  61922   }
 61490  61923   static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
 61491  61924     int i;
 61492  61925     Btree *p;
 61493  61926     assert( sqlite3_mutex_held(db->mutex) );
 61494  61927     for(i=0; i<db->nDb; i++){
 61495  61928       p = db->aDb[i].pBt;
 61496  61929       if( p ) sqlite3BtreeLeave(p);
 61497  61930     }
 61498  61931   }
 61499  61932   SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
 61500         -  if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
        61933  +  if( db->noSharedCache==0 ) btreeLeaveAll(db);
 61501  61934   }
 61502  61935   
 61503  61936   #ifndef NDEBUG
 61504  61937   /*
 61505  61938   ** Return true if the current thread holds the database connection
 61506  61939   ** mutex and all required BtShared mutexes.
 61507  61940   **
................................................................................
 62458  62891   **
 62459  62892   ** Calling this routine with a NULL cursor pointer returns false.
 62460  62893   **
 62461  62894   ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
 62462  62895   ** back to where it ought to be if this routine returns true.
 62463  62896   */
 62464  62897   SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
 62465         -  return pCur->eState!=CURSOR_VALID;
        62898  +  assert( EIGHT_BYTE_ALIGNMENT(pCur)
        62899  +       || pCur==sqlite3BtreeFakeValidCursor() );
        62900  +  assert( offsetof(BtCursor, eState)==0 );
        62901  +  assert( sizeof(pCur->eState)==1 );
        62902  +  return CURSOR_VALID != *(u8*)pCur;
 62466  62903   }
 62467  62904   
 62468  62905   /*
 62469  62906   ** Return a pointer to a fake BtCursor object that will always answer
 62470  62907   ** false to the sqlite3BtreeCursorHasMoved() routine above.  The fake
 62471  62908   ** cursor returned must not be used with any other Btree interface.
 62472  62909   */
................................................................................
 64565  65002             pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
 64566  65003       }
 64567  65004     }
 64568  65005   }
 64569  65006   #else
 64570  65007   # define setDefaultSyncFlag(pBt,safety_level)
 64571  65008   #endif
        65009  +
        65010  +/* Forward declaration */
        65011  +static int newDatabase(BtShared*);
        65012  +
 64572  65013   
 64573  65014   /*
 64574  65015   ** Get a reference to pPage1 of the database file.  This will
 64575  65016   ** also acquire a readlock on that file.
 64576  65017   **
 64577  65018   ** SQLITE_OK is returned on success.  If the file is not a
 64578  65019   ** well-formed database file, then SQLITE_CORRUPT is returned.
................................................................................
 64596  65037     /* Do some checking to help insure the file we opened really is
 64597  65038     ** a valid database file. 
 64598  65039     */
 64599  65040     nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
 64600  65041     sqlite3PagerPagecount(pBt->pPager, &nPageFile);
 64601  65042     if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
 64602  65043       nPage = nPageFile;
        65044  +  }
        65045  +  if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
        65046  +    nPage = 0;
 64603  65047     }
 64604  65048     if( nPage>0 ){
 64605  65049       u32 pageSize;
 64606  65050       u32 usableSize;
 64607  65051       u8 *page1 = pPage1->aData;
 64608  65052       rc = SQLITE_NOTADB;
 64609  65053       /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
................................................................................
 67813  68257     u32 ovflPageSize;
 67814  68258   
 67815  68259     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 67816  68260     pPage->xParseCell(pPage, pCell, pInfo);
 67817  68261     if( pInfo->nLocal==pInfo->nPayload ){
 67818  68262       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
 67819  68263     }
 67820         -  if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
        68264  +  testcase( pCell + pInfo->nSize == pPage->aDataEnd );
        68265  +  testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
        68266  +  if( pCell + pInfo->nSize > pPage->aDataEnd ){
 67821  68267       /* Cell extends past end of page */
 67822  68268       return SQLITE_CORRUPT_PAGE(pPage);
 67823  68269     }
 67824  68270     ovflPgno = get4byte(pCell + pInfo->nSize - 4);
 67825  68271     pBt = pPage->pBt;
 67826  68272     assert( pBt->usableSize > 4 );
 67827  68273     ovflPageSize = pBt->usableSize - 4;
................................................................................
 69739  70185   
 69740  70186     if( pFree ){
 69741  70187       sqlite3PageFree(pFree);
 69742  70188     }
 69743  70189     return rc;
 69744  70190   }
 69745  70191   
        70192  +/* Overwrite content from pX into pDest.  Only do the write if the
        70193  +** content is different from what is already there.
        70194  +*/
        70195  +static int btreeOverwriteContent(
        70196  +  MemPage *pPage,           /* MemPage on which writing will occur */
        70197  +  u8 *pDest,                /* Pointer to the place to start writing */
        70198  +  const BtreePayload *pX,   /* Source of data to write */
        70199  +  int iOffset,              /* Offset of first byte to write */
        70200  +  int iAmt                  /* Number of bytes to be written */
        70201  +){
        70202  +  int nData = pX->nData - iOffset;
        70203  +  if( nData<=0 ){
        70204  +    /* Overwritting with zeros */
        70205  +    int i;
        70206  +    for(i=0; i<iAmt && pDest[i]==0; i++){}
        70207  +    if( i<iAmt ){
        70208  +      int rc = sqlite3PagerWrite(pPage->pDbPage);
        70209  +      if( rc ) return rc;
        70210  +      memset(pDest + i, 0, iAmt - i);
        70211  +    }
        70212  +  }else{
        70213  +    if( nData<iAmt ){
        70214  +      /* Mixed read data and zeros at the end.  Make a recursive call
        70215  +      ** to write the zeros then fall through to write the real data */
        70216  +      int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
        70217  +                                 iAmt-nData);
        70218  +      if( rc ) return rc;
        70219  +      iAmt = nData;
        70220  +    }
        70221  +    if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
        70222  +      int rc = sqlite3PagerWrite(pPage->pDbPage);
        70223  +      if( rc ) return rc;
        70224  +      memcpy(pDest, ((u8*)pX->pData) + iOffset, iAmt);
        70225  +    }
        70226  +  }
        70227  +  return SQLITE_OK;
        70228  +}
        70229  +
        70230  +/*
        70231  +** Overwrite the cell that cursor pCur is pointing to with fresh content
        70232  +** contained in pX.
        70233  +*/
        70234  +static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
        70235  +  int iOffset;                        /* Next byte of pX->pData to write */
        70236  +  int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
        70237  +  int rc;                             /* Return code */
        70238  +  MemPage *pPage = pCur->pPage;       /* Page being written */
        70239  +  BtShared *pBt;                      /* Btree */
        70240  +  Pgno ovflPgno;                      /* Next overflow page to write */
        70241  +  u32 ovflPageSize;                   /* Size to write on overflow page */
        70242  +
        70243  +  if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
        70244  +    return SQLITE_CORRUPT_BKPT;
        70245  +  }
        70246  +  /* Overwrite the local portion first */
        70247  +  rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
        70248  +                             0, pCur->info.nLocal);
        70249  +  if( rc ) return rc;
        70250  +  if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
        70251  +
        70252  +  /* Now overwrite the overflow pages */
        70253  +  iOffset = pCur->info.nLocal;
        70254  +  assert( nTotal>=0 );
        70255  +  assert( iOffset>=0 );
        70256  +  ovflPgno = get4byte(pCur->info.pPayload + iOffset);
        70257  +  pBt = pPage->pBt;
        70258  +  ovflPageSize = pBt->usableSize - 4;
        70259  +  do{
        70260  +    rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
        70261  +    if( rc ) return rc;
        70262  +    if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
        70263  +      rc = SQLITE_CORRUPT_BKPT;
        70264  +    }else{
        70265  +      if( iOffset+ovflPageSize<(u32)nTotal ){
        70266  +        ovflPgno = get4byte(pPage->aData);
        70267  +      }else{
        70268  +        ovflPageSize = nTotal - iOffset;
        70269  +      }
        70270  +      rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
        70271  +                                 iOffset, ovflPageSize);
        70272  +    }
        70273  +    sqlite3PagerUnref(pPage->pDbPage);
        70274  +    if( rc ) return rc;
        70275  +    iOffset += ovflPageSize;
        70276  +  }while( iOffset<nTotal );
        70277  +  return SQLITE_OK;    
        70278  +}
        70279  +
 69746  70280   
 69747  70281   /*
 69748  70282   ** Insert a new record into the BTree.  The content of the new record
 69749  70283   ** is described by the pX object.  The pCur cursor is used only to
 69750  70284   ** define what table the record should be inserted into, and is left
 69751  70285   ** pointing at a random location.
 69752  70286   **
................................................................................
 69829  70363     if( pCur->pKeyInfo==0 ){
 69830  70364       assert( pX->pKey==0 );
 69831  70365       /* If this is an insert into a table b-tree, invalidate any incrblob 
 69832  70366       ** cursors open on the row being replaced */
 69833  70367       invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
 69834  70368   
 69835  70369       /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 
 69836         -    ** to a row with the same key as the new entry being inserted.  */
 69837         -    assert( (flags & BTREE_SAVEPOSITION)==0 || 
 69838         -            ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
        70370  +    ** to a row with the same key as the new entry being inserted.
        70371  +    */
        70372  +#ifdef SQLITE_DEBUG
        70373  +    if( flags & BTREE_SAVEPOSITION ){
        70374  +      assert( pCur->curFlags & BTCF_ValidNKey );
        70375  +      assert( pX->nKey==pCur->info.nKey );
        70376  +      assert( pCur->info.nSize!=0 );
        70377  +      assert( loc==0 );
        70378  +    }
        70379  +#endif
 69839  70380   
 69840         -    /* If the cursor is currently on the last row and we are appending a
 69841         -    ** new row onto the end, set the "loc" to avoid an unnecessary
 69842         -    ** btreeMoveto() call */
        70381  +    /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
        70382  +    ** that the cursor is not pointing to a row to be overwritten.
        70383  +    ** So do a complete check.
        70384  +    */
 69843  70385       if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
 69844         -      loc = 0;
        70386  +      /* The cursor is pointing to the entry that is to be
        70387  +      ** overwritten */
        70388  +      assert( pX->nData>=0 && pX->nZero>=0 );
        70389  +      if( pCur->info.nSize!=0
        70390  +       && pCur->info.nPayload==(u32)pX->nData+pX->nZero
        70391  +      ){
        70392  +        /* New entry is the same size as the old.  Do an overwrite */
        70393  +        return btreeOverwriteCell(pCur, pX);
        70394  +      }
        70395  +      assert( loc==0 );
 69845  70396       }else if( loc==0 ){
        70397  +      /* The cursor is *not* pointing to the cell to be overwritten, nor
        70398  +      ** to an adjacent cell.  Move the cursor so that it is pointing either
        70399  +      ** to the cell to be overwritten or an adjacent cell.
        70400  +      */
 69846  70401         rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
 69847  70402         if( rc ) return rc;
 69848  70403       }
 69849         -  }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
 69850         -    if( pX->nMem ){
 69851         -      UnpackedRecord r;
 69852         -      r.pKeyInfo = pCur->pKeyInfo;
 69853         -      r.aMem = pX->aMem;
 69854         -      r.nField = pX->nMem;
 69855         -      r.default_rc = 0;
 69856         -      r.errCode = 0;
 69857         -      r.r1 = 0;
 69858         -      r.r2 = 0;
 69859         -      r.eqSeen = 0;
 69860         -      rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
 69861         -    }else{
 69862         -      rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
 69863         -    }
 69864         -    if( rc ) return rc;
        70404  +  }else{
        70405  +    /* This is an index or a WITHOUT ROWID table */
        70406  +
        70407  +    /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 
        70408  +    ** to a row with the same key as the new entry being inserted.
        70409  +    */
        70410  +    assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
        70411  +
        70412  +    /* If the cursor is not already pointing either to the cell to be
        70413  +    ** overwritten, or if a new cell is being inserted, if the cursor is
        70414  +    ** not pointing to an immediately adjacent cell, then move the cursor
        70415  +    ** so that it does.
        70416  +    */
        70417  +    if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
        70418  +      if( pX->nMem ){
        70419  +        UnpackedRecord r;
        70420  +        r.pKeyInfo = pCur->pKeyInfo;
        70421  +        r.aMem = pX->aMem;
        70422  +        r.nField = pX->nMem;
        70423  +        r.default_rc = 0;
        70424  +        r.errCode = 0;
        70425  +        r.r1 = 0;
        70426  +        r.r2 = 0;
        70427  +        r.eqSeen = 0;
        70428  +        rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
        70429  +      }else{
        70430  +        rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
        70431  +      }
        70432  +      if( rc ) return rc;
        70433  +    }
        70434  +
        70435  +    /* If the cursor is currently pointing to an entry to be overwritten
        70436  +    ** and the new content is the same as as the old, then use the
        70437  +    ** overwrite optimization.
        70438  +    */
        70439  +    if( loc==0 ){
        70440  +      getCellInfo(pCur);
        70441  +      if( pCur->info.nKey==pX->nKey ){
        70442  +        BtreePayload x2;
        70443  +        x2.pData = pX->pKey;
        70444  +        x2.nData = pX->nKey;
        70445  +        x2.nZero = 0;
        70446  +        return btreeOverwriteCell(pCur, &x2);
        70447  +      }
        70448  +    }
        70449  +
 69865  70450     }
 69866  70451     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 69867  70452   
 69868  70453     pPage = pCur->pPage;
 69869  70454     assert( pPage->intKey || pX->nKey>=0 );
 69870  70455     assert( pPage->leaf || !pPage->intKey );
 69871  70456   
................................................................................
 70696  71281   ){
 70697  71282     va_list ap;
 70698  71283     if( !pCheck->mxErr ) return;
 70699  71284     pCheck->mxErr--;
 70700  71285     pCheck->nErr++;
 70701  71286     va_start(ap, zFormat);
 70702  71287     if( pCheck->errMsg.nChar ){
 70703         -    sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
        71288  +    sqlite3_str_append(&pCheck->errMsg, "\n", 1);
 70704  71289     }
 70705  71290     if( pCheck->zPfx ){
 70706         -    sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
        71291  +    sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
 70707  71292     }
 70708         -  sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap);
        71293  +  sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
 70709  71294     va_end(ap);
 70710         -  if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
        71295  +  if( pCheck->errMsg.accError==SQLITE_NOMEM ){
 70711  71296       pCheck->mallocFailed = 1;
 70712  71297     }
 70713  71298   }
 70714  71299   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 70715  71300   
 70716  71301   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 70717  71302   
................................................................................
 71287  71872   
 71288  71873     /* Clean  up and report errors.
 71289  71874     */
 71290  71875   integrity_ck_cleanup:
 71291  71876     sqlite3PageFree(sCheck.heap);
 71292  71877     sqlite3_free(sCheck.aPgRef);
 71293  71878     if( sCheck.mallocFailed ){
 71294         -    sqlite3StrAccumReset(&sCheck.errMsg);
        71879  +    sqlite3_str_reset(&sCheck.errMsg);
 71295  71880       sCheck.nErr++;
 71296  71881     }
 71297  71882     *pnErr = sCheck.nErr;
 71298         -  if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
        71883  +  if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
 71299  71884     /* Make sure this analysis did not leave any unref() pages. */
 71300  71885     assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
 71301  71886     sqlite3BtreeLeave(p);
 71302  71887     return sqlite3StrAccumFinish(&sCheck.errMsg);
 71303  71888   }
 71304  71889   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 71305  71890   
................................................................................
 73191  73776     }else{
 73192  73777       pMem->u.i = val;
 73193  73778       pMem->flags = MEM_Int;
 73194  73779     }
 73195  73780   }
 73196  73781   
 73197  73782   /* A no-op destructor */
 73198         -static void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
        73783  +SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
 73199  73784   
 73200  73785   /*
 73201  73786   ** Set the value stored in *pMem should already be a NULL.
 73202  73787   ** Also store a pointer to go with it.
 73203  73788   */
 73204  73789   SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
 73205  73790     Mem *pMem,
................................................................................
 73869  74454       zVal = &pExpr->u.zToken[2];
 73870  74455       nVal = sqlite3Strlen30(zVal)-1;
 73871  74456       assert( zVal[nVal]=='\'' );
 73872  74457       sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
 73873  74458                            0, SQLITE_DYNAMIC);
 73874  74459     }
 73875  74460   #endif
 73876         -
 73877  74461   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 73878  74462     else if( op==TK_FUNCTION && pCtx!=0 ){
 73879  74463       rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
 73880  74464     }
 73881  74465   #endif
        74466  +  else if( op==TK_TRUEFALSE ){
        74467  +     pVal = valueNew(db, pCtx);
        74468  +     pVal->flags = MEM_Int;
        74469  +     pVal->u.i = pExpr->u.zToken[4]==0;
        74470  +  }
 73882  74471   
 73883  74472     *ppVal = pVal;
 73884  74473     return rc;
 73885  74474   
 73886  74475   no_mem:
 73887  74476   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 73888  74477     if( pCtx==0 || pCtx->pParse->nErr==0 )
................................................................................
 74533  75122     int p4type          /* P4 operand type */
 74534  75123   ){
 74535  75124     char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
 74536  75125     if( p4copy ) memcpy(p4copy, zP4, 8);
 74537  75126     return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
 74538  75127   }
 74539  75128   
        75129  +#ifndef SQLITE_OMIT_EXPLAIN
        75130  +/*
        75131  +** Return the address of the current EXPLAIN QUERY PLAN baseline.
        75132  +** 0 means "none".
        75133  +*/
        75134  +SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
        75135  +  VdbeOp *pOp;
        75136  +  if( pParse->addrExplain==0 ) return 0;
        75137  +  pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
        75138  +  return pOp->p2;
        75139  +}
        75140  +
        75141  +/*
        75142  +** Add a new OP_Explain opcode.
        75143  +**
        75144  +** If the bPush flag is true, then make this opcode the parent for
        75145  +** subsequent Explains until sqlite3VdbeExplainPop() is called.
        75146  +*/
        75147  +SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
        75148  +  if( pParse->explain==2 ){
        75149  +    char *zMsg;
        75150  +    Vdbe *v = pParse->pVdbe;
        75151  +    va_list ap;
        75152  +    int iThis;
        75153  +    va_start(ap, zFmt);
        75154  +    zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
        75155  +    va_end(ap);
        75156  +    v = pParse->pVdbe;
        75157  +    iThis = v->nOp;
        75158  +    sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
        75159  +                      zMsg, P4_DYNAMIC);
        75160  +    if( bPush) pParse->addrExplain = iThis;
        75161  +  }
        75162  +}
        75163  +
        75164  +/*
        75165  +** Pop the EXPLAIN QUERY PLAN stack one level.
        75166  +*/
        75167  +SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
        75168  +  pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
        75169  +}
        75170  +#endif /* SQLITE_OMIT_EXPLAIN */
        75171  +
 74540  75172   /*
 74541  75173   ** Add an OP_ParseSchema opcode.  This routine is broken out from
 74542  75174   ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
 74543  75175   ** as having been used.
 74544  75176   **
 74545  75177   ** The zWhere string must have been obtained from sqlite3_malloc().
 74546  75178   ** This routine will take ownership of the allocated memory.
................................................................................
 74622  75254   SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
 74623  75255     Parse *p = v->pParse;
 74624  75256     int j = ADDR(x);
 74625  75257     assert( v->magic==VDBE_MAGIC_INIT );
 74626  75258     assert( j<p->nLabel );
 74627  75259     assert( j>=0 );
 74628  75260     if( p->aLabel ){
        75261  +#ifdef SQLITE_DEBUG
        75262  +    if( p->db->flags & SQLITE_VdbeAddopTrace ){
        75263  +      printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
        75264  +    }
        75265  +#endif
        75266  +    assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
 74629  75267       p->aLabel[j] = v->nOp;
 74630  75268     }
 74631  75269   }
 74632  75270   
        75271  +#ifdef SQLITE_COVERAGE_TEST
        75272  +/*
        75273  +** Return TRUE if and only if the label x has already been resolved.
        75274  +** Return FALSE (zero) if label x is still unresolved.
        75275  +**
        75276  +** This routine is only used inside of testcase() macros, and so it
        75277  +** only exists when measuring test coverage.
        75278  +*/
        75279  +SQLITE_PRIVATE int sqlite3VdbeLabelHasBeenResolved(Vdbe *v, int x){
        75280  +  return v->pParse->aLabel && v->pParse->aLabel[ADDR(x)]>=0;
        75281  +}
        75282  +#endif /* SQLITE_COVERAGE_TEST */
        75283  +
 74633  75284   /*
 74634  75285   ** Mark the VDBE as one that can only be run one time.
 74635  75286   */
 74636  75287   SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
 74637  75288     p->runOnlyOnce = 1;
 74638  75289   }
 74639  75290   
................................................................................
 74770  75421     ** through all opcodes and hasAbort may be set incorrectly. Return
 74771  75422     ** true for this case to prevent the assert() in the callers frame
 74772  75423     ** from failing.  */
 74773  75424     return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
 74774  75425                 || (hasCreateTable && hasInitCoroutine) );
 74775  75426   }
 74776  75427   #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
        75428  +
        75429  +#ifdef SQLITE_DEBUG
        75430  +/*
        75431  +** Increment the nWrite counter in the VDBE if the cursor is not an
        75432  +** ephemeral cursor, or if the cursor argument is NULL.
        75433  +*/
        75434  +SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
        75435  +  if( pC==0
        75436  +   || (pC->eCurType!=CURTYPE_SORTER
        75437  +       && pC->eCurType!=CURTYPE_PSEUDO
        75438  +       && !pC->isEphemeral)
        75439  +  ){
        75440  +    p->nWrite++;
        75441  +  }
        75442  +}
        75443  +#endif
        75444  +
        75445  +#ifdef SQLITE_DEBUG
        75446  +/*
        75447  +** Assert if an Abort at this point in time might result in a corrupt
        75448  +** database.
        75449  +*/
        75450  +SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
        75451  +  assert( p->nWrite==0 || p->usesStmtJournal );
        75452  +}
        75453  +#endif
 74777  75454   
 74778  75455   /*
 74779  75456   ** This routine is called after all opcodes have been inserted.  It loops
 74780  75457   ** through all the opcodes and fixes up some details.
 74781  75458   **
 74782  75459   ** (1) For each jump instruction with a negative P2 value (a label)
 74783  75460   **     resolve the P2 value to an actual address.
................................................................................
 74930  75607     int i;
 74931  75608     for(i=0; i<p->nOp; i++){
 74932  75609       assert( p->aOp[i].opcode!=OP_ResultRow );
 74933  75610     }
 74934  75611   }
 74935  75612   #endif
 74936  75613   
        75614  +/*
        75615  +** Generate code (a single OP_Abortable opcode) that will
        75616  +** verify that the VDBE program can safely call Abort in the current
        75617  +** context.
        75618  +*/
        75619  +#if defined(SQLITE_DEBUG)
        75620  +SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
        75621  +  if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
        75622  +}
        75623  +#endif
        75624  +
 74937  75625   /*
 74938  75626   ** This function returns a pointer to the array of opcodes associated with
 74939  75627   ** the Vdbe passed as the first argument. It is the callers responsibility
 74940  75628   ** to arrange for the returned array to be eventually freed using the 
 74941  75629   ** vdbeFreeOpArray() function.
 74942  75630   **
 74943  75631   ** Before returning, *pnOp is set to the number of entries in the returned
................................................................................
 75474  76162   ** Translate the P4.pExpr value for an OP_CursorHint opcode into text
 75475  76163   ** that can be displayed in the P4 column of EXPLAIN output.
 75476  76164   */
 75477  76165   static void displayP4Expr(StrAccum *p, Expr *pExpr){
 75478  76166     const char *zOp = 0;
 75479  76167     switch( pExpr->op ){
 75480  76168       case TK_STRING:
 75481         -      sqlite3XPrintf(p, "%Q", pExpr->u.zToken);
        76169  +      sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
 75482  76170         break;
 75483  76171       case TK_INTEGER:
 75484         -      sqlite3XPrintf(p, "%d", pExpr->u.iValue);
        76172  +      sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
 75485  76173         break;
 75486  76174       case TK_NULL:
 75487         -      sqlite3XPrintf(p, "NULL");
        76175  +      sqlite3_str_appendf(p, "NULL");
 75488  76176         break;
 75489  76177       case TK_REGISTER: {
 75490         -      sqlite3XPrintf(p, "r[%d]", pExpr->iTable);
        76178  +      sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
 75491  76179         break;
 75492  76180       }
 75493  76181       case TK_COLUMN: {
 75494  76182         if( pExpr->iColumn<0 ){
 75495         -        sqlite3XPrintf(p, "rowid");
        76183  +        sqlite3_str_appendf(p, "rowid");
 75496  76184         }else{
 75497         -        sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn);
        76185  +        sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
 75498  76186         }
 75499  76187         break;
 75500  76188       }
 75501  76189       case TK_LT:      zOp = "LT";      break;
 75502  76190       case TK_LE:      zOp = "LE";      break;
 75503  76191       case TK_GT:      zOp = "GT";      break;
 75504  76192       case TK_GE:      zOp = "GE";      break;
................................................................................
 75522  76210       case TK_UPLUS:   zOp = "PLUS";    break;
 75523  76211       case TK_BITNOT:  zOp = "BITNOT";  break;
 75524  76212       case TK_NOT:     zOp = "NOT";     break;
 75525  76213       case TK_ISNULL:  zOp = "ISNULL";  break;
 75526  76214       case TK_NOTNULL: zOp = "NOTNULL"; break;
 75527  76215   
 75528  76216       default:
 75529         -      sqlite3XPrintf(p, "%s", "expr");
        76217  +      sqlite3_str_appendf(p, "%s", "expr");
 75530  76218         break;
 75531  76219     }
 75532  76220   
 75533  76221     if( zOp ){
 75534         -    sqlite3XPrintf(p, "%s(", zOp);
        76222  +    sqlite3_str_appendf(p, "%s(", zOp);
 75535  76223       displayP4Expr(p, pExpr->pLeft);
 75536  76224       if( pExpr->pRight ){
 75537         -      sqlite3StrAccumAppend(p, ",", 1);
        76225  +      sqlite3_str_append(p, ",", 1);
 75538  76226         displayP4Expr(p, pExpr->pRight);
 75539  76227       }
 75540         -    sqlite3StrAccumAppend(p, ")", 1);
        76228  +    sqlite3_str_append(p, ")", 1);
 75541  76229     }
 75542  76230   }
 75543  76231   #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
 75544  76232   
 75545  76233   
 75546  76234   #if VDBE_DISPLAY_P4
 75547  76235   /*
................................................................................
 75554  76242     assert( nTemp>=20 );
 75555  76243     sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
 75556  76244     switch( pOp->p4type ){
 75557  76245       case P4_KEYINFO: {
 75558  76246         int j;
 75559  76247         KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
 75560  76248         assert( pKeyInfo->aSortOrder!=0 );
 75561         -      sqlite3XPrintf(&x, "k(%d", pKeyInfo->nKeyField);
        76249  +      sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
 75562  76250         for(j=0; j<pKeyInfo->nKeyField; j++){
 75563  76251           CollSeq *pColl = pKeyInfo->aColl[j];
 75564  76252           const char *zColl = pColl ? pColl->zName : "";
 75565  76253           if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
 75566         -        sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
        76254  +        sqlite3_str_appendf(&x, ",%s%s", 
        76255  +               pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
 75567  76256         }
 75568         -      sqlite3StrAccumAppend(&x, ")", 1);
        76257  +      sqlite3_str_append(&x, ")", 1);
 75569  76258         break;
 75570  76259       }
 75571  76260   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 75572  76261       case P4_EXPR: {
 75573  76262         displayP4Expr(&x, pOp->p4.pExpr);
 75574  76263         break;
 75575  76264       }
 75576  76265   #endif
 75577  76266       case P4_COLLSEQ: {
 75578  76267         CollSeq *pColl = pOp->p4.pColl;
 75579         -      sqlite3XPrintf(&x, "(%.20s)", pColl->zName);
        76268  +      sqlite3_str_appendf(&x, "(%.20s)", pColl->zName);
 75580  76269         break;
 75581  76270       }
 75582  76271       case P4_FUNCDEF: {
 75583  76272         FuncDef *pDef = pOp->p4.pFunc;
 75584         -      sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
        76273  +      sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
 75585  76274         break;
 75586  76275       }
 75587  76276   #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 75588  76277       case P4_FUNCCTX: {
 75589  76278         FuncDef *pDef = pOp->p4.pCtx->pFunc;
 75590         -      sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
        76279  +      sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
 75591  76280         break;
 75592  76281       }
 75593  76282   #endif
 75594  76283       case P4_INT64: {
 75595         -      sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64);
        76284  +      sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
 75596  76285         break;
 75597  76286       }
 75598  76287       case P4_INT32: {
 75599         -      sqlite3XPrintf(&x, "%d", pOp->p4.i);
        76288  +      sqlite3_str_appendf(&x, "%d", pOp->p4.i);
 75600  76289         break;
 75601  76290       }
 75602  76291       case P4_REAL: {
 75603         -      sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal);
        76292  +      sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
 75604  76293         break;
 75605  76294       }
 75606  76295       case P4_MEM: {
 75607  76296         Mem *pMem = pOp->p4.pMem;
 75608  76297         if( pMem->flags & MEM_Str ){
 75609  76298           zP4 = pMem->z;
 75610  76299         }else if( pMem->flags & MEM_Int ){
 75611         -        sqlite3XPrintf(&x, "%lld", pMem->u.i);
        76300  +        sqlite3_str_appendf(&x, "%lld", pMem->u.i);
 75612  76301         }else if( pMem->flags & MEM_Real ){
 75613         -        sqlite3XPrintf(&x, "%.16g", pMem->u.r);
        76302  +        sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
 75614  76303         }else if( pMem->flags & MEM_Null ){
 75615  76304           zP4 = "NULL";
 75616  76305         }else{
 75617  76306           assert( pMem->flags & MEM_Blob );
 75618  76307           zP4 = "(blob)";
 75619  76308         }
 75620  76309         break;
 75621  76310       }
 75622  76311   #ifndef SQLITE_OMIT_VIRTUALTABLE
 75623  76312       case P4_VTAB: {
 75624  76313         sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
 75625         -      sqlite3XPrintf(&x, "vtab:%p", pVtab);
        76314  +      sqlite3_str_appendf(&x, "vtab:%p", pVtab);
 75626  76315         break;
 75627  76316       }
 75628  76317   #endif
 75629  76318       case P4_INTARRAY: {
 75630  76319         int i;
 75631  76320         int *ai = pOp->p4.ai;
 75632  76321         int n = ai[0];   /* The first element of an INTARRAY is always the
 75633  76322                          ** count of the number of elements to follow */
 75634  76323         for(i=1; i<=n; i++){
 75635         -        sqlite3XPrintf(&x, ",%d", ai[i]);
        76324  +        sqlite3_str_appendf(&x, ",%d", ai[i]);
 75636  76325         }
 75637  76326         zTemp[0] = '[';
 75638         -      sqlite3StrAccumAppend(&x, "]", 1);
        76327  +      sqlite3_str_append(&x, "]", 1);
 75639  76328         break;
 75640  76329       }
 75641  76330       case P4_SUBPROGRAM: {
 75642         -      sqlite3XPrintf(&x, "program");
        76331  +      sqlite3_str_appendf(&x, "program");
 75643  76332         break;
 75644  76333       }
 75645  76334       case P4_DYNBLOB:
 75646  76335       case P4_ADVANCE: {
 75647  76336         zTemp[0] = 0;
 75648  76337         break;
 75649  76338       }
 75650  76339       case P4_TABLE: {
 75651         -      sqlite3XPrintf(&x, "%s", pOp->p4.pTab->zName);
        76340  +      sqlite3_str_appendf(&x, "%s", pOp->p4.pTab->zName);
 75652  76341         break;
 75653  76342       }
 75654  76343       default: {
 75655  76344         zP4 = pOp->p4.z;
 75656  76345         if( zP4==0 ){
 75657  76346           zP4 = zTemp;
 75658  76347           zTemp[0] = 0;
................................................................................
 75856  76545   ** running the code, it invokes the callback once for each instruction.
 75857  76546   ** This feature is used to implement "EXPLAIN".
 75858  76547   **
 75859  76548   ** When p->explain==1, each instruction is listed.  When
 75860  76549   ** p->explain==2, only OP_Explain instructions are listed and these
 75861  76550   ** are shown in a different format.  p->explain==2 is used to implement
 75862  76551   ** EXPLAIN QUERY PLAN.
        76552  +** 2018-04-24:  In p->explain==2 mode, the OP_Init opcodes of triggers
        76553  +** are also shown, so that the boundaries between the main program and
        76554  +** each trigger are clear.
 75863  76555   **
 75864  76556   ** When p->explain==1, first the main program is listed, then each of
 75865  76557   ** the trigger subprograms are listed one by one.
 75866  76558   */
 75867  76559   SQLITE_PRIVATE int sqlite3VdbeList(
 75868  76560     Vdbe *p                   /* The VDBE */
 75869  76561   ){
................................................................................
 75918  76610         apSub = (SubProgram **)pSub->z;
 75919  76611       }
 75920  76612       for(i=0; i<nSub; i++){
 75921  76613         nRow += apSub[i]->nOp;
 75922  76614       }
 75923  76615     }
 75924  76616   
 75925         -  do{
        76617  +  while(1){  /* Loop exits via break */
 75926  76618       i = p->pc++;
 75927  76619       if( i>=nRow ){
 75928  76620         p->rc = SQLITE_OK;
 75929  76621         rc = SQLITE_DONE;
 75930  76622         break;
 75931  76623       }
 75932  76624       if( i<p->nOp ){
................................................................................
 75964  76656           apSub = (SubProgram **)pSub->z;
 75965  76657           apSub[nSub++] = pOp->p4.pProgram;
 75966  76658           pSub->flags |= MEM_Blob;
 75967  76659           pSub->n = nSub*sizeof(SubProgram*);
 75968  76660           nRow += pOp->p4.pProgram->nOp;
 75969  76661         }
 75970  76662       }
 75971         -  }while( p->explain==2 && pOp->opcode!=OP_Explain );
        76663  +    if( p->explain<2 ) break;
        76664  +    if( pOp->opcode==OP_Explain ) break;
        76665  +    if( pOp->opcode==OP_Init && p->pc>1 ) break;
        76666  +  }
 75972  76667   
 75973  76668     if( rc==SQLITE_OK ){
 75974  76669       if( db->u1.isInterrupted ){
 75975  76670         p->rc = SQLITE_INTERRUPT;
 75976  76671         rc = SQLITE_ERROR;
 75977  76672         sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
 75978  76673       }else{
................................................................................
 77156  77851     if( p->aMem ){
 77157  77852       for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
 77158  77853     }
 77159  77854   #endif
 77160  77855     sqlite3DbFree(db, p->zErrMsg);
 77161  77856     p->zErrMsg = 0;
 77162  77857     p->pResultSet = 0;
        77858  +#ifdef SQLITE_DEBUG
        77859  +  p->nWrite = 0;
        77860  +#endif
 77163  77861   
 77164  77862     /* Save profiling information from this VDBE run.
 77165  77863     */
 77166  77864   #ifdef VDBE_PROFILE
 77167  77865     {
 77168  77866       FILE *out = fopen("vdbe_profile.out", "a");
 77169  77867       if( out ){
................................................................................
 78078  78776       if( x<r ) return -1;
 78079  78777       if( x>r ) return +1;
 78080  78778       return 0;
 78081  78779     }else{
 78082  78780       i64 y;
 78083  78781       double s;
 78084  78782       if( r<-9223372036854775808.0 ) return +1;
 78085         -    if( r>9223372036854775807.0 ) return -1;
        78783  +    if( r>=9223372036854775808.0 ) return -1;
 78086  78784       y = (i64)r;
 78087  78785       if( i<y ) return -1;
 78088         -    if( i>y ){
 78089         -      if( y==SMALLEST_INT64 && r>0.0 ) return -1;
 78090         -      return +1;
 78091         -    }
        78786  +    if( i>y ) return +1;
 78092  78787       s = (double)i;
 78093  78788       if( s<r ) return -1;
 78094  78789       if( s>r ) return +1;
 78095  78790       return 0;
 78096  78791     }
 78097  78792   }
 78098  78793   
................................................................................
 79762  80457     if( *piTime==0 ){
 79763  80458       rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
 79764  80459       if( rc ) *piTime = 0;
 79765  80460     }
 79766  80461     return *piTime;
 79767  80462   }
 79768  80463   
 79769         -/*
 79770         -** The following is the implementation of an SQL function that always
 79771         -** fails with an error message stating that the function is used in the
 79772         -** wrong context.  The sqlite3_overload_function() API might construct
 79773         -** SQL function that use this routine so that the functions will exist
 79774         -** for name resolution but are actually overloaded by the xFindFunction
 79775         -** method of virtual tables.
 79776         -*/
 79777         -SQLITE_PRIVATE void sqlite3InvalidFunction(
 79778         -  sqlite3_context *context,  /* The function calling context */
 79779         -  int NotUsed,               /* Number of arguments to the function */
 79780         -  sqlite3_value **NotUsed2   /* Value of each argument */
 79781         -){
 79782         -  const char *zName = context->pFunc->zName;
 79783         -  char *zErr;
 79784         -  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 79785         -  zErr = sqlite3_mprintf(
 79786         -      "unable to use function %s in the requested context", zName);
 79787         -  sqlite3_result_error(context, zErr, -1);
 79788         -  sqlite3_free(zErr);
 79789         -}
 79790         -
 79791  80464   /*
 79792  80465   ** Create a new aggregate context for p and return a pointer to
 79793  80466   ** its pMem->z element.
 79794  80467   */
 79795  80468   static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
 79796  80469     Mem *pMem = p->pMem;
 79797  80470     assert( (pMem->flags & MEM_Agg)==0 );
................................................................................
 81051  81724     db = p->db;
 81052  81725     sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase), 
 81053  81726                         db->aLimit[SQLITE_LIMIT_LENGTH]);
 81054  81727     if( db->nVdbeExec>1 ){
 81055  81728       while( *zRawSql ){
 81056  81729         const char *zStart = zRawSql;
 81057  81730         while( *(zRawSql++)!='\n' && *zRawSql );
 81058         -      sqlite3StrAccumAppend(&out, "-- ", 3);
        81731  +      sqlite3_str_append(&out, "-- ", 3);
 81059  81732         assert( (zRawSql - zStart) > 0 );
 81060         -      sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
        81733  +      sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
 81061  81734       }
 81062  81735     }else if( p->nVar==0 ){
 81063         -    sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql));
        81736  +    sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
 81064  81737     }else{
 81065  81738       while( zRawSql[0] ){
 81066  81739         n = findNextHostParameter(zRawSql, &nToken);
 81067  81740         assert( n>0 );
 81068         -      sqlite3StrAccumAppend(&out, zRawSql, n);
        81741  +      sqlite3_str_append(&out, zRawSql, n);
 81069  81742         zRawSql += n;
 81070  81743         assert( zRawSql[0] || nToken==0 );
 81071  81744         if( nToken==0 ) break;
 81072  81745         if( zRawSql[0]=='?' ){
 81073  81746           if( nToken>1 ){
 81074  81747             assert( sqlite3Isdigit(zRawSql[1]) );
 81075  81748             sqlite3GetInt32(&zRawSql[1], &idx);
................................................................................
 81087  81760           assert( idx>0 );
 81088  81761         }
 81089  81762         zRawSql += nToken;
 81090  81763         nextIndex = idx + 1;
 81091  81764         assert( idx>0 && idx<=p->nVar );
 81092  81765         pVar = &p->aVar[idx-1];
 81093  81766         if( pVar->flags & MEM_Null ){
 81094         -        sqlite3StrAccumAppend(&out, "NULL", 4);
        81767  +        sqlite3_str_append(&out, "NULL", 4);
 81095  81768         }else if( pVar->flags & MEM_Int ){
 81096         -        sqlite3XPrintf(&out, "%lld", pVar->u.i);
        81769  +        sqlite3_str_appendf(&out, "%lld", pVar->u.i);
 81097  81770         }else if( pVar->flags & MEM_Real ){
 81098         -        sqlite3XPrintf(&out, "%!.15g", pVar->u.r);
        81771  +        sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
 81099  81772         }else if( pVar->flags & MEM_Str ){
 81100  81773           int nOut;  /* Number of bytes of the string text to include in output */
 81101  81774   #ifndef SQLITE_OMIT_UTF16
 81102  81775           u8 enc = ENC(db);
 81103  81776           if( enc!=SQLITE_UTF8 ){
 81104  81777             memset(&utf8, 0, sizeof(utf8));
 81105  81778             utf8.db = db;
 81106  81779             sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
 81107  81780             if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
 81108         -            out.accError = STRACCUM_NOMEM;
        81781  +            out.accError = SQLITE_NOMEM;
 81109  81782               out.nAlloc = 0;
 81110  81783             }
 81111  81784             pVar = &utf8;
 81112  81785           }
 81113  81786   #endif
 81114  81787           nOut = pVar->n;
 81115  81788   #ifdef SQLITE_TRACE_SIZE_LIMIT
 81116  81789           if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
 81117  81790             nOut = SQLITE_TRACE_SIZE_LIMIT;
 81118  81791             while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
 81119  81792           }
 81120  81793   #endif    
 81121         -        sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
        81794  +        sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
 81122  81795   #ifdef SQLITE_TRACE_SIZE_LIMIT
 81123  81796           if( nOut<pVar->n ){
 81124         -          sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
        81797  +          sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
 81125  81798           }
 81126  81799   #endif
 81127  81800   #ifndef SQLITE_OMIT_UTF16
 81128  81801           if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
 81129  81802   #endif
 81130  81803         }else if( pVar->flags & MEM_Zero ){
 81131         -        sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
        81804  +        sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
 81132  81805         }else{
 81133  81806           int nOut;  /* Number of bytes of the blob to include in output */
 81134  81807           assert( pVar->flags & MEM_Blob );
 81135         -        sqlite3StrAccumAppend(&out, "x'", 2);
        81808  +        sqlite3_str_append(&out, "x'", 2);
 81136  81809           nOut = pVar->n;
 81137  81810   #ifdef SQLITE_TRACE_SIZE_LIMIT
 81138  81811           if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
 81139  81812   #endif
 81140  81813           for(i=0; i<nOut; i++){
 81141         -          sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
        81814  +          sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
 81142  81815           }
 81143         -        sqlite3StrAccumAppend(&out, "'", 1);
        81816  +        sqlite3_str_append(&out, "'", 1);
 81144  81817   #ifdef SQLITE_TRACE_SIZE_LIMIT
 81145  81818           if( nOut<pVar->n ){
 81146         -          sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
        81819  +          sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
 81147  81820           }
 81148  81821   #endif
 81149  81822         }
 81150  81823       }
 81151  81824     }
 81152         -  if( out.accError ) sqlite3StrAccumReset(&out);
        81825  +  if( out.accError ) sqlite3_str_reset(&out);
 81153  81826     return sqlite3StrAccumFinish(&out);
 81154  81827   }
 81155  81828   
 81156  81829   #endif /* #ifndef SQLITE_OMIT_TRACE */
 81157  81830   
 81158  81831   /************** End of vdbetrace.c *******************************************/
 81159  81832   /************** Begin file vdbe.c ********************************************/
................................................................................
 82159  82832   ** Check the value in register P3.  If it is NULL then Halt using
 82160  82833   ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
 82161  82834   ** value in register P3 is not NULL, then this routine is a no-op.
 82162  82835   ** The P5 parameter should be 1.
 82163  82836   */
 82164  82837   case OP_HaltIfNull: {      /* in3 */
 82165  82838     pIn3 = &aMem[pOp->p3];
        82839  +#ifdef SQLITE_DEBUG
        82840  +  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
        82841  +#endif
 82166  82842     if( (pIn3->flags & MEM_Null)==0 ) break;
 82167  82843     /* Fall through into OP_Halt */
 82168  82844   }
 82169  82845   
 82170  82846   /* Opcode:  Halt P1 P2 * P4 P5
 82171  82847   **
 82172  82848   ** Exit immediately.  All open cursors, etc are closed
................................................................................
 82198  82874   ** is the same as executing Halt.
 82199  82875   */
 82200  82876   case OP_Halt: {
 82201  82877     VdbeFrame *pFrame;
 82202  82878     int pcx;
 82203  82879   
 82204  82880     pcx = (int)(pOp - aOp);
        82881  +#ifdef SQLITE_DEBUG
        82882  +  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
        82883  +#endif
 82205  82884     if( pOp->p1==SQLITE_OK && p->pFrame ){
 82206  82885       /* Halt the sub-program. Return control to the parent frame. */
 82207  82886       pFrame = p->pFrame;
 82208  82887       p->pFrame = pFrame->pParent;
 82209  82888       p->nFrame--;
 82210  82889       sqlite3VdbeSetChanges(db, p->nChange);
 82211  82890       pcx = sqlite3VdbeFrameRestore(pFrame);
................................................................................
 84568  85247   ** size, and so forth.  P1==0 is the main database file and P1==1 is the 
 84569  85248   ** database file used to store temporary tables.
 84570  85249   **
 84571  85250   ** A transaction must be started before executing this opcode.
 84572  85251   */
 84573  85252   case OP_SetCookie: {
 84574  85253     Db *pDb;
        85254  +
        85255  +  sqlite3VdbeIncrWriteCounter(p, 0);
 84575  85256     assert( pOp->p2<SQLITE_N_BTREE_META );
 84576  85257     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 84577  85258     assert( DbMaskTest(p->btreeMask, pOp->p1) );
 84578  85259     assert( p->readOnly==0 );
 84579  85260     pDb = &db->aDb[pOp->p1];
 84580  85261     assert( pDb->pBt!=0 );
 84581  85262     assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
................................................................................
 85531  86212     VdbeFrame *pFrame;     /* Root frame of VDBE */
 85532  86213   
 85533  86214     v = 0;
 85534  86215     res = 0;
 85535  86216     pOut = out2Prerelease(p, pOp);
 85536  86217     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 85537  86218     pC = p->apCsr[pOp->p1];
 85538         -  if( !pC->isTable ){
 85539         -    rc = SQLITE_CORRUPT_BKPT;
 85540         -    goto abort_due_to_error;
 85541         -  }
 85542  86219     assert( pC!=0 );
        86220  +  assert( pC->isTable );
 85543  86221     assert( pC->eCurType==CURTYPE_BTREE );
 85544  86222     assert( pC->uc.pCursor!=0 );
 85545  86223     {
 85546  86224       /* The next rowid or record number (different terms for the same
 85547  86225       ** thing) is obtained in a two-step algorithm.
 85548  86226       **
 85549  86227       ** First we attempt to find the largest existing rowid and add one
................................................................................
 85704  86382     pC = p->apCsr[pOp->p1];
 85705  86383     assert( pC!=0 );
 85706  86384     assert( pC->eCurType==CURTYPE_BTREE );
 85707  86385     assert( pC->uc.pCursor!=0 );
 85708  86386     assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
 85709  86387     assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
 85710  86388     REGISTER_TRACE(pOp->p2, pData);
        86389  +  sqlite3VdbeIncrWriteCounter(p, pC);
 85711  86390   
 85712  86391     if( pOp->opcode==OP_Insert ){
 85713  86392       pKey = &aMem[pOp->p3];
 85714  86393       assert( pKey->flags & MEM_Int );
 85715  86394       assert( memIsValid(pKey) );
 85716  86395       REGISTER_TRACE(pOp->p3, pKey);
 85717  86396       x.nKey = pKey->u.i;
................................................................................
 85818  86497     opflags = pOp->p2;
 85819  86498     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 85820  86499     pC = p->apCsr[pOp->p1];
 85821  86500     assert( pC!=0 );
 85822  86501     assert( pC->eCurType==CURTYPE_BTREE );
 85823  86502     assert( pC->uc.pCursor!=0 );
 85824  86503     assert( pC->deferredMoveto==0 );
        86504  +  sqlite3VdbeIncrWriteCounter(p, pC);
 85825  86505   
 85826  86506   #ifdef SQLITE_DEBUG
 85827  86507     if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
 85828  86508       /* If p5 is zero, the seek operation that positioned the cursor prior to
 85829  86509       ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
 85830  86510       ** the row that is being deleted */
 85831  86511       i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
................................................................................
 85986  86666   **
 85987  86667   ** If cursor P1 is an index, then the content is the key of the row.
 85988  86668   ** If cursor P2 is a table, then the content extracted is the data.
 85989  86669   **
 85990  86670   ** If the P1 cursor must be pointing to a valid row (not a NULL row)
 85991  86671   ** of a real table, not a pseudo-table.
 85992  86672   **
 85993         -** If P3!=0 then this opcode is allowed to make an ephermeral pointer
        86673  +** If P3!=0 then this opcode is allowed to make an ephemeral pointer
 85994  86674   ** into the database page.  That means that the content of the output
 85995  86675   ** register will be invalidated as soon as the cursor moves - including
 85996         -** moves caused by other cursors that "save" the the current cursors
        86676  +** moves caused by other cursors that "save" the current cursors
 85997  86677   ** position in order that they can write to the same table.  If P3==0
 85998  86678   ** then a copy of the data is made into memory.  P3!=0 is faster, but
 85999  86679   ** P3==0 is safer.
 86000  86680   **
 86001  86681   ** If P3!=0 then the content of the P2 register is unsuitable for use
 86002  86682   ** in OP_Result and any OP_Result will invalidate the P2 register content.
 86003  86683   ** The P2 register content is invalidated by opcodes like OP_Function or
................................................................................
 86436  87116   case OP_SorterInsert:       /* in2 */
 86437  87117   case OP_IdxInsert: {        /* in2 */
 86438  87118     VdbeCursor *pC;
 86439  87119     BtreePayload x;
 86440  87120   
 86441  87121     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 86442  87122     pC = p->apCsr[pOp->p1];
        87123  +  sqlite3VdbeIncrWriteCounter(p, pC);
 86443  87124     assert( pC!=0 );
 86444  87125     assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
 86445  87126     pIn2 = &aMem[pOp->p2];
 86446  87127     assert( pIn2->flags & MEM_Blob );
 86447  87128     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 86448  87129     assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
 86449  87130     assert( pC->isTable==0 );
................................................................................
 86482  87163   
 86483  87164     assert( pOp->p3>0 );
 86484  87165     assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
 86485  87166     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 86486  87167     pC = p->apCsr[pOp->p1];
 86487  87168     assert( pC!=0 );
 86488  87169     assert( pC->eCurType==CURTYPE_BTREE );
        87170  +  sqlite3VdbeIncrWriteCounter(p, pC);
 86489  87171     pCrsr = pC->uc.pCursor;
 86490  87172     assert( pCrsr!=0 );
 86491  87173     assert( pOp->p5==0 );
 86492  87174     r.pKeyInfo = pC->pKeyInfo;
 86493  87175     r.nField = (u16)pOp->p3;
 86494  87176     r.default_rc = 0;
 86495  87177     r.aMem = &aMem[pOp->p2];
................................................................................
 86704  87386   **
 86705  87387   ** See also: Clear
 86706  87388   */
 86707  87389   case OP_Destroy: {     /* out2 */
 86708  87390     int iMoved;
 86709  87391     int iDb;
 86710  87392   
        87393  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86711  87394     assert( p->readOnly==0 );
 86712  87395     assert( pOp->p1>1 );
 86713  87396     pOut = out2Prerelease(p, pOp);
 86714  87397     pOut->flags = MEM_Null;
 86715  87398     if( db->nVdbeRead > db->nVDestroy+1 ){
 86716  87399       rc = SQLITE_LOCKED;
 86717  87400       p->errorAction = OE_Abort;
................................................................................
 86753  87436   ** also incremented by the number of rows in the table being cleared.
 86754  87437   **
 86755  87438   ** See also: Destroy
 86756  87439   */
 86757  87440   case OP_Clear: {
 86758  87441     int nChange;
 86759  87442    
        87443  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86760  87444     nChange = 0;
 86761  87445     assert( p->readOnly==0 );
 86762  87446     assert( DbMaskTest(p->btreeMask, pOp->p2) );
 86763  87447     rc = sqlite3BtreeClearTable(
 86764  87448         db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
 86765  87449     );
 86766  87450     if( pOp->p3 ){
................................................................................
 86802  87486   
 86803  87487   /* Opcode: CreateBtree P1 P2 P3 * *
 86804  87488   ** Synopsis: r[P2]=root iDb=P1 flags=P3
 86805  87489   **
 86806  87490   ** Allocate a new b-tree in the main database file if P1==0 or in the
 86807  87491   ** TEMP database file if P1==1 or in an attached database if
 86808  87492   ** P1>1.  The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
 86809         -** it must be 2 (BTREE_BLOBKEY) for a index or WITHOUT ROWID table.
        87493  +** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
 86810  87494   ** The root page number of the new b-tree is stored in register P2.
 86811  87495   */
 86812  87496   case OP_CreateBtree: {          /* out2 */
 86813  87497     int pgno;
 86814  87498     Db *pDb;
 86815  87499   
        87500  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86816  87501     pOut = out2Prerelease(p, pOp);
 86817  87502     pgno = 0;
 86818  87503     assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
 86819  87504     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 86820  87505     assert( DbMaskTest(p->btreeMask, pOp->p1) );
 86821  87506     assert( p->readOnly==0 );
 86822  87507     pDb = &db->aDb[pOp->p1];
................................................................................
 86828  87513   }
 86829  87514   
 86830  87515   /* Opcode: SqlExec * * * P4 *
 86831  87516   **
 86832  87517   ** Run the SQL statement or statements specified in the P4 string.
 86833  87518   */
 86834  87519   case OP_SqlExec: {
        87520  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86835  87521     db->nSqlExec++;
 86836  87522     rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
 86837  87523     db->nSqlExec--;
 86838  87524     if( rc ) goto abort_due_to_error;
 86839  87525     break;
 86840  87526   }
 86841  87527   
................................................................................
 86917  87603   ** Remove the internal (in-memory) data structures that describe
 86918  87604   ** the table named P4 in database P1.  This is called after a table
 86919  87605   ** is dropped from disk (using the Destroy opcode) in order to keep 
 86920  87606   ** the internal representation of the
 86921  87607   ** schema consistent with what is on disk.
 86922  87608   */
 86923  87609   case OP_DropTable: {
        87610  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86924  87611     sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
 86925  87612     break;
 86926  87613   }
 86927  87614   
 86928  87615   /* Opcode: DropIndex P1 * * P4 *
 86929  87616   **
 86930  87617   ** Remove the internal (in-memory) data structures that describe
 86931  87618   ** the index named P4 in database P1.  This is called after an index
 86932  87619   ** is dropped from disk (using the Destroy opcode)
 86933  87620   ** in order to keep the internal representation of the
 86934  87621   ** schema consistent with what is on disk.
 86935  87622   */
 86936  87623   case OP_DropIndex: {
        87624  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86937  87625     sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
 86938  87626     break;
 86939  87627   }
 86940  87628   
 86941  87629   /* Opcode: DropTrigger P1 * * P4 *
 86942  87630   **
 86943  87631   ** Remove the internal (in-memory) data structures that describe
 86944  87632   ** the trigger named P4 in database P1.  This is called after a trigger
 86945  87633   ** is dropped from disk (using the Destroy opcode) in order to keep 
 86946  87634   ** the internal representation of the
 86947  87635   ** schema consistent with what is on disk.
 86948  87636   */
 86949  87637   case OP_DropTrigger: {
        87638  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86950  87639     sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
 86951  87640     break;
 86952  87641   }
 86953  87642   
 86954  87643   
 86955  87644   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 86956  87645   /* Opcode: IntegrityCk P1 P2 P3 P4 P5
................................................................................
 87989  88678   **
 87990  88679   ** Store in register P3 the value of the P2-th column of
 87991  88680   ** the current row of the virtual-table of cursor P1.
 87992  88681   **
 87993  88682   ** If the VColumn opcode is being used to fetch the value of
 87994  88683   ** an unchanging column during an UPDATE operation, then the P5
 87995  88684   ** value is 1.  Otherwise, P5 is 0.  The P5 value is returned
 87996         -** by sqlite3_vtab_nochange() routine can can be used
        88685  +** by sqlite3_vtab_nochange() routine and can be used
 87997  88686   ** by virtual table implementations to return special "no-change"
 87998  88687   ** marks which can be more efficient, depending on the virtual table.
 87999  88688   */
 88000  88689   case OP_VColumn: {
 88001  88690     sqlite3_vtab *pVtab;
 88002  88691     const sqlite3_module *pModule;
 88003  88692     Mem *pDest;
................................................................................
 88152  88841     Mem **apArg;
 88153  88842     Mem *pX;
 88154  88843   
 88155  88844     assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback 
 88156  88845          || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
 88157  88846     );
 88158  88847     assert( p->readOnly==0 );
        88848  +  sqlite3VdbeIncrWriteCounter(p, 0);
 88159  88849     pVtab = pOp->p4.pVtab->pVtab;
 88160  88850     if( pVtab==0 || NEVER(pVtab->pModule==0) ){
 88161  88851       rc = SQLITE_LOCKED;
 88162  88852       goto abort_due_to_error;
 88163  88853     }
 88164  88854     pModule = pVtab->pModule;
 88165  88855     nArg = pOp->p2;
................................................................................
 88468  89158       assert( pC->eCurType==CURTYPE_BTREE );
 88469  89159       sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
 88470  89160                              pOp->p4.pExpr, aMem);
 88471  89161     }
 88472  89162     break;
 88473  89163   }
 88474  89164   #endif /* SQLITE_ENABLE_CURSOR_HINTS */
        89165  +
        89166  +#ifdef SQLITE_DEBUG
        89167  +/* Opcode:  Abortable   * * * * *
        89168  +**
        89169  +** Verify that an Abort can happen.  Assert if an Abort at this point
        89170  +** might cause database corruption.  This opcode only appears in debugging
        89171  +** builds.
        89172  +**
        89173  +** An Abort is safe if either there have been no writes, or if there is
        89174  +** an active statement journal.
        89175  +*/
        89176  +case OP_Abortable: {
        89177  +  sqlite3VdbeAssertAbortable(p);
        89178  +  break;
        89179  +}
        89180  +#endif
 88475  89181   
 88476  89182   /* Opcode: Noop * * * * *
 88477  89183   **
 88478  89184   ** Do nothing.  This instruction is often useful as a jump
 88479  89185   ** destination.
 88480  89186   */
 88481  89187   /*
 88482  89188   ** The magic Explain opcode are only inserted when explain==2 (which
 88483  89189   ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
 88484  89190   ** This opcode records information from the optimizer.  It is the
 88485  89191   ** the same as a no-op.  This opcodesnever appears in a real VM program.
 88486  89192   */
 88487         -default: {          /* This is really OP_Noop and OP_Explain */
        89193  +default: {          /* This is really OP_Noop, OP_Explain */
 88488  89194     assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
        89195  +
 88489  89196     break;
 88490  89197   }
 88491  89198   
 88492  89199   /*****************************************************************************
 88493  89200   ** The cases of the switch statement above this line should all be indented
 88494  89201   ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
 88495  89202   ** readability.  From this point on down, the normal indentation rules are
................................................................................
 92526  93233     sqlite3 *db;           /* The database connection */
 92527  93234   
 92528  93235     assert( iCol>=0 && iCol<pEList->nExpr );
 92529  93236     pOrig = pEList->a[iCol].pExpr;
 92530  93237     assert( pOrig!=0 );
 92531  93238     db = pParse->db;
 92532  93239     pDup = sqlite3ExprDup(db, pOrig, 0);
 92533         -  if( pDup==0 ) return;
 92534         -  if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
 92535         -  if( pExpr->op==TK_COLLATE ){
 92536         -    pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
 92537         -  }
 92538         -  ExprSetProperty(pDup, EP_Alias);
 92539         -
 92540         -  /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
 92541         -  ** prevents ExprDelete() from deleting the Expr structure itself,
 92542         -  ** allowing it to be repopulated by the memcpy() on the following line.
 92543         -  ** The pExpr->u.zToken might point into memory that will be freed by the
 92544         -  ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
 92545         -  ** make a copy of the token before doing the sqlite3DbFree().
 92546         -  */
 92547         -  ExprSetProperty(pExpr, EP_Static);
 92548         -  sqlite3ExprDelete(db, pExpr);
 92549         -  memcpy(pExpr, pDup, sizeof(*pExpr));
 92550         -  if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
 92551         -    assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
 92552         -    pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
 92553         -    pExpr->flags |= EP_MemToken;
 92554         -  }
 92555         -  sqlite3DbFree(db, pDup);
        93240  +  if( pDup!=0 ){
        93241  +    if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
        93242  +    if( pExpr->op==TK_COLLATE ){
        93243  +      pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
        93244  +    }
        93245  +    ExprSetProperty(pDup, EP_Alias);
        93246  +
        93247  +    /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
        93248  +    ** prevents ExprDelete() from deleting the Expr structure itself,
        93249  +    ** allowing it to be repopulated by the memcpy() on the following line.
        93250  +    ** The pExpr->u.zToken might point into memory that will be freed by the
        93251  +    ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
        93252  +    ** make a copy of the token before doing the sqlite3DbFree().
        93253  +    */
        93254  +    ExprSetProperty(pExpr, EP_Static);
        93255  +    sqlite3ExprDelete(db, pExpr);
        93256  +    memcpy(pExpr, pDup, sizeof(*pExpr));
        93257  +    if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
        93258  +      assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
        93259  +      pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
        93260  +      pExpr->flags |= EP_MemToken;
        93261  +    }
        93262  +    sqlite3DbFree(db, pDup);
        93263  +  }
        93264  +  ExprSetProperty(pExpr, EP_Alias);
 92556  93265   }
 92557  93266   
 92558  93267   
 92559  93268   /*
 92560  93269   ** Return TRUE if the name zCol occurs anywhere in the USING clause.
 92561  93270   **
 92562  93271   ** Return FALSE if the USING clause is NULL or if it does not contain
................................................................................
 92642  93351     int cntTab = 0;                   /* Number of matching table names */
 92643  93352     int nSubquery = 0;                /* How many levels of subquery */
 92644  93353     sqlite3 *db = pParse->db;         /* The database connection */
 92645  93354     struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
 92646  93355     struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
 92647  93356     NameContext *pTopNC = pNC;        /* First namecontext in the list */
 92648  93357     Schema *pSchema = 0;              /* Schema of the expression */
 92649         -  int isTrigger = 0;                /* True if resolved to a trigger column */
        93358  +  int eNewExprOp = TK_COLUMN;       /* New value for pExpr->op on success */
 92650  93359     Table *pTab = 0;                  /* Table hold the row */
 92651  93360     Column *pCol;                     /* A column of pTab */
 92652  93361   
 92653  93362     assert( pNC );     /* the name context cannot be NULL. */
 92654  93363     assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 92655  93364     assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 92656  93365   
................................................................................
 92747  93456           if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
 92748  93457             ExprSetProperty(pExpr, EP_CanBeNull);
 92749  93458           }
 92750  93459           pSchema = pExpr->pTab->pSchema;
 92751  93460         }
 92752  93461       } /* if( pSrcList ) */
 92753  93462   
 92754         -#ifndef SQLITE_OMIT_TRIGGER
        93463  +#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
 92755  93464       /* If we have not already resolved the name, then maybe 
 92756         -    ** it is a new.* or old.* trigger argument reference
        93465  +    ** it is a new.* or old.* trigger argument reference.  Or
        93466  +    ** maybe it is an excluded.* from an upsert.
 92757  93467       */
 92758         -    if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
 92759         -      int op = pParse->eTriggerOp;
 92760         -      assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
 92761         -      if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
 92762         -        pExpr->iTable = 1;
 92763         -        pTab = pParse->pTriggerTab;
 92764         -      }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
 92765         -        pExpr->iTable = 0;
 92766         -        pTab = pParse->pTriggerTab;
 92767         -      }else{
 92768         -        pTab = 0;
        93468  +    if( zDb==0 && zTab!=0 && cntTab==0 ){
        93469  +      pTab = 0;
        93470  +#ifndef SQLITE_OMIT_TRIGGER
        93471  +      if( pParse->pTriggerTab!=0 ){
        93472  +        int op = pParse->eTriggerOp;
        93473  +        assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
        93474  +        if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
        93475  +          pExpr->iTable = 1;
        93476  +          pTab = pParse->pTriggerTab;
        93477  +        }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
        93478  +          pExpr->iTable = 0;
        93479  +          pTab = pParse->pTriggerTab;
        93480  +        }
 92769  93481         }
        93482  +#endif /* SQLITE_OMIT_TRIGGER */
        93483  +#ifndef SQLITE_OMIT_UPSERT
        93484  +      if( (pNC->ncFlags & NC_UUpsert)!=0 ){
        93485  +        Upsert *pUpsert = pNC->uNC.pUpsert;
        93486  +        if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
        93487  +          pTab = pUpsert->pUpsertSrc->a[0].pTab;
        93488  +          pExpr->iTable = 2;
        93489  +        }
        93490  +      }
        93491  +#endif /* SQLITE_OMIT_UPSERT */
 92770  93492   
 92771  93493         if( pTab ){ 
 92772  93494           int iCol;
 92773  93495           pSchema = pTab->pSchema;
 92774  93496           cntTab++;
 92775  93497           for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
 92776  93498             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
................................................................................
 92782  93504           }
 92783  93505           if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
 92784  93506             /* IMP: R-51414-32910 */
 92785  93507             iCol = -1;
 92786  93508           }
 92787  93509           if( iCol<pTab->nCol ){
 92788  93510             cnt++;
 92789         -          if( iCol<0 ){
 92790         -            pExpr->affinity = SQLITE_AFF_INTEGER;
 92791         -          }else if( pExpr->iTable==0 ){
 92792         -            testcase( iCol==31 );
 92793         -            testcase( iCol==32 );
 92794         -            pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 92795         -          }else{
 92796         -            testcase( iCol==31 );
 92797         -            testcase( iCol==32 );
 92798         -            pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 92799         -          }
 92800         -          pExpr->iColumn = (i16)iCol;
 92801         -          pExpr->pTab = pTab;
 92802         -          isTrigger = 1;
 92803         -        }
 92804         -      }
 92805         -    }
 92806         -#endif /* !defined(SQLITE_OMIT_TRIGGER) */
        93511  +#ifndef SQLITE_OMIT_UPSERT
        93512  +          if( pExpr->iTable==2 ){
        93513  +            testcase( iCol==(-1) );
        93514  +            pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
        93515  +            eNewExprOp = TK_REGISTER;
        93516  +            ExprSetProperty(pExpr, EP_Alias);
        93517  +          }else
        93518  +#endif /* SQLITE_OMIT_UPSERT */
        93519  +          {
        93520  +#ifndef SQLITE_OMIT_TRIGGER
        93521  +            if( iCol<0 ){
        93522  +              pExpr->affinity = SQLITE_AFF_INTEGER;
        93523  +            }else if( pExpr->iTable==0 ){
        93524  +              testcase( iCol==31 );
        93525  +              testcase( iCol==32 );
        93526  +              pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
        93527  +            }else{
        93528  +              testcase( iCol==31 );
        93529  +              testcase( iCol==32 );
        93530  +              pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
        93531  +            }
        93532  +            pExpr->pTab = pTab;
        93533  +            pExpr->iColumn = (i16)iCol;
        93534  +            eNewExprOp = TK_TRIGGER;
        93535  +#endif /* SQLITE_OMIT_TRIGGER */
        93536  +          }
        93537  +        }
        93538  +      }
        93539  +    }
        93540  +#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
 92807  93541   
 92808  93542       /*
 92809  93543       ** Perhaps the name is a reference to the ROWID
 92810  93544       */
 92811  93545       if( cnt==0
 92812  93546        && cntTab==1
 92813  93547        && pMatch
................................................................................
 92834  93568       **
 92835  93569       ** The ability to use an output result-set column in the WHERE, GROUP BY,
 92836  93570       ** or HAVING clauses, or as part of a larger expression in the ORDER BY
 92837  93571       ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
 92838  93572       ** is supported for backwards compatibility only. Hence, we issue a warning
 92839  93573       ** on sqlite3_log() whenever the capability is used.
 92840  93574       */
 92841         -    if( (pEList = pNC->pEList)!=0
 92842         -     && zTab==0
        93575  +    if( (pNC->ncFlags & NC_UEList)!=0
 92843  93576        && cnt==0
        93577  +     && zTab==0
 92844  93578       ){
        93579  +      pEList = pNC->uNC.pEList;
        93580  +      assert( pEList!=0 );
 92845  93581         for(j=0; j<pEList->nExpr; j++){
 92846  93582           char *zAs = pEList->a[j].zName;
 92847  93583           if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
 92848  93584             Expr *pOrig;
 92849  93585             assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 92850  93586             assert( pExpr->x.pList==0 );
 92851  93587             assert( pExpr->x.pSelect==0 );
................................................................................
 92934  93670   
 92935  93671     /* Clean up and return
 92936  93672     */
 92937  93673     sqlite3ExprDelete(db, pExpr->pLeft);
 92938  93674     pExpr->pLeft = 0;
 92939  93675     sqlite3ExprDelete(db, pExpr->pRight);
 92940  93676     pExpr->pRight = 0;
 92941         -  pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
        93677  +  pExpr->op = eNewExprOp;
 92942  93678     ExprSetProperty(pExpr, EP_Leaf);
 92943  93679   lookupname_end:
 92944  93680     if( cnt==1 ){
 92945  93681       assert( pNC!=0 );
 92946  93682       if( !ExprHasProperty(pExpr, EP_Alias) ){
 92947  93683         sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
 92948  93684       }
................................................................................
 93366  94102     pEList = pSelect->pEList;
 93367  94103   
 93368  94104     /* Resolve all names in the ORDER BY term expression
 93369  94105     */
 93370  94106     memset(&nc, 0, sizeof(nc));
 93371  94107     nc.pParse = pParse;
 93372  94108     nc.pSrcList = pSelect->pSrc;
 93373         -  nc.pEList = pEList;
 93374         -  nc.ncFlags = NC_AllowAgg;
        94109  +  nc.uNC.pEList = pEList;
        94110  +  nc.ncFlags = NC_AllowAgg|NC_UEList;
 93375  94111     nc.nErr = 0;
 93376  94112     db = pParse->db;
 93377  94113     savedSuppErr = db->suppressErr;
 93378  94114     db->suppressErr = 1;
 93379  94115     rc = sqlite3ResolveExprNames(&nc, pE);
 93380  94116     db->suppressErr = savedSuppErr;
 93381  94117     if( rc ) return 0;
................................................................................
 93750  94486       ** other expressions in the SELECT statement. This is so that
 93751  94487       ** expressions in the WHERE clause (etc.) can refer to expressions by
 93752  94488       ** aliases in the result set.
 93753  94489       **
 93754  94490       ** Minor point: If this is the case, then the expression will be
 93755  94491       ** re-evaluated for each reference to it.
 93756  94492       */
 93757         -    sNC.pEList = p->pEList;
        94493  +    assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert))==0 );
        94494  +    sNC.uNC.pEList = p->pEList;
        94495  +    sNC.ncFlags |= NC_UEList;
 93758  94496       if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
 93759  94497       if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
 93760  94498   
 93761  94499       /* Resolve names in table-valued-function arguments */
 93762  94500       for(i=0; i<p->pSrc->nSrc; i++){
 93763  94501         struct SrcList_item *pItem = &p->pSrc->a[i];
 93764  94502         if( pItem->fg.isTabFunc
................................................................................
 93983  94721   ** Any errors cause an error message to be set in pParse.
 93984  94722   */
 93985  94723   SQLITE_PRIVATE void sqlite3ResolveSelfReference(
 93986  94724     Parse *pParse,      /* Parsing context */
 93987  94725     Table *pTab,        /* The table being referenced */
 93988  94726     int type,           /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */
 93989  94727     Expr *pExpr,        /* Expression to resolve.  May be NULL. */
 93990         -  ExprList *pList     /* Expression list to resolve.  May be NUL. */
        94728  +  ExprList *pList     /* Expression list to resolve.  May be NULL. */
 93991  94729   ){
 93992  94730     SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
 93993  94731     NameContext sNC;                /* Name context for pParse->pNewTable */
 93994  94732   
 93995  94733     assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr );
 93996  94734     memset(&sNC, 0, sizeof(sNC));
 93997  94735     memset(&sSrc, 0, sizeof(sSrc));
................................................................................
 95369  96107         }
 95370  96108       }
 95371  96109       pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 95372  96110       pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
 95373  96111       pItem->sortOrder = pOldItem->sortOrder;
 95374  96112       pItem->done = 0;
 95375  96113       pItem->bSpanIsTab = pOldItem->bSpanIsTab;
        96114  +    pItem->bSorterRef = pOldItem->bSorterRef;
 95376  96115       pItem->u = pOldItem->u;
 95377  96116     }
 95378  96117     return pNew;
 95379  96118   }
 95380  96119   
 95381  96120   /*
 95382  96121   ** If cursors, triggers, views and subqueries are all omitted from
................................................................................
 95831  96570         testcase( pExpr->op==TK_AGG_FUNCTION );
 95832  96571         testcase( pExpr->op==TK_AGG_COLUMN );
 95833  96572         if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
 95834  96573           return WRC_Continue;
 95835  96574         }
 95836  96575         /* Fall through */
 95837  96576       case TK_IF_NULL_ROW:
        96577  +    case TK_REGISTER:
        96578  +      testcase( pExpr->op==TK_REGISTER );
 95838  96579         testcase( pExpr->op==TK_IF_NULL_ROW );
 95839  96580         pWalker->eCode = 0;
 95840  96581         return WRC_Abort;
 95841  96582       case TK_VARIABLE:
 95842  96583         if( pWalker->eCode==5 ){
 95843  96584           /* Silently convert bound parameters that appear inside of CREATE
 95844  96585           ** statements into a NULL when parsing the CREATE statement text out
................................................................................
 95848  96589           /* A bound parameter in a CREATE statement that originates from
 95849  96590           ** sqlite3_prepare() causes an error */
 95850  96591           pWalker->eCode = 0;
 95851  96592           return WRC_Abort;
 95852  96593         }
 95853  96594         /* Fall through */
 95854  96595       default:
 95855         -      testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail will disallow */
 95856         -      testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail will disallow */
        96596  +      testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
        96597  +      testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
 95857  96598         return WRC_Continue;
 95858  96599     }
 95859  96600   }
 95860  96601   static int exprIsConst(Expr *p, int initFlag, int iCur){
 95861  96602     Walker w;
 95862  96603     w.eCode = initFlag;
 95863  96604     w.xExprCallback = exprNodeIsConstant;
................................................................................
 96413  97154               if( aiMap ) aiMap[i] = j;
 96414  97155             }
 96415  97156     
 96416  97157             assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
 96417  97158             if( colUsed==(MASKBIT(nExpr)-1) ){
 96418  97159               /* If we reach this point, that means the index pIdx is usable */
 96419  97160               int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
 96420         -#ifndef SQLITE_OMIT_EXPLAIN
 96421         -            sqlite3VdbeAddOp4(v, OP_Explain, 0, 0, 0,
 96422         -              sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName),
 96423         -              P4_DYNAMIC);
 96424         -#endif
        97161  +            ExplainQueryPlan((pParse, 0,
        97162  +                              "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
 96425  97163               sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
 96426  97164               sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 96427  97165               VdbeComment((v, "%s", pIdx->zName));
 96428  97166               assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
 96429  97167               eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
 96430  97168     
 96431  97169               if( prRhsHasNull ){
................................................................................
 96612  97350     ** If all of the above are false, then we can run this code just once
 96613  97351     ** save the results, and reuse the same result on subsequent invocations.
 96614  97352     */
 96615  97353     if( !ExprHasProperty(pExpr, EP_VarSelect) ){
 96616  97354       jmpIfDynamic = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
 96617  97355     }
 96618  97356   
 96619         -#ifndef SQLITE_OMIT_EXPLAIN
 96620         -  if( pParse->explain==2 ){
 96621         -    char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
 96622         -        jmpIfDynamic>=0?"":"CORRELATED ",
 96623         -        pExpr->op==TK_IN?"LIST":"SCALAR",
 96624         -        pParse->iNextSelectId
 96625         -    );
 96626         -    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 96627         -  }
 96628         -#endif
 96629         -
 96630  97357     switch( pExpr->op ){
 96631  97358       case TK_IN: {
 96632  97359         int addr;                   /* Address of OP_OpenEphemeral instruction */
 96633  97360         Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
 96634  97361         KeyInfo *pKeyInfo = 0;      /* Key information */
 96635  97362         int nVal;                   /* Size of vector pLeft */
 96636  97363         
................................................................................
 96660  97387           **
 96661  97388           ** Generate code to write the results of the select into the temporary
 96662  97389           ** table allocated and opened above.
 96663  97390           */
 96664  97391           Select *pSelect = pExpr->x.pSelect;
 96665  97392           ExprList *pEList = pSelect->pEList;
 96666  97393   
        97394  +        ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY",
        97395  +            jmpIfDynamic>=0?"":"CORRELATED "
        97396  +        ));
 96667  97397           assert( !isRowid );
 96668  97398           /* If the LHS and RHS of the IN operator do not match, that
 96669  97399           ** error will have been caught long before we reach this point. */
 96670  97400           if( ALWAYS(pEList->nExpr==nVal) ){
 96671  97401             SelectDest dest;
 96672  97402             int i;
 96673  97403             sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
................................................................................
 96701  97431           ** a column, use numeric affinity.
 96702  97432           */
 96703  97433           char affinity;            /* Affinity of the LHS of the IN */
 96704  97434           int i;
 96705  97435           ExprList *pList = pExpr->x.pList;
 96706  97436           struct ExprList_item *pItem;
 96707  97437           int r1, r2, r3;
 96708         -
 96709  97438           affinity = sqlite3ExprAffinity(pLeft);
 96710  97439           if( !affinity ){
 96711  97440             affinity = SQLITE_AFF_BLOB;
 96712  97441           }
 96713  97442           if( pKeyInfo ){
 96714  97443             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 96715  97444             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
................................................................................
 96782  97511   
 96783  97512         testcase( pExpr->op==TK_EXISTS );
 96784  97513         testcase( pExpr->op==TK_SELECT );
 96785  97514         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 96786  97515         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 96787  97516   
 96788  97517         pSel = pExpr->x.pSelect;
        97518  +      ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY",
        97519  +            jmpIfDynamic>=0?"":"CORRELATED "));
 96789  97520         nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
 96790  97521         sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
 96791  97522         pParse->nMem += nReg;
 96792  97523         if( pExpr->op==TK_SELECT ){
 96793  97524           dest.eDest = SRT_Mem;
 96794  97525           dest.iSdst = dest.iSDParm;
 96795  97526           dest.nSdst = nReg;
................................................................................
 97544  98275   
 97545  98276     assert( target>0 && target<=pParse->nMem );
 97546  98277     if( v==0 ){
 97547  98278       assert( pParse->db->mallocFailed );
 97548  98279       return 0;
 97549  98280     }
 97550  98281   
        98282  +expr_code_doover:
 97551  98283     if( pExpr==0 ){
 97552  98284       op = TK_NULL;
 97553  98285     }else{
 97554  98286       op = pExpr->op;
 97555  98287     }
 97556  98288     switch( op ){
 97557  98289       case TK_AGG_COLUMN: {
................................................................................
 98004  98736       case TK_BETWEEN: {
 98005  98737         exprCodeBetween(pParse, pExpr, target, 0, 0);
 98006  98738         return target;
 98007  98739       }
 98008  98740       case TK_SPAN:
 98009  98741       case TK_COLLATE: 
 98010  98742       case TK_UPLUS: {
 98011         -      return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
        98743  +      pExpr = pExpr->pLeft;
        98744  +      goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
 98012  98745       }
 98013  98746   
 98014  98747       case TK_TRIGGER: {
 98015  98748         /* If the opcode is TK_TRIGGER, then the expression is a reference
 98016  98749         ** to a column in the new.* or old.* pseudo-tables available to
 98017  98750         ** trigger programs. In this case Expr.iTable is set to 1 for the
 98018  98751         ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
................................................................................
 98042  98775   
 98043  98776         assert( pExpr->iTable==0 || pExpr->iTable==1 );
 98044  98777         assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
 98045  98778         assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
 98046  98779         assert( p1>=0 && p1<(pTab->nCol*2+2) );
 98047  98780   
 98048  98781         sqlite3VdbeAddOp2(v, OP_Param, p1, target);
 98049         -      VdbeComment((v, "%s.%s -> $%d",
        98782  +      VdbeComment((v, "r[%d]=%s.%s", target,
 98050  98783           (pExpr->iTable ? "new" : "old"),
 98051         -        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
 98052         -        target
        98784  +        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName)
 98053  98785         ));
 98054  98786   
 98055  98787   #ifndef SQLITE_OMIT_FLOATING_POINT
 98056  98788         /* If the column has REAL affinity, it may currently be stored as an
 98057  98789         ** integer. Use OP_RealAffinity to make sure it is really real.
 98058  98790         **
 98059  98791         ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
................................................................................
 98377  99109     assert( pList!=0 );
 98378  99110     assert( target>0 );
 98379  99111     assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
 98380  99112     n = pList->nExpr;
 98381  99113     if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
 98382  99114     for(pItem=pList->a, i=0; i<n; i++, pItem++){
 98383  99115       Expr *pExpr = pItem->pExpr;
        99116  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
        99117  +    if( pItem->bSorterRef ){
        99118  +      i--;
        99119  +      n--;
        99120  +    }else
        99121  +#endif
 98384  99122       if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
 98385  99123         if( flags & SQLITE_ECEL_OMITREF ){
 98386  99124           i--;
 98387  99125           n--;
 98388  99126         }else{
 98389  99127           sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
 98390  99128         }
................................................................................
 98905  99643         return 1;
 98906  99644       }
 98907  99645       return 2;
 98908  99646     }
 98909  99647     if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
 98910  99648       if( pA->op==TK_FUNCTION ){
 98911  99649         if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
        99650  +    }else if( pA->op==TK_COLLATE ){
        99651  +      if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
 98912  99652       }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
 98913         -      return pA->op==TK_COLLATE ? 1 : 2;
        99653  +      return 2;
 98914  99654       }
 98915  99655     }
 98916  99656     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
 98917  99657     if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
 98918  99658       if( combinedFlags & EP_xIsSelect ) return 2;
 98919  99659       if( sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
 98920  99660       if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
 98921  99661       if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
 98922         -    if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
        99662  +    assert( (combinedFlags & EP_Reduced)==0 );
        99663  +    if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){
 98923  99664         if( pA->iColumn!=pB->iColumn ) return 2;
 98924  99665         if( pA->iTable!=pB->iTable 
 98925  99666          && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
 98926  99667       }
 98927  99668     }
 98928  99669     return 0;
 98929  99670   }
................................................................................
 99261 100002   ** for additional information.
 99262 100003   */
 99263 100004   static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
 99264 100005     int i;
 99265 100006     NameContext *pNC = pWalker->u.pNC;
 99266 100007     Parse *pParse = pNC->pParse;
 99267 100008     SrcList *pSrcList = pNC->pSrcList;
 99268         -  AggInfo *pAggInfo = pNC->pAggInfo;
       100009  +  AggInfo *pAggInfo = pNC->uNC.pAggInfo;
 99269 100010   
       100011  +  assert( pNC->ncFlags & NC_UAggInfo );
 99270 100012     switch( pExpr->op ){
 99271 100013       case TK_AGG_COLUMN:
 99272 100014       case TK_COLUMN: {
 99273 100015         testcase( pExpr->op==TK_AGG_COLUMN );
 99274 100016         testcase( pExpr->op==TK_COLUMN );
 99275 100017         /* Check to see if the column is in one of the tables in the FROM
 99276 100018         ** clause of the aggregate query */
................................................................................
102432 103174       }
102433 103175       assert( pVfs );
102434 103176       flags |= SQLITE_OPEN_MAIN_DB;
102435 103177       rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
102436 103178       sqlite3_free( zPath );
102437 103179       db->nDb++;
102438 103180     }
102439         -  db->skipBtreeMutex = 0;
       103181  +  db->noSharedCache = 0;
102440 103182     if( rc==SQLITE_CONSTRAINT ){
102441 103183       rc = SQLITE_ERROR;
102442 103184       zErrDyn = sqlite3MPrintf(db, "database is already attached");
102443 103185     }else if( rc==SQLITE_OK ){
102444 103186       Pager *pPager;
102445 103187       pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
102446 103188       if( !pNew->pSchema ){
................................................................................
102504 103246     ** If this fails, or if opening the file failed, then close the file and 
102505 103247     ** remove the entry from the db->aDb[] array. i.e. put everything back the
102506 103248     ** way we found it.
102507 103249     */
102508 103250     if( rc==SQLITE_OK ){
102509 103251       sqlite3BtreeEnterAll(db);
102510 103252       db->init.iDb = 0;
       103253  +    db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
102511 103254       rc = sqlite3Init(db, &zErrDyn);
102512 103255       sqlite3BtreeLeaveAll(db);
102513 103256       assert( zErrDyn==0 || rc!=SQLITE_OK );
102514 103257     }
102515 103258   #ifdef SQLITE_USER_AUTHENTICATION
102516 103259     if( rc==SQLITE_OK ){
102517 103260       u8 newAuth = 0;
................................................................................
102776 103519         pItem->zDatabase = 0;
102777 103520         pItem->pSchema = pFix->pSchema;
102778 103521       }
102779 103522   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
102780 103523       if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
102781 103524       if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
102782 103525   #endif
       103526  +    if( pItem->fg.isTabFunc && sqlite3FixExprList(pFix, pItem->u1.pFuncArg) ){
       103527  +      return 1;
       103528  +    }
102783 103529     }
102784 103530     return 0;
102785 103531   }
102786 103532   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
102787 103533   SQLITE_PRIVATE int sqlite3FixSelect(
102788 103534     DbFixer *pFix,       /* Context of the fixation */
102789 103535     Select *pSelect      /* The SELECT statement to be fixed to one database */
................................................................................
102875 103621       }
102876 103622       if( sqlite3FixExpr(pFix, pStep->pWhere) ){
102877 103623         return 1;
102878 103624       }
102879 103625       if( sqlite3FixExprList(pFix, pStep->pExprList) ){
102880 103626         return 1;
102881 103627       }
       103628  +#ifndef SQLITE_OMIT_UPSERT
       103629  +    if( pStep->pUpsert ){
       103630  +      Upsert *pUp = pStep->pUpsert;
       103631  +      if( sqlite3FixExprList(pFix, pUp->pUpsertTarget)
       103632  +       || sqlite3FixExpr(pFix, pUp->pUpsertTargetWhere)
       103633  +       || sqlite3FixExprList(pFix, pUp->pUpsertSet)
       103634  +       || sqlite3FixExpr(pFix, pUp->pUpsertWhere)
       103635  +      ){
       103636  +        return 1;
       103637  +      }
       103638  +    }
       103639  +#endif
102882 103640       pStep = pStep->pNext;
102883 103641     }
102884 103642     return 0;
102885 103643   }
102886 103644   #endif
102887 103645   
102888 103646   /************** End of attach.c **********************************************/
................................................................................
103035 103793     sqlite3 *db = pParse->db;
103036 103794     Table *pTab = 0;      /* The table being read */
103037 103795     const char *zCol;     /* Name of the column of the table */
103038 103796     int iSrc;             /* Index in pTabList->a[] of table being read */
103039 103797     int iDb;              /* The index of the database the expression refers to */
103040 103798     int iCol;             /* Index of column in table */
103041 103799   
       103800  +  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
103042 103801     if( db->xAuth==0 ) return;
103043 103802     iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
103044 103803     if( iDb<0 ){
103045 103804       /* An attempt to read a column out of a subquery or other
103046 103805       ** temporary table. */
103047 103806       return;
103048 103807     }
103049 103808   
103050         -  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
103051 103809     if( pExpr->op==TK_TRIGGER ){
103052 103810       pTab = pParse->pTriggerTab;
103053 103811     }else{
103054 103812       assert( pTabList );
103055 103813       for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
103056 103814         if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
103057 103815           pTab = pTabList->a[iSrc].pTab;
................................................................................
103502 104260   SQLITE_PRIVATE Table *sqlite3LocateTable(
103503 104261     Parse *pParse,         /* context in which to report errors */
103504 104262     u32 flags,             /* LOCATE_VIEW or LOCATE_NOERR */
103505 104263     const char *zName,     /* Name of the table we are looking for */
103506 104264     const char *zDbase     /* Name of the database.  Might be NULL */
103507 104265   ){
103508 104266     Table *p;
       104267  +  sqlite3 *db = pParse->db;
103509 104268   
103510 104269     /* Read the database schema. If an error occurs, leave an error message
103511 104270     ** and code in pParse and return NULL. */
103512         -  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
       104271  +  if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 
       104272  +   && SQLITE_OK!=sqlite3ReadSchema(pParse)
       104273  +  ){
103513 104274       return 0;
103514 104275     }
103515 104276   
103516         -  p = sqlite3FindTable(pParse->db, zName, zDbase);
       104277  +  p = sqlite3FindTable(db, zName, zDbase);
103517 104278     if( p==0 ){
103518 104279       const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
103519 104280   #ifndef SQLITE_OMIT_VIRTUALTABLE
103520         -    if( sqlite3FindDbName(pParse->db, zDbase)<1 ){
       104281  +    if( sqlite3FindDbName(db, zDbase)<1 ){
103521 104282         /* If zName is the not the name of a table in the schema created using
103522 104283         ** CREATE, then check to see if it is the name of an virtual table that
103523 104284         ** can be an eponymous virtual table. */
103524         -      Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName);
       104285  +      Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
103525 104286         if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
103526         -        pMod = sqlite3PragmaVtabRegister(pParse->db, zName);
       104287  +        pMod = sqlite3PragmaVtabRegister(db, zName);
103527 104288         }
103528 104289         if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
103529 104290           return pMod->pEpoTab;
103530 104291         }
103531 104292       }
103532 104293   #endif
103533 104294       if( (flags & LOCATE_NOERR)==0 ){
................................................................................
103684 104445     int i;
103685 104446     assert( iDb<db->nDb );
103686 104447   
103687 104448     if( iDb>=0 ){
103688 104449       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
103689 104450       DbSetProperty(db, iDb, DB_ResetWanted);
103690 104451       DbSetProperty(db, 1, DB_ResetWanted);
       104452  +    db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
103691 104453     }
103692 104454   
103693 104455     if( db->nSchemaLock==0 ){
103694 104456       for(i=0; i<db->nDb; i++){
103695 104457         if( DbHasProperty(db, i, DB_ResetWanted) ){
103696 104458           sqlite3SchemaClear(db->aDb[i].pSchema);
103697 104459         }
................................................................................
103709 104471     assert( db->nSchemaLock==0 );
103710 104472     for(i=0; i<db->nDb; i++){
103711 104473       Db *pDb = &db->aDb[i];
103712 104474       if( pDb->pSchema ){
103713 104475         sqlite3SchemaClear(pDb->pSchema);
103714 104476       }
103715 104477     }
103716         -  db->mDbFlags &= ~DBFLAG_SchemaChange;
       104478  +  db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
103717 104479     sqlite3VtabUnlockList(db);
103718 104480     sqlite3BtreeLeaveAll(db);
103719 104481     sqlite3CollapseDatabaseArray(db);
103720 104482   }
103721 104483   
103722 104484   /*
103723 104485   ** This routine is called when a commit occurs.
................................................................................
104254 105016     pCol = &p->aCol[p->nCol];
104255 105017     memset(pCol, 0, sizeof(p->aCol[0]));
104256 105018     pCol->zName = z;
104257 105019     sqlite3ColumnPropertiesFromName(p, pCol);
104258 105020    
104259 105021     if( pType->n==0 ){
104260 105022       /* If there is no type specified, columns have the default affinity
104261         -    ** 'BLOB'. */
       105023  +    ** 'BLOB' with a default size of 4 bytes. */
104262 105024       pCol->affinity = SQLITE_AFF_BLOB;
104263 105025       pCol->szEst = 1;
       105026  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       105027  +    if( 4>=sqlite3GlobalConfig.szSorterRef ){
       105028  +      pCol->colFlags |= COLFLAG_SORTERREF;
       105029  +    }
       105030  +#endif
104264 105031     }else{
104265 105032       zType = z + sqlite3Strlen30(z) + 1;
104266 105033       memcpy(zType, pType->z, pType->n);
104267 105034       zType[pType->n] = 0;
104268 105035       sqlite3Dequote(zType);
104269         -    pCol->affinity = sqlite3AffinityType(zType, &pCol->szEst);
       105036  +    pCol->affinity = sqlite3AffinityType(zType, pCol);
104270 105037       pCol->colFlags |= COLFLAG_HASTYPE;
104271 105038     }
104272 105039     p->nCol++;
104273 105040     pParse->constraintName.n = 0;
104274 105041   }
104275 105042   
104276 105043   /*
................................................................................
104322 105089   ** 'REAL'        | SQLITE_AFF_REAL
104323 105090   ** 'FLOA'        | SQLITE_AFF_REAL
104324 105091   ** 'DOUB'        | SQLITE_AFF_REAL
104325 105092   **
104326 105093   ** If none of the substrings in the above table are found,
104327 105094   ** SQLITE_AFF_NUMERIC is returned.
104328 105095   */
104329         -SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
       105096  +SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, Column *pCol){
104330 105097     u32 h = 0;
104331 105098     char aff = SQLITE_AFF_NUMERIC;
104332 105099     const char *zChar = 0;
104333 105100   
104334 105101     assert( zIn!=0 );
104335 105102     while( zIn[0] ){
104336 105103       h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
................................................................................
104359 105126   #endif
104360 105127       }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
104361 105128         aff = SQLITE_AFF_INTEGER;
104362 105129         break;
104363 105130       }
104364 105131     }
104365 105132   
104366         -  /* If pszEst is not NULL, store an estimate of the field size.  The
       105133  +  /* If pCol is not NULL, store an estimate of the field size.  The
104367 105134     ** estimate is scaled so that the size of an integer is 1.  */
104368         -  if( pszEst ){
104369         -    *pszEst = 1;   /* default size is approx 4 bytes */
       105135  +  if( pCol ){
       105136  +    int v = 0;   /* default size is approx 4 bytes */
104370 105137       if( aff<SQLITE_AFF_NUMERIC ){
104371 105138         if( zChar ){
104372 105139           while( zChar[0] ){
104373 105140             if( sqlite3Isdigit(zChar[0]) ){
104374         -            int v = 0;
       105141  +            /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
104375 105142               sqlite3GetInt32(zChar, &v);
104376         -            v = v/4 + 1;
104377         -            if( v>255 ) v = 255;
104378         -            *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
104379 105143               break;
104380 105144             }
104381 105145             zChar++;
104382 105146           }
104383 105147         }else{
104384         -        *pszEst = 5;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
       105148  +        v = 16;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
104385 105149         }
104386 105150       }
       105151  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       105152  +    if( v>=sqlite3GlobalConfig.szSorterRef ){
       105153  +      pCol->colFlags |= COLFLAG_SORTERREF;
       105154  +    }
       105155  +#endif
       105156  +    v = v/4 + 1;
       105157  +    if( v>255 ) v = 255;
       105158  +    pCol->szEst = v;
104387 105159     }
104388 105160     return aff;
104389 105161   }
104390 105162   
104391 105163   /*
104392 105164   ** The expression is the default value for the most recently added column
104393 105165   ** of the table currently under construction.
................................................................................
105988 106760                       sqlite3KeyInfoRef(pKey), P4_KEYINFO);
105989 106761   
105990 106762     /* Open the table. Loop through all rows of the table, inserting index
105991 106763     ** records into the sorter. */
105992 106764     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
105993 106765     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
105994 106766     regRecord = sqlite3GetTempReg(pParse);
       106767  +  sqlite3MultiWrite(pParse);
105995 106768   
105996 106769     sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
105997 106770     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
105998 106771     sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
105999 106772     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
106000 106773     sqlite3VdbeJumpHere(v, addr1);
106001 106774     if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
106002 106775     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
106003 106776                       (char *)pKey, P4_KEYINFO);
106004 106777     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
106005 106778   
106006 106779     addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
106007 106780     if( IsUniqueIndex(pIndex) ){
106008         -    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
106009         -    sqlite3VdbeGoto(v, j2);
       106781  +    int j2 = sqlite3VdbeGoto(v, 1);
106010 106782       addr2 = sqlite3VdbeCurrentAddr(v);
       106783  +    sqlite3VdbeVerifyAbortable(v, OE_Abort);
106011 106784       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
106012 106785                            pIndex->nKeyCol); VdbeCoverage(v);
106013 106786       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
       106787  +    sqlite3VdbeJumpHere(v, j2);
106014 106788     }else{
106015 106789       addr2 = sqlite3VdbeCurrentAddr(v);
106016 106790     }
106017 106791     sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
106018 106792     sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
106019 106793     sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
106020 106794     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
................................................................................
106169 106943     assert( pTab!=0 );
106170 106944     assert( pParse->nErr==0 );
106171 106945     if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
106172 106946          && db->init.busy==0
106173 106947   #if SQLITE_USER_AUTHENTICATION
106174 106948          && sqlite3UserAuthTable(pTab->zName)==0
106175 106949   #endif
106176         -       && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
       106950  +#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
       106951  +       && sqlite3StrICmp(&pTab->zName[7],"master")!=0
       106952  +#endif
       106953  +       && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
       106954  + ){
106177 106955       sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
106178 106956       goto exit_create_index;
106179 106957     }
106180 106958   #ifndef SQLITE_OMIT_VIEW
106181 106959     if( pTab->pSelect ){
106182 106960       sqlite3ErrorMsg(pParse, "views may not be indexed");
106183 106961       goto exit_create_index;
................................................................................
107348 108126     char *zErr;
107349 108127     int j;
107350 108128     StrAccum errMsg;
107351 108129     Table *pTab = pIdx->pTable;
107352 108130   
107353 108131     sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
107354 108132     if( pIdx->aColExpr ){
107355         -    sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName);
       108133  +    sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
107356 108134     }else{
107357 108135       for(j=0; j<pIdx->nKeyCol; j++){
107358 108136         char *zCol;
107359 108137         assert( pIdx->aiColumn[j]>=0 );
107360 108138         zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
107361         -      if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
107362         -      sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
107363         -      sqlite3StrAccumAppend(&errMsg, ".", 1);
107364         -      sqlite3StrAccumAppendAll(&errMsg, zCol);
       108139  +      if( j ) sqlite3_str_append(&errMsg, ", ", 2);
       108140  +      sqlite3_str_appendall(&errMsg, pTab->zName);
       108141  +      sqlite3_str_append(&errMsg, ".", 1);
       108142  +      sqlite3_str_appendall(&errMsg, zCol);
107365 108143       }
107366 108144     }
107367 108145     zErr = sqlite3StrAccumFinish(&errMsg);
107368 108146     sqlite3HaltConstraint(pParse, 
107369 108147       IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY 
107370 108148                               : SQLITE_CONSTRAINT_UNIQUE,
107371 108149       onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
................................................................................
108043 108821     /* If the createFlag parameter is true and the search did not reveal an
108044 108822     ** exact match for the name, number of arguments and encoding, then add a
108045 108823     ** new entry to the hash table and return it.
108046 108824     */
108047 108825     if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
108048 108826         (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
108049 108827       FuncDef *pOther;
       108828  +    u8 *z;
108050 108829       pBest->zName = (const char*)&pBest[1];
108051 108830       pBest->nArg = (u16)nArg;
108052 108831       pBest->funcFlags = enc;
108053 108832       memcpy((char*)&pBest[1], zName, nName+1);
       108833  +    for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
108054 108834       pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
108055 108835       if( pOther==pBest ){
108056 108836         sqlite3DbFree(db, pBest);
108057 108837         sqlite3OomFault(db);
108058 108838         return 0;
108059 108839       }else{
108060 108840         pBest->pNext = pOther;
................................................................................
108364 109144     int iDataCur = 0;      /* VDBE cursor for the canonical data source */
108365 109145     int iIdxCur = 0;       /* Cursor number of the first index */
108366 109146     int nIdx;              /* Number of indices */
108367 109147     sqlite3 *db;           /* Main database structure */
108368 109148     AuthContext sContext;  /* Authorization context */
108369 109149     NameContext sNC;       /* Name context to resolve expressions in */
108370 109150     int iDb;               /* Database number */
108371         -  int memCnt = -1;       /* Memory cell used for change counting */
       109151  +  int memCnt = 0;        /* Memory cell used for change counting */
108372 109152     int rcauth;            /* Value returned by authorization callback */
108373 109153     int eOnePass;          /* ONEPASS_OFF or _SINGLE or _MULTI */
108374 109154     int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
108375 109155     u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
108376 109156     Index *pPk;            /* The PRIMARY KEY index on the table */
108377 109157     int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
108378 109158     i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
................................................................................
108469 109249     /* Begin generating code.
108470 109250     */
108471 109251     v = sqlite3GetVdbe(pParse);
108472 109252     if( v==0 ){
108473 109253       goto delete_from_cleanup;
108474 109254     }
108475 109255     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
108476         -  sqlite3BeginWriteOperation(pParse, 1, iDb);
       109256  +  sqlite3BeginWriteOperation(pParse, bComplex, iDb);
108477 109257   
108478 109258     /* If we are trying to delete from a view, realize that view into
108479 109259     ** an ephemeral table.
108480 109260     */
108481 109261   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
108482 109262     if( isView ){
108483 109263       sqlite3MaterializeView(pParse, pTab, 
................................................................................
108497 109277     if( sqlite3ResolveExprNames(&sNC, pWhere) ){
108498 109278       goto delete_from_cleanup;
108499 109279     }
108500 109280   
108501 109281     /* Initialize the counter of the number of rows deleted, if
108502 109282     ** we are counting rows.
108503 109283     */
108504         -  if( db->flags & SQLITE_CountRows ){
       109284  +  if( (db->flags & SQLITE_CountRows)!=0
       109285  +   && !pParse->nested
       109286  +   && !pParse->pTriggerTab
       109287  +  ){
108505 109288       memCnt = ++pParse->nMem;
108506 109289       sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
108507 109290     }
108508 109291   
108509 109292   #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
108510 109293     /* Special case: A DELETE without a WHERE clause deletes everything.
108511 109294     ** It is easier just to erase the whole table. Prior to version 3.6.5,
................................................................................
108525 109308   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108526 109309      && db->xPreUpdateCallback==0
108527 109310   #endif
108528 109311     ){
108529 109312       assert( !isView );
108530 109313       sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
108531 109314       if( HasRowid(pTab) ){
108532         -      sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
       109315  +      sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
108533 109316                           pTab->zName, P4_STATIC);
108534 109317       }
108535 109318       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108536 109319         assert( pIdx->pSchema==pTab->pSchema );
108537 109320         sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
108538 109321       }
108539 109322     }else
................................................................................
108570 109353       **  ONEPASS_MULTI:  One-pass approach - any number of rows may be deleted.
108571 109354       */
108572 109355       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
108573 109356       if( pWInfo==0 ) goto delete_from_cleanup;
108574 109357       eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
108575 109358       assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
108576 109359       assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
       109360  +    if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
108577 109361     
108578 109362       /* Keep track of the number of rows to be deleted */
108579         -    if( db->flags & SQLITE_CountRows ){
       109363  +    if( memCnt ){
108580 109364         sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
108581 109365       }
108582 109366     
108583 109367       /* Extract the rowid or primary key for the current row */
108584 109368       if( pPk ){
108585 109369         for(i=0; i<nPk; i++){
108586 109370           assert( pPk->aiColumn[i]>=0 );
................................................................................
108675 109459       }  
108676 109460     
108677 109461       /* Delete the row */
108678 109462   #ifndef SQLITE_OMIT_VIRTUALTABLE
108679 109463       if( IsVirtual(pTab) ){
108680 109464         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
108681 109465         sqlite3VtabMakeWritable(pParse, pTab);
108682         -      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
108683         -      sqlite3VdbeChangeP5(v, OE_Abort);
108684 109466         assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
108685 109467         sqlite3MayAbort(pParse);
108686         -      if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){
108687         -        pParse->isMultiWrite = 0;
       109468  +      if( eOnePass==ONEPASS_SINGLE ){
       109469  +        sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
       109470  +        if( sqlite3IsToplevel(pParse) ){
       109471  +          pParse->isMultiWrite = 0;
       109472  +        }
108688 109473         }
       109474  +      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
       109475  +      sqlite3VdbeChangeP5(v, OE_Abort);
108689 109476       }else
108690 109477   #endif
108691 109478       {
108692 109479         int count = (pParse->nested==0);    /* True to count changes */
108693 109480         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
108694 109481             iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
108695 109482       }
................................................................................
108715 109502       sqlite3AutoincrementEnd(pParse);
108716 109503     }
108717 109504   
108718 109505     /* Return the number of rows that were deleted. If this routine is 
108719 109506     ** generating code because of a call to sqlite3NestedParse(), do not
108720 109507     ** invoke the callback function.
108721 109508     */
108722         -  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
       109509  +  if( memCnt ){
108723 109510       sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
108724 109511       sqlite3VdbeSetNumCols(v, 1);
108725 109512       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
108726 109513     }
108727 109514   
108728 109515   delete_from_cleanup:
108729 109516     sqlite3AuthContextPop(&sContext);
................................................................................
109323 110110   
109324 110111     if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
109325 110112       x.nArg = argc-1;
109326 110113       x.nUsed = 0;
109327 110114       x.apArg = argv+1;
109328 110115       sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
109329 110116       str.printfFlags = SQLITE_PRINTF_SQLFUNC;
109330         -    sqlite3XPrintf(&str, zFormat, &x);
       110117  +    sqlite3_str_appendf(&str, zFormat, &x);
109331 110118       n = str.nChar;
109332 110119       sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
109333 110120                           SQLITE_DYNAMIC);
109334 110121     }
109335 110122   }
109336 110123   
109337 110124   /*
................................................................................
110726 111513         if( argc==2 ){
110727 111514           zSep = (char*)sqlite3_value_text(argv[1]);
110728 111515           nSep = sqlite3_value_bytes(argv[1]);
110729 111516         }else{
110730 111517           zSep = ",";
110731 111518           nSep = 1;
110732 111519         }
110733         -      if( zSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
       111520  +      if( zSep ) sqlite3_str_append(pAccum, zSep, nSep);
110734 111521       }
110735 111522       zVal = (char*)sqlite3_value_text(argv[0]);
110736 111523       nVal = sqlite3_value_bytes(argv[0]);
110737         -    if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
       111524  +    if( zVal ) sqlite3_str_append(pAccum, zVal, nVal);
110738 111525     }
110739 111526   }
110740 111527   static void groupConcatFinalize(sqlite3_context *context){
110741 111528     StrAccum *pAccum;
110742 111529     pAccum = sqlite3_aggregate_context(context, 0);
110743 111530     if( pAccum ){
110744         -    if( pAccum->accError==STRACCUM_TOOBIG ){
       111531  +    if( pAccum->accError==SQLITE_TOOBIG ){
110745 111532         sqlite3_result_error_toobig(context);
110746         -    }else if( pAccum->accError==STRACCUM_NOMEM ){
       111533  +    }else if( pAccum->accError==SQLITE_NOMEM ){
110747 111534         sqlite3_result_error_nomem(context);
110748 111535       }else{    
110749 111536         sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
110750 111537                             sqlite3_free);
110751 111538       }
110752 111539     }
110753 111540   }
................................................................................
111316 112103     int isIgnore          /* If true, pretend pTab contains all NULL values */
111317 112104   ){
111318 112105     int i;                                    /* Iterator variable */
111319 112106     Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
111320 112107     int iCur = pParse->nTab - 1;              /* Cursor number to use */
111321 112108     int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
111322 112109   
       112110  +  sqlite3VdbeVerifyAbortable(v,
       112111  +    (!pFKey->isDeferred
       112112  +      && !(pParse->db->flags & SQLITE_DeferFKs)
       112113  +      && !pParse->pToplevel 
       112114  +      && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
       112115  +
111323 112116     /* If nIncr is less than zero, then check at runtime if there are any
111324 112117     ** outstanding constraints to resolve. If there are not, there is no need
111325 112118     ** to check if deleting this row resolves any outstanding violations.
111326 112119     **
111327 112120     ** Check if any of the key columns in the child table row are NULL. If 
111328 112121     ** any are, then the constraint is considered satisfied. No need to 
111329 112122     ** search for a matching row in the parent table.  */
................................................................................
111723 112516       ** transactions are not able to rollback schema changes.  
111724 112517       **
111725 112518       ** If the SQLITE_DeferFKs flag is set, then this is not required, as
111726 112519       ** the statement transaction will not be rolled back even if FK
111727 112520       ** constraints are violated.
111728 112521       */
111729 112522       if( (db->flags & SQLITE_DeferFKs)==0 ){
       112523  +      sqlite3VdbeVerifyAbortable(v, OE_Abort);
111730 112524         sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
111731 112525         VdbeCoverage(v);
111732 112526         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
111733 112527             OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
111734 112528       }
111735 112529   
111736 112530       if( iSkip ){
................................................................................
112635 113429   */
112636 113430   static int autoIncBegin(
112637 113431     Parse *pParse,      /* Parsing context */
112638 113432     int iDb,            /* Index of the database holding pTab */
112639 113433     Table *pTab         /* The table we are writing to */
112640 113434   ){
112641 113435     int memId = 0;      /* Register holding maximum rowid */
       113436  +  assert( pParse->db->aDb[iDb].pSchema!=0 );
112642 113437     if( (pTab->tabFlags & TF_Autoincrement)!=0
112643 113438      && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
112644 113439     ){
112645 113440       Parse *pToplevel = sqlite3ParseToplevel(pParse);
112646 113441       AutoincInfo *pInfo;
       113442  +    Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
       113443  +
       113444  +    /* Verify that the sqlite_sequence table exists and is an ordinary
       113445  +    ** rowid table with exactly two columns.
       113446  +    ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
       113447  +    if( pSeqTab==0
       113448  +     || !HasRowid(pSeqTab)
       113449  +     || IsVirtual(pSeqTab)
       113450  +     || pSeqTab->nCol!=2
       113451  +    ){
       113452  +      pParse->nErr++;
       113453  +      pParse->rc = SQLITE_CORRUPT_SEQUENCE;
       113454  +      return 0;
       113455  +    }
112647 113456   
112648 113457       pInfo = pToplevel->pAinc;
112649 113458       while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
112650 113459       if( pInfo==0 ){
112651 113460         pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
112652 113461         if( pInfo==0 ) return 0;
112653 113462         pInfo->pNext = pToplevel->pAinc;
................................................................................
112897 113706   **      D: cleanup
112898 113707   */
112899 113708   SQLITE_PRIVATE void sqlite3Insert(
112900 113709     Parse *pParse,        /* Parser context */
112901 113710     SrcList *pTabList,    /* Name of table into which we are inserting */
112902 113711     Select *pSelect,      /* A SELECT statement to use as the data source */
112903 113712     IdList *pColumn,      /* Column names corresponding to IDLIST. */
112904         -  int onError           /* How to handle constraint errors */
       113713  +  int onError,          /* How to handle constraint errors */
       113714  +  Upsert *pUpsert       /* ON CONFLICT clauses for upsert, or NULL */
112905 113715   ){
112906 113716     sqlite3 *db;          /* The main database structure */
112907 113717     Table *pTab;          /* The table to insert into.  aka TABLE */
112908 113718     int i, j;             /* Loop counters */
112909 113719     Vdbe *v;              /* Generate code into this virtual machine */
112910 113720     Index *pIdx;          /* For looping over indices of the table */
112911 113721     int nColumn;          /* Number of columns in the data */
................................................................................
113192 114002     if( pColumn!=0 && nColumn!=pColumn->nId ){
113193 114003       sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
113194 114004       goto insert_cleanup;
113195 114005     }
113196 114006       
113197 114007     /* Initialize the count of rows to be inserted
113198 114008     */
113199         -  if( db->flags & SQLITE_CountRows ){
       114009  +  if( (db->flags & SQLITE_CountRows)!=0
       114010  +   && !pParse->nested
       114011  +   && !pParse->pTriggerTab
       114012  +  ){
113200 114013       regRowCount = ++pParse->nMem;
113201 114014       sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
113202 114015     }
113203 114016   
113204 114017     /* If this is not a view, open the table and and all indices */
113205 114018     if( !isView ){
113206 114019       int nIdx;
................................................................................
113212 114025       }
113213 114026       for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
113214 114027         assert( pIdx );
113215 114028         aRegIdx[i] = ++pParse->nMem;
113216 114029         pParse->nMem += pIdx->nColumn;
113217 114030       }
113218 114031     }
       114032  +#ifndef SQLITE_OMIT_UPSERT
       114033  +  if( pUpsert ){
       114034  +    pTabList->a[0].iCursor = iDataCur;
       114035  +    pUpsert->pUpsertSrc = pTabList;
       114036  +    pUpsert->regData = regData;
       114037  +    pUpsert->iDataCur = iDataCur;
       114038  +    pUpsert->iIdxCur = iIdxCur;
       114039  +    if( pUpsert->pUpsertTarget ){
       114040  +      sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
       114041  +    }
       114042  +  }
       114043  +#endif
       114044  +
113219 114045   
113220 114046     /* This is the top of the main insertion loop */
113221 114047     if( useTempTable ){
113222 114048       /* This block codes the top of loop only.  The complete loop is the
113223 114049       ** following pseudocode (template 4):
113224 114050       **
113225 114051       **         rewind temp table, if empty goto D
................................................................................
113414 114240         sqlite3MayAbort(pParse);
113415 114241       }else
113416 114242   #endif
113417 114243       {
113418 114244         int isReplace;    /* Set to true if constraints may cause a replace */
113419 114245         int bUseSeek;     /* True to use OPFLAG_SEEKRESULT */
113420 114246         sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
113421         -          regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0
       114247  +          regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
113422 114248         );
113423 114249         sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
113424 114250   
113425 114251         /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
113426 114252         ** constraints or (b) there are no triggers and this table is not a
113427 114253         ** parent table in a foreign key constraint. It is safe to set the
113428 114254         ** flag in the second case as if any REPLACE constraint is hit, an
................................................................................
113437 114263             regIns, aRegIdx, 0, appendFlag, bUseSeek
113438 114264         );
113439 114265       }
113440 114266     }
113441 114267   
113442 114268     /* Update the count of rows that are inserted
113443 114269     */
113444         -  if( (db->flags & SQLITE_CountRows)!=0 ){
       114270  +  if( regRowCount ){
113445 114271       sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
113446 114272     }
113447 114273   
113448 114274     if( pTrigger ){
113449 114275       /* Code AFTER triggers */
113450 114276       sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 
113451 114277           pTab, regData-2-pTab->nCol, onError, endOfLoop);
................................................................................
113474 114300     }
113475 114301   
113476 114302     /*
113477 114303     ** Return the number of rows inserted. If this routine is 
113478 114304     ** generating code because of a call to sqlite3NestedParse(), do not
113479 114305     ** invoke the callback function.
113480 114306     */
113481         -  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
       114307  +  if( regRowCount ){
113482 114308       sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
113483 114309       sqlite3VdbeSetNumCols(v, 1);
113484 114310       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
113485 114311     }
113486 114312   
113487 114313   insert_cleanup:
113488 114314     sqlite3SrcListDelete(db, pTabList);
113489 114315     sqlite3ExprListDelete(db, pList);
       114316  +  sqlite3UpsertDelete(db, pUpsert);
113490 114317     sqlite3SelectDelete(db, pSelect);
113491 114318     sqlite3IdListDelete(db, pColumn);
113492 114319     sqlite3DbFree(db, aRegIdx);
113493 114320   }
113494 114321   
113495 114322   /* Make sure "isView" and other macros defined above are undefined. Otherwise
113496 114323   ** they may interfere with compilation of other functions in this file
................................................................................
113553 114380     }
113554 114381     testcase( w.eCode==0 );
113555 114382     testcase( w.eCode==CKCNSTRNT_COLUMN );
113556 114383     testcase( w.eCode==CKCNSTRNT_ROWID );
113557 114384     testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
113558 114385     return !w.eCode;
113559 114386   }
       114387  +
       114388  +/*
       114389  +** An instance of the ConstraintAddr object remembers the byte-code addresses
       114390  +** for sections of the constraint checks that deal with uniqueness constraints
       114391  +** on the rowid and on the upsert constraint.
       114392  +**
       114393  +** This information is passed into checkReorderConstraintChecks() to insert
       114394  +** some OP_Goto operations so that the rowid and upsert constraints occur
       114395  +** in the correct order relative to other constraints.
       114396  +*/
       114397  +typedef struct ConstraintAddr ConstraintAddr;
       114398  +struct ConstraintAddr {
       114399  +  int ipkTop;          /* Subroutine for rowid constraint check */
       114400  +  int upsertTop;       /* Label for upsert constraint check subroutine */
       114401  +  int upsertTop2;      /* Copy of upsertTop not cleared by the call */
       114402  +  int upsertBtm;       /* upsert constraint returns to this label */
       114403  +  int ipkBtm;          /* Return opcode rowid constraint check */
       114404  +};
       114405  +
       114406  +/*
       114407  +** Generate any OP_Goto operations needed to cause constraints to be
       114408  +** run that haven't already been run.
       114409  +*/
       114410  +static void reorderConstraintChecks(Vdbe *v, ConstraintAddr *p){
       114411  +  if( p->upsertTop ){
       114412  +    testcase( sqlite3VdbeLabelHasBeenResolved(v, p->upsertTop) );
       114413  +    sqlite3VdbeGoto(v, p->upsertTop);
       114414  +    VdbeComment((v, "call upsert subroutine"));
       114415  +    sqlite3VdbeResolveLabel(v, p->upsertBtm);
       114416  +    p->upsertTop = 0;
       114417  +  }
       114418  +  if( p->ipkTop ){
       114419  +    sqlite3VdbeGoto(v, p->ipkTop);
       114420  +    VdbeComment((v, "call rowid unique-check subroutine"));
       114421  +    sqlite3VdbeJumpHere(v, p->ipkBtm);
       114422  +    p->ipkTop = 0;
       114423  +  }
       114424  +}
113560 114425   
113561 114426   /*
113562 114427   ** Generate code to do constraint checks prior to an INSERT or an UPDATE
113563 114428   ** on table pTab.
113564 114429   **
113565 114430   ** The regNewData parameter is the first register in a range that contains
113566 114431   ** the data to be inserted or the data after the update.  There will be
................................................................................
113649 114514     int iIdxCur,         /* First index cursor */
113650 114515     int regNewData,      /* First register in a range holding values to insert */
113651 114516     int regOldData,      /* Previous content.  0 for INSERTs */
113652 114517     u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
113653 114518     u8 overrideError,    /* Override onError to this if not OE_Default */
113654 114519     int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
113655 114520     int *pbMayReplace,   /* OUT: Set to true if constraint may cause a replace */
113656         -  int *aiChng          /* column i is unchanged if aiChng[i]<0 */
       114521  +  int *aiChng,         /* column i is unchanged if aiChng[i]<0 */
       114522  +  Upsert *pUpsert      /* ON CONFLICT clauses, if any.  NULL otherwise */
113657 114523   ){
113658 114524     Vdbe *v;             /* VDBE under constrution */
113659 114525     Index *pIdx;         /* Pointer to one of the indices */
113660 114526     Index *pPk = 0;      /* The PRIMARY KEY index */
113661 114527     sqlite3 *db;         /* Database connection */
113662 114528     int i;               /* loop counter */
113663 114529     int ix;              /* Index loop counter */
113664 114530     int nCol;            /* Number of columns */
113665 114531     int onError;         /* Conflict resolution strategy */
113666 114532     int addr1;           /* Address of jump instruction */
113667 114533     int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
113668 114534     int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
113669         -  int ipkTop = 0;      /* Top of the rowid change constraint check */
113670         -  int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
       114535  +  ConstraintAddr sAddr;/* Address information for constraint reordering */
       114536  +  Index *pUpIdx = 0;   /* Index to which to apply the upsert */
113671 114537     u8 isUpdate;         /* True if this is an UPDATE operation */
113672 114538     u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
       114539  +  int upsertBypass = 0;  /* Address of Goto to bypass upsert subroutine */
113673 114540   
113674 114541     isUpdate = regOldData!=0;
113675 114542     db = pParse->db;
113676 114543     v = sqlite3GetVdbe(pParse);
113677 114544     assert( v!=0 );
113678 114545     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
113679 114546     nCol = pTab->nCol;
       114547  +  memset(&sAddr, 0, sizeof(sAddr));
113680 114548     
113681 114549     /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
113682 114550     ** normal rowid tables.  nPkField is the number of key fields in the 
113683 114551     ** pPk index or 1 for a rowid table.  In other words, nPkField is the
113684 114552     ** number of fields in the true primary key of the table. */
113685 114553     if( HasRowid(pTab) ){
113686 114554       pPk = 0;
................................................................................
113755 114623       pParse->iSelfTab = -(regNewData+1);
113756 114624       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
113757 114625       for(i=0; i<pCheck->nExpr; i++){
113758 114626         int allOk;
113759 114627         Expr *pExpr = pCheck->a[i].pExpr;
113760 114628         if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
113761 114629         allOk = sqlite3VdbeMakeLabel(v);
       114630  +      sqlite3VdbeVerifyAbortable(v, onError);
113762 114631         sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
113763 114632         if( onError==OE_Ignore ){
113764 114633           sqlite3VdbeGoto(v, ignoreDest);
113765 114634         }else{
113766 114635           char *zName = pCheck->a[i].zName;
113767 114636           if( zName==0 ) zName = pTab->zName;
113768 114637           if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
................................................................................
113771 114640                                 P5_ConstraintCheck);
113772 114641         }
113773 114642         sqlite3VdbeResolveLabel(v, allOk);
113774 114643       }
113775 114644       pParse->iSelfTab = 0;
113776 114645     }
113777 114646   #endif /* !defined(SQLITE_OMIT_CHECK) */
       114647  +
       114648  +  /* UNIQUE and PRIMARY KEY constraints should be handled in the following
       114649  +  ** order:
       114650  +  **
       114651  +  **   (1)  OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
       114652  +  **   (2)  OE_Update
       114653  +  **   (3)  OE_Replace
       114654  +  **
       114655  +  ** OE_Fail and OE_Ignore must happen before any changes are made.
       114656  +  ** OE_Update guarantees that only a single row will change, so it
       114657  +  ** must happen before OE_Replace.  Technically, OE_Abort and OE_Rollback
       114658  +  ** could happen in any order, but they are grouped up front for
       114659  +  ** convenience.
       114660  +  **
       114661  +  ** Constraint checking code is generated in this order:
       114662  +  **   (A)  The rowid constraint
       114663  +  **   (B)  Unique index constraints that do not have OE_Replace as their
       114664  +  **        default conflict resolution strategy
       114665  +  **   (C)  Unique index that do use OE_Replace by default.
       114666  +  **
       114667  +  ** The ordering of (2) and (3) is accomplished by making sure the linked
       114668  +  ** list of indexes attached to a table puts all OE_Replace indexes last
       114669  +  ** in the list.  See sqlite3CreateIndex() for where that happens.
       114670  +  */
       114671  +
       114672  +  if( pUpsert ){
       114673  +    if( pUpsert->pUpsertTarget==0 ){
       114674  +      /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
       114675  +      ** Make all unique constraint resolution be OE_Ignore */
       114676  +      assert( pUpsert->pUpsertSet==0 );
       114677  +      overrideError = OE_Ignore;
       114678  +      pUpsert = 0;
       114679  +    }else if( (pUpIdx = pUpsert->pUpsertIdx)!=0 ){
       114680  +      /* If the constraint-target is on some column other than
       114681  +      ** then ROWID, then we might need to move the UPSERT around
       114682  +      ** so that it occurs in the correct order. */
       114683  +      sAddr.upsertTop = sAddr.upsertTop2 = sqlite3VdbeMakeLabel(v);
       114684  +      sAddr.upsertBtm = sqlite3VdbeMakeLabel(v);
       114685  +    }
       114686  +  }
113778 114687   
113779 114688     /* If rowid is changing, make sure the new rowid does not previously
113780 114689     ** exist in the table.
113781 114690     */
113782 114691     if( pkChng && pPk==0 ){
113783 114692       int addrRowidOk = sqlite3VdbeMakeLabel(v);
113784 114693   
................................................................................
113785 114694       /* Figure out what action to take in case of a rowid collision */
113786 114695       onError = pTab->keyConf;
113787 114696       if( overrideError!=OE_Default ){
113788 114697         onError = overrideError;
113789 114698       }else if( onError==OE_Default ){
113790 114699         onError = OE_Abort;
113791 114700       }
       114701  +
       114702  +    /* figure out whether or not upsert applies in this case */
       114703  +    if( pUpsert && pUpsert->pUpsertIdx==0 ){
       114704  +      if( pUpsert->pUpsertSet==0 ){
       114705  +        onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
       114706  +      }else{
       114707  +        onError = OE_Update;  /* DO UPDATE */
       114708  +      }
       114709  +    }
       114710  +
       114711  +    /* If the response to a rowid conflict is REPLACE but the response
       114712  +    ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
       114713  +    ** to defer the running of the rowid conflict checking until after
       114714  +    ** the UNIQUE constraints have run.
       114715  +    */
       114716  +    assert( OE_Update>OE_Replace );
       114717  +    assert( OE_Ignore<OE_Replace );
       114718  +    assert( OE_Fail<OE_Replace );
       114719  +    assert( OE_Abort<OE_Replace );
       114720  +    assert( OE_Rollback<OE_Replace );
       114721  +    if( onError>=OE_Replace
       114722  +     && (pUpsert || onError!=overrideError)
       114723  +     && pTab->pIndex
       114724  +    ){
       114725  +      sAddr.ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
       114726  +    }
113792 114727   
113793 114728       if( isUpdate ){
113794 114729         /* pkChng!=0 does not mean that the rowid has changed, only that
113795 114730         ** it might have changed.  Skip the conflict logic below if the rowid
113796 114731         ** is unchanged. */
113797 114732         sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
113798 114733         sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
113799 114734         VdbeCoverage(v);
113800 114735       }
113801 114736   
113802         -    /* If the response to a rowid conflict is REPLACE but the response
113803         -    ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
113804         -    ** to defer the running of the rowid conflict checking until after
113805         -    ** the UNIQUE constraints have run.
113806         -    */
113807         -    if( onError==OE_Replace && overrideError!=OE_Replace ){
113808         -      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113809         -        if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
113810         -          ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
113811         -          break;
113812         -        }
113813         -      }
113814         -    }
113815         -
113816 114737       /* Check to see if the new rowid already exists in the table.  Skip
113817 114738       ** the following conflict logic if it does not. */
       114739  +    VdbeNoopComment((v, "uniqueness check for ROWID"));
       114740  +    sqlite3VdbeVerifyAbortable(v, onError);
113818 114741       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
113819 114742       VdbeCoverage(v);
113820 114743   
113821         -    /* Generate code that deals with a rowid collision */
113822 114744       switch( onError ){
113823 114745         default: {
113824 114746           onError = OE_Abort;
113825 114747           /* Fall thru into the next case */
113826 114748         }
113827 114749         case OE_Rollback:
113828 114750         case OE_Abort:
113829 114751         case OE_Fail: {
       114752  +        testcase( onError==OE_Rollback );
       114753  +        testcase( onError==OE_Abort );
       114754  +        testcase( onError==OE_Fail );
113830 114755           sqlite3RowidConstraint(pParse, onError, pTab);
113831 114756           break;
113832 114757         }
113833 114758         case OE_Replace: {
113834 114759           /* If there are DELETE triggers on this table and the
113835 114760           ** recursive-triggers flag is set, call GenerateRowDelete() to
113836 114761           ** remove the conflicting row from the table. This will fire
................................................................................
113859 114784           }
113860 114785           if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
113861 114786             sqlite3MultiWrite(pParse);
113862 114787             sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
113863 114788                                      regNewData, 1, 0, OE_Replace, 1, -1);
113864 114789           }else{
113865 114790   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
113866         -          if( HasRowid(pTab) ){
113867         -            /* This OP_Delete opcode fires the pre-update-hook only. It does
113868         -            ** not modify the b-tree. It is more efficient to let the coming
113869         -            ** OP_Insert replace the existing entry than it is to delete the
113870         -            ** existing entry and then insert a new one. */
113871         -            sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
113872         -            sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
113873         -          }
       114791  +          assert( HasRowid(pTab) );
       114792  +          /* This OP_Delete opcode fires the pre-update-hook only. It does
       114793  +          ** not modify the b-tree. It is more efficient to let the coming
       114794  +          ** OP_Insert replace the existing entry than it is to delete the
       114795  +          ** existing entry and then insert a new one. */
       114796  +          sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
       114797  +          sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
113874 114798   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
113875 114799             if( pTab->pIndex ){
113876 114800               sqlite3MultiWrite(pParse);
113877 114801               sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
113878 114802             }
113879 114803           }
113880 114804           seenReplace = 1;
113881 114805           break;
113882 114806         }
       114807  +#ifndef SQLITE_OMIT_UPSERT
       114808  +      case OE_Update: {
       114809  +        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
       114810  +        /* Fall through */
       114811  +      }
       114812  +#endif
113883 114813         case OE_Ignore: {
113884         -        /*assert( seenReplace==0 );*/
       114814  +        testcase( onError==OE_Ignore );
113885 114815           sqlite3VdbeGoto(v, ignoreDest);
113886 114816           break;
113887 114817         }
113888 114818       }
113889 114819       sqlite3VdbeResolveLabel(v, addrRowidOk);
113890         -    if( ipkTop ){
113891         -      ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
113892         -      sqlite3VdbeJumpHere(v, ipkTop);
       114820  +    if( sAddr.ipkTop ){
       114821  +      sAddr.ipkBtm = sqlite3VdbeAddOp0(v, OP_Goto);
       114822  +      sqlite3VdbeJumpHere(v, sAddr.ipkTop-1);
113893 114823       }
113894 114824     }
113895 114825   
113896 114826     /* Test all UNIQUE constraints by creating entries for each UNIQUE
113897 114827     ** index and making sure that duplicate entries do not already exist.
113898 114828     ** Compute the revised record entries for indices as we go.
113899 114829     **
................................................................................
113903 114833     for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
113904 114834       int regIdx;          /* Range of registers hold conent for pIdx */
113905 114835       int regR;            /* Range of registers holding conflicting PK */
113906 114836       int iThisCur;        /* Cursor for this UNIQUE index */
113907 114837       int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
113908 114838   
113909 114839       if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
       114840  +    if( pUpIdx==pIdx ){
       114841  +      addrUniqueOk = sAddr.upsertBtm;
       114842  +      upsertBypass = sqlite3VdbeGoto(v, 0);
       114843  +      VdbeComment((v, "Skip upsert subroutine"));
       114844  +      sqlite3VdbeResolveLabel(v, sAddr.upsertTop2);
       114845  +    }else{
       114846  +      addrUniqueOk = sqlite3VdbeMakeLabel(v);
       114847  +    }
       114848  +    VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName));
113910 114849       if( bAffinityDone==0 ){
113911 114850         sqlite3TableAffinity(v, pTab, regNewData+1);
113912 114851         bAffinityDone = 1;
113913 114852       }
113914 114853       iThisCur = iIdxCur+ix;
113915         -    addrUniqueOk = sqlite3VdbeMakeLabel(v);
       114854  +
113916 114855   
113917 114856       /* Skip partial indices for which the WHERE clause is not true */
113918 114857       if( pIdx->pPartIdxWhere ){
113919 114858         sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
113920 114859         pParse->iSelfTab = -(regNewData+1);
113921 114860         sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
113922 114861                               SQLITE_JUMPIFNULL);
................................................................................
113967 114906         continue;  /* pIdx is not a UNIQUE index */
113968 114907       }
113969 114908       if( overrideError!=OE_Default ){
113970 114909         onError = overrideError;
113971 114910       }else if( onError==OE_Default ){
113972 114911         onError = OE_Abort;
113973 114912       }
       114913  +
       114914  +    /* Figure out if the upsert clause applies to this index */
       114915  +    if( pUpIdx==pIdx ){
       114916  +      if( pUpsert->pUpsertSet==0 ){
       114917  +        onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
       114918  +      }else{
       114919  +        onError = OE_Update;  /* DO UPDATE */
       114920  +      }
       114921  +    }
       114922  +
       114923  +    /* Invoke subroutines to handle IPK replace and upsert prior to running
       114924  +    ** the first REPLACE constraint check. */
       114925  +    if( onError==OE_Replace ){
       114926  +      testcase( sAddr.ipkTop );
       114927  +      testcase( sAddr.upsertTop
       114928  +             && sqlite3VdbeLabelHasBeenResolved(v,sAddr.upsertTop) );
       114929  +      reorderConstraintChecks(v, &sAddr);
       114930  +    }
113974 114931   
113975 114932       /* Collision detection may be omitted if all of the following are true:
113976 114933       **   (1) The conflict resolution algorithm is REPLACE
113977 114934       **   (2) The table is a WITHOUT ROWID table
113978 114935       **   (3) There are no secondary indexes on the table
113979 114936       **   (4) No delete triggers need to be fired if there is a conflict
113980 114937       **   (5) No FK constraint counters need to be updated if a conflict occurs.
................................................................................
113989 114946       ){
113990 114947         sqlite3VdbeResolveLabel(v, addrUniqueOk);
113991 114948         continue;
113992 114949       }
113993 114950   
113994 114951       /* Check to see if the new index entry will be unique */
113995 114952       sqlite3ExprCachePush(pParse);
       114953  +    sqlite3VdbeVerifyAbortable(v, onError);
113996 114954       sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
113997 114955                            regIdx, pIdx->nKeyCol); VdbeCoverage(v);
113998 114956   
113999 114957       /* Generate code to handle collisions */
114000 114958       regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
114001 114959       if( isUpdate || onError==OE_Replace ){
114002 114960         if( HasRowid(pTab) ){
................................................................................
114050 115008             }
114051 115009           }
114052 115010         }
114053 115011       }
114054 115012   
114055 115013       /* Generate code that executes if the new index entry is not unique */
114056 115014       assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
114057         -        || onError==OE_Ignore || onError==OE_Replace );
       115015  +        || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
114058 115016       switch( onError ){
114059 115017         case OE_Rollback:
114060 115018         case OE_Abort:
114061 115019         case OE_Fail: {
       115020  +        testcase( onError==OE_Rollback );
       115021  +        testcase( onError==OE_Abort );
       115022  +        testcase( onError==OE_Fail );
114062 115023           sqlite3UniqueConstraint(pParse, onError, pIdx);
114063 115024           break;
114064 115025         }
       115026  +#ifndef SQLITE_OMIT_UPSERT
       115027  +      case OE_Update: {
       115028  +        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
       115029  +        /* Fall through */
       115030  +      }
       115031  +#endif
114065 115032         case OE_Ignore: {
       115033  +        testcase( onError==OE_Ignore );
114066 115034           sqlite3VdbeGoto(v, ignoreDest);
114067 115035           break;
114068 115036         }
114069 115037         default: {
114070 115038           Trigger *pTrigger = 0;
114071 115039           assert( onError==OE_Replace );
114072         -        sqlite3MultiWrite(pParse);
114073 115040           if( db->flags&SQLITE_RecTriggers ){
114074 115041             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
       115042  +        }
       115043  +        if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
       115044  +          sqlite3MultiWrite(pParse);
114075 115045           }
114076 115046           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
114077 115047               regR, nPkField, 0, OE_Replace,
114078 115048               (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
114079 115049           seenReplace = 1;
114080 115050           break;
114081 115051         }
114082 115052       }
114083         -    sqlite3VdbeResolveLabel(v, addrUniqueOk);
       115053  +    if( pUpIdx==pIdx ){
       115054  +      sqlite3VdbeJumpHere(v, upsertBypass);
       115055  +    }else{
       115056  +      sqlite3VdbeResolveLabel(v, addrUniqueOk);
       115057  +    }
114084 115058       sqlite3ExprCachePop(pParse);
114085 115059       if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
       115060  +
114086 115061     }
114087         -  if( ipkTop ){
114088         -    sqlite3VdbeGoto(v, ipkTop+1);
114089         -    sqlite3VdbeJumpHere(v, ipkBottom);
114090         -  }
       115062  +  testcase( sAddr.ipkTop!=0 );
       115063  +  testcase( sAddr.upsertTop
       115064  +         && sqlite3VdbeLabelHasBeenResolved(v,sAddr.upsertTop) );
       115065  +  reorderConstraintChecks(v, &sAddr);
114091 115066     
114092 115067     *pbMayReplace = seenReplace;
114093 115068     VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
114094 115069   }
114095 115070   
114096 115071   #ifdef SQLITE_ENABLE_NULL_TRIM
114097 115072   /*
................................................................................
114583 115558     }
114584 115559     if( HasRowid(pSrc) ){
114585 115560       u8 insFlags;
114586 115561       sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
114587 115562       emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
114588 115563       if( pDest->iPKey>=0 ){
114589 115564         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
       115565  +      sqlite3VdbeVerifyAbortable(v, onError);
114590 115566         addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
114591 115567         VdbeCoverage(v);
114592 115568         sqlite3RowidConstraint(pParse, onError, pDest);
114593 115569         sqlite3VdbeJumpHere(v, addr2);
114594 115570         autoIncStep(pParse, regAutoinc, regRowid);
114595 115571       }else if( pDest->pIndex==0 ){
114596 115572         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
................................................................................
115140 116116                         sqlite3_stmt**,const void**);
115141 116117     int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
115142 116118     void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
115143 116119     void *(*value_pointer)(sqlite3_value*,const char*);
115144 116120     int (*vtab_nochange)(sqlite3_context*);
115145 116121     int (*value_nochange)(sqlite3_value*);
115146 116122     const char *(*vtab_collation)(sqlite3_index_info*,int);
       116123  +  /* Version 3.24.0 and later */
       116124  +  int (*keyword_count)(void);
       116125  +  int (*keyword_name)(int,const char**,int*);
       116126  +  int (*keyword_check)(const char*,int);
       116127  +  sqlite3_str *(*str_new)(sqlite3*);
       116128  +  char *(*str_finish)(sqlite3_str*);
       116129  +  void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
       116130  +  void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
       116131  +  void (*str_append)(sqlite3_str*, const char *zIn, int N);
       116132  +  void (*str_appendall)(sqlite3_str*, const char *zIn);
       116133  +  void (*str_appendchar)(sqlite3_str*, int N, char C);
       116134  +  void (*str_reset)(sqlite3_str*);
       116135  +  int (*str_errcode)(sqlite3_str*);
       116136  +  int (*str_length)(sqlite3_str*);
       116137  +  char *(*str_value)(sqlite3_str*);
115147 116138   };
115148 116139   
115149 116140   /*
115150 116141   ** This is the function signature used for all extension entry points.  It
115151 116142   ** is also defined in the file "loadext.c".
115152 116143   */
115153 116144   typedef int (*sqlite3_loadext_entry)(
................................................................................
115410 116401   #define sqlite3_bind_pointer           sqlite3_api->bind_pointer
115411 116402   #define sqlite3_result_pointer         sqlite3_api->result_pointer
115412 116403   #define sqlite3_value_pointer          sqlite3_api->value_pointer
115413 116404   /* Version 3.22.0 and later */
115414 116405   #define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
115415 116406   #define sqlite3_value_nochange         sqlite3_api->value_nochange
115416 116407   #define sqlite3_vtab_collation         sqlite3_api->vtab_collation
       116408  +/* Version 3.24.0 and later */
       116409  +#define sqlite3_keyword_count          sqlite3_api->keyword_count
       116410  +#define sqlite3_keyword_name           sqlite3_api->keyword_name
       116411  +#define sqlite3_keyword_check          sqlite3_api->keyword_check
       116412  +#define sqlite3_str_new                sqlite3_api->str_new
       116413  +#define sqlite3_str_finish             sqlite3_api->str_finish
       116414  +#define sqlite3_str_appendf            sqlite3_api->str_appendf
       116415  +#define sqlite3_str_vappendf           sqlite3_api->str_vappendf
       116416  +#define sqlite3_str_append             sqlite3_api->str_append
       116417  +#define sqlite3_str_appendall          sqlite3_api->str_appendall
       116418  +#define sqlite3_str_appendchar         sqlite3_api->str_appendchar
       116419  +#define sqlite3_str_reset              sqlite3_api->str_reset
       116420  +#define sqlite3_str_errcode            sqlite3_api->str_errcode
       116421  +#define sqlite3_str_length             sqlite3_api->str_length
       116422  +#define sqlite3_str_value              sqlite3_api->str_value
115417 116423   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
115418 116424   
115419 116425   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
115420 116426     /* This case when the file really is being compiled as a loadable 
115421 116427     ** extension */
115422 116428   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
115423 116429   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
115848 116854     sqlite3_prepare16_v3,
115849 116855     sqlite3_bind_pointer,
115850 116856     sqlite3_result_pointer,
115851 116857     sqlite3_value_pointer,
115852 116858     /* Version 3.22.0 and later */
115853 116859     sqlite3_vtab_nochange,
115854 116860     sqlite3_value_nochange,
115855         -  sqlite3_vtab_collation
       116861  +  sqlite3_vtab_collation,
       116862  +  /* Version 3.24.0 and later */
       116863  +  sqlite3_keyword_count,
       116864  +  sqlite3_keyword_name,
       116865  +  sqlite3_keyword_check,
       116866  +  sqlite3_str_new,
       116867  +  sqlite3_str_finish,
       116868  +  sqlite3_str_appendf,
       116869  +  sqlite3_str_vappendf,
       116870  +  sqlite3_str_append,
       116871  +  sqlite3_str_appendall,
       116872  +  sqlite3_str_appendchar,
       116873  +  sqlite3_str_reset,
       116874  +  sqlite3_str_errcode,
       116875  +  sqlite3_str_length,
       116876  +  sqlite3_str_value
115856 116877   };
115857 116878   
115858 116879   /*
115859 116880   ** Attempt to load an SQLite extension library contained in the file
115860 116881   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
115861 116882   ** default entry point name (sqlite3_extension_init) is used.  Use
115862 116883   ** of the default name is recommended.
................................................................................
115914 116935   
115915 116936     zEntry = zProc ? zProc : "sqlite3_extension_init";
115916 116937   
115917 116938     handle = sqlite3OsDlOpen(pVfs, zFile);
115918 116939   #if SQLITE_OS_UNIX || SQLITE_OS_WIN
115919 116940     for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
115920 116941       char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
115921         -    int bExists = 0;
115922 116942       if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
115923         -    sqlite3OsAccess(pVfs, zAltFile, SQLITE_ACCESS_EXISTS, &bExists);
115924         -    if( bExists )  handle = sqlite3OsDlOpen(pVfs, zAltFile);
       116943  +    handle = sqlite3OsDlOpen(pVfs, zAltFile);
115925 116944       sqlite3_free(zAltFile);
115926 116945     }
115927 116946   #endif
115928 116947     if( handle==0 ){
115929 116948       if( pzErrMsg ){
115930 116949         *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
115931 116950         if( zErrmsg ){
................................................................................
119089 120108     char cSep = '(';
119090 120109     StrAccum acc;
119091 120110     char zBuf[200];
119092 120111   
119093 120112     UNUSED_PARAMETER(argc);
119094 120113     UNUSED_PARAMETER(argv);
119095 120114     sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
119096         -  sqlite3StrAccumAppendAll(&acc, "CREATE TABLE x");
       120115  +  sqlite3_str_appendall(&acc, "CREATE TABLE x");
119097 120116     for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
119098         -    sqlite3XPrintf(&acc, "%c\"%s\"", cSep, pragCName[j]);
       120117  +    sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
119099 120118       cSep = ',';
119100 120119     }
119101 120120     if( i==0 ){
119102         -    sqlite3XPrintf(&acc, "(\"%s\"", pPragma->zName);
       120121  +    sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
119103 120122       cSep = ',';
119104 120123       i++;
119105 120124     }
119106 120125     j = 0;
119107 120126     if( pPragma->mPragFlg & PragFlg_Result1 ){
119108         -    sqlite3StrAccumAppendAll(&acc, ",arg HIDDEN");
       120127  +    sqlite3_str_appendall(&acc, ",arg HIDDEN");
119109 120128       j++;
119110 120129     }
119111 120130     if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
119112         -    sqlite3StrAccumAppendAll(&acc, ",schema HIDDEN");
       120131  +    sqlite3_str_appendall(&acc, ",schema HIDDEN");
119113 120132       j++;
119114 120133     }
119115         -  sqlite3StrAccumAppend(&acc, ")", 1);
       120134  +  sqlite3_str_append(&acc, ")", 1);
119116 120135     sqlite3StrAccumFinish(&acc);
119117 120136     assert( strlen(zBuf) < sizeof(zBuf)-1 );
119118 120137     rc = sqlite3_declare_vtab(db, zBuf);
119119 120138     if( rc==SQLITE_OK ){
119120 120139       pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
119121 120140       if( pTab==0 ){
119122 120141         rc = SQLITE_NOMEM;
................................................................................
119260 120279         pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
119261 120280         if( pCsr->azArg[j]==0 ){
119262 120281           return SQLITE_NOMEM;
119263 120282         }
119264 120283       }
119265 120284     }
119266 120285     sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
119267         -  sqlite3StrAccumAppendAll(&acc, "PRAGMA ");
       120286  +  sqlite3_str_appendall(&acc, "PRAGMA ");
119268 120287     if( pCsr->azArg[1] ){
119269         -    sqlite3XPrintf(&acc, "%Q.", pCsr->azArg[1]);
       120288  +    sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
119270 120289     }
119271         -  sqlite3StrAccumAppendAll(&acc, pTab->pName->zName);
       120290  +  sqlite3_str_appendall(&acc, pTab->pName->zName);
119272 120291     if( pCsr->azArg[0] ){
119273         -    sqlite3XPrintf(&acc, "=%Q", pCsr->azArg[0]);
       120292  +    sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
119274 120293     }
119275 120294     zSql = sqlite3StrAccumFinish(&acc);
119276 120295     if( zSql==0 ) return SQLITE_NOMEM;
119277 120296     rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
119278 120297     sqlite3_free(zSql);
119279 120298     if( rc!=SQLITE_OK ){
119280 120299       pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
................................................................................
119509 120528     Db *pDb;
119510 120529     char const *azArg[4];
119511 120530     int meta[5];
119512 120531     InitData initData;
119513 120532     const char *zMasterName;
119514 120533     int openedTransaction = 0;
119515 120534   
       120535  +  assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
119516 120536     assert( iDb>=0 && iDb<db->nDb );
119517 120537     assert( db->aDb[iDb].pSchema );
119518 120538     assert( sqlite3_mutex_held(db->mutex) );
119519 120539     assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
119520 120540   
119521 120541     db->init.busy = 1;
119522 120542   
................................................................................
119578 120598     **    meta[9]   unused
119579 120599     **
119580 120600     ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
119581 120601     ** the possible values of meta[4].
119582 120602     */
119583 120603     for(i=0; i<ArraySize(meta); i++){
119584 120604       sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
       120605  +  }
       120606  +  if( (db->flags & SQLITE_ResetDatabase)!=0 ){
       120607  +    memset(meta, 0, sizeof(meta));
119585 120608     }
119586 120609     pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
119587 120610   
119588 120611     /* If opening a non-empty database, check the text encoding. For the
119589 120612     ** main database, set sqlite3.enc to the encoding of the main database.
119590 120613     ** For an attached db, it is an error if the encoding is not the same
119591 120614     ** as sqlite3.enc.
................................................................................
119738 120761     /* Do the main schema first */
119739 120762     if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
119740 120763       rc = sqlite3InitOne(db, 0, pzErrMsg);
119741 120764       if( rc ) return rc;
119742 120765     }
119743 120766     /* All other schemas after the main schema. The "temp" schema must be last */
119744 120767     for(i=db->nDb-1; i>0; i--){
       120768  +    assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
119745 120769       if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
119746 120770         rc = sqlite3InitOne(db, i, pzErrMsg);
119747 120771         if( rc ) return rc;
119748 120772       }
119749 120773     }
119750 120774     if( commit_internal ){
119751 120775       sqlite3CommitInternalChanges(db);
................................................................................
119759 120783   */
119760 120784   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
119761 120785     int rc = SQLITE_OK;
119762 120786     sqlite3 *db = pParse->db;
119763 120787     assert( sqlite3_mutex_held(db->mutex) );
119764 120788     if( !db->init.busy ){
119765 120789       rc = sqlite3Init(db, &pParse->zErrMsg);
119766         -  }
119767         -  if( rc!=SQLITE_OK ){
119768         -    pParse->rc = rc;
119769         -    pParse->nErr++;
       120790  +    if( rc!=SQLITE_OK ){
       120791  +      pParse->rc = rc;
       120792  +      pParse->nErr++;
       120793  +    }else if( db->noSharedCache ){
       120794  +      db->mDbFlags |= DBFLAG_SchemaKnownOk;
       120795  +    }
119770 120796     }
119771 120797     return rc;
119772 120798   }
119773 120799   
119774 120800   
119775 120801   /*
119776 120802   ** Check schema cookies in all databases.  If any cookie is out
................................................................................
119973 120999     }
119974 121000     rc = sParse.rc;
119975 121001   
119976 121002   #ifndef SQLITE_OMIT_EXPLAIN
119977 121003     if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
119978 121004       static const char * const azColName[] = {
119979 121005          "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
119980         -       "selectid", "order", "from", "detail"
       121006  +       "id", "parent", "notused", "detail"
119981 121007       };
119982 121008       int iFirst, mx;
119983 121009       if( sParse.explain==2 ){
119984 121010         sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
119985 121011         iFirst = 8;
119986 121012         mx = 12;
119987 121013       }else{
................................................................................
120287 121313   /*
120288 121314   ** Trace output macros
120289 121315   */
120290 121316   #if SELECTTRACE_ENABLED
120291 121317   /***/ int sqlite3SelectTrace = 0;
120292 121318   # define SELECTTRACE(K,P,S,X)  \
120293 121319     if(sqlite3SelectTrace&(K))   \
120294         -    sqlite3DebugPrintf("%s/%p: ",(S)->zSelName,(S)),\
       121320  +    sqlite3DebugPrintf("%s/%d/%p: ",(S)->zSelName,(P)->addrExplain,(S)),\
120295 121321       sqlite3DebugPrintf X
120296 121322   #else
120297 121323   # define SELECTTRACE(K,P,S,X)
120298 121324   #endif
120299 121325   
120300 121326   
120301 121327   /*
................................................................................
120310 121336     int tabTnct;    /* Ephemeral table used for DISTINCT processing */
120311 121337     int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
120312 121338   };
120313 121339   
120314 121340   /*
120315 121341   ** An instance of the following object is used to record information about
120316 121342   ** the ORDER BY (or GROUP BY) clause of query is being coded.
       121343  +**
       121344  +** The aDefer[] array is used by the sorter-references optimization. For
       121345  +** example, assuming there is no index that can be used for the ORDER BY,
       121346  +** for the query:
       121347  +**
       121348  +**     SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
       121349  +**
       121350  +** it may be more efficient to add just the "a" values to the sorter, and
       121351  +** retrieve the associated "bigblob" values directly from table t1 as the
       121352  +** 10 smallest "a" values are extracted from the sorter.
       121353  +**
       121354  +** When the sorter-reference optimization is used, there is one entry in the
       121355  +** aDefer[] array for each database table that may be read as values are
       121356  +** extracted from the sorter.
120317 121357   */
120318 121358   typedef struct SortCtx SortCtx;
120319 121359   struct SortCtx {
120320 121360     ExprList *pOrderBy;   /* The ORDER BY (or GROUP BY clause) */
120321 121361     int nOBSat;           /* Number of ORDER BY terms satisfied by indices */
120322 121362     int iECursor;         /* Cursor number for the sorter */
120323 121363     int regReturn;        /* Register holding block-output return address */
120324 121364     int labelBkOut;       /* Start label for the block-output subroutine */
120325 121365     int addrSortIndex;    /* Address of the OP_SorterOpen or OP_OpenEphemeral */
120326 121366     int labelDone;        /* Jump here when done, ex: LIMIT reached */
120327 121367     u8 sortFlags;         /* Zero or more SORTFLAG_* bits */
120328 121368     u8 bOrderedInnerLoop; /* ORDER BY correctly sorts the inner loop */
       121369  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       121370  +  u8 nDefer;            /* Number of valid entries in aDefer[] */
       121371  +  struct DeferredCsr {
       121372  +    Table *pTab;        /* Table definition */
       121373  +    int iCsr;           /* Cursor number for table */
       121374  +    int nKey;           /* Number of PK columns for table pTab (>=1) */
       121375  +  } aDefer[4];
       121376  +#endif
       121377  +  struct RowLoadInfo *pDeferredRowLoad;  /* Deferred row loading info or NULL */
120329 121378   };
120330 121379   #define SORTFLAG_UseSorter  0x01   /* Use SorterOpen instead of OpenEphemeral */
120331 121380   
120332 121381   /*
120333 121382   ** Delete all the content of a Select structure.  Deallocate the structure
120334 121383   ** itself only if bFree is true.
120335 121384   */
................................................................................
120779 121828   /* Forward reference */
120780 121829   static KeyInfo *keyInfoFromExprList(
120781 121830     Parse *pParse,       /* Parsing context */
120782 121831     ExprList *pList,     /* Form the KeyInfo object from this ExprList */
120783 121832     int iStart,          /* Begin with this column of pList */
120784 121833     int nExtra           /* Add this many extra columns to the end */
120785 121834   );
       121835  +
       121836  +/*
       121837  +** An instance of this object holds information (beyond pParse and pSelect)
       121838  +** needed to load the next result row that is to be added to the sorter.
       121839  +*/
       121840  +typedef struct RowLoadInfo RowLoadInfo;
       121841  +struct RowLoadInfo {
       121842  +  int regResult;               /* Store results in array of registers here */
       121843  +  u8 ecelFlags;                /* Flag argument to ExprCodeExprList() */
       121844  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       121845  +  ExprList *pExtra;            /* Extra columns needed by sorter refs */
       121846  +  int regExtraResult;          /* Where to load the extra columns */
       121847  +#endif
       121848  +};
       121849  +
       121850  +/*
       121851  +** This routine does the work of loading query data into an array of
       121852  +** registers so that it can be added to the sorter.
       121853  +*/
       121854  +static void innerLoopLoadRow(
       121855  +  Parse *pParse,             /* Statement under construction */
       121856  +  Select *pSelect,           /* The query being coded */
       121857  +  RowLoadInfo *pInfo         /* Info needed to complete the row load */
       121858  +){
       121859  +  sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
       121860  +                          0, pInfo->ecelFlags);
       121861  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       121862  +  if( pInfo->pExtra ){
       121863  +    sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
       121864  +    sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
       121865  +  }
       121866  +#endif
       121867  +}
       121868  +
       121869  +/*
       121870  +** Code the OP_MakeRecord instruction that generates the entry to be
       121871  +** added into the sorter.
       121872  +**
       121873  +** Return the register in which the result is stored.
       121874  +*/
       121875  +static int makeSorterRecord(
       121876  +  Parse *pParse,
       121877  +  SortCtx *pSort,
       121878  +  Select *pSelect,
       121879  +  int regBase,
       121880  +  int nBase
       121881  +){
       121882  +  int nOBSat = pSort->nOBSat;
       121883  +  Vdbe *v = pParse->pVdbe;
       121884  +  int regOut = ++pParse->nMem;
       121885  +  if( pSort->pDeferredRowLoad ){
       121886  +    innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
       121887  +  }
       121888  +  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
       121889  +  return regOut;
       121890  +}
120786 121891   
120787 121892   /*
120788 121893   ** Generate code that will push the record in registers regData
120789 121894   ** through regData+nData-1 onto the sorter.
120790 121895   */
120791 121896   static void pushOntoSorter(
120792 121897     Parse *pParse,         /* Parser context */
120793 121898     SortCtx *pSort,        /* Information about the ORDER BY clause */
120794 121899     Select *pSelect,       /* The whole SELECT statement */
120795 121900     int regData,           /* First register holding data to be sorted */
120796 121901     int regOrigData,       /* First register holding data before packing */
120797         -  int nData,             /* Number of elements in the data array */
       121902  +  int nData,             /* Number of elements in the regData data array */
120798 121903     int nPrefixReg         /* No. of reg prior to regData available for use */
120799 121904   ){
120800 121905     Vdbe *v = pParse->pVdbe;                         /* Stmt under construction */
120801 121906     int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
120802 121907     int nExpr = pSort->pOrderBy->nExpr;              /* No. of ORDER BY terms */
120803 121908     int nBase = nExpr + bSeq + nData;                /* Fields in sorter record */
120804 121909     int regBase;                                     /* Regs for sorter record */
120805         -  int regRecord = ++pParse->nMem;                  /* Assembled sorter record */
       121910  +  int regRecord = 0;                               /* Assembled sorter record */
120806 121911     int nOBSat = pSort->nOBSat;                      /* ORDER BY terms to skip */
120807 121912     int op;                            /* Opcode to add sorter record to sorter */
120808 121913     int iLimit;                        /* LIMIT counter */
       121914  +  int iSkip = 0;                     /* End of the sorter insert loop */
120809 121915   
120810 121916     assert( bSeq==0 || bSeq==1 );
       121917  +
       121918  +  /* Three cases:
       121919  +  **   (1) The data to be sorted has already been packed into a Record
       121920  +  **       by a prior OP_MakeRecord.  In this case nData==1 and regData
       121921  +  **       will be completely unrelated to regOrigData.
       121922  +  **   (2) All output columns are included in the sort record.  In that
       121923  +  **       case regData==regOrigData.
       121924  +  **   (3) Some output columns are omitted from the sort record due to
       121925  +  **       the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
       121926  +  **       SQLITE_ECEL_OMITREF optimization, or due to the 
       121927  +  **       SortCtx.pDeferredRowLoad optimiation.  In any of these cases
       121928  +  **       regOrigData is 0 to prevent this routine from trying to copy
       121929  +  **       values that might not yet exist.
       121930  +  */
120811 121931     assert( nData==1 || regData==regOrigData || regOrigData==0 );
       121932  +
120812 121933     if( nPrefixReg ){
120813 121934       assert( nPrefixReg==nExpr+bSeq );
120814         -    regBase = regData - nExpr - bSeq;
       121935  +    regBase = regData - nPrefixReg;
120815 121936     }else{
120816 121937       regBase = pParse->nMem + 1;
120817 121938       pParse->nMem += nBase;
120818 121939     }
120819 121940     assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
120820 121941     iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
120821 121942     pSort->labelDone = sqlite3VdbeMakeLabel(v);
................................................................................
120823 121944                             SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
120824 121945     if( bSeq ){
120825 121946       sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
120826 121947     }
120827 121948     if( nPrefixReg==0 && nData>0 ){
120828 121949       sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
120829 121950     }
120830         -  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
120831 121951     if( nOBSat>0 ){
120832 121952       int regPrevKey;   /* The first nOBSat columns of the previous row */
120833 121953       int addrFirst;    /* Address of the OP_IfNot opcode */
120834 121954       int addrJmp;      /* Address of the OP_Jump opcode */
120835 121955       VdbeOp *pOp;      /* Opcode that opens the sorter */
120836 121956       int nKey;         /* Number of sorting key columns, including OP_Sequence */
120837 121957       KeyInfo *pKI;     /* Original KeyInfo on the sorter table */
120838 121958   
       121959  +    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
120839 121960       regPrevKey = pParse->nMem+1;
120840 121961       pParse->nMem += pSort->nOBSat;
120841 121962       nKey = nExpr - pSort->nOBSat + bSeq;
120842 121963       if( bSeq ){
120843 121964         addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr); 
120844 121965       }else{
120845 121966         addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
................................................................................
120865 121986         sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
120866 121987         VdbeCoverage(v);
120867 121988       }
120868 121989       sqlite3VdbeJumpHere(v, addrFirst);
120869 121990       sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
120870 121991       sqlite3VdbeJumpHere(v, addrJmp);
120871 121992     }
       121993  +  if( iLimit ){
       121994  +    /* At this point the values for the new sorter entry are stored
       121995  +    ** in an array of registers. They need to be composed into a record
       121996  +    ** and inserted into the sorter if either (a) there are currently
       121997  +    ** less than LIMIT+OFFSET items or (b) the new record is smaller than 
       121998  +    ** the largest record currently in the sorter. If (b) is true and there
       121999  +    ** are already LIMIT+OFFSET items in the sorter, delete the largest
       122000  +    ** entry before inserting the new one. This way there are never more 
       122001  +    ** than LIMIT+OFFSET items in the sorter.
       122002  +    **
       122003  +    ** If the new record does not need to be inserted into the sorter,
       122004  +    ** jump to the next iteration of the loop. Or, if the
       122005  +    ** pSort->bOrderedInnerLoop flag is set to indicate that the inner
       122006  +    ** loop delivers items in sorted order, jump to the next iteration
       122007  +    ** of the outer loop.
       122008  +    */
       122009  +    int iCsr = pSort->iECursor;
       122010  +    sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
       122011  +    VdbeCoverage(v);
       122012  +    sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
       122013  +    iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
       122014  +                                 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
       122015  +    VdbeCoverage(v);
       122016  +    sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
       122017  +  }
       122018  +  if( regRecord==0 ){
       122019  +    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
       122020  +  }
120872 122021     if( pSort->sortFlags & SORTFLAG_UseSorter ){
120873 122022       op = OP_SorterInsert;
120874 122023     }else{
120875 122024       op = OP_IdxInsert;
120876 122025     }
120877 122026     sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
120878 122027                          regBase+nOBSat, nBase-nOBSat);
120879         -  if( iLimit ){
120880         -    int addr;
120881         -    int r1 = 0;
120882         -    /* Fill the sorter until it contains LIMIT+OFFSET entries.  (The iLimit
120883         -    ** register is initialized with value of LIMIT+OFFSET.)  After the sorter
120884         -    ** fills up, delete the least entry in the sorter after each insert.
120885         -    ** Thus we never hold more than the LIMIT+OFFSET rows in memory at once */
120886         -    addr = sqlite3VdbeAddOp1(v, OP_IfNotZero, iLimit); VdbeCoverage(v);
120887         -    sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
120888         -    if( pSort->bOrderedInnerLoop ){
120889         -      r1 = ++pParse->nMem;
120890         -      sqlite3VdbeAddOp3(v, OP_Column, pSort->iECursor, nExpr, r1);
120891         -      VdbeComment((v, "seq"));
120892         -    }
120893         -    sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor);
120894         -    if( pSort->bOrderedInnerLoop ){
120895         -      /* If the inner loop is driven by an index such that values from
120896         -      ** the same iteration of the inner loop are in sorted order, then
120897         -      ** immediately jump to the next iteration of an inner loop if the
120898         -      ** entry from the current iteration does not fit into the top
120899         -      ** LIMIT+OFFSET entries of the sorter. */
120900         -      int iBrk = sqlite3VdbeCurrentAddr(v) + 2;
120901         -      sqlite3VdbeAddOp3(v, OP_Eq, regBase+nExpr, iBrk, r1);
120902         -      sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
120903         -      VdbeCoverage(v);
120904         -    }
120905         -    sqlite3VdbeJumpHere(v, addr);
       122028  +  if( iSkip ){
       122029  +    assert( pSort->bOrderedInnerLoop==0 || pSort->bOrderedInnerLoop==1 );
       122030  +    sqlite3VdbeChangeP2(v, iSkip,
       122031  +         sqlite3VdbeCurrentAddr(v) + pSort->bOrderedInnerLoop);
120906 122032     }
120907 122033   }
120908 122034   
120909 122035   /*
120910 122036   ** Add code to implement the OFFSET
120911 122037   */
120912 122038   static void codeOffset(
................................................................................
120944 122070     sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
120945 122071     sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
120946 122072     sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
120947 122073     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
120948 122074     sqlite3ReleaseTempReg(pParse, r1);
120949 122075   }
120950 122076   
       122077  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122078  +/*
       122079  +** This function is called as part of inner-loop generation for a SELECT
       122080  +** statement with an ORDER BY that is not optimized by an index. It 
       122081  +** determines the expressions, if any, that the sorter-reference 
       122082  +** optimization should be used for. The sorter-reference optimization
       122083  +** is used for SELECT queries like:
       122084  +**
       122085  +**   SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
       122086  +**
       122087  +** If the optimization is used for expression "bigblob", then instead of
       122088  +** storing values read from that column in the sorter records, the PK of
       122089  +** the row from table t1 is stored instead. Then, as records are extracted from
       122090  +** the sorter to return to the user, the required value of bigblob is
       122091  +** retrieved directly from table t1. If the values are very large, this 
       122092  +** can be more efficient than storing them directly in the sorter records.
       122093  +**
       122094  +** The ExprList_item.bSorterRef flag is set for each expression in pEList 
       122095  +** for which the sorter-reference optimization should be enabled. 
       122096  +** Additionally, the pSort->aDefer[] array is populated with entries
       122097  +** for all cursors required to evaluate all selected expressions. Finally.
       122098  +** output variable (*ppExtra) is set to an expression list containing
       122099  +** expressions for all extra PK values that should be stored in the
       122100  +** sorter records.
       122101  +*/
       122102  +static void selectExprDefer(
       122103  +  Parse *pParse,                  /* Leave any error here */
       122104  +  SortCtx *pSort,                 /* Sorter context */
       122105  +  ExprList *pEList,               /* Expressions destined for sorter */
       122106  +  ExprList **ppExtra              /* Expressions to append to sorter record */
       122107  +){
       122108  +  int i;
       122109  +  int nDefer = 0;
       122110  +  ExprList *pExtra = 0;
       122111  +  for(i=0; i<pEList->nExpr; i++){
       122112  +    struct ExprList_item *pItem = &pEList->a[i];
       122113  +    if( pItem->u.x.iOrderByCol==0 ){
       122114  +      Expr *pExpr = pItem->pExpr;
       122115  +      Table *pTab = pExpr->pTab;
       122116  +      if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
       122117  +       && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
       122118  +      ){
       122119  +        int j;
       122120  +        for(j=0; j<nDefer; j++){
       122121  +          if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
       122122  +        }
       122123  +        if( j==nDefer ){
       122124  +          if( nDefer==ArraySize(pSort->aDefer) ){
       122125  +            continue;
       122126  +          }else{
       122127  +            int nKey = 1;
       122128  +            int k;
       122129  +            Index *pPk = 0;
       122130  +            if( !HasRowid(pTab) ){
       122131  +              pPk = sqlite3PrimaryKeyIndex(pTab);
       122132  +              nKey = pPk->nKeyCol;
       122133  +            }
       122134  +            for(k=0; k<nKey; k++){
       122135  +              Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
       122136  +              if( pNew ){
       122137  +                pNew->iTable = pExpr->iTable;
       122138  +                pNew->pTab = pExpr->pTab;
       122139  +                pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
       122140  +                pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
       122141  +              }
       122142  +            }
       122143  +            pSort->aDefer[nDefer].pTab = pExpr->pTab;
       122144  +            pSort->aDefer[nDefer].iCsr = pExpr->iTable;
       122145  +            pSort->aDefer[nDefer].nKey = nKey;
       122146  +            nDefer++;
       122147  +          }
       122148  +        }
       122149  +        pItem->bSorterRef = 1;
       122150  +      }
       122151  +    }
       122152  +  }
       122153  +  pSort->nDefer = (u8)nDefer;
       122154  +  *ppExtra = pExtra;
       122155  +}
       122156  +#endif
       122157  +
120951 122158   /*
120952 122159   ** This routine generates the code for the inside of the inner loop
120953 122160   ** of a SELECT.
120954 122161   **
120955 122162   ** If srcTab is negative, then the p->pEList expressions
120956 122163   ** are evaluated in order to get the data for this row.  If srcTab is
120957 122164   ** zero or more, then data is pulled from srcTab and p->pEList is used only 
................................................................................
120970 122177     Vdbe *v = pParse->pVdbe;
120971 122178     int i;
120972 122179     int hasDistinct;            /* True if the DISTINCT keyword is present */
120973 122180     int eDest = pDest->eDest;   /* How to dispose of results */
120974 122181     int iParm = pDest->iSDParm; /* First argument to disposal method */
120975 122182     int nResultCol;             /* Number of result columns */
120976 122183     int nPrefixReg = 0;         /* Number of extra registers before regResult */
       122184  +  RowLoadInfo sRowLoadInfo;   /* Info for deferred row loading */
120977 122185   
120978 122186     /* Usually, regResult is the first cell in an array of memory cells
120979 122187     ** containing the current result row. In this case regOrig is set to the
120980 122188     ** same value. However, if the results are being sent to the sorter, the
120981 122189     ** values for any expressions that are also part of the sort-key are omitted
120982 122190     ** from this array. In this case regOrig is set to zero.  */
120983 122191     int regResult;              /* Start of memory holding current results */
................................................................................
121016 122224     regOrig = regResult = pDest->iSdst;
121017 122225     if( srcTab>=0 ){
121018 122226       for(i=0; i<nResultCol; i++){
121019 122227         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
121020 122228         VdbeComment((v, "%s", p->pEList->a[i].zName));
121021 122229       }
121022 122230     }else if( eDest!=SRT_Exists ){
       122231  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122232  +    ExprList *pExtra = 0;
       122233  +#endif
121023 122234       /* If the destination is an EXISTS(...) expression, the actual
121024 122235       ** values returned by the SELECT are not required.
121025 122236       */
121026         -    u8 ecelFlags;
       122237  +    u8 ecelFlags;    /* "ecel" is an abbreviation of "ExprCodeExprList" */
       122238  +    ExprList *pEList;
121027 122239       if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
121028 122240         ecelFlags = SQLITE_ECEL_DUP;
121029 122241       }else{
121030 122242         ecelFlags = 0;
121031 122243       }
121032 122244       if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
121033 122245         /* For each expression in p->pEList that is a copy of an expression in
121034 122246         ** the ORDER BY clause (pSort->pOrderBy), set the associated 
121035 122247         ** iOrderByCol value to one more than the index of the ORDER BY 
121036 122248         ** expression within the sort-key that pushOntoSorter() will generate.
121037 122249         ** This allows the p->pEList field to be omitted from the sorted record,
121038 122250         ** saving space and CPU cycles.  */
121039 122251         ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
       122252  +
121040 122253         for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
121041 122254           int j;
121042 122255           if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
121043 122256             p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
121044 122257           }
121045 122258         }
121046         -      regOrig = 0;
       122259  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122260  +      selectExprDefer(pParse, pSort, p->pEList, &pExtra);
       122261  +      if( pExtra && pParse->db->mallocFailed==0 ){
       122262  +        /* If there are any extra PK columns to add to the sorter records,
       122263  +        ** allocate extra memory cells and adjust the OpenEphemeral 
       122264  +        ** instruction to account for the larger records. This is only
       122265  +        ** required if there are one or more WITHOUT ROWID tables with
       122266  +        ** composite primary keys in the SortCtx.aDefer[] array.  */
       122267  +        VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
       122268  +        pOp->p2 += (pExtra->nExpr - pSort->nDefer);
       122269  +        pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
       122270  +        pParse->nMem += pExtra->nExpr;
       122271  +      }
       122272  +#endif
       122273  +
       122274  +      /* Adjust nResultCol to account for columns that are omitted
       122275  +      ** from the sorter by the optimizations in this branch */
       122276  +      pEList = p->pEList;
       122277  +      for(i=0; i<pEList->nExpr; i++){
       122278  +        if( pEList->a[i].u.x.iOrderByCol>0
       122279  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122280  +         || pEList->a[i].bSorterRef
       122281  +#endif
       122282  +        ){
       122283  +          nResultCol--;
       122284  +          regOrig = 0;
       122285  +        }
       122286  +      }
       122287  +
       122288  +      testcase( regOrig );
       122289  +      testcase( eDest==SRT_Set );
       122290  +      testcase( eDest==SRT_Mem );
       122291  +      testcase( eDest==SRT_Coroutine );
       122292  +      testcase( eDest==SRT_Output );
121047 122293         assert( eDest==SRT_Set || eDest==SRT_Mem 
121048 122294              || eDest==SRT_Coroutine || eDest==SRT_Output );
121049 122295       }
121050         -    nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult,
121051         -                                         0,ecelFlags);
       122296  +    sRowLoadInfo.regResult = regResult;
       122297  +    sRowLoadInfo.ecelFlags = ecelFlags;
       122298  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122299  +    sRowLoadInfo.pExtra = pExtra;
       122300  +    sRowLoadInfo.regExtraResult = regResult + nResultCol;
       122301  +    if( pExtra ) nResultCol += pExtra->nExpr;
       122302  +#endif
       122303  +    if( p->iLimit
       122304  +     && (ecelFlags & SQLITE_ECEL_OMITREF)!=0 
       122305  +     && nPrefixReg>0
       122306  +    ){
       122307  +      assert( pSort!=0 );
       122308  +      assert( hasDistinct==0 );
       122309  +      pSort->pDeferredRowLoad = &sRowLoadInfo;
       122310  +      regOrig = 0;
       122311  +    }else{
       122312  +      innerLoopLoadRow(pParse, p, &sRowLoadInfo);
       122313  +    }
121052 122314     }
121053 122315   
121054 122316     /* If the DISTINCT keyword was present on the SELECT statement
121055 122317     ** and this row has been seen before, then do not make this row
121056 122318     ** part of the result.
121057 122319     */
121058 122320     if( hasDistinct ){
................................................................................
121160 122422           sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
121161 122423           VdbeCoverage(v);
121162 122424           sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
121163 122425           assert( pSort==0 );
121164 122426         }
121165 122427   #endif
121166 122428         if( pSort ){
121167         -        pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg);
       122429  +        assert( regResult==regOrig );
       122430  +        pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
121168 122431         }else{
121169 122432           int r2 = sqlite3GetTempReg(pParse);
121170 122433           sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
121171 122434           sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
121172 122435           sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
121173 122436           sqlite3ReleaseTempReg(pParse, r2);
121174 122437         }
................................................................................
121427 122690   **
121428 122691   **   "USE TEMP B-TREE FOR xxx"
121429 122692   **
121430 122693   ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
121431 122694   ** is determined by the zUsage argument.
121432 122695   */
121433 122696   static void explainTempTable(Parse *pParse, const char *zUsage){
121434         -  if( pParse->explain==2 ){
121435         -    Vdbe *v = pParse->pVdbe;
121436         -    char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
121437         -    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
121438         -  }
       122697  +  ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
121439 122698   }
121440 122699   
121441 122700   /*
121442 122701   ** Assign expression b to lvalue a. A second, no-op, version of this macro
121443 122702   ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
121444 122703   ** in sqlite3Select() to assign values to structure member variables that
121445 122704   ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
................................................................................
121449 122708   
121450 122709   #else
121451 122710   /* No-op versions of the explainXXX() functions and macros. */
121452 122711   # define explainTempTable(y,z)
121453 122712   # define explainSetInteger(y,z)
121454 122713   #endif
121455 122714   
121456         -#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
121457         -/*
121458         -** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
121459         -** is a no-op. Otherwise, it adds a single row of output to the EQP result,
121460         -** where the caption is of one of the two forms:
121461         -**
121462         -**   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
121463         -**   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
121464         -**
121465         -** where iSub1 and iSub2 are the integers passed as the corresponding
121466         -** function parameters, and op is the text representation of the parameter
121467         -** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
121468         -** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is 
121469         -** false, or the second form if it is true.
121470         -*/
121471         -static void explainComposite(
121472         -  Parse *pParse,                  /* Parse context */
121473         -  int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
121474         -  int iSub1,                      /* Subquery id 1 */
121475         -  int iSub2,                      /* Subquery id 2 */
121476         -  int bUseTmp                     /* True if a temp table was used */
121477         -){
121478         -  assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
121479         -  if( pParse->explain==2 ){
121480         -    Vdbe *v = pParse->pVdbe;
121481         -    char *zMsg = sqlite3MPrintf(
121482         -        pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
121483         -        bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
121484         -    );
121485         -    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
121486         -  }
121487         -}
121488         -#else
121489         -/* No-op versions of the explainXXX() functions and macros. */
121490         -# define explainComposite(v,w,x,y,z)
121491         -#endif
121492 122715   
121493 122716   /*
121494 122717   ** If the inner loop was generated using a non-null pOrderBy argument,
121495 122718   ** then the results were placed in a sorter.  After the loop is terminated
121496 122719   ** we need to run the sorter and output the results.  The following
121497 122720   ** routine generates the code needed to do that.
121498 122721   */
................................................................................
121502 122725     SortCtx *pSort,   /* Information on the ORDER BY clause */
121503 122726     int nColumn,      /* Number of columns of data */
121504 122727     SelectDest *pDest /* Write the sorted results here */
121505 122728   ){
121506 122729     Vdbe *v = pParse->pVdbe;                     /* The prepared statement */
121507 122730     int addrBreak = pSort->labelDone;            /* Jump here to exit loop */
121508 122731     int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
121509         -  int addr;
       122732  +  int addr;                       /* Top of output loop. Jump for Next. */
121510 122733     int addrOnce = 0;
121511 122734     int iTab;
121512 122735     ExprList *pOrderBy = pSort->pOrderBy;
121513 122736     int eDest = pDest->eDest;
121514 122737     int iParm = pDest->iSDParm;
121515 122738     int regRow;
121516 122739     int regRowid;
121517 122740     int iCol;
121518         -  int nKey;
       122741  +  int nKey;                       /* Number of key columns in sorter record */
121519 122742     int iSortTab;                   /* Sorter cursor to read from */
121520         -  int nSortData;                  /* Trailing values to read from sorter */
121521 122743     int i;
121522 122744     int bSeq;                       /* True if sorter record includes seq. no. */
       122745  +  int nRefKey = 0;
121523 122746     struct ExprList_item *aOutEx = p->pEList->a;
121524 122747   
121525 122748     assert( addrBreak<0 );
121526 122749     if( pSort->labelBkOut ){
121527 122750       sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
121528 122751       sqlite3VdbeGoto(v, addrBreak);
121529 122752       sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
121530 122753     }
       122754  +
       122755  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122756  +  /* Open any cursors needed for sorter-reference expressions */
       122757  +  for(i=0; i<pSort->nDefer; i++){
       122758  +    Table *pTab = pSort->aDefer[i].pTab;
       122759  +    int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       122760  +    sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
       122761  +    nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
       122762  +  }
       122763  +#endif
       122764  +
121531 122765     iTab = pSort->iECursor;
121532 122766     if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
121533 122767       regRowid = 0;
121534 122768       regRow = pDest->iSdst;
121535         -    nSortData = nColumn;
121536 122769     }else{
121537 122770       regRowid = sqlite3GetTempReg(pParse);
121538 122771       regRow = sqlite3GetTempRange(pParse, nColumn);
121539         -    nSortData = nColumn;
121540 122772     }
121541 122773     nKey = pOrderBy->nExpr - pSort->nOBSat;
121542 122774     if( pSort->sortFlags & SORTFLAG_UseSorter ){
121543 122775       int regSortOut = ++pParse->nMem;
121544 122776       iSortTab = pParse->nTab++;
121545 122777       if( pSort->labelBkOut ){
121546 122778         addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
121547 122779       }
121548         -    sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData);
       122780  +    sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, 
       122781  +        nKey+1+nColumn+nRefKey);
121549 122782       if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
121550 122783       addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
121551 122784       VdbeCoverage(v);
121552 122785       codeOffset(v, p->iOffset, addrContinue);
121553 122786       sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
121554 122787       bSeq = 0;
121555 122788     }else{
121556 122789       addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
121557 122790       codeOffset(v, p->iOffset, addrContinue);
121558 122791       iSortTab = iTab;
121559 122792       bSeq = 1;
121560 122793     }
121561         -  for(i=0, iCol=nKey+bSeq-1; i<nSortData; i++){
       122794  +  for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
       122795  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122796  +    if( aOutEx[i].bSorterRef ) continue;
       122797  +#endif
121562 122798       if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
121563 122799     }
121564         -  for(i=nSortData-1; i>=0; i--){
121565         -    int iRead;
121566         -    if( aOutEx[i].u.x.iOrderByCol ){
121567         -      iRead = aOutEx[i].u.x.iOrderByCol-1;
121568         -    }else{
121569         -      iRead = iCol--;
121570         -    }
121571         -    sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
121572         -    VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
       122800  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122801  +  if( pSort->nDefer ){
       122802  +    int iKey = iCol+1;
       122803  +    int regKey = sqlite3GetTempRange(pParse, nRefKey);
       122804  +
       122805  +    for(i=0; i<pSort->nDefer; i++){
       122806  +      int iCsr = pSort->aDefer[i].iCsr;
       122807  +      Table *pTab = pSort->aDefer[i].pTab;
       122808  +      int nKey = pSort->aDefer[i].nKey;
       122809  +
       122810  +      sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
       122811  +      if( HasRowid(pTab) ){
       122812  +        sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
       122813  +        sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr, 
       122814  +            sqlite3VdbeCurrentAddr(v)+1, regKey);
       122815  +      }else{
       122816  +        int k;
       122817  +        int iJmp;
       122818  +        assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
       122819  +        for(k=0; k<nKey; k++){
       122820  +          sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
       122821  +        }
       122822  +        iJmp = sqlite3VdbeCurrentAddr(v);
       122823  +        sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
       122824  +        sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
       122825  +        sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
       122826  +      }
       122827  +    }
       122828  +    sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
       122829  +  }
       122830  +#endif
       122831  +  for(i=nColumn-1; i>=0; i--){
       122832  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122833  +    if( aOutEx[i].bSorterRef ){
       122834  +      sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
       122835  +    }else
       122836  +#endif
       122837  +    {
       122838  +      int iRead;
       122839  +      if( aOutEx[i].u.x.iOrderByCol ){
       122840  +        iRead = aOutEx[i].u.x.iOrderByCol-1;
       122841  +      }else{
       122842  +        iRead = iCol--;
       122843  +      }
       122844  +      sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
       122845  +      VdbeComment((v, "%s", aOutEx[i].zName?aOutEx[i].zName : aOutEx[i].zSpan));
       122846  +    }
121573 122847     }
121574 122848     switch( eDest ){
121575 122849       case SRT_Table:
121576 122850       case SRT_EphemTab: {
121577 122851         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
121578 122852         sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
121579 122853         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
................................................................................
121883 123157   #ifndef SQLITE_OMIT_EXPLAIN
121884 123158     /* If this is an EXPLAIN, skip this step */
121885 123159     if( pParse->explain ){
121886 123160       return;
121887 123161     }
121888 123162   #endif
121889 123163   
121890         -  if( pParse->colNamesSet || db->mallocFailed ) return;
       123164  +  if( pParse->colNamesSet ) return;
121891 123165     /* Column names are determined by the left-most term of a compound select */
121892 123166     while( pSelect->pPrior ) pSelect = pSelect->pPrior;
121893 123167     SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
121894 123168     pTabList = pSelect->pSrc;
121895 123169     pEList = pSelect->pEList;
121896 123170     assert( v!=0 );
121897 123171     assert( pTabList!=0 );
................................................................................
122410 123684     }
122411 123685   
122412 123686     /* Detach the ORDER BY clause from the compound SELECT */
122413 123687     p->pOrderBy = 0;
122414 123688   
122415 123689     /* Store the results of the setup-query in Queue. */
122416 123690     pSetup->pNext = 0;
       123691  +  ExplainQueryPlan((pParse, 1, "SETUP"));
122417 123692     rc = sqlite3Select(pParse, pSetup, &destQueue);
122418 123693     pSetup->pNext = p;
122419 123694     if( rc ) goto end_of_recursive_query;
122420 123695   
122421 123696     /* Find the next row in the Queue and output that row */
122422 123697     addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
122423 123698   
................................................................................
122444 123719     /* Execute the recursive SELECT taking the single row in Current as
122445 123720     ** the value for the recursive-table. Store the results in the Queue.
122446 123721     */
122447 123722     if( p->selFlags & SF_Aggregate ){
122448 123723       sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
122449 123724     }else{
122450 123725       p->pPrior = 0;
       123726  +    ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
122451 123727       sqlite3Select(pParse, p, &destQueue);
122452 123728       assert( p->pPrior==0 );
122453 123729       p->pPrior = pSetup;
122454 123730     }
122455 123731   
122456 123732     /* Keep running the loop until the Queue is empty */
122457 123733     sqlite3VdbeGoto(v, addrTop);
................................................................................
122489 123765   ** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
122490 123766   */
122491 123767   static int multiSelectValues(
122492 123768     Parse *pParse,        /* Parsing context */
122493 123769     Select *p,            /* The right-most of SELECTs to be coded */
122494 123770     SelectDest *pDest     /* What to do with query results */
122495 123771   ){
122496         -  Select *pPrior;
122497         -  Select *pRightmost = p;
122498 123772     int nRow = 1;
122499 123773     int rc = 0;
       123774  +  int bShowAll = p->pLimit==0;
122500 123775     assert( p->selFlags & SF_MultiValue );
122501 123776     do{
122502 123777       assert( p->selFlags & SF_Values );
122503 123778       assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
122504 123779       assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
122505 123780       if( p->pPrior==0 ) break;
122506 123781       assert( p->pPrior->pNext==p );
122507 123782       p = p->pPrior;
122508         -    nRow++;
       123783  +    nRow += bShowAll;
122509 123784     }while(1);
       123785  +  ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
       123786  +                    nRow==1 ? "" : "S"));
122510 123787     while( p ){
122511         -    pPrior = p->pPrior;
122512         -    p->pPrior = 0;
122513         -    rc = sqlite3Select(pParse, p, pDest);
122514         -    p->pPrior = pPrior;
122515         -    if( rc || pRightmost->pLimit ) break;
       123788  +    selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
       123789  +    if( !bShowAll ) break;
122516 123790       p->nSelectRow = nRow;
122517 123791       p = p->pNext;
122518 123792     }
122519 123793     return rc;
122520 123794   }
122521 123795   
122522 123796   /*
................................................................................
122557 123831   ){
122558 123832     int rc = SQLITE_OK;   /* Success code from a subroutine */
122559 123833     Select *pPrior;       /* Another SELECT immediately to our left */
122560 123834     Vdbe *v;              /* Generate code to this VDBE */
122561 123835     SelectDest dest;      /* Alternative data destination */
122562 123836     Select *pDelete = 0;  /* Chain of simple selects to delete */
122563 123837     sqlite3 *db;          /* Database connection */
122564         -#ifndef SQLITE_OMIT_EXPLAIN
122565         -  int iSub1 = 0;        /* EQP id of left-hand query */
122566         -  int iSub2 = 0;        /* EQP id of right-hand query */
122567         -#endif
122568 123838   
122569 123839     /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
122570 123840     ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
122571 123841     */
122572 123842     assert( p && p->pPrior );  /* Calling function guarantees this much */
122573 123843     assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
122574 123844     db = pParse->db;
................................................................................
122611 123881     }else
122612 123882   #endif
122613 123883   
122614 123884     /* Compound SELECTs that have an ORDER BY clause are handled separately.
122615 123885     */
122616 123886     if( p->pOrderBy ){
122617 123887       return multiSelectOrderBy(pParse, p, pDest);
122618         -  }else
122619         -
122620         -  /* Generate code for the left and right SELECT statements.
122621         -  */
122622         -  switch( p->op ){
122623         -    case TK_ALL: {
122624         -      int addr = 0;
122625         -      int nLimit;
122626         -      assert( !pPrior->pLimit );
122627         -      pPrior->iLimit = p->iLimit;
122628         -      pPrior->iOffset = p->iOffset;
122629         -      pPrior->pLimit = p->pLimit;
122630         -      explainSetInteger(iSub1, pParse->iNextSelectId);
122631         -      rc = sqlite3Select(pParse, pPrior, &dest);
122632         -      p->pLimit = 0;
122633         -      if( rc ){
122634         -        goto multi_select_end;
122635         -      }
122636         -      p->pPrior = 0;
122637         -      p->iLimit = pPrior->iLimit;
122638         -      p->iOffset = pPrior->iOffset;
122639         -      if( p->iLimit ){
122640         -        addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
122641         -        VdbeComment((v, "Jump ahead if LIMIT reached"));
122642         -        if( p->iOffset ){
122643         -          sqlite3VdbeAddOp3(v, OP_OffsetLimit,
122644         -                            p->iLimit, p->iOffset+1, p->iOffset);
122645         -        }
122646         -      }
122647         -      explainSetInteger(iSub2, pParse->iNextSelectId);
122648         -      rc = sqlite3Select(pParse, p, &dest);
122649         -      testcase( rc!=SQLITE_OK );
122650         -      pDelete = p->pPrior;
122651         -      p->pPrior = pPrior;
122652         -      p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
122653         -      if( pPrior->pLimit
122654         -       && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
122655         -       && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) 
122656         -      ){
122657         -        p->nSelectRow = sqlite3LogEst((u64)nLimit);
122658         -      }
122659         -      if( addr ){
122660         -        sqlite3VdbeJumpHere(v, addr);
122661         -      }
122662         -      break;
122663         -    }
122664         -    case TK_EXCEPT:
122665         -    case TK_UNION: {
122666         -      int unionTab;    /* Cursor number of the temporary table holding result */
122667         -      u8 op = 0;       /* One of the SRT_ operations to apply to self */
122668         -      int priorOp;     /* The SRT_ operation to apply to prior selects */
122669         -      Expr *pLimit;    /* Saved values of p->nLimit  */
122670         -      int addr;
122671         -      SelectDest uniondest;
122672         -
122673         -      testcase( p->op==TK_EXCEPT );
122674         -      testcase( p->op==TK_UNION );
122675         -      priorOp = SRT_Union;
122676         -      if( dest.eDest==priorOp ){
122677         -        /* We can reuse a temporary table generated by a SELECT to our
122678         -        ** right.
122679         -        */
122680         -        assert( p->pLimit==0 );      /* Not allowed on leftward elements */
122681         -        unionTab = dest.iSDParm;
122682         -      }else{
122683         -        /* We will need to create our own temporary table to hold the
122684         -        ** intermediate results.
122685         -        */
122686         -        unionTab = pParse->nTab++;
       123888  +  }else{
       123889  +
       123890  +#ifndef SQLITE_OMIT_EXPLAIN
       123891  +    if( pPrior->pPrior==0 ){
       123892  +      ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
       123893  +      ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
       123894  +    }
       123895  +#endif
       123896  +
       123897  +    /* Generate code for the left and right SELECT statements.
       123898  +    */
       123899  +    switch( p->op ){
       123900  +      case TK_ALL: {
       123901  +        int addr = 0;
       123902  +        int nLimit;
       123903  +        assert( !pPrior->pLimit );
       123904  +        pPrior->iLimit = p->iLimit;
       123905  +        pPrior->iOffset = p->iOffset;
       123906  +        pPrior->pLimit = p->pLimit;
       123907  +        rc = sqlite3Select(pParse, pPrior, &dest);
       123908  +        p->pLimit = 0;
       123909  +        if( rc ){
       123910  +          goto multi_select_end;
       123911  +        }
       123912  +        p->pPrior = 0;
       123913  +        p->iLimit = pPrior->iLimit;
       123914  +        p->iOffset = pPrior->iOffset;
       123915  +        if( p->iLimit ){
       123916  +          addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
       123917  +          VdbeComment((v, "Jump ahead if LIMIT reached"));
       123918  +          if( p->iOffset ){
       123919  +            sqlite3VdbeAddOp3(v, OP_OffsetLimit,
       123920  +                              p->iLimit, p->iOffset+1, p->iOffset);
       123921  +          }
       123922  +        }
       123923  +        ExplainQueryPlan((pParse, 1, "UNION ALL"));
       123924  +        rc = sqlite3Select(pParse, p, &dest);
       123925  +        testcase( rc!=SQLITE_OK );
       123926  +        pDelete = p->pPrior;
       123927  +        p->pPrior = pPrior;
       123928  +        p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
       123929  +        if( pPrior->pLimit
       123930  +         && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
       123931  +         && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) 
       123932  +        ){
       123933  +          p->nSelectRow = sqlite3LogEst((u64)nLimit);
       123934  +        }
       123935  +        if( addr ){
       123936  +          sqlite3VdbeJumpHere(v, addr);
       123937  +        }
       123938  +        break;
       123939  +      }
       123940  +      case TK_EXCEPT:
       123941  +      case TK_UNION: {
       123942  +        int unionTab;    /* Cursor number of the temp table holding result */
       123943  +        u8 op = 0;       /* One of the SRT_ operations to apply to self */
       123944  +        int priorOp;     /* The SRT_ operation to apply to prior selects */
       123945  +        Expr *pLimit;    /* Saved values of p->nLimit  */
       123946  +        int addr;
       123947  +        SelectDest uniondest;
       123948  +  
       123949  +        testcase( p->op==TK_EXCEPT );
       123950  +        testcase( p->op==TK_UNION );
       123951  +        priorOp = SRT_Union;
       123952  +        if( dest.eDest==priorOp ){
       123953  +          /* We can reuse a temporary table generated by a SELECT to our
       123954  +          ** right.
       123955  +          */
       123956  +          assert( p->pLimit==0 );      /* Not allowed on leftward elements */
       123957  +          unionTab = dest.iSDParm;
       123958  +        }else{
       123959  +          /* We will need to create our own temporary table to hold the
       123960  +          ** intermediate results.
       123961  +          */
       123962  +          unionTab = pParse->nTab++;
       123963  +          assert( p->pOrderBy==0 );
       123964  +          addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
       123965  +          assert( p->addrOpenEphm[0] == -1 );
       123966  +          p->addrOpenEphm[0] = addr;
       123967  +          findRightmost(p)->selFlags |= SF_UsesEphemeral;
       123968  +          assert( p->pEList );
       123969  +        }
       123970  +  
       123971  +        /* Code the SELECT statements to our left
       123972  +        */
       123973  +        assert( !pPrior->pOrderBy );
       123974  +        sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
       123975  +        rc = sqlite3Select(pParse, pPrior, &uniondest);
       123976  +        if( rc ){
       123977  +          goto multi_select_end;
       123978  +        }
       123979  +  
       123980  +        /* Code the current SELECT statement
       123981  +        */
       123982  +        if( p->op==TK_EXCEPT ){
       123983  +          op = SRT_Except;
       123984  +        }else{
       123985  +          assert( p->op==TK_UNION );
       123986  +          op = SRT_Union;
       123987  +        }
       123988  +        p->pPrior = 0;
       123989  +        pLimit = p->pLimit;
       123990  +        p->pLimit = 0;
       123991  +        uniondest.eDest = op;
       123992  +        ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
       123993  +                          selectOpName(p->op)));
       123994  +        rc = sqlite3Select(pParse, p, &uniondest);
       123995  +        testcase( rc!=SQLITE_OK );
       123996  +        /* Query flattening in sqlite3Select() might refill p->pOrderBy.
       123997  +        ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
       123998  +        sqlite3ExprListDelete(db, p->pOrderBy);
       123999  +        pDelete = p->pPrior;
       124000  +        p->pPrior = pPrior;
       124001  +        p->pOrderBy = 0;
       124002  +        if( p->op==TK_UNION ){
       124003  +          p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
       124004  +        }
       124005  +        sqlite3ExprDelete(db, p->pLimit);
       124006  +        p->pLimit = pLimit;
       124007  +        p->iLimit = 0;
       124008  +        p->iOffset = 0;
       124009  +  
       124010  +        /* Convert the data in the temporary table into whatever form
       124011  +        ** it is that we currently need.
       124012  +        */
       124013  +        assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
       124014  +        if( dest.eDest!=priorOp ){
       124015  +          int iCont, iBreak, iStart;
       124016  +          assert( p->pEList );
       124017  +          iBreak = sqlite3VdbeMakeLabel(v);
       124018  +          iCont = sqlite3VdbeMakeLabel(v);
       124019  +          computeLimitRegisters(pParse, p, iBreak);
       124020  +          sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
       124021  +          iStart = sqlite3VdbeCurrentAddr(v);
       124022  +          selectInnerLoop(pParse, p, unionTab,
       124023  +                          0, 0, &dest, iCont, iBreak);
       124024  +          sqlite3VdbeResolveLabel(v, iCont);
       124025  +          sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
       124026  +          sqlite3VdbeResolveLabel(v, iBreak);
       124027  +          sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
       124028  +        }
       124029  +        break;
       124030  +      }
       124031  +      default: assert( p->op==TK_INTERSECT ); {
       124032  +        int tab1, tab2;
       124033  +        int iCont, iBreak, iStart;
       124034  +        Expr *pLimit;
       124035  +        int addr;
       124036  +        SelectDest intersectdest;
       124037  +        int r1;
       124038  +  
       124039  +        /* INTERSECT is different from the others since it requires
       124040  +        ** two temporary tables.  Hence it has its own case.  Begin
       124041  +        ** by allocating the tables we will need.
       124042  +        */
       124043  +        tab1 = pParse->nTab++;
       124044  +        tab2 = pParse->nTab++;
122687 124045           assert( p->pOrderBy==0 );
122688         -        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
       124046  +  
       124047  +        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
122689 124048           assert( p->addrOpenEphm[0] == -1 );
122690 124049           p->addrOpenEphm[0] = addr;
122691 124050           findRightmost(p)->selFlags |= SF_UsesEphemeral;
122692 124051           assert( p->pEList );
122693         -      }
122694         -
122695         -      /* Code the SELECT statements to our left
122696         -      */
122697         -      assert( !pPrior->pOrderBy );
122698         -      sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
122699         -      explainSetInteger(iSub1, pParse->iNextSelectId);
122700         -      rc = sqlite3Select(pParse, pPrior, &uniondest);
122701         -      if( rc ){
122702         -        goto multi_select_end;
122703         -      }
122704         -
122705         -      /* Code the current SELECT statement
122706         -      */
122707         -      if( p->op==TK_EXCEPT ){
122708         -        op = SRT_Except;
122709         -      }else{
122710         -        assert( p->op==TK_UNION );
122711         -        op = SRT_Union;
122712         -      }
122713         -      p->pPrior = 0;
122714         -      pLimit = p->pLimit;
122715         -      p->pLimit = 0;
122716         -      uniondest.eDest = op;
122717         -      explainSetInteger(iSub2, pParse->iNextSelectId);
122718         -      rc = sqlite3Select(pParse, p, &uniondest);
122719         -      testcase( rc!=SQLITE_OK );
122720         -      /* Query flattening in sqlite3Select() might refill p->pOrderBy.
122721         -      ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
122722         -      sqlite3ExprListDelete(db, p->pOrderBy);
122723         -      pDelete = p->pPrior;
122724         -      p->pPrior = pPrior;
122725         -      p->pOrderBy = 0;
122726         -      if( p->op==TK_UNION ){
122727         -        p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
122728         -      }
122729         -      sqlite3ExprDelete(db, p->pLimit);
122730         -      p->pLimit = pLimit;
122731         -      p->iLimit = 0;
122732         -      p->iOffset = 0;
122733         -
122734         -      /* Convert the data in the temporary table into whatever form
122735         -      ** it is that we currently need.
122736         -      */
122737         -      assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
122738         -      if( dest.eDest!=priorOp ){
122739         -        int iCont, iBreak, iStart;
       124052  +  
       124053  +        /* Code the SELECTs to our left into temporary table "tab1".
       124054  +        */
       124055  +        sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
       124056  +        rc = sqlite3Select(pParse, pPrior, &intersectdest);
       124057  +        if( rc ){
       124058  +          goto multi_select_end;
       124059  +        }
       124060  +  
       124061  +        /* Code the current SELECT into temporary table "tab2"
       124062  +        */
       124063  +        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
       124064  +        assert( p->addrOpenEphm[1] == -1 );
       124065  +        p->addrOpenEphm[1] = addr;
       124066  +        p->pPrior = 0;
       124067  +        pLimit = p->pLimit;
       124068  +        p->pLimit = 0;
       124069  +        intersectdest.iSDParm = tab2;
       124070  +        ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
       124071  +                          selectOpName(p->op)));
       124072  +        rc = sqlite3Select(pParse, p, &intersectdest);
       124073  +        testcase( rc!=SQLITE_OK );
       124074  +        pDelete = p->pPrior;
       124075  +        p->pPrior = pPrior;
       124076  +        if( p->nSelectRow>pPrior->nSelectRow ){
       124077  +          p->nSelectRow = pPrior->nSelectRow;
       124078  +        }
       124079  +        sqlite3ExprDelete(db, p->pLimit);
       124080  +        p->pLimit = pLimit;
       124081  +  
       124082  +        /* Generate code to take the intersection of the two temporary
       124083  +        ** tables.
       124084  +        */
122740 124085           assert( p->pEList );
122741 124086           iBreak = sqlite3VdbeMakeLabel(v);
122742 124087           iCont = sqlite3VdbeMakeLabel(v);
122743 124088           computeLimitRegisters(pParse, p, iBreak);
122744         -        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
122745         -        iStart = sqlite3VdbeCurrentAddr(v);
122746         -        selectInnerLoop(pParse, p, unionTab,
       124089  +        sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
       124090  +        r1 = sqlite3GetTempReg(pParse);
       124091  +        iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
       124092  +        sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
       124093  +        VdbeCoverage(v);
       124094  +        sqlite3ReleaseTempReg(pParse, r1);
       124095  +        selectInnerLoop(pParse, p, tab1,
122747 124096                           0, 0, &dest, iCont, iBreak);
122748 124097           sqlite3VdbeResolveLabel(v, iCont);
122749         -        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
       124098  +        sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
122750 124099           sqlite3VdbeResolveLabel(v, iBreak);
122751         -        sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
122752         -      }
122753         -      break;
122754         -    }
122755         -    default: assert( p->op==TK_INTERSECT ); {
122756         -      int tab1, tab2;
122757         -      int iCont, iBreak, iStart;
122758         -      Expr *pLimit;
122759         -      int addr;
122760         -      SelectDest intersectdest;
122761         -      int r1;
122762         -
122763         -      /* INTERSECT is different from the others since it requires
122764         -      ** two temporary tables.  Hence it has its own case.  Begin
122765         -      ** by allocating the tables we will need.
122766         -      */
122767         -      tab1 = pParse->nTab++;
122768         -      tab2 = pParse->nTab++;
122769         -      assert( p->pOrderBy==0 );
122770         -
122771         -      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
122772         -      assert( p->addrOpenEphm[0] == -1 );
122773         -      p->addrOpenEphm[0] = addr;
122774         -      findRightmost(p)->selFlags |= SF_UsesEphemeral;
122775         -      assert( p->pEList );
122776         -
122777         -      /* Code the SELECTs to our left into temporary table "tab1".
122778         -      */
122779         -      sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
122780         -      explainSetInteger(iSub1, pParse->iNextSelectId);
122781         -      rc = sqlite3Select(pParse, pPrior, &intersectdest);
122782         -      if( rc ){
122783         -        goto multi_select_end;
122784         -      }
122785         -
122786         -      /* Code the current SELECT into temporary table "tab2"
122787         -      */
122788         -      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
122789         -      assert( p->addrOpenEphm[1] == -1 );
122790         -      p->addrOpenEphm[1] = addr;
122791         -      p->pPrior = 0;
122792         -      pLimit = p->pLimit;
122793         -      p->pLimit = 0;
122794         -      intersectdest.iSDParm = tab2;
122795         -      explainSetInteger(iSub2, pParse->iNextSelectId);
122796         -      rc = sqlite3Select(pParse, p, &intersectdest);
122797         -      testcase( rc!=SQLITE_OK );
122798         -      pDelete = p->pPrior;
122799         -      p->pPrior = pPrior;
122800         -      if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
122801         -      sqlite3ExprDelete(db, p->pLimit);
122802         -      p->pLimit = pLimit;
122803         -
122804         -      /* Generate code to take the intersection of the two temporary
122805         -      ** tables.
122806         -      */
122807         -      assert( p->pEList );
122808         -      iBreak = sqlite3VdbeMakeLabel(v);
122809         -      iCont = sqlite3VdbeMakeLabel(v);
122810         -      computeLimitRegisters(pParse, p, iBreak);
122811         -      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
122812         -      r1 = sqlite3GetTempReg(pParse);
122813         -      iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
122814         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
122815         -      sqlite3ReleaseTempReg(pParse, r1);
122816         -      selectInnerLoop(pParse, p, tab1,
122817         -                      0, 0, &dest, iCont, iBreak);
122818         -      sqlite3VdbeResolveLabel(v, iCont);
122819         -      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
122820         -      sqlite3VdbeResolveLabel(v, iBreak);
122821         -      sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
122822         -      sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
122823         -      break;
122824         -    }
122825         -  }
122826         -
122827         -  explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
122828         -
       124100  +        sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
       124101  +        sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
       124102  +        break;
       124103  +      }
       124104  +    }
       124105  +  
       124106  +  #ifndef SQLITE_OMIT_EXPLAIN
       124107  +    if( p->pNext==0 ){
       124108  +      ExplainQueryPlanPop(pParse);
       124109  +    }
       124110  +  #endif
       124111  +  }
       124112  +  
122829 124113     /* Compute collating sequences used by 
122830 124114     ** temporary tables needed to implement the compound select.
122831 124115     ** Attach the KeyInfo structure to all temporary tables.
122832 124116     **
122833 124117     ** This section is run by the right-most SELECT statement only.
122834 124118     ** SELECT statements to the left always skip this part.  The right-most
122835 124119     ** SELECT might also skip this part if it has no ORDER BY clause and
................................................................................
123159 124443     int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
123160 124444     KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
123161 124445     KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
123162 124446     sqlite3 *db;          /* Database connection */
123163 124447     ExprList *pOrderBy;   /* The ORDER BY clause */
123164 124448     int nOrderBy;         /* Number of terms in the ORDER BY clause */
123165 124449     int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
123166         -#ifndef SQLITE_OMIT_EXPLAIN
123167         -  int iSub1;            /* EQP id of left-hand query */
123168         -  int iSub2;            /* EQP id of right-hand query */
123169         -#endif
123170 124450   
123171 124451     assert( p->pOrderBy!=0 );
123172 124452     assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
123173 124453     db = pParse->db;
123174 124454     v = pParse->pVdbe;
123175 124455     assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
123176 124456     labelEnd = sqlite3VdbeMakeLabel(v);
................................................................................
123282 124562     regAddrA = ++pParse->nMem;
123283 124563     regAddrB = ++pParse->nMem;
123284 124564     regOutA = ++pParse->nMem;
123285 124565     regOutB = ++pParse->nMem;
123286 124566     sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
123287 124567     sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
123288 124568   
       124569  +  ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
       124570  +
123289 124571     /* Generate a coroutine to evaluate the SELECT statement to the
123290 124572     ** left of the compound operator - the "A" select.
123291 124573     */
123292 124574     addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
123293 124575     addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
123294 124576     VdbeComment((v, "left SELECT"));
123295 124577     pPrior->iLimit = regLimitA;
123296         -  explainSetInteger(iSub1, pParse->iNextSelectId);
       124578  +  ExplainQueryPlan((pParse, 1, "LEFT"));
123297 124579     sqlite3Select(pParse, pPrior, &destA);
123298 124580     sqlite3VdbeEndCoroutine(v, regAddrA);
123299 124581     sqlite3VdbeJumpHere(v, addr1);
123300 124582   
123301 124583     /* Generate a coroutine to evaluate the SELECT statement on 
123302 124584     ** the right - the "B" select
123303 124585     */
................................................................................
123304 124586     addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
123305 124587     addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
123306 124588     VdbeComment((v, "right SELECT"));
123307 124589     savedLimit = p->iLimit;
123308 124590     savedOffset = p->iOffset;
123309 124591     p->iLimit = regLimitB;
123310 124592     p->iOffset = 0;  
123311         -  explainSetInteger(iSub2, pParse->iNextSelectId);
       124593  +  ExplainQueryPlan((pParse, 1, "RIGHT"));
123312 124594     sqlite3Select(pParse, p, &destB);
123313 124595     p->iLimit = savedLimit;
123314 124596     p->iOffset = savedOffset;
123315 124597     sqlite3VdbeEndCoroutine(v, regAddrB);
123316 124598   
123317 124599     /* Generate a subroutine that outputs the current row of the A
123318 124600     ** select as the next output row of the compound select.
................................................................................
123416 124698       sqlite3SelectDelete(db, p->pPrior);
123417 124699     }
123418 124700     p->pPrior = pPrior;
123419 124701     pPrior->pNext = p;
123420 124702   
123421 124703     /*** TBD:  Insert subroutine calls to close cursors on incomplete
123422 124704     **** subqueries ****/
123423         -  explainComposite(pParse, p->op, iSub1, iSub2, 0);
       124705  +  ExplainQueryPlanPop(pParse);
123424 124706     return pParse->nErr!=0;
123425 124707   }
123426 124708   #endif
123427 124709   
123428 124710   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
123429 124711   
123430 124712   /* An instance of the SubstContext object describes an substitution edit
................................................................................
123903 125185       if( pNew==0 ){
123904 125186         p->pPrior = pPrior;
123905 125187       }else{
123906 125188         pNew->pPrior = pPrior;
123907 125189         if( pPrior ) pPrior->pNext = pNew;
123908 125190         pNew->pNext = p;
123909 125191         p->pPrior = pNew;
123910         -      SELECTTRACE(2,pParse,p,
123911         -         ("compound-subquery flattener creates %s.%p as peer\n",
123912         -         pNew->zSelName, pNew));
       125192  +      SELECTTRACE(2,pParse,p,("compound-subquery flattener"
       125193  +                              " creates %s.%p as peer\n",pNew->zSelName, pNew));
123913 125194       }
123914 125195       if( db->mallocFailed ) return 1;
123915 125196     }
123916 125197   
123917 125198     /* Begin flattening the iFrom-th entry of the FROM clause 
123918 125199     ** in the outer query.
123919 125200     */
................................................................................
125205 126486   static void explainSimpleCount(
125206 126487     Parse *pParse,                  /* Parse context */
125207 126488     Table *pTab,                    /* Table being queried */
125208 126489     Index *pIdx                     /* Index used to optimize scan, or NULL */
125209 126490   ){
125210 126491     if( pParse->explain==2 ){
125211 126492       int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
125212         -    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
       126493  +    sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
125213 126494           pTab->zName,
125214 126495           bCover ? " USING COVERING INDEX " : "",
125215 126496           bCover ? pIdx->zName : ""
125216 126497       );
125217         -    sqlite3VdbeAddOp4(
125218         -        pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
125219         -    );
125220 126498     }
125221 126499   }
125222 126500   #else
125223 126501   # define explainSimpleCount(a,b,c)
125224 126502   #endif
125225 126503   
125226 126504   /*
................................................................................
125425 126703     SortCtx sSort;         /* Info on how to code the ORDER BY clause */
125426 126704     AggInfo sAggInfo;      /* Information used by aggregate queries */
125427 126705     int iEnd;              /* Address of the end of the query */
125428 126706     sqlite3 *db;           /* The database connection */
125429 126707     ExprList *pMinMaxOrderBy = 0;  /* Added ORDER BY for min/max queries */
125430 126708     u8 minMaxFlag;                 /* Flag for min/max queries */
125431 126709   
125432         -#ifndef SQLITE_OMIT_EXPLAIN
125433         -  int iRestoreSelectId = pParse->iSelectId;
125434         -  pParse->iSelectId = pParse->iNextSelectId++;
125435         -#endif
125436         -
125437 126710     db = pParse->db;
       126711  +  v = sqlite3GetVdbe(pParse);
125438 126712     if( p==0 || db->mallocFailed || pParse->nErr ){
125439 126713       return 1;
125440 126714     }
125441 126715     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
125442 126716     memset(&sAggInfo, 0, sizeof(sAggInfo));
125443 126717   #if SELECTTRACE_ENABLED
125444         -  SELECTTRACE(1,pParse,p, ("begin processing:\n"));
       126718  +  SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
125445 126719     if( sqlite3SelectTrace & 0x100 ){
125446 126720       sqlite3TreeViewSelect(0, p, 0);
125447 126721     }
125448 126722   #endif
125449 126723   
125450 126724     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
125451 126725     assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
................................................................................
125468 126742     pTabList = p->pSrc;
125469 126743     if( pParse->nErr || db->mallocFailed ){
125470 126744       goto select_end;
125471 126745     }
125472 126746     assert( p->pEList!=0 );
125473 126747     isAgg = (p->selFlags & SF_Aggregate)!=0;
125474 126748   #if SELECTTRACE_ENABLED
125475         -  if( sqlite3SelectTrace & 0x100 ){
125476         -    SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
       126749  +  if( sqlite3SelectTrace & 0x104 ){
       126750  +    SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
125477 126751       sqlite3TreeViewSelect(0, p, 0);
125478 126752     }
125479 126753   #endif
125480 126754   
125481         -  /* Get a pointer the VDBE under construction, allocating a new VDBE if one
125482         -  ** does not already exist */
125483         -  v = sqlite3GetVdbe(pParse);
125484         -  if( v==0 ) goto select_end;
125485 126755     if( pDest->eDest==SRT_Output ){
125486 126756       generateColumnNames(pParse, p);
125487 126757     }
125488 126758   
125489 126759     /* Try to various optimizations (flattening subqueries, and strength
125490 126760     ** reduction of join operators) in the FROM clause up into the main query
125491 126761     */
................................................................................
125570 126840   
125571 126841   #ifndef SQLITE_OMIT_COMPOUND_SELECT
125572 126842     /* Handle compound SELECT statements using the separate multiSelect()
125573 126843     ** procedure.
125574 126844     */
125575 126845     if( p->pPrior ){
125576 126846       rc = multiSelect(pParse, p, pDest);
125577         -    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
125578 126847   #if SELECTTRACE_ENABLED
125579         -    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
       126848  +    SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
       126849  +    if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
       126850  +      sqlite3TreeViewSelect(0, p, 0);
       126851  +    }
125580 126852   #endif
       126853  +    if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
125581 126854       return rc;
125582 126855     }
125583 126856   #endif
125584 126857   
125585 126858     /* For each term in the FROM clause, do two things:
125586 126859     ** (1) Authorized unreferenced tables
125587 126860     ** (2) Generate code for all sub-queries
................................................................................
125685 126958         int addrTop = sqlite3VdbeCurrentAddr(v)+1;
125686 126959        
125687 126960         pItem->regReturn = ++pParse->nMem;
125688 126961         sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
125689 126962         VdbeComment((v, "%s", pItem->pTab->zName));
125690 126963         pItem->addrFillSub = addrTop;
125691 126964         sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
125692         -      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
       126965  +      ExplainQueryPlan((pParse, 1, "CO-ROUTINE 0x%p", pSub));
125693 126966         sqlite3Select(pParse, pSub, &dest);
125694 126967         pItem->pTab->nRowLogEst = pSub->nSelectRow;
125695 126968         pItem->fg.viaCoroutine = 1;
125696 126969         pItem->regResult = dest.iSdst;
125697 126970         sqlite3VdbeEndCoroutine(v, pItem->regReturn);
125698 126971         sqlite3VdbeJumpHere(v, addrTop-1);
125699 126972         sqlite3ClearTempRegCache(pParse);
................................................................................
125720 126993           VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
125721 126994         }else{
125722 126995           VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
125723 126996         }
125724 126997         pPrior = isSelfJoinView(pTabList, pItem);
125725 126998         if( pPrior ){
125726 126999           sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
125727         -        explainSetInteger(pItem->iSelectId, pPrior->iSelectId);
125728 127000           assert( pPrior->pSelect!=0 );
125729 127001           pSub->nSelectRow = pPrior->pSelect->nSelectRow;
125730 127002         }else{
125731 127003           sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
125732         -        explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
       127004  +        ExplainQueryPlan((pParse, 1, "MATERIALIZE 0x%p", pSub));
125733 127005           sqlite3Select(pParse, pSub, &dest);
125734 127006         }
125735 127007         pItem->pTab->nRowLogEst = pSub->nSelectRow;
125736 127008         if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
125737 127009         retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
125738 127010         VdbeComment((v, "end %s", pItem->pTab->zName));
125739 127011         sqlite3VdbeChangeP1(v, topAddr, retAddr);
................................................................................
125952 127224       /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
125953 127225       ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
125954 127226       ** SELECT statement.
125955 127227       */
125956 127228       memset(&sNC, 0, sizeof(sNC));
125957 127229       sNC.pParse = pParse;
125958 127230       sNC.pSrcList = pTabList;
125959         -    sNC.pAggInfo = &sAggInfo;
       127231  +    sNC.uNC.pAggInfo = &sAggInfo;
       127232  +    VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
125960 127233       sAggInfo.mnReg = pParse->nMem+1;
125961 127234       sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
125962 127235       sAggInfo.pGroupBy = pGroupBy;
125963 127236       sqlite3ExprAnalyzeAggList(&sNC, pEList);
125964 127237       sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
125965 127238       if( pHaving ){
125966 127239         if( pGroupBy ){
................................................................................
126341 127614   
126342 127615     /* If there is an ORDER BY clause, then we need to sort the results
126343 127616     ** and send them to the callback one by one.
126344 127617     */
126345 127618     if( sSort.pOrderBy ){
126346 127619       explainTempTable(pParse,
126347 127620                        sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
       127621  +    assert( p->pEList==pEList );
126348 127622       generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
126349 127623     }
126350 127624   
126351 127625     /* Jump here to skip this query
126352 127626     */
126353 127627     sqlite3VdbeResolveLabel(v, iEnd);
126354 127628   
................................................................................
126356 127630     ** set the return code to 1. Otherwise 0. */
126357 127631     rc = (pParse->nErr>0);
126358 127632   
126359 127633     /* Control jumps to here if an error is encountered above, or upon
126360 127634     ** successful coding of the SELECT.
126361 127635     */
126362 127636   select_end:
126363         -  explainSetInteger(pParse->iSelectId, iRestoreSelectId);
126364 127637     sqlite3ExprListDelete(db, pMinMaxOrderBy);
126365 127638     sqlite3DbFree(db, sAggInfo.aCol);
126366 127639     sqlite3DbFree(db, sAggInfo.aFunc);
126367 127640   #if SELECTTRACE_ENABLED
126368         -  SELECTTRACE(1,pParse,p,("end processing\n"));
       127641  +  SELECTTRACE(0x1,pParse,p,("end processing\n"));
       127642  +  if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
       127643  +    sqlite3TreeViewSelect(0, p, 0);
       127644  +  }
126369 127645   #endif
       127646  +  ExplainQueryPlanPop(pParse);
126370 127647     return rc;
126371 127648   }
126372 127649   
126373 127650   /************** End of select.c **********************************************/
126374 127651   /************** Begin file table.c *******************************************/
126375 127652   /*
126376 127653   ** 2001 September 15
................................................................................
126596 127873       TriggerStep * pTmp = pTriggerStep;
126597 127874       pTriggerStep = pTriggerStep->pNext;
126598 127875   
126599 127876       sqlite3ExprDelete(db, pTmp->pWhere);
126600 127877       sqlite3ExprListDelete(db, pTmp->pExprList);
126601 127878       sqlite3SelectDelete(db, pTmp->pSelect);
126602 127879       sqlite3IdListDelete(db, pTmp->pIdList);
       127880  +    sqlite3UpsertDelete(db, pTmp->pUpsert);
126603 127881       sqlite3DbFree(db, pTmp->zSpan);
126604 127882   
126605 127883       sqlite3DbFree(db, pTmp);
126606 127884     }
126607 127885   }
126608 127886   
126609 127887   /*
................................................................................
126987 128265   */
126988 128266   SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
126989 128267     sqlite3 *db,        /* The database connection */
126990 128268     Token *pTableName,  /* Name of the table into which we insert */
126991 128269     IdList *pColumn,    /* List of columns in pTableName to insert into */
126992 128270     Select *pSelect,    /* A SELECT statement that supplies values */
126993 128271     u8 orconf,          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
       128272  +  Upsert *pUpsert,    /* ON CONFLICT clauses for upsert */
126994 128273     const char *zStart, /* Start of SQL text */
126995 128274     const char *zEnd    /* End of SQL text */
126996 128275   ){
126997 128276     TriggerStep *pTriggerStep;
126998 128277   
126999 128278     assert(pSelect != 0 || db->mallocFailed);
127000 128279   
127001 128280     pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName, zStart, zEnd);
127002 128281     if( pTriggerStep ){
127003 128282       pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
127004 128283       pTriggerStep->pIdList = pColumn;
       128284  +    pTriggerStep->pUpsert = pUpsert;
127005 128285       pTriggerStep->orconf = orconf;
127006 128286     }else{
       128287  +    testcase( pColumn );
127007 128288       sqlite3IdListDelete(db, pColumn);
       128289  +    testcase( pUpsert );
       128290  +    sqlite3UpsertDelete(db, pUpsert);
127008 128291     }
127009 128292     sqlite3SelectDelete(db, pSelect);
127010 128293   
127011 128294     return pTriggerStep;
127012 128295   }
127013 128296   
127014 128297   /*
................................................................................
127317 128600   
127318 128601       switch( pStep->op ){
127319 128602         case TK_UPDATE: {
127320 128603           sqlite3Update(pParse, 
127321 128604             targetSrcList(pParse, pStep),
127322 128605             sqlite3ExprListDup(db, pStep->pExprList, 0), 
127323 128606             sqlite3ExprDup(db, pStep->pWhere, 0), 
127324         -          pParse->eOrconf, 0, 0
       128607  +          pParse->eOrconf, 0, 0, 0
127325 128608           );
127326 128609           break;
127327 128610         }
127328 128611         case TK_INSERT: {
127329 128612           sqlite3Insert(pParse, 
127330 128613             targetSrcList(pParse, pStep),
127331 128614             sqlite3SelectDup(db, pStep->pSelect, 0), 
127332 128615             sqlite3IdListDup(db, pStep->pIdList), 
127333         -          pParse->eOrconf
       128616  +          pParse->eOrconf,
       128617  +          sqlite3UpsertDup(db, pStep->pUpsert)
127334 128618           );
127335 128619           break;
127336 128620         }
127337 128621         case TK_DELETE: {
127338 128622           sqlite3DeleteFrom(pParse, 
127339 128623             targetSrcList(pParse, pStep),
127340 128624             sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
................................................................................
127804 129088   SQLITE_PRIVATE void sqlite3Update(
127805 129089     Parse *pParse,         /* The parser context */
127806 129090     SrcList *pTabList,     /* The table in which we should change things */
127807 129091     ExprList *pChanges,    /* Things to be changed */
127808 129092     Expr *pWhere,          /* The WHERE clause.  May be null */
127809 129093     int onError,           /* How to handle constraint errors */
127810 129094     ExprList *pOrderBy,    /* ORDER BY clause. May be null */
127811         -  Expr *pLimit           /* LIMIT clause. May be null */
       129095  +  Expr *pLimit,          /* LIMIT clause. May be null */
       129096  +  Upsert *pUpsert        /* ON CONFLICT clause, or null */
127812 129097   ){
127813 129098     int i, j;              /* Loop counters */
127814 129099     Table *pTab;           /* The table to be updated */
127815 129100     int addrTop = 0;       /* VDBE instruction address of the start of the loop */
127816 129101     WhereInfo *pWInfo;     /* Information about the WHERE clause */
127817 129102     Vdbe *v;               /* The virtual database engine */
127818 129103     Index *pIdx;           /* For looping over indices */
................................................................................
127911 129196     }
127912 129197   
127913 129198     /* Allocate a cursors for the main database table and for all indices.
127914 129199     ** The index cursors might not be used, but if they are used they
127915 129200     ** need to occur right after the database cursor.  So go ahead and
127916 129201     ** allocate enough space, just in case.
127917 129202     */
127918         -  pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
       129203  +  iBaseCur = iDataCur = pParse->nTab++;
127919 129204     iIdxCur = iDataCur+1;
127920 129205     pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
       129206  +  testcase( pPk!=0 && pPk!=pTab->pIndex );
127921 129207     for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
127922         -    if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){
       129208  +    if( pPk==pIdx ){
127923 129209         iDataCur = pParse->nTab;
127924         -      pTabList->a[0].iCursor = iDataCur;
127925 129210       }
127926 129211       pParse->nTab++;
127927 129212     }
       129213  +  if( pUpsert ){
       129214  +    /* On an UPSERT, reuse the same cursors already opened by INSERT */
       129215  +    iDataCur = pUpsert->iDataCur;
       129216  +    iIdxCur = pUpsert->iIdxCur;
       129217  +    pParse->nTab = iBaseCur;
       129218  +  }
       129219  +  pTabList->a[0].iCursor = iDataCur;
127928 129220   
127929 129221     /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].  
127930 129222     ** Initialize aXRef[] and aToOpen[] to their default values.
127931 129223     */
127932 129224     aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
127933 129225     if( aXRef==0 ) goto update_cleanup;
127934 129226     aRegIdx = aXRef+pTab->nCol;
................................................................................
127937 129229     aToOpen[nIdx+1] = 0;
127938 129230     for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
127939 129231   
127940 129232     /* Initialize the name-context */
127941 129233     memset(&sNC, 0, sizeof(sNC));
127942 129234     sNC.pParse = pParse;
127943 129235     sNC.pSrcList = pTabList;
       129236  +  sNC.uNC.pUpsert = pUpsert;
       129237  +  sNC.ncFlags = NC_UUpsert;
127944 129238   
127945 129239     /* Resolve the column names in all the expressions of the
127946 129240     ** of the UPDATE statement.  Also find the column index
127947 129241     ** for each column to be updated in the pChanges array.  For each
127948 129242     ** column to be updated, make sure we have authorization to change
127949 129243     ** that column.
127950 129244     */
................................................................................
128040 129334       memset(aToOpen, 1, nIdx+1);
128041 129335     }
128042 129336   
128043 129337     /* Begin generating code. */
128044 129338     v = sqlite3GetVdbe(pParse);
128045 129339     if( v==0 ) goto update_cleanup;
128046 129340     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
128047         -  sqlite3BeginWriteOperation(pParse, 1, iDb);
       129341  +  sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
128048 129342   
128049 129343     /* Allocate required registers. */
128050 129344     if( !IsVirtual(pTab) ){
128051 129345       regRowSet = ++pParse->nMem;
128052 129346       regOldRowid = regNewRowid = ++pParse->nMem;
128053 129347       if( chngPk || pTrigger || hasFK ){
128054 129348         regOld = pParse->nMem + 1;
................................................................................
128091 129385     if( IsVirtual(pTab) ){
128092 129386       updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
128093 129387                          pWhere, onError);
128094 129388       goto update_cleanup;
128095 129389     }
128096 129390   #endif
128097 129391   
128098         -  /* Initialize the count of updated rows */
128099         -  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
       129392  +  /* Jump to labelBreak to abandon further processing of this UPDATE */
       129393  +  labelContinue = labelBreak = sqlite3VdbeMakeLabel(v);
       129394  +
       129395  +  /* Not an UPSERT.  Normal processing.  Begin by
       129396  +  ** initialize the count of updated rows */
       129397  +  if( (db->flags&SQLITE_CountRows)!=0
       129398  +   && !pParse->pTriggerTab
       129399  +   && !pParse->nested
       129400  +   && pUpsert==0
       129401  +  ){
128100 129402       regRowCount = ++pParse->nMem;
128101 129403       sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
128102 129404     }
128103 129405   
128104 129406     if( HasRowid(pTab) ){
128105 129407       sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
128106 129408     }else{
128107 129409       assert( pPk!=0 );
128108 129410       nPk = pPk->nKeyCol;
128109 129411       iPk = pParse->nMem+1;
128110 129412       pParse->nMem += nPk;
128111 129413       regKey = ++pParse->nMem;
128112         -    iEph = pParse->nTab++;
128113         -
128114         -    sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
128115         -    addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
128116         -    sqlite3VdbeSetP4KeyInfo(pParse, pPk);
128117         -  }
128118         -
128119         -  /* Begin the database scan. 
128120         -  **
128121         -  ** Do not consider a single-pass strategy for a multi-row update if
128122         -  ** there are any triggers or foreign keys to process, or rows may
128123         -  ** be deleted as a result of REPLACE conflict handling. Any of these
128124         -  ** things might disturb a cursor being used to scan through the table
128125         -  ** or index, causing a single-pass approach to malfunction.  */
128126         -  flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
128127         -  if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
128128         -    flags |= WHERE_ONEPASS_MULTIROW;
128129         -  }
128130         -  pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
128131         -  if( pWInfo==0 ) goto update_cleanup;
128132         -
128133         -  /* A one-pass strategy that might update more than one row may not
128134         -  ** be used if any column of the index used for the scan is being
128135         -  ** updated. Otherwise, if there is an index on "b", statements like
128136         -  ** the following could create an infinite loop:
128137         -  **
128138         -  **   UPDATE t1 SET b=b+1 WHERE b>?
128139         -  **
128140         -  ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
128141         -  ** strategy that uses an index for which one or more columns are being
128142         -  ** updated.  */
128143         -  eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
128144         -  if( eOnePass==ONEPASS_MULTI ){
128145         -    int iCur = aiCurOnePass[1];
128146         -    if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
128147         -      eOnePass = ONEPASS_OFF;
128148         -    }
128149         -    assert( iCur!=iDataCur || !HasRowid(pTab) );
128150         -  }
128151         -  
       129414  +    if( pUpsert==0 ){
       129415  +      iEph = pParse->nTab++;
       129416  +        sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
       129417  +      addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
       129418  +      sqlite3VdbeSetP4KeyInfo(pParse, pPk);
       129419  +    }
       129420  +  }
       129421  +  
       129422  +  if( pUpsert ){
       129423  +    /* If this is an UPSERT, then all cursors have already been opened by
       129424  +    ** the outer INSERT and the data cursor should be pointing at the row
       129425  +    ** that is to be updated.  So bypass the code that searches for the
       129426  +    ** row(s) to be updated.
       129427  +    */
       129428  +    pWInfo = 0;
       129429  +    eOnePass = ONEPASS_SINGLE;
       129430  +    sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL);
       129431  +  }else{
       129432  +    /* Begin the database scan. 
       129433  +    **
       129434  +    ** Do not consider a single-pass strategy for a multi-row update if
       129435  +    ** there are any triggers or foreign keys to process, or rows may
       129436  +    ** be deleted as a result of REPLACE conflict handling. Any of these
       129437  +    ** things might disturb a cursor being used to scan through the table
       129438  +    ** or index, causing a single-pass approach to malfunction.  */
       129439  +    flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
       129440  +    if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
       129441  +      flags |= WHERE_ONEPASS_MULTIROW;
       129442  +    }
       129443  +    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
       129444  +    if( pWInfo==0 ) goto update_cleanup;
       129445  +  
       129446  +    /* A one-pass strategy that might update more than one row may not
       129447  +    ** be used if any column of the index used for the scan is being
       129448  +    ** updated. Otherwise, if there is an index on "b", statements like
       129449  +    ** the following could create an infinite loop:
       129450  +    **
       129451  +    **   UPDATE t1 SET b=b+1 WHERE b>?
       129452  +    **
       129453  +    ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
       129454  +    ** strategy that uses an index for which one or more columns are being
       129455  +    ** updated.  */
       129456  +    eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
       129457  +    if( eOnePass!=ONEPASS_SINGLE ){
       129458  +      sqlite3MultiWrite(pParse);
       129459  +      if( eOnePass==ONEPASS_MULTI ){
       129460  +        int iCur = aiCurOnePass[1];
       129461  +        if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
       129462  +          eOnePass = ONEPASS_OFF;
       129463  +        }
       129464  +        assert( iCur!=iDataCur || !HasRowid(pTab) );
       129465  +      }
       129466  +    }
       129467  +  }
       129468  +
128152 129469     if( HasRowid(pTab) ){
128153 129470       /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
128154 129471       ** mode, write the rowid into the FIFO. In either of the one-pass modes,
128155 129472       ** leave it in register regOldRowid.  */
128156 129473       sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
128157 129474       if( eOnePass==ONEPASS_OFF ){
128158 129475         sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
................................................................................
128164 129481       ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table 
128165 129482       ** is not required) and leave the PK fields in the array of registers.  */
128166 129483       for(i=0; i<nPk; i++){
128167 129484         assert( pPk->aiColumn[i]>=0 );
128168 129485         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
128169 129486       }
128170 129487       if( eOnePass ){
128171         -      sqlite3VdbeChangeToNoop(v, addrOpen);
       129488  +      if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
128172 129489         nKey = nPk;
128173 129490         regKey = iPk;
128174 129491       }else{
128175 129492         sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
128176 129493                           sqlite3IndexAffinityStr(db, pPk), nPk);
128177 129494         sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
128178 129495       }
128179 129496     }
128180 129497   
128181         -  if( eOnePass!=ONEPASS_MULTI ){
128182         -    sqlite3WhereEnd(pWInfo);
128183         -  }
128184         -
128185         -  labelBreak = sqlite3VdbeMakeLabel(v);
128186         -  if( !isView ){
128187         -    int addrOnce = 0;
128188         -
128189         -    /* Open every index that needs updating. */
       129498  +  if( pUpsert==0 ){
       129499  +    if( eOnePass!=ONEPASS_MULTI ){
       129500  +      sqlite3WhereEnd(pWInfo);
       129501  +    }
       129502  +  
       129503  +    if( !isView ){
       129504  +      int addrOnce = 0;
       129505  +  
       129506  +      /* Open every index that needs updating. */
       129507  +      if( eOnePass!=ONEPASS_OFF ){
       129508  +        if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
       129509  +        if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
       129510  +      }
       129511  +  
       129512  +      if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
       129513  +        addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
       129514  +      }
       129515  +      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur,
       129516  +                                 aToOpen, 0, 0);
       129517  +      if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
       129518  +    }
       129519  +  
       129520  +    /* Top of the update loop */
128190 129521       if( eOnePass!=ONEPASS_OFF ){
128191         -      if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
128192         -      if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
128193         -    }
128194         -
128195         -    if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
128196         -      addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
128197         -    }
128198         -    sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
128199         -                               0, 0);
128200         -    if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
128201         -  }
128202         -
128203         -  /* Top of the update loop */
128204         -  if( eOnePass!=ONEPASS_OFF ){
128205         -    if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
128206         -      assert( pPk );
128207         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
128208         -      VdbeCoverageNeverTaken(v);
128209         -    }
128210         -    if( eOnePass==ONEPASS_SINGLE ){
128211         -      labelContinue = labelBreak;
128212         -    }else{
       129522  +      if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
       129523  +        assert( pPk );
       129524  +        sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey,nKey);
       129525  +        VdbeCoverageNeverTaken(v);
       129526  +      }
       129527  +      if( eOnePass!=ONEPASS_SINGLE ){
       129528  +        labelContinue = sqlite3VdbeMakeLabel(v);
       129529  +      }
       129530  +      sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
       129531  +      VdbeCoverageIf(v, pPk==0);
       129532  +      VdbeCoverageIf(v, pPk!=0);
       129533  +    }else if( pPk ){
128213 129534         labelContinue = sqlite3VdbeMakeLabel(v);
       129535  +      sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
       129536  +      addrTop = sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);
       129537  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
       129538  +      VdbeCoverage(v);
       129539  +    }else{
       129540  +      labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet,labelBreak,
       129541  +                               regOldRowid);
       129542  +      VdbeCoverage(v);
       129543  +      sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
       129544  +      VdbeCoverage(v);
128214 129545       }
128215         -    sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
128216         -    VdbeCoverageIf(v, pPk==0);
128217         -    VdbeCoverageIf(v, pPk!=0);
128218         -  }else if( pPk ){
128219         -    labelContinue = sqlite3VdbeMakeLabel(v);
128220         -    sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
128221         -    addrTop = sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);
128222         -    sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
128223         -    VdbeCoverage(v);
128224         -  }else{
128225         -    labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
128226         -                             regOldRowid);
128227         -    VdbeCoverage(v);
128228         -    sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
128229         -    VdbeCoverage(v);
128230 129546     }
128231 129547   
128232         -  /* If the record number will change, set register regNewRowid to
128233         -  ** contain the new value. If the record number is not being modified,
       129548  +  /* If the rowid value will change, set register regNewRowid to
       129549  +  ** contain the new value. If the rowid is not being modified,
128234 129550     ** then regNewRowid is the same register as regOldRowid, which is
128235 129551     ** already populated.  */
128236 129552     assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
128237 129553     if( chngRowid ){
128238 129554       sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
128239 129555       sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
128240 129556     }
................................................................................
128290 129606           ** into a register. This is done if there are no BEFORE triggers, or
128291 129607           ** if there are one or more BEFORE triggers that use this value via
128292 129608           ** a new.* reference in a trigger program.
128293 129609           */
128294 129610           testcase( i==31 );
128295 129611           testcase( i==32 );
128296 129612           sqlite3ExprCodeGetColumnToReg(pParse, pTab, i, iDataCur, regNew+i);
       129613  +        if( tmask & TRIGGER_BEFORE ){
       129614  +          /* This value will be recomputed in After-BEFORE-trigger-reload-loop
       129615  +          ** below, so make sure that it is not cached and reused.
       129616  +          ** Ticket d85fffd6ffe856092ed8daefa811b1e399706b28. */
       129617  +          sqlite3ExprCacheRemove(pParse, regNew+i, 1);
       129618  +        }
128297 129619         }else{
128298 129620           sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
128299 129621         }
128300 129622       }
128301 129623     }
128302 129624   
128303 129625     /* Fire any BEFORE UPDATE triggers. This happens before constraints are
................................................................................
128318 129640         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
128319 129641         VdbeCoverage(v);
128320 129642       }else{
128321 129643         sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
128322 129644         VdbeCoverage(v);
128323 129645       }
128324 129646   
128325         -    /* If it did not delete it, the row-trigger may still have modified 
       129647  +    /* After-BEFORE-trigger-reload-loop:
       129648  +    ** If it did not delete it, the BEFORE trigger may still have modified 
128326 129649       ** some of the columns of the row being updated. Load the values for 
128327         -    ** all columns not modified by the update statement into their 
128328         -    ** registers in case this has happened.
       129650  +    ** all columns not modified by the update statement into their registers
       129651  +    ** in case this has happened. Only unmodified columns are reloaded.
       129652  +    ** The values computed for modified columns use the values before the
       129653  +    ** BEFORE trigger runs.  See test case trigger1-18.0 (added 2018-04-26)
       129654  +    ** for an example.
128329 129655       */
128330 129656       for(i=0; i<pTab->nCol; i++){
128331 129657         if( aXRef[i]<0 && i!=pTab->iPKey ){
128332 129658           sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
128333 129659         }
128334 129660       }
128335 129661     }
................................................................................
128337 129663     if( !isView ){
128338 129664       int addr1 = 0;        /* Address of jump instruction */
128339 129665   
128340 129666       /* Do constraint checks. */
128341 129667       assert( regOldRowid>0 );
128342 129668       sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
128343 129669           regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
128344         -        aXRef);
       129670  +        aXRef, 0);
128345 129671   
128346 129672       /* Do FK constraint checks. */
128347 129673       if( hasFK ){
128348 129674         sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
128349 129675       }
128350 129676   
128351 129677       /* Delete the index entries associated with the current record.  */
................................................................................
128407 129733       if( hasFK ){
128408 129734         sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
128409 129735       }
128410 129736     }
128411 129737   
128412 129738     /* Increment the row counter 
128413 129739     */
128414         -  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
       129740  +  if( regRowCount ){
128415 129741       sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
128416 129742     }
128417 129743   
128418 129744     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
128419 129745         TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
128420 129746   
128421 129747     /* Repeat the above with the next record to be updated, until
................................................................................
128434 129760     }
128435 129761     sqlite3VdbeResolveLabel(v, labelBreak);
128436 129762   
128437 129763     /* Update the sqlite_sequence table by storing the content of the
128438 129764     ** maximum rowid counter values recorded while inserting into
128439 129765     ** autoincrement tables.
128440 129766     */
128441         -  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
       129767  +  if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
128442 129768       sqlite3AutoincrementEnd(pParse);
128443 129769     }
128444 129770   
128445 129771     /*
128446         -  ** Return the number of rows that were changed. If this routine is 
128447         -  ** generating code because of a call to sqlite3NestedParse(), do not
128448         -  ** invoke the callback function.
       129772  +  ** Return the number of rows that were changed, if we are tracking
       129773  +  ** that information.
128449 129774     */
128450         -  if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
       129775  +  if( regRowCount ){
128451 129776       sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
128452 129777       sqlite3VdbeSetNumCols(v, 1);
128453 129778       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
128454 129779     }
128455 129780   
128456 129781   update_cleanup:
128457 129782     sqlite3AuthContextPop(&sContext);
................................................................................
128515 129840     WhereInfo *pWInfo;
128516 129841     int nArg = 2 + pTab->nCol;      /* Number of arguments to VUpdate */
128517 129842     int regArg;                     /* First register in VUpdate arg array */
128518 129843     int regRec;                     /* Register in which to assemble record */
128519 129844     int regRowid;                   /* Register for ephem table rowid */
128520 129845     int iCsr = pSrc->a[0].iCursor;  /* Cursor used for virtual table scan */
128521 129846     int aDummy[2];                  /* Unused arg for sqlite3WhereOkOnePass() */
128522         -  int bOnePass;                   /* True to use onepass strategy */
       129847  +  int eOnePass;                   /* True to use onepass strategy */
128523 129848     int addr;                       /* Address of OP_OpenEphemeral */
128524 129849   
128525 129850     /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
128526 129851     ** create and open the ephemeral table in which the records created from
128527 129852     ** these arguments will be temporarily stored. */
128528 129853     assert( v );
128529 129854     ephemTab = pParse->nTab++;
................................................................................
128560 129885       assert( pPk!=0 );
128561 129886       assert( pPk->nKeyCol==1 );
128562 129887       iPk = pPk->aiColumn[0];
128563 129888       sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
128564 129889       sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
128565 129890     }
128566 129891   
128567         -  bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
       129892  +  eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
128568 129893   
128569         -  if( bOnePass ){
       129894  +  /* There is no ONEPASS_MULTI on virtual tables */
       129895  +  assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
       129896  +
       129897  +  if( eOnePass ){
128570 129898       /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
128571         -    ** above. Also, if this is a top-level parse (not a trigger), clear the
128572         -    ** multi-write flag so that the VM does not open a statement journal */
       129899  +    ** above. */
128573 129900       sqlite3VdbeChangeToNoop(v, addr);
128574         -    if( sqlite3IsToplevel(pParse) ){
128575         -      pParse->isMultiWrite = 0;
128576         -    }
       129901  +    sqlite3VdbeAddOp1(v, OP_Close, iCsr);
128577 129902     }else{
128578 129903       /* Create a record from the argument register contents and insert it into
128579 129904       ** the ephemeral table. */
       129905  +    sqlite3MultiWrite(pParse);
128580 129906       sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
128581 129907   #ifdef SQLITE_DEBUG
128582 129908       /* Signal an assert() within OP_MakeRecord that it is allowed to
128583 129909       ** accept no-change records with serial_type 10 */
128584 129910       sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
128585 129911   #endif
128586 129912       sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
128587 129913       sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
128588 129914     }
128589 129915   
128590 129916   
128591         -  if( bOnePass==0 ){
       129917  +  if( eOnePass==ONEPASS_OFF ){
128592 129918       /* End the virtual table scan */
128593 129919       sqlite3WhereEnd(pWInfo);
128594 129920   
128595 129921       /* Begin scannning through the ephemeral table. */
128596 129922       addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
128597 129923   
128598 129924       /* Extract arguments from the current row of the ephemeral table and 
................................................................................
128604 129930     sqlite3VtabMakeWritable(pParse, pTab);
128605 129931     sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
128606 129932     sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
128607 129933     sqlite3MayAbort(pParse);
128608 129934   
128609 129935     /* End of the ephemeral table scan. Or, if using the onepass strategy,
128610 129936     ** jump to here if the scan visited zero rows. */
128611         -  if( bOnePass==0 ){
       129937  +  if( eOnePass==ONEPASS_OFF ){
128612 129938       sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
128613 129939       sqlite3VdbeJumpHere(v, addr);
128614 129940       sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
128615 129941     }else{
128616 129942       sqlite3WhereEnd(pWInfo);
128617 129943     }
128618 129944   }
128619 129945   #endif /* SQLITE_OMIT_VIRTUALTABLE */
128620 129946   
128621 129947   /************** End of update.c **********************************************/
       129948  +/************** Begin file upsert.c ******************************************/
       129949  +/*
       129950  +** 2018-04-12
       129951  +**
       129952  +** The author disclaims copyright to this source code.  In place of
       129953  +** a legal notice, here is a blessing:
       129954  +**
       129955  +**    May you do good and not evil.
       129956  +**    May you find forgiveness for yourself and forgive others.
       129957  +**    May you share freely, never taking more than you give.
       129958  +**
       129959  +*************************************************************************
       129960  +** This file contains code to implement various aspects of UPSERT
       129961  +** processing and handling of the Upsert object.
       129962  +*/
       129963  +/* #include "sqliteInt.h" */
       129964  +
       129965  +#ifndef SQLITE_OMIT_UPSERT
       129966  +/*
       129967  +** Free a list of Upsert objects
       129968  +*/
       129969  +SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
       129970  +  if( p ){
       129971  +    sqlite3ExprListDelete(db, p->pUpsertTarget);
       129972  +    sqlite3ExprDelete(db, p->pUpsertTargetWhere);
       129973  +    sqlite3ExprListDelete(db, p->pUpsertSet);
       129974  +    sqlite3ExprDelete(db, p->pUpsertWhere);
       129975  +    sqlite3DbFree(db, p);
       129976  +  }
       129977  +}
       129978  +
       129979  +/*
       129980  +** Duplicate an Upsert object.
       129981  +*/
       129982  +SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
       129983  +  if( p==0 ) return 0;
       129984  +  return sqlite3UpsertNew(db,
       129985  +           sqlite3ExprListDup(db, p->pUpsertTarget, 0),
       129986  +           sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
       129987  +           sqlite3ExprListDup(db, p->pUpsertSet, 0),
       129988  +           sqlite3ExprDup(db, p->pUpsertWhere, 0)
       129989  +         );
       129990  +}
       129991  +
       129992  +/*
       129993  +** Create a new Upsert object.
       129994  +*/
       129995  +SQLITE_PRIVATE Upsert *sqlite3UpsertNew(
       129996  +  sqlite3 *db,           /* Determines which memory allocator to use */
       129997  +  ExprList *pTarget,     /* Target argument to ON CONFLICT, or NULL */
       129998  +  Expr *pTargetWhere,    /* Optional WHERE clause on the target */
       129999  +  ExprList *pSet,        /* UPDATE columns, or NULL for a DO NOTHING */
       130000  +  Expr *pWhere           /* WHERE clause for the ON CONFLICT UPDATE */
       130001  +){
       130002  +  Upsert *pNew;
       130003  +  pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
       130004  +  if( pNew==0 ){
       130005  +    sqlite3ExprListDelete(db, pTarget);
       130006  +    sqlite3ExprDelete(db, pTargetWhere);
       130007  +    sqlite3ExprListDelete(db, pSet);
       130008  +    sqlite3ExprDelete(db, pWhere);
       130009  +    return 0;
       130010  +  }else{
       130011  +    pNew->pUpsertTarget = pTarget;
       130012  +    pNew->pUpsertTargetWhere = pTargetWhere;
       130013  +    pNew->pUpsertSet = pSet;
       130014  +    pNew->pUpsertWhere = pWhere;
       130015  +    pNew->pUpsertIdx = 0;
       130016  +  }
       130017  +  return pNew;
       130018  +}
       130019  +
       130020  +/*
       130021  +** Analyze the ON CONFLICT clause described by pUpsert.  Resolve all
       130022  +** symbols in the conflict-target.
       130023  +**
       130024  +** Return SQLITE_OK if everything works, or an error code is something
       130025  +** is wrong.
       130026  +*/
       130027  +SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(
       130028  +  Parse *pParse,     /* The parsing context */
       130029  +  SrcList *pTabList, /* Table into which we are inserting */
       130030  +  Upsert *pUpsert    /* The ON CONFLICT clauses */
       130031  +){
       130032  +  Table *pTab;            /* That table into which we are inserting */
       130033  +  int rc;                 /* Result code */
       130034  +  int iCursor;            /* Cursor used by pTab */
       130035  +  Index *pIdx;            /* One of the indexes of pTab */
       130036  +  ExprList *pTarget;      /* The conflict-target clause */
       130037  +  Expr *pTerm;            /* One term of the conflict-target clause */
       130038  +  NameContext sNC;        /* Context for resolving symbolic names */
       130039  +  Expr sCol[2];           /* Index column converted into an Expr */
       130040  +
       130041  +  assert( pTabList->nSrc==1 );
       130042  +  assert( pTabList->a[0].pTab!=0 );
       130043  +  assert( pUpsert!=0 );
       130044  +  assert( pUpsert->pUpsertTarget!=0 );
       130045  +
       130046  +  /* Resolve all symbolic names in the conflict-target clause, which
       130047  +  ** includes both the list of columns and the optional partial-index
       130048  +  ** WHERE clause.
       130049  +  */
       130050  +  memset(&sNC, 0, sizeof(sNC));
       130051  +  sNC.pParse = pParse;
       130052  +  sNC.pSrcList = pTabList;
       130053  +  rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
       130054  +  if( rc ) return rc;
       130055  +  rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
       130056  +  if( rc ) return rc;
       130057  +
       130058  +  /* Check to see if the conflict target matches the rowid. */  
       130059  +  pTab = pTabList->a[0].pTab;
       130060  +  pTarget = pUpsert->pUpsertTarget;
       130061  +  iCursor = pTabList->a[0].iCursor;
       130062  +  if( HasRowid(pTab) 
       130063  +   && pTarget->nExpr==1
       130064  +   && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
       130065  +   && pTerm->iColumn==XN_ROWID
       130066  +  ){
       130067  +    /* The conflict-target is the rowid of the primary table */
       130068  +    assert( pUpsert->pUpsertIdx==0 );
       130069  +    return SQLITE_OK;
       130070  +  }
       130071  +
       130072  +  /* Initialize sCol[0..1] to be an expression parse tree for a
       130073  +  ** single column of an index.  The sCol[0] node will be the TK_COLLATE
       130074  +  ** operator and sCol[1] will be the TK_COLUMN operator.  Code below
       130075  +  ** will populate the specific collation and column number values
       130076  +  ** prior to comparing against the conflict-target expression.
       130077  +  */
       130078  +  memset(sCol, 0, sizeof(sCol));
       130079  +  sCol[0].op = TK_COLLATE;
       130080  +  sCol[0].pLeft = &sCol[1];
       130081  +  sCol[1].op = TK_COLUMN;
       130082  +  sCol[1].iTable = pTabList->a[0].iCursor;
       130083  +
       130084  +  /* Check for matches against other indexes */
       130085  +  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       130086  +    int ii, jj, nn;
       130087  +    if( !IsUniqueIndex(pIdx) ) continue;
       130088  +    if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
       130089  +    if( pIdx->pPartIdxWhere ){
       130090  +      if( pUpsert->pUpsertTargetWhere==0 ) continue;
       130091  +      if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
       130092  +                             pIdx->pPartIdxWhere, iCursor)!=0 ){
       130093  +        continue;
       130094  +      }
       130095  +    }
       130096  +    nn = pIdx->nKeyCol;
       130097  +    for(ii=0; ii<nn; ii++){
       130098  +      Expr *pExpr;
       130099  +      sCol[0].u.zToken = (char*)pIdx->azColl[ii];
       130100  +      if( pIdx->aiColumn[ii]==XN_EXPR ){
       130101  +        assert( pIdx->aColExpr!=0 );
       130102  +        assert( pIdx->aColExpr->nExpr>ii );
       130103  +        pExpr = pIdx->aColExpr->a[ii].pExpr;
       130104  +        if( pExpr->op!=TK_COLLATE ){
       130105  +          sCol[0].pLeft = pExpr;
       130106  +          pExpr = &sCol[0];
       130107  +        }
       130108  +      }else{
       130109  +        sCol[0].pLeft = &sCol[1];
       130110  +        sCol[1].iColumn = pIdx->aiColumn[ii];
       130111  +        pExpr = &sCol[0];
       130112  +      }
       130113  +      for(jj=0; jj<nn; jj++){
       130114  +        if( sqlite3ExprCompare(pParse, pTarget->a[jj].pExpr, pExpr,iCursor)<2 ){
       130115  +          break;  /* Column ii of the index matches column jj of target */
       130116  +        }
       130117  +      }
       130118  +      if( jj>=nn ){
       130119  +        /* The target contains no match for column jj of the index */
       130120  +        break;
       130121  +      }
       130122  +    }
       130123  +    if( ii<nn ){
       130124  +      /* Column ii of the index did not match any term of the conflict target.
       130125  +      ** Continue the search with the next index. */
       130126  +      continue;
       130127  +    }
       130128  +    pUpsert->pUpsertIdx = pIdx;
       130129  +    return SQLITE_OK;
       130130  +  }
       130131  +  sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
       130132  +                          "PRIMARY KEY or UNIQUE constraint");
       130133  +  return SQLITE_ERROR;
       130134  +}
       130135  +
       130136  +/*
       130137  +** Generate bytecode that does an UPDATE as part of an upsert.
       130138  +**
       130139  +** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
       130140  +** In this case parameter iCur is a cursor open on the table b-tree that
       130141  +** currently points to the conflicting table row. Otherwise, if pIdx
       130142  +** is not NULL, then pIdx is the constraint that failed and iCur is a
       130143  +** cursor points to the conflicting row.
       130144  +*/
       130145  +SQLITE_PRIVATE void sqlite3UpsertDoUpdate(
       130146  +  Parse *pParse,        /* The parsing and code-generating context */
       130147  +  Upsert *pUpsert,      /* The ON CONFLICT clause for the upsert */
       130148  +  Table *pTab,          /* The table being updated */
       130149  +  Index *pIdx,          /* The UNIQUE constraint that failed */
       130150  +  int iCur              /* Cursor for pIdx (or pTab if pIdx==NULL) */
       130151  +){
       130152  +  Vdbe *v = pParse->pVdbe;
       130153  +  sqlite3 *db = pParse->db;
       130154  +  SrcList *pSrc;            /* FROM clause for the UPDATE */
       130155  +  int iDataCur = pUpsert->iDataCur;
       130156  +
       130157  +  assert( v!=0 );
       130158  +  VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
       130159  +  if( pIdx && iCur!=iDataCur ){
       130160  +    if( HasRowid(pTab) ){
       130161  +      int regRowid = sqlite3GetTempReg(pParse);
       130162  +      sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regRowid);
       130163  +      sqlite3VdbeAddOp3(v, OP_SeekRowid, iDataCur, 0, regRowid);
       130164  +      VdbeCoverage(v);
       130165  +      sqlite3ReleaseTempReg(pParse, regRowid);
       130166  +    }else{
       130167  +      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
       130168  +      int nPk = pPk->nKeyCol;
       130169  +      int iPk = pParse->nMem+1;
       130170  +      int i;
       130171  +      pParse->nMem += nPk;
       130172  +      for(i=0; i<nPk; i++){
       130173  +        int k;
       130174  +        assert( pPk->aiColumn[i]>=0 );
       130175  +        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
       130176  +        sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
       130177  +        VdbeComment((v, "%s.%s", pIdx->zName,
       130178  +                    pTab->aCol[pPk->aiColumn[i]].zName));
       130179  +      }
       130180  +      sqlite3VdbeVerifyAbortable(v, OE_Abort);
       130181  +      i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
       130182  +      VdbeCoverage(v);
       130183  +      sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0, 
       130184  +            "corrupt database", P4_STATIC);
       130185  +      sqlite3VdbeJumpHere(v, i);
       130186  +    }
       130187  +  }
       130188  +  /* pUpsert does not own pUpsertSrc - the outer INSERT statement does.  So
       130189  +  ** we have to make a copy before passing it down into sqlite3Update() */
       130190  +  pSrc = sqlite3SrcListDup(db, pUpsert->pUpsertSrc, 0);
       130191  +  sqlite3Update(pParse, pSrc, pUpsert->pUpsertSet,
       130192  +      pUpsert->pUpsertWhere, OE_Abort, 0, 0, pUpsert);
       130193  +  pUpsert->pUpsertSet = 0;    /* Will have been deleted by sqlite3Update() */
       130194  +  pUpsert->pUpsertWhere = 0;  /* Will have been deleted by sqlite3Update() */
       130195  +  VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
       130196  +}
       130197  +
       130198  +#endif /* SQLITE_OMIT_UPSERT */
       130199  +
       130200  +/************** End of upsert.c **********************************************/
128622 130201   /************** Begin file vacuum.c ******************************************/
128623 130202   /*
128624 130203   ** 2003 April 6
128625 130204   **
128626 130205   ** The author disclaims copyright to this source code.  In place of
128627 130206   ** a legal notice, here is a blessing:
128628 130207   **
................................................................................
128657 130236   
128658 130237     /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
128659 130238     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
128660 130239     if( rc!=SQLITE_OK ) return rc;
128661 130240     while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
128662 130241       const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
128663 130242       assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
128664         -    assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB );
128665         -    if( zSubSql && zSubSql[0]!='S' ){
       130243  +    /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
       130244  +    ** or INSERT.  Historically there have been attacks that first
       130245  +    ** corrupt the sqlite_master.sql field with other kinds of statements
       130246  +    ** then run VACUUM to get those statements to execute at inappropriate
       130247  +    ** times. */
       130248  +    if( zSubSql
       130249  +     && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
       130250  +    ){
128666 130251         rc = execSql(db, pzErrMsg, zSubSql);
128667 130252         if( rc!=SQLITE_OK ) break;
128668 130253       }
128669 130254     }
128670 130255     assert( rc!=SQLITE_ROW );
128671 130256     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
128672 130257     if( rc ){
................................................................................
128871 130456         " WHERE type='table'AND name<>'sqlite_sequence'"
128872 130457         " AND coalesce(rootpage,1)>0",
128873 130458         zDbMain
128874 130459     );
128875 130460     if( rc!=SQLITE_OK ) goto end_of_vacuum;
128876 130461     rc = execSqlF(db, pzErrMsg,
128877 130462         "SELECT sql FROM \"%w\".sqlite_master"
128878         -      " WHERE type='index' AND length(sql)>10",
       130463  +      " WHERE type='index'",
128879 130464         zDbMain
128880 130465     );
128881 130466     if( rc!=SQLITE_OK ) goto end_of_vacuum;
128882 130467     db->init.iDb = 0;
128883 130468   
128884 130469     /* Loop through the tables in the main database. For each, do
128885 130470     ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
................................................................................
130041 131626     Table *pTab;
130042 131627     sqlite3_vtab *pVtab;
130043 131628     sqlite3_module *pMod;
130044 131629     void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
130045 131630     void *pArg = 0;
130046 131631     FuncDef *pNew;
130047 131632     int rc = 0;
130048         -  char *zLowerName;
130049         -  unsigned char *z;
130050         -
130051 131633   
130052 131634     /* Check to see the left operand is a column in a virtual table */
130053 131635     if( NEVER(pExpr==0) ) return pDef;
130054 131636     if( pExpr->op!=TK_COLUMN ) return pDef;
130055 131637     pTab = pExpr->pTab;
130056 131638     if( pTab==0 ) return pDef;
130057 131639     if( !IsVirtual(pTab) ) return pDef;
................................................................................
130058 131640     pVtab = sqlite3GetVTable(db, pTab)->pVtab;
130059 131641     assert( pVtab!=0 );
130060 131642     assert( pVtab->pModule!=0 );
130061 131643     pMod = (sqlite3_module *)pVtab->pModule;
130062 131644     if( pMod->xFindFunction==0 ) return pDef;
130063 131645    
130064 131646     /* Call the xFindFunction method on the virtual table implementation
130065         -  ** to see if the implementation wants to overload this function 
       131647  +  ** to see if the implementation wants to overload this function.
       131648  +  **
       131649  +  ** Though undocumented, we have historically always invoked xFindFunction
       131650  +  ** with an all lower-case function name.  Continue in this tradition to
       131651  +  ** avoid any chance of an incompatibility.
130066 131652     */
130067         -  zLowerName = sqlite3DbStrDup(db, pDef->zName);
130068         -  if( zLowerName ){
130069         -    for(z=(unsigned char*)zLowerName; *z; z++){
130070         -      *z = sqlite3UpperToLower[*z];
       131653  +#ifdef SQLITE_DEBUG
       131654  +  {
       131655  +    int i;
       131656  +    for(i=0; pDef->zName[i]; i++){
       131657  +      unsigned char x = (unsigned char)pDef->zName[i];
       131658  +      assert( x==sqlite3UpperToLower[x] );
130071 131659       }
130072         -    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg);
130073         -    sqlite3DbFree(db, zLowerName);
130074 131660     }
       131661  +#endif
       131662  +  rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
130075 131663     if( rc==0 ){
130076 131664       return pDef;
130077 131665     }
130078 131666   
130079 131667     /* Create a new ephemeral function definition for the overloaded
130080 131668     ** function */
130081 131669     pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
................................................................................
130727 132315   
130728 132316   /* wherecode.c: */
130729 132317   #ifndef SQLITE_OMIT_EXPLAIN
130730 132318   SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
130731 132319     Parse *pParse,                  /* Parse context */
130732 132320     SrcList *pTabList,              /* Table list this loop refers to */
130733 132321     WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
130734         -  int iLevel,                     /* Value for "level" column of output */
130735         -  int iFrom,                      /* Value for "from" column of output */
130736 132322     u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
130737 132323   );
130738 132324   #else
130739         -# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
       132325  +# define sqlite3WhereExplainOneScan(u,v,w,x) 0
130740 132326   #endif /* SQLITE_OMIT_EXPLAIN */
130741 132327   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
130742 132328   SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
130743 132329     Vdbe *v,                        /* Vdbe to add scanstatus entry to */
130744 132330     SrcList *pSrclist,              /* FROM clause pLvl reads data from */
130745 132331     WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
130746 132332     int addrExplain                 /* Address of OP_Explain (or 0) */
................................................................................
130852 132438     int iTerm,                  /* Zero-based index of first term. */
130853 132439     int bAnd,                   /* Non-zero to append " AND " */
130854 132440     const char *zOp             /* Name of the operator */
130855 132441   ){
130856 132442     int i;
130857 132443   
130858 132444     assert( nTerm>=1 );
130859         -  if( bAnd ) sqlite3StrAccumAppend(pStr, " AND ", 5);
       132445  +  if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
130860 132446   
130861         -  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, "(", 1);
       132447  +  if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
130862 132448     for(i=0; i<nTerm; i++){
130863         -    if( i ) sqlite3StrAccumAppend(pStr, ",", 1);
130864         -    sqlite3StrAccumAppendAll(pStr, explainIndexColumnName(pIdx, iTerm+i));
       132449  +    if( i ) sqlite3_str_append(pStr, ",", 1);
       132450  +    sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
130865 132451     }
130866         -  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, ")", 1);
       132452  +  if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
130867 132453   
130868         -  sqlite3StrAccumAppend(pStr, zOp, 1);
       132454  +  sqlite3_str_append(pStr, zOp, 1);
130869 132455   
130870         -  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, "(", 1);
       132456  +  if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
130871 132457     for(i=0; i<nTerm; i++){
130872         -    if( i ) sqlite3StrAccumAppend(pStr, ",", 1);
130873         -    sqlite3StrAccumAppend(pStr, "?", 1);
       132458  +    if( i ) sqlite3_str_append(pStr, ",", 1);
       132459  +    sqlite3_str_append(pStr, "?", 1);
130874 132460     }
130875         -  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, ")", 1);
       132461  +  if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
130876 132462   }
130877 132463   
130878 132464   /*
130879 132465   ** Argument pLevel describes a strategy for scanning table pTab. This 
130880 132466   ** function appends text to pStr that describes the subset of table
130881 132467   ** rows scanned by the strategy in the form of an SQL expression.
130882 132468   **
................................................................................
130892 132478   static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
130893 132479     Index *pIndex = pLoop->u.btree.pIndex;
130894 132480     u16 nEq = pLoop->u.btree.nEq;
130895 132481     u16 nSkip = pLoop->nSkip;
130896 132482     int i, j;
130897 132483   
130898 132484     if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
130899         -  sqlite3StrAccumAppend(pStr, " (", 2);
       132485  +  sqlite3_str_append(pStr, " (", 2);
130900 132486     for(i=0; i<nEq; i++){
130901 132487       const char *z = explainIndexColumnName(pIndex, i);
130902         -    if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
130903         -    sqlite3XPrintf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
       132488  +    if( i ) sqlite3_str_append(pStr, " AND ", 5);
       132489  +    sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
130904 132490     }
130905 132491   
130906 132492     j = i;
130907 132493     if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
130908 132494       explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
130909 132495       i = 1;
130910 132496     }
130911 132497     if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
130912 132498       explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
130913 132499     }
130914         -  sqlite3StrAccumAppend(pStr, ")", 1);
       132500  +  sqlite3_str_append(pStr, ")", 1);
130915 132501   }
130916 132502   
130917 132503   /*
130918 132504   ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
130919 132505   ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
130920 132506   ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
130921 132507   ** is added to the output to describe the table scan strategy in pLevel.
................................................................................
130923 132509   ** If an OP_Explain opcode is added to the VM, its address is returned.
130924 132510   ** Otherwise, if no OP_Explain is coded, zero is returned.
130925 132511   */
130926 132512   SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
130927 132513     Parse *pParse,                  /* Parse context */
130928 132514     SrcList *pTabList,              /* Table list this loop refers to */
130929 132515     WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
130930         -  int iLevel,                     /* Value for "level" column of output */
130931         -  int iFrom,                      /* Value for "from" column of output */
130932 132516     u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
130933 132517   ){
130934 132518     int ret = 0;
130935 132519   #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
130936 132520     if( sqlite3ParseToplevel(pParse)->explain==2 )
130937 132521   #endif
130938 132522     {
130939 132523       struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
130940 132524       Vdbe *v = pParse->pVdbe;      /* VM being constructed */
130941 132525       sqlite3 *db = pParse->db;     /* Database handle */
130942         -    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
130943 132526       int isSearch;                 /* True for a SEARCH. False for SCAN. */
130944 132527       WhereLoop *pLoop;             /* The controlling WhereLoop object */
130945 132528       u32 flags;                    /* Flags that describe this loop */
130946 132529       char *zMsg;                   /* Text to add to EQP output */
130947 132530       StrAccum str;                 /* EQP output string */
130948 132531       char zBuf[100];               /* Initial space for EQP output string */
130949 132532   
................................................................................
130952 132535       if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
130953 132536   
130954 132537       isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
130955 132538               || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
130956 132539               || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
130957 132540   
130958 132541       sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
130959         -    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
       132542  +    sqlite3_str_appendall(&str, isSearch ? "SEARCH" : "SCAN");
130960 132543       if( pItem->pSelect ){
130961         -      sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId);
       132544  +      sqlite3_str_appendf(&str, " SUBQUERY 0x%p", pItem->pSelect);
130962 132545       }else{
130963         -      sqlite3XPrintf(&str, " TABLE %s", pItem->zName);
       132546  +      sqlite3_str_appendf(&str, " TABLE %s", pItem->zName);
130964 132547       }
130965 132548   
130966 132549       if( pItem->zAlias ){
130967         -      sqlite3XPrintf(&str, " AS %s", pItem->zAlias);
       132550  +      sqlite3_str_appendf(&str, " AS %s", pItem->zAlias);
130968 132551       }
130969 132552       if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
130970 132553         const char *zFmt = 0;
130971 132554         Index *pIdx;
130972 132555   
130973 132556         assert( pLoop->u.btree.pIndex!=0 );
130974 132557         pIdx = pLoop->u.btree.pIndex;
................................................................................
130983 132566           zFmt = "AUTOMATIC COVERING INDEX";
130984 132567         }else if( flags & WHERE_IDX_ONLY ){
130985 132568           zFmt = "COVERING INDEX %s";
130986 132569         }else{
130987 132570           zFmt = "INDEX %s";
130988 132571         }
130989 132572         if( zFmt ){
130990         -        sqlite3StrAccumAppend(&str, " USING ", 7);
130991         -        sqlite3XPrintf(&str, zFmt, pIdx->zName);
       132573  +        sqlite3_str_append(&str, " USING ", 7);
       132574  +        sqlite3_str_appendf(&str, zFmt, pIdx->zName);
130992 132575           explainIndexRange(&str, pLoop);
130993 132576         }
130994 132577       }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
130995 132578         const char *zRangeOp;
130996 132579         if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
130997 132580           zRangeOp = "=";
130998 132581         }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
................................................................................
130999 132582           zRangeOp = ">? AND rowid<";
131000 132583         }else if( flags&WHERE_BTM_LIMIT ){
131001 132584           zRangeOp = ">";
131002 132585         }else{
131003 132586           assert( flags&WHERE_TOP_LIMIT);
131004 132587           zRangeOp = "<";
131005 132588         }
131006         -      sqlite3XPrintf(&str, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
       132589  +      sqlite3_str_appendf(&str, 
       132590  +          " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
131007 132591       }
131008 132592   #ifndef SQLITE_OMIT_VIRTUALTABLE
131009 132593       else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
131010         -      sqlite3XPrintf(&str, " VIRTUAL TABLE INDEX %d:%s",
       132594  +      sqlite3_str_appendf(&str, " VIRTUAL TABLE INDEX %d:%s",
131011 132595                     pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
131012 132596       }
131013 132597   #endif
131014 132598   #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
131015 132599       if( pLoop->nOut>=10 ){
131016         -      sqlite3XPrintf(&str, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
       132600  +      sqlite3_str_appendf(&str, " (~%llu rows)",
       132601  +             sqlite3LogEstToInt(pLoop->nOut));
131017 132602       }else{
131018         -      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
       132603  +      sqlite3_str_append(&str, " (~1 row)", 9);
131019 132604       }
131020 132605   #endif
131021 132606       zMsg = sqlite3StrAccumFinish(&str);
131022         -    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
       132607  +    ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
       132608  +                            pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
131023 132609     }
131024 132610     return ret;
131025 132611   }
131026 132612   #endif /* SQLITE_OMIT_EXPLAIN */
131027 132613   
131028 132614   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
131029 132615   /*
................................................................................
132016 133602     addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
132017 133603     addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
132018 133604   
132019 133605     /* If this is the right table of a LEFT OUTER JOIN, allocate and
132020 133606     ** initialize a memory cell that records if this table matches any
132021 133607     ** row of the left table of the join.
132022 133608     */
       133609  +  assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
       133610  +       || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
       133611  +  );
132023 133612     if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
132024 133613       pLevel->iLeftJoin = ++pParse->nMem;
132025 133614       sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
132026 133615       VdbeComment((v, "init LEFT JOIN no-match flag"));
132027 133616     }
132028 133617   
132029 133618     /* Compute a safe address to jump to if we discover that the table for
................................................................................
132549 134138         sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
132550 134139                              iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
132551 134140       }
132552 134141   
132553 134142       /* If pIdx is an index on one or more expressions, then look through
132554 134143       ** all the expressions in pWInfo and try to transform matching expressions
132555 134144       ** into reference to index columns.
       134145  +    **
       134146  +    ** Do not do this for the RHS of a LEFT JOIN. This is because the 
       134147  +    ** expression may be evaluated after OP_NullRow has been executed on
       134148  +    ** the cursor. In this case it is important to do the full evaluation,
       134149  +    ** as the result of the expression may not be NULL, even if all table
       134150  +    ** column values are.  https://www.sqlite.org/src/info/7fa8049685b50b5a
132556 134151       */
132557         -    whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
132558         -
       134152  +    if( pLevel->iLeftJoin==0 ){
       134153  +      whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
       134154  +    }
132559 134155   
132560 134156       /* Record the instruction used to terminate the loop. */
132561 134157       if( pLoop->wsFlags & WHERE_ONEROW ){
132562 134158         pLevel->op = OP_Noop;
132563 134159       }else if( bRev ){
132564 134160         pLevel->op = OP_Prev;
132565 134161       }else{
................................................................................
132707 134303       ** See ticket http://www.sqlite.org/src/info/f2369304e4
132708 134304       */
132709 134305       if( pWC->nTerm>1 ){
132710 134306         int iTerm;
132711 134307         for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
132712 134308           Expr *pExpr = pWC->a[iTerm].pExpr;
132713 134309           if( &pWC->a[iTerm] == pTerm ) continue;
132714         -        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
132715 134310           testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
132716 134311           testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
132717 134312           if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
132718 134313           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
132719 134314           testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
132720 134315           pExpr = sqlite3ExprDup(db, pExpr, 0);
132721 134316           pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
................................................................................
132726 134321       }
132727 134322   
132728 134323       /* Run a separate WHERE clause for each term of the OR clause.  After
132729 134324       ** eliminating duplicates from other WHERE clauses, the action for each
132730 134325       ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
132731 134326       */
132732 134327       wctrlFlags =  WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
       134328  +    ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
132733 134329       for(ii=0; ii<pOrWc->nTerm; ii++){
132734 134330         WhereTerm *pOrTerm = &pOrWc->a[ii];
132735 134331         if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
132736 134332           WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
132737 134333           Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
132738 134334           int jmp1 = 0;                   /* Address of jump operation */
132739         -        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
       134335  +        assert( (pTabItem[0].fg.jointype & JT_LEFT)==0 
       134336  +             || ExprHasProperty(pOrExpr, EP_FromJoin) 
       134337  +        );
       134338  +        if( pAndExpr ){
132740 134339             pAndExpr->pLeft = pOrExpr;
132741 134340             pOrExpr = pAndExpr;
132742 134341           }
132743 134342           /* Loop through table entries that match term pOrTerm. */
132744 134343           WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
132745 134344           pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
132746 134345                                         wctrlFlags, iCovCur);
132747 134346           assert( pSubWInfo || pParse->nErr || db->mallocFailed );
132748 134347           if( pSubWInfo ){
132749 134348             WhereLoop *pSubLoop;
132750 134349             int addrExplain = sqlite3WhereExplainOneScan(
132751         -              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
       134350  +              pParse, pOrTab, &pSubWInfo->a[0], 0
132752 134351             );
132753 134352             sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
132754 134353   
132755 134354             /* This is the sub-WHERE clause body.  First skip over
132756 134355             ** duplicate rows from prior sub-WHERE clauses, and record the
132757 134356             ** rowid (or PRIMARY KEY) for the current row so that the same
132758 134357             ** row will be skipped in subsequent sub-WHERE clauses.
................................................................................
132843 134442             }
132844 134443   
132845 134444             /* Finish the loop through table entries that match term pOrTerm. */
132846 134445             sqlite3WhereEnd(pSubWInfo);
132847 134446           }
132848 134447         }
132849 134448       }
       134449  +    ExplainQueryPlanPop(pParse);
132850 134450       pLevel->u.pCovidx = pCov;
132851 134451       if( pCov ) pLevel->iIdxCur = iCovCur;
132852 134452       if( pAndExpr ){
132853 134453         pAndExpr->pLeft = 0;
132854 134454         sqlite3ExprDelete(db, pAndExpr);
132855 134455       }
132856 134456       sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
................................................................................
132915 134515           testcase( pWInfo->untestedTerms==0
132916 134516               && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
132917 134517           pWInfo->untestedTerms = 1;
132918 134518           continue;
132919 134519         }
132920 134520         pE = pTerm->pExpr;
132921 134521         assert( pE!=0 );
132922         -      if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
       134522  +      if( (pTabItem->fg.jointype&JT_LEFT) && !ExprHasProperty(pE,EP_FromJoin) ){
132923 134523           continue;
132924 134524         }
132925 134525         
132926 134526         if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
132927 134527           iNext = 2;
132928 134528           continue;
132929 134529         }
................................................................................
133842 135442           testcase( idxNew==0 );
133843 135443           exprAnalyze(pSrc, pWC, idxNew);
133844 135444           pTerm = &pWC->a[idxTerm];
133845 135445           markTermAsChild(pWC, idxNew, idxTerm);
133846 135446         }else{
133847 135447           sqlite3ExprListDelete(db, pList);
133848 135448         }
133849         -      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 3 */
133850 135449       }
133851 135450     }
133852 135451   }
133853 135452   #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
133854 135453   
133855 135454   /*
133856 135455   ** We already know that pExpr is a binary operator where both operands are
................................................................................
136979 138578       }
136980 138579       if( pTerm->prereqRight & pNew->maskSelf ) continue;
136981 138580   
136982 138581       /* Do not allow the upper bound of a LIKE optimization range constraint
136983 138582       ** to mix with a lower range bound from some other source */
136984 138583       if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
136985 138584   
136986         -    /* Do not allow IS constraints from the WHERE clause to be used by the
       138585  +    /* Do not allow constraints from the WHERE clause to be used by the
136987 138586       ** right table of a LEFT JOIN.  Only constraints in the ON clause are
136988 138587       ** allowed */
136989 138588       if( (pSrc->fg.jointype & JT_LEFT)!=0
136990 138589        && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
136991         -     && (eOp & (WO_IS|WO_ISNULL))!=0
136992 138590       ){
136993         -      testcase( eOp & WO_IS );
136994         -      testcase( eOp & WO_ISNULL );
136995 138591         continue;
136996 138592       }
136997 138593   
136998 138594       if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
136999 138595         pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE;
137000 138596       }else{
137001 138597         pBuilder->bldFlags |= SQLITE_BLDF_INDEXED;
................................................................................
137424 139020           pNew->nSkip = 0;
137425 139021           pNew->u.btree.pIndex = 0;
137426 139022           pNew->nLTerm = 1;
137427 139023           pNew->aLTerm[0] = pTerm;
137428 139024           /* TUNING: One-time cost for computing the automatic index is
137429 139025           ** estimated to be X*N*log2(N) where N is the number of rows in
137430 139026           ** the table being indexed and where X is 7 (LogEst=28) for normal
137431         -        ** tables or 1.375 (LogEst=4) for views and subqueries.  The value
       139027  +        ** tables or 0.5 (LogEst=-10) for views and subqueries.  The value
137432 139028           ** of X is smaller for views and subqueries so that the query planner
137433 139029           ** will be more aggressive about generating automatic indexes for
137434 139030           ** those objects, since there is no opportunity to add schema
137435 139031           ** indexes on subqueries and views. */
137436         -        pNew->rSetup = rLogSize + rSize + 4;
       139032  +        pNew->rSetup = rLogSize + rSize;
137437 139033           if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
137438         -          pNew->rSetup += 24;
       139034  +          pNew->rSetup += 28;
       139035  +        }else{
       139036  +          pNew->rSetup -= 10;
137439 139037           }
137440 139038           ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
137441 139039           if( pNew->rSetup<0 ) pNew->rSetup = 0;
137442 139040           /* TUNING: Each index lookup yields 20 rows in the table.  This
137443 139041           ** is more than the usual guess of 10 rows, since we have no way
137444 139042           ** of knowing how selective the index will ultimately be.  It would
137445 139043           ** not be unreasonable to make this value much larger. */
................................................................................
138567 140165           LogEst rUnsorted;                 /* Unsorted cost of (pFrom+pWLoop) */
138568 140166           i8 isOrdered = pFrom->isOrdered;  /* isOrdered for (pFrom+pWLoop) */
138569 140167           Bitmask maskNew;                  /* Mask of src visited by (..) */
138570 140168           Bitmask revMask = 0;              /* Mask of rev-order loops for (..) */
138571 140169   
138572 140170           if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
138573 140171           if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
138574         -        if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){
       140172  +        if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
138575 140173             /* Do not use an automatic index if the this loop is expected
138576         -          ** to run less than 2 times. */
       140174  +          ** to run less than 1.25 times.  It is tempting to also exclude
       140175  +          ** automatic index usage on an outer loop, but sometimes an automatic
       140176  +          ** index is useful in the outer loop of a correlated subquery. */
138577 140177             assert( 10==sqlite3LogEst(2) );
138578 140178             continue;
138579 140179           }
       140180  +
138580 140181           /* At this point, pWLoop is a candidate to be the next loop. 
138581 140182           ** Compute its cost */
138582 140183           rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
138583 140184           rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
138584 140185           nOut = pFrom->nRow + pWLoop->nOut;
138585 140186           maskNew = pFrom->maskLoop | pWLoop->maskSelf;
138586 140187           if( isOrdered<0 ){
................................................................................
139154 140755     /* Special case: No FROM clause
139155 140756     */
139156 140757     if( nTabList==0 ){
139157 140758       if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
139158 140759       if( wctrlFlags & WHERE_WANT_DISTINCT ){
139159 140760         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
139160 140761       }
       140762  +    ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
139161 140763     }else{
139162 140764       /* Assign a bit from the bitmask to every term in the FROM clause.
139163 140765       **
139164 140766       ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
139165 140767       **
139166 140768       ** The rule of the previous sentence ensures thta if X is the bitmask for
139167 140769       ** a table T, then X-1 is the bitmask for all other tables to the left of T.
................................................................................
139549 141151       if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
139550 141152         constructAutomaticIndex(pParse, &pWInfo->sWC,
139551 141153                   &pTabList->a[pLevel->iFrom], notReady, pLevel);
139552 141154         if( db->mallocFailed ) goto whereBeginError;
139553 141155       }
139554 141156   #endif
139555 141157       addrExplain = sqlite3WhereExplainOneScan(
139556         -        pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
       141158  +        pParse, pTabList, pLevel, wctrlFlags
139557 141159       );
139558 141160       pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
139559 141161       notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
139560 141162       pWInfo->iContinue = pLevel->addrCont;
139561 141163       if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
139562 141164         sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
139563 141165       }
................................................................................
139981 141583   **                       This is typically a union of many types, one of
139982 141584   **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
139983 141585   **                       for terminal symbols is called "yy0".
139984 141586   **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
139985 141587   **                       zero the stack is dynamically sized using realloc()
139986 141588   **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
139987 141589   **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
       141590  +**    sqlite3ParserARG_PARAM     Code to pass %extra_argument as a subroutine parameter
139988 141591   **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
139989 141592   **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
       141593  +**    sqlite3ParserCTX_*         As sqlite3ParserARG_ except for %extra_context
139990 141594   **    YYERRORSYMBOL      is the code number of the error symbol.  If not
139991 141595   **                       defined, then do no error processing.
139992 141596   **    YYNSTATE           the combined number of states.
139993 141597   **    YYNRULE            the number of rules in the grammar
139994 141598   **    YYNTOKEN           Number of terminal symbols
139995 141599   **    YY_MAX_SHIFT       Maximum value for shift actions
139996 141600   **    YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
................................................................................
140002 141606   **    YY_MAX_REDUCE      Maximum value for reduce actions
140003 141607   */
140004 141608   #ifndef INTERFACE
140005 141609   # define INTERFACE 1
140006 141610   #endif
140007 141611   /************* Begin control #defines *****************************************/
140008 141612   #define YYCODETYPE unsigned char
140009         -#define YYNOCODE 253
       141613  +#define YYNOCODE 255
140010 141614   #define YYACTIONTYPE unsigned short int
140011         -#define YYWILDCARD 83
       141615  +#define YYWILDCARD 84
140012 141616   #define sqlite3ParserTOKENTYPE Token
140013 141617   typedef union {
140014 141618     int yyinit;
140015 141619     sqlite3ParserTOKENTYPE yy0;
140016         -  int yy4;
140017         -  struct TrigEvent yy90;
140018         -  TriggerStep* yy203;
140019         -  struct {int value; int mask;} yy215;
140020         -  SrcList* yy259;
140021         -  Expr* yy314;
140022         -  ExprList* yy322;
140023         -  const char* yy336;
140024         -  IdList* yy384;
140025         -  Select* yy387;
140026         -  With* yy451;
       141620  +  const char* yy36;
       141621  +  TriggerStep* yy47;
       141622  +  With* yy91;
       141623  +  struct {int value; int mask;} yy107;
       141624  +  Expr* yy182;
       141625  +  Upsert* yy198;
       141626  +  ExprList* yy232;
       141627  +  struct TrigEvent yy300;
       141628  +  Select* yy399;
       141629  +  SrcList* yy427;
       141630  +  int yy502;
       141631  +  IdList* yy510;
140027 141632   } YYMINORTYPE;
140028 141633   #ifndef YYSTACKDEPTH
140029 141634   #define YYSTACKDEPTH 100
140030 141635   #endif
140031         -#define sqlite3ParserARG_SDECL Parse *pParse;
140032         -#define sqlite3ParserARG_PDECL ,Parse *pParse
140033         -#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
140034         -#define sqlite3ParserARG_STORE yypParser->pParse = pParse
       141636  +#define sqlite3ParserARG_SDECL
       141637  +#define sqlite3ParserARG_PDECL
       141638  +#define sqlite3ParserARG_PARAM
       141639  +#define sqlite3ParserARG_FETCH
       141640  +#define sqlite3ParserARG_STORE
       141641  +#define sqlite3ParserCTX_SDECL Parse *pParse;
       141642  +#define sqlite3ParserCTX_PDECL ,Parse *pParse
       141643  +#define sqlite3ParserCTX_PARAM ,pParse
       141644  +#define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
       141645  +#define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
140035 141646   #define YYFALLBACK 1
140036         -#define YYNSTATE             472
140037         -#define YYNRULE              333
140038         -#define YYNTOKEN             143
140039         -#define YY_MAX_SHIFT         471
140040         -#define YY_MIN_SHIFTREDUCE   681
140041         -#define YY_MAX_SHIFTREDUCE   1013
140042         -#define YY_ERROR_ACTION      1014
140043         -#define YY_ACCEPT_ACTION     1015
140044         -#define YY_NO_ACTION         1016
140045         -#define YY_MIN_REDUCE        1017
140046         -#define YY_MAX_REDUCE        1349
       141647  +#define YYNSTATE             490
       141648  +#define YYNRULE              341
       141649  +#define YYNTOKEN             145
       141650  +#define YY_MAX_SHIFT         489
       141651  +#define YY_MIN_SHIFTREDUCE   705
       141652  +#define YY_MAX_SHIFTREDUCE   1045
       141653  +#define YY_ERROR_ACTION      1046
       141654  +#define YY_ACCEPT_ACTION     1047
       141655  +#define YY_NO_ACTION         1048
       141656  +#define YY_MIN_REDUCE        1049
       141657  +#define YY_MAX_REDUCE        1389
140047 141658   /************* End control #defines *******************************************/
140048 141659   
140049 141660   /* Define the yytestcase() macro to be a no-op if is not already defined
140050 141661   ** otherwise.
140051 141662   **
140052 141663   ** Applications can choose to define yytestcase() in the %include section
140053 141664   ** to a macro that can assist in verifying code coverage.  For production
................................................................................
140105 141716   **  yy_shift_ofst[]    For each state, the offset into yy_action for
140106 141717   **                     shifting terminals.
140107 141718   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
140108 141719   **                     shifting non-terminals after a reduce.
140109 141720   **  yy_default[]       Default action for each state.
140110 141721   **
140111 141722   *********** Begin parsing tables **********************************************/
140112         -#define YY_ACTTAB_COUNT (1566)
       141723  +#define YY_ACTTAB_COUNT (1657)
140113 141724   static const YYACTIONTYPE yy_action[] = {
140114         - /*     0 */  1169, 1015,  167,  167,    1,  168,  466, 1313,  466, 1083,
140115         - /*    10 */  1062,  466,   97,   94,  183, 1057,  466,  329, 1083,  342,
140116         - /*    20 */    97,   94,  183,  459,  459,  459,  436,   57,   57,   57,
140117         - /*    30 */    57,  807,   57,   57,  367,  367,  367,   57,   57,  808,
140118         - /*    40 */  1270, 1088, 1088,  104,  105,   95,  991,  991,  868,  871,
140119         - /*    50 */   860,  860,  102,  102,  103,  103,  103,  103,  233,  233,
140120         - /*    60 */   326, 1011,  449,  437,  449,  446,  351,  449,  461, 1142,
140121         - /*    70 */   463,  342,  449,  426, 1316,  209,  180,  742,   80,  299,
140122         - /*    80 */   857,  857,  869,  872,  101,  101,  101,  101,  100,  100,
140123         - /*    90 */    99,   99,   99,   98,  368,  104,  105,   95,  991,  991,
140124         - /*   100 */   868,  871,  860,  860,  102,  102,  103,  103,  103,  103,
140125         - /*   110 */    99,   99,   99,   98,  368,  355,   97,   94,  183,  228,
140126         - /*   120 */   106, 1012,  407,  342,  101,  101,  101,  101,  100,  100,
140127         - /*   130 */    99,   99,   99,   98,  368,  861,  101,  101,  101,  101,
140128         - /*   140 */   100,  100,   99,   99,   99,   98,  368,  104,  105,   95,
140129         - /*   150 */   991,  991,  868,  871,  860,  860,  102,  102,  103,  103,
140130         - /*   160 */   103,  103,  201,  368,  375,  420,  417,  416,  387,  273,
140131         - /*   170 */    65,   97,   94,  183,  168,  342,  415,  951, 1343,  396,
140132         - /*   180 */    66, 1343,  320,  959,  371,  970,  334,  340,  101,  101,
140133         - /*   190 */   101,  101,  100,  100,   99,   99,   99,   98,  368,  104,
140134         - /*   200 */   105,   95,  991,  991,  868,  871,  860,  860,  102,  102,
140135         - /*   210 */   103,  103,  103,  103,  373,  100,  100,   99,   99,   99,
140136         - /*   220 */    98,  368,  970,  971,  972,  201, 1100,  342,  420,  417,
140137         - /*   230 */   416,  287,  366,  365,  337,  970, 1162,  463,  949,  415,
140138         - /*   240 */   101,  101,  101,  101,  100,  100,   99,   99,   99,   98,
140139         - /*   250 */   368,  104,  105,   95,  991,  991,  868,  871,  860,  860,
140140         - /*   260 */   102,  102,  103,  103,  103,  103,  777,  241,  233,  233,
140141         - /*   270 */     9,  847,  970,  971,  972,  390,  998, 1141,  998,  342,
140142         - /*   280 */   463,  252,  829,  719,   98,  368,  840,  298,  338,  142,
140143         - /*   290 */   839,  339,  101,  101,  101,  101,  100,  100,   99,   99,
140144         - /*   300 */    99,   98,  368,  104,  105,   95,  991,  991,  868,  871,
140145         - /*   310 */   860,  860,  102,  102,  103,  103,  103,  103,  272,  466,
140146         - /*   320 */   392,  839,  839,  841,   97,   94,  183,  390, 1317,  253,
140147         - /*   330 */   456,  342,  125,  166,  807,  712,  208,  407,  386,  970,
140148         - /*   340 */    57,   57,  808,  238,  101,  101,  101,  101,  100,  100,
140149         - /*   350 */    99,   99,   99,   98,  368,  104,  105,   95,  991,  991,
140150         - /*   360 */   868,  871,  860,  860,  102,  102,  103,  103,  103,  103,
140151         - /*   370 */   466,  108,  466,  267,  465,  442,  970,  971,  972,  261,
140152         - /*   380 */   951, 1344,  909,  342, 1344,  142,  829,  848, 1292,  959,
140153         - /*   390 */   371,   55,   55,   57,   57,  242,  101,  101,  101,  101,
140154         - /*   400 */   100,  100,   99,   99,   99,   98,  368,  104,  105,   95,
140155         - /*   410 */   991,  991,  868,  871,  860,  860,  102,  102,  103,  103,
140156         - /*   420 */   103,  103,  272,  382,  262,  253,  456,  310,  364,  253,
140157         - /*   430 */   456,   86,  264,   84,  266,  342,  441,  176,  175,  834,
140158         - /*   440 */   464,  949,  767,  767,  332,  313, 1094,  396,  101,  101,
140159         - /*   450 */   101,  101,  100,  100,   99,   99,   99,   98,  368,  104,
140160         - /*   460 */   105,   95,  991,  991,  868,  871,  860,  860,  102,  102,
140161         - /*   470 */   103,  103,  103,  103,  227,  227,  233,  233,  233,  233,
140162         - /*   480 */   387,  273,  234,  234,  326,  950,  463,  342,  463,  298,
140163         - /*   490 */   463,  914,  914,  404,  463, 1037,  123,  265,   27,  970,
140164         - /*   500 */   101,  101,  101,  101,  100,  100,   99,   99,   99,   98,
140165         - /*   510 */   368,  104,  105,   95,  991,  991,  868,  871,  860,  860,
140166         - /*   520 */   102,  102,  103,  103,  103,  103,  435,  233,  233,  466,
140167         - /*   530 */   285,  686,  687,  688,  127,  271,  970,  971,  972,  463,
140168         - /*   540 */  1345,  327,  342,  407,  157, 1012,  988,   13,   13,  181,
140169         - /*   550 */    41,   41,  101,  101,  101,  101,  100,  100,   99,   99,
140170         - /*   560 */    99,   98,  368,  715,  794,  378,  104,  105,   95,  991,
140171         - /*   570 */   991,  868,  871,  860,  860,  102,  102,  103,  103,  103,
140172         - /*   580 */   103,  970,  378,  377,  346,  239,  847, 1086, 1086,  280,
140173         - /*   590 */  1169,  283,  204,  203,  202,  177,  298,  342,  407,  298,
140174         - /*   600 */   715,  840,  169,  299,  407,  839,   82,  101,  101,  101,
140175         - /*   610 */   101,  100,  100,   99,   99,   99,   98,  368,  970,  971,
140176         - /*   620 */   972,  104,  105,   95,  991,  991,  868,  871,  860,  860,
140177         - /*   630 */   102,  102,  103,  103,  103,  103,  839,  839,  841,  362,
140178         - /*   640 */   240,  124, 1169,  172,  126,  378, 1269, 1169, 1066,  342,
140179         - /*   650 */   253,  456,  407,  407,  407,  396,  352,  401,  407,  429,
140180         - /*   660 */   398,   85,  101,  101,  101,  101,  100,  100,   99,   99,
140181         - /*   670 */    99,   98,  368,  104,  105,   95,  991,  991,  868,  871,
140182         - /*   680 */   860,  860,  102,  102,  103,  103,  103,  103, 1169,  466,
140183         - /*   690 */   230,  233,  233,  792, 1235, 1095, 1091, 1293,    1,   77,
140184         - /*   700 */   278,  342,  205,  463,  974,  911, 1040,  348,  353,  911,
140185         - /*   710 */    42,   42,   79,  403,  101,  101,  101,  101,  100,  100,
140186         - /*   720 */    99,   99,   99,   98,  368,  104,   93,   95,  991,  991,
140187         - /*   730 */   868,  871,  860,  860,  102,  102,  103,  103,  103,  103,
140188         - /*   740 */   402,    9,  974,  243,  772,  458,  348,  232,  180,  771,
140189         - /*   750 */   946,  312,  342,  328,  363,  349,  143,  831,  389, 1278,
140190         - /*   760 */   211,  211,   21,  347,  432,  182,  101,  101,  101,  101,
140191         - /*   770 */   100,  100,   99,   99,   99,   98,  368,  105,   95,  991,
140192         - /*   780 */   991,  868,  871,  860,  860,  102,  102,  103,  103,  103,
140193         - /*   790 */   103,  792,  724,   22,  732,  731,  233,  233, 1239,  256,
140194         - /*   800 */   391,  274,  342,  211,   79,  360,  257,  413,  463,  397,
140195         - /*   810 */   207,  288,  260,  450,   79, 1239, 1241,  101,  101,  101,
140196         - /*   820 */   101,  100,  100,   99,   99,   99,   98,  368,   95,  991,
140197         - /*   830 */   991,  868,  871,  860,  860,  102,  102,  103,  103,  103,
140198         - /*   840 */   103,   91,  457,  296,    3,  233,  233,    5,  438,  212,
140199         - /*   850 */   331,  394,  739,  740,  295,  898,  894,  463,  460,  207,
140200         - /*   860 */   801, 1237,  722,  211,  698,  843, 1283,  101,  101,  101,
140201         - /*   870 */   101,  100,  100,   99,   99,   99,   98,  368, 1239,  380,
140202         - /*   880 */   357,  369,  233,  233,  989,  219,  236,  297,  423,  292,
140203         - /*   890 */   422,  206,  454,  898,  463,  970,   91,  457,  290,    3,
140204         - /*   900 */   722,  142,  268,  843,  847,  466, 1258,  149,  388,  425,
140205         - /*   910 */    88,   89,  769,  460,  930,   87,  447,   90,  369,  468,
140206         - /*   920 */   467,  385,  989,  839, 1257,  439,   57,   57,  395,  931,
140207         - /*   930 */  1065,  158,  970,  971,  972,  772,  369,  471, 1019,  399,
140208         - /*   940 */   771,  253,  456,  254,  932,  119,  891,  454,  233,  233,
140209         - /*   950 */     4,  970, 1096,  275,  839,  839,  841,  842,   19,  847,
140210         - /*   960 */   463,  449,  448,  163,  453,   88,   89,  776,  970, 1127,
140211         - /*   970 */   279,  930,   90,  369,  468,  467,   91,  457,  839,    3,
140212         - /*   980 */   235, 1064,  466, 1228,  233,  233,  931,  970,  970,  971,
140213         - /*   990 */   972,  970,  908,  460,  908,    2,  463,   81,  457,  212,
140214         - /*  1000 */     3,  932,  282,   10,   10,  970,  971,  972,  189,  839,
140215         - /*  1010 */   839,  841,  842,   19,  460,  284,  369,  354,  907,  286,
140216         - /*  1020 */   907,  753,  466, 1079,  970,  971,  972,  454,  970,  971,
140217         - /*  1030 */   972,  754,  970, 1063,  989,  372,  792,  369, 1118,  847,
140218         - /*  1040 */   291,  452,  466,   10,   10,   88,   89,  142,  454,  168,
140219         - /*  1050 */   300,  412,   90,  369,  468,  467,  793,  356,  839,  706,
140220         - /*  1060 */   847,  341,  121,   10,   10,  301,   88,   89,  379,  970,
140221         - /*  1070 */   971,  972,  989,   90,  369,  468,  467,  244,  205,  839,
140222         - /*  1080 */  1306,  245, 1135,  245,  250, 1168, 1114,  253,  456,  839,
140223         - /*  1090 */   839,  841,  842,   19, 1125,  237,  122,  451, 1174,  733,
140224         - /*  1100 */   324,  324,  323,  222,  321,  466, 1046,  695,  182,  225,
140225         - /*  1110 */   839,  839,  841,  842,   19,  103,  103,  103,  103,   96,
140226         - /*  1120 */   185,  466,  259, 1039, 1028,  170,   10,   10, 1027,  421,
140227         - /*  1130 */   258, 1029, 1300,  708,  792,  466,  408,  734,    8,  347,
140228         - /*  1140 */   444,  174,   12,   12,  290,  101,  101,  101,  101,  100,
140229         - /*  1150 */   100,   99,   99,   99,   98,  368,   32,   32,  466,  187,
140230         - /*  1160 */   466, 1111,  103,  103,  103,  103,  188,  466,  325,  138,
140231         - /*  1170 */   186,  708,  303,  305,  307,  358,  970,  270,  393,   43,
140232         - /*  1180 */    43,   44,   44, 1157,  333,  178,  418,  294,   45,   45,
140233         - /*  1190 */  1232,  318,  101,  101,  101,  101,  100,  100,   99,   99,
140234         - /*  1200 */    99,   98,  368,  381,  343,  366,  365,  466,  263,  253,
140235         - /*  1210 */   456,  466, 1062,  970,  971,  972, 1231,  997,  309,  466,
140236         - /*  1220 */   455,  466,  427,  466,  995,  173,  996, 1303,   46,   46,
140237         - /*  1230 */   145,  376,   37,   37, 1006, 1277,  466,  214, 1275,   64,
140238         - /*  1240 */    47,   47,   33,   33,   34,   34, 1003,   67,  466,  998,
140239         - /*  1250 */   350,  998,  466,  155,  233,  233,  466,   36,   36,   24,
140240         - /*  1260 */   140,   77, 1154,  466,  383,  466,  463,  428,  466,   48,
140241         - /*  1270 */    48,  466,  147,   49,   49,  466,  150,   50,   50,  466,
140242         - /*  1280 */   151,  152,  466,  384,   11,   11,   51,   51,  466,  110,
140243         - /*  1290 */   110,  153,   52,   52,  411,  466,   38,   38,  466,  191,
140244         - /*  1300 */    53,   53,  466,   54,   54,  466,  400,  466,  330,   39,
140245         - /*  1310 */    39,  466, 1164,  466,   25,  466,   56,   56,  466,  131,
140246         - /*  1320 */   131,   72,  466,  132,  132,  159,  133,  133,   61,   61,
140247         - /*  1330 */  1226,  195,   40,   40,  111,  111,   58,   58,  406,  112,
140248         - /*  1340 */   112,  466,  277,  113,  113,  466,  226,  466, 1246,  466,
140249         - /*  1350 */   197,  466,  164,  466,  409,  466,  198,  466,  199,  466,
140250         - /*  1360 */   335,  281,  109,  109,  466, 1030,  130,  130,  129,  129,
140251         - /*  1370 */   117,  117,  116,  116,  114,  114,  115,  115,   60,   60,
140252         - /*  1380 */    62,   62,  466,  359,  466,   59,   59,  424, 1082, 1081,
140253         - /*  1390 */  1080,  724, 1073, 1054,  336,  293, 1053, 1052, 1315,  431,
140254         - /*  1400 */   361,   76,  248,   31,   31,   35,   35, 1072,  249,  440,
140255         - /*  1410 */   302,  434,  213, 1122,    6,  311, 1212,  107,   83,  251,
140256         - /*  1420 */    78, 1123,  445,  220,  443, 1036,  304,   23, 1121,  469,
140257         - /*  1430 */   965,  221,  223, 1104,  314,  224,  344,  317,  315,  316,
140258         - /*  1440 */   470,  306, 1025, 1120,  308, 1262, 1020,  134,  120,  246,
140259         - /*  1450 */   682,  370,  171,  255, 1263,  135,  184, 1261, 1260,  374,
140260         - /*  1460 */   118,  906,  904,  827, 1050,  146,  136,  137,  148, 1049,
140261         - /*  1470 */    63, 1047,  756,  190,  269,  920,  154,  156,   68,   69,
140262         - /*  1480 */    70,   71,  139,  923,  192,  193,  144,  919,  345,  128,
140263         - /*  1490 */    14,  194,  276,  211, 1000,  405,  196,  161,  912,  160,
140264         - /*  1500 */    26,  697,  410,  295,  200,  289,  414,  162,  419,   73,
140265         - /*  1510 */    15,   16,  141,   74,   28,  247,  846,  845,  735,  874,
140266         - /*  1520 */   954,   75,  430,  955,   29,  433,  179,  229,  231,  800,
140267         - /*  1530 */   165,  795,   87,  210,  889,   79,  875,   17,  873,  877,
140268         - /*  1540 */   929,   18,  928,  216,  215,  878,   20,   30,  462,  844,
140269         - /*  1550 */   707,   92,  766,  770,    7,  322,  217,  218,  319, 1308,
140270         - /*  1560 */   960, 1016, 1016, 1016, 1016, 1307,
       141725  + /*     0 */   349,   99,   96,  185,   99,   96,  185,  233, 1047,    1,
       141726  + /*    10 */     1,  489,    2, 1051,  484,  477,  477,  477,  260,  351,
       141727  + /*    20 */   121, 1310, 1120, 1120, 1178, 1115, 1094, 1128,  380,  380,
       141728  + /*    30 */   380,  835,  454,  410, 1115,   59,   59, 1357,  425,  836,
       141729  + /*    40 */   710,  711,  712,  106,  107,   97, 1023, 1023,  900,  903,
       141730  + /*    50 */   892,  892,  104,  104,  105,  105,  105,  105,  346,  238,
       141731  + /*    60 */   238,   99,   96,  185,  238,  238,  889,  889,  901,  904,
       141732  + /*    70 */   460,  481,  351,   99,   96,  185,  481,  347, 1177,   82,
       141733  + /*    80 */   388,  214,  182,   23,  194,  103,  103,  103,  103,  102,
       141734  + /*    90 */   102,  101,  101,  101,  100,  381,  106,  107,   97, 1023,
       141735  + /*   100 */  1023,  900,  903,  892,  892,  104,  104,  105,  105,  105,
       141736  + /*   110 */   105,   10,  385,  484,   24,  484, 1333,  489,    2, 1051,
       141737  + /*   120 */   335, 1043,  108,  893,  260,  351,  121,   99,   96,  185,
       141738  + /*   130 */   100,  381,  386, 1128,   59,   59,   59,   59,  103,  103,
       141739  + /*   140 */   103,  103,  102,  102,  101,  101,  101,  100,  381,  106,
       141740  + /*   150 */   107,   97, 1023, 1023,  900,  903,  892,  892,  104,  104,
       141741  + /*   160 */   105,  105,  105,  105,  360,  238,  238,  170,  170,  467,
       141742  + /*   170 */   455,  467,  464,   67,  381,  329,  169,  481,  351,  343,
       141743  + /*   180 */   338,  400, 1044,   68,  101,  101,  101,  100,  381,  393,
       141744  + /*   190 */   194,  103,  103,  103,  103,  102,  102,  101,  101,  101,
       141745  + /*   200 */   100,  381,  106,  107,   97, 1023, 1023,  900,  903,  892,
       141746  + /*   210 */   892,  104,  104,  105,  105,  105,  105,  483,  385,  103,
       141747  + /*   220 */   103,  103,  103,  102,  102,  101,  101,  101,  100,  381,
       141748  + /*   230 */   268,  351,  946,  946,  422,  296,  102,  102,  101,  101,
       141749  + /*   240 */   101,  100,  381,  861,  103,  103,  103,  103,  102,  102,
       141750  + /*   250 */   101,  101,  101,  100,  381,  106,  107,   97, 1023, 1023,
       141751  + /*   260 */   900,  903,  892,  892,  104,  104,  105,  105,  105,  105,
       141752  + /*   270 */   484,  983, 1383,  206, 1353, 1383,  438,  435,  434,  281,
       141753  + /*   280 */   396,  269, 1089,  941,  351, 1002,  433,  861,  743,  401,
       141754  + /*   290 */   282,   57,   57,  482,  145,  791,  791,  103,  103,  103,
       141755  + /*   300 */   103,  102,  102,  101,  101,  101,  100,  381,  106,  107,
       141756  + /*   310 */    97, 1023, 1023,  900,  903,  892,  892,  104,  104,  105,
       141757  + /*   320 */   105,  105,  105,  281, 1002, 1003, 1004,  206,  879,  319,
       141758  + /*   330 */   438,  435,  434,  981,  259,  474,  360,  351, 1118, 1118,
       141759  + /*   340 */   433,  736,  379,  378,  872, 1002, 1356,  322,  871,  766,
       141760  + /*   350 */   103,  103,  103,  103,  102,  102,  101,  101,  101,  100,
       141761  + /*   360 */   381,  106,  107,   97, 1023, 1023,  900,  903,  892,  892,
       141762  + /*   370 */   104,  104,  105,  105,  105,  105,  484,  801,  484,  871,
       141763  + /*   380 */   871,  873,  401,  282, 1002, 1003, 1004, 1030,  360, 1030,
       141764  + /*   390 */   351,  983, 1384,  213,  880, 1384,  145,   59,   59,   59,
       141765  + /*   400 */    59, 1002,  244,  103,  103,  103,  103,  102,  102,  101,
       141766  + /*   410 */   101,  101,  100,  381,  106,  107,   97, 1023, 1023,  900,
       141767  + /*   420 */   903,  892,  892,  104,  104,  105,  105,  105,  105,  274,
       141768  + /*   430 */   484,  110,  467,  479,  467,  444,  259,  474,  232,  232,
       141769  + /*   440 */  1002, 1003, 1004,  351,  210,  335,  982,  866, 1385,  336,
       141770  + /*   450 */   481,   59,   59,  981,  245,  307,  103,  103,  103,  103,
       141771  + /*   460 */   102,  102,  101,  101,  101,  100,  381,  106,  107,   97,
       141772  + /*   470 */  1023, 1023,  900,  903,  892,  892,  104,  104,  105,  105,
       141773  + /*   480 */   105,  105,  453,  459,  484,  408,  377,  259,  474,  271,
       141774  + /*   490 */   183,  273,  209,  208,  207,  356,  351,  307,  178,  177,
       141775  + /*   500 */   127, 1006, 1098,   14,   14,   43,   43, 1044,  425,  103,
       141776  + /*   510 */   103,  103,  103,  102,  102,  101,  101,  101,  100,  381,
       141777  + /*   520 */   106,  107,   97, 1023, 1023,  900,  903,  892,  892,  104,
       141778  + /*   530 */   104,  105,  105,  105,  105,  294, 1132,  408,  160,  484,
       141779  + /*   540 */   408, 1006,  129,  962, 1209,  239,  239,  481,  307,  425,
       141780  + /*   550 */  1309, 1097,  351,  235,  243,  272,  820,  481,  963,  425,
       141781  + /*   560 */    11,   11,  103,  103,  103,  103,  102,  102,  101,  101,
       141782  + /*   570 */   101,  100,  381,  964,  362, 1002,  106,  107,   97, 1023,
       141783  + /*   580 */  1023,  900,  903,  892,  892,  104,  104,  105,  105,  105,
       141784  + /*   590 */   105, 1275,  161,  126,  777,  289, 1209,  292, 1072,  357,
       141785  + /*   600 */  1209, 1127,  476,  357,  778,  425,  247,  425,  351,  248,
       141786  + /*   610 */   414,  364,  414,  171, 1002, 1003, 1004,   84,  103,  103,
       141787  + /*   620 */   103,  103,  102,  102,  101,  101,  101,  100,  381, 1002,
       141788  + /*   630 */   184,  484,  106,  107,   97, 1023, 1023,  900,  903,  892,
       141789  + /*   640 */   892,  104,  104,  105,  105,  105,  105, 1123, 1209,  287,
       141790  + /*   650 */   484, 1209,   11,   11,  179,  820,  259,  474,  307,  237,
       141791  + /*   660 */   182,  351,  321,  365,  414,  308,  367,  366, 1002, 1003,
       141792  + /*   670 */  1004,   44,   44,   87,  103,  103,  103,  103,  102,  102,
       141793  + /*   680 */   101,  101,  101,  100,  381,  106,  107,   97, 1023, 1023,
       141794  + /*   690 */   900,  903,  892,  892,  104,  104,  105,  105,  105,  105,
       141795  + /*   700 */   246,  368,  280,  128,   10,  358,  146,  796,  835,  258,
       141796  + /*   710 */  1020,   88,  795,   86,  351,  421,  836,  943,  376,  348,
       141797  + /*   720 */   191,  943, 1318,  267,  308,  279,  456,  103,  103,  103,
       141798  + /*   730 */   103,  102,  102,  101,  101,  101,  100,  381,  106,   95,
       141799  + /*   740 */    97, 1023, 1023,  900,  903,  892,  892,  104,  104,  105,
       141800  + /*   750 */   105,  105,  105,  420,  249,  238,  238,  238,  238,   79,
       141801  + /*   760 */   375,  125,  305,   29,  262,  978,  351,  481,  337,  481,
       141802  + /*   770 */   756,  755,  304,  278,  415,   15,   81,  940, 1126,  940,
       141803  + /*   780 */   103,  103,  103,  103,  102,  102,  101,  101,  101,  100,
       141804  + /*   790 */   381,  107,   97, 1023, 1023,  900,  903,  892,  892,  104,
       141805  + /*   800 */   104,  105,  105,  105,  105,  457,  263,  484,  174,  484,
       141806  + /*   810 */   238,  238,  863,  407,  402,  216,  216,  351,  409,  193,
       141807  + /*   820 */   283,  216,  481,   81,  763,  764,  266,    5,   13,   13,
       141808  + /*   830 */    34,   34,  103,  103,  103,  103,  102,  102,  101,  101,
       141809  + /*   840 */   101,  100,  381,   97, 1023, 1023,  900,  903,  892,  892,
       141810  + /*   850 */   104,  104,  105,  105,  105,  105,   93,  475, 1002,    4,
       141811  + /*   860 */   403, 1002,  340,  431, 1002,  297,  212, 1277,   81,  746,
       141812  + /*   870 */  1163,  152,  926,  478,  166,  212,  757,  829,  930,  939,
       141813  + /*   880 */   216,  939,  858,  103,  103,  103,  103,  102,  102,  101,
       141814  + /*   890 */   101,  101,  100,  381,  238,  238,  382, 1002, 1003, 1004,
       141815  + /*   900 */  1002, 1003, 1004, 1002, 1003, 1004,  481,  439,  472,  746,
       141816  + /*   910 */   105,  105,  105,  105,   98,  758, 1162,  145,  930,  412,
       141817  + /*   920 */   879,  406,  793,   81,  395,   89,   90,   91,  105,  105,
       141818  + /*   930 */   105,  105, 1323,   92,  484,  382,  486,  485,  240,  275,
       141819  + /*   940 */   871,  103,  103,  103,  103,  102,  102,  101,  101,  101,
       141820  + /*   950 */   100,  381, 1096,  371,  355,   45,   45,  259,  474,  103,
       141821  + /*   960 */   103,  103,  103,  102,  102,  101,  101,  101,  100,  381,
       141822  + /*   970 */  1150,  871,  871,  873,  874,   21, 1332,  991,  384,  730,
       141823  + /*   980 */   722,  242,  123, 1298,  124,  875,  333,  333,  332,  227,
       141824  + /*   990 */   330,  991,  384,  719,  256,  242,  484,  391,  413, 1297,
       141825  + /*  1000 */   333,  333,  332,  227,  330,  748,  187,  719,  265,  470,
       141826  + /*  1010 */  1279, 1002,  484,  417,  391,  390,  264,   11,   11,  284,
       141827  + /*  1020 */   187,  732,  265,   93,  475,  875,    4, 1279, 1281,  419,
       141828  + /*  1030 */   264,  369,  416,   11,   11, 1159,  288,  484,  399, 1346,
       141829  + /*  1040 */   478,  379,  378,  291,  484,  293,  189,  250,  295, 1027,
       141830  + /*  1050 */  1002, 1003, 1004,  190, 1029, 1111,  140,  188,   11,   11,
       141831  + /*  1060 */   189,  732, 1028,  382,  923,   46,   46,  190, 1095,  230,
       141832  + /*  1070 */   140,  188,  462,   93,  475,  472,    4,  300,  309,  391,
       141833  + /*  1080 */   373,    6, 1069,  217,  739,  310, 1030,  879, 1030, 1171,
       141834  + /*  1090 */   478,  352, 1279,   90,   91,  800,  259,  474, 1208,  484,
       141835  + /*  1100 */    92, 1268,  382,  486,  485,  352, 1002,  871,  879,  426,
       141836  + /*  1110 */   259,  474,  172,  382,  238,  238, 1146,  170, 1021,  389,
       141837  + /*  1120 */    47,   47, 1157,  739,  872,  472,  481,  469,  871,  350,
       141838  + /*  1130 */  1214,   83,  475,  389,    4, 1078, 1071,  879,  871,  871,
       141839  + /*  1140 */   873,  874,   21,   90,   91, 1002, 1003, 1004,  478,  251,
       141840  + /*  1150 */    92,  251,  382,  486,  485,  443,  370,  871, 1021,  871,
       141841  + /*  1160 */   871,  873,  224,  241,  306,  441,  301,  440,  211, 1060,
       141842  + /*  1170 */   820,  382,  822,  447,  299, 1059,  484, 1061, 1143,  962,
       141843  + /*  1180 */   430,  796,  484,  472, 1340,  312,  795,  465,  871,  871,
       141844  + /*  1190 */   873,  874,   21,  314,  963,  879,  316,   59,   59, 1002,
       141845  + /*  1200 */     9,   90,   91,   48,   48,  238,  238,  210,   92,  964,
       141846  + /*  1210 */   382,  486,  485,  176,  334,  871,  242,  481, 1193,  238,
       141847  + /*  1220 */   238,  333,  333,  332,  227,  330,  394,  270,  719,  277,
       141848  + /*  1230 */   471,  481,  467,  466,  484,  145,  217, 1201, 1002, 1003,
       141849  + /*  1240 */  1004,  187,    3,  265,  184,  445,  871,  871,  873,  874,
       141850  + /*  1250 */    21,  264, 1337,  450, 1051,   39,   39,  392,  356,  260,
       141851  + /*  1260 */   342,  121,  468,  411,  436,  821,  180, 1094, 1128,  820,
       141852  + /*  1270 */   303, 1021, 1272, 1271,  299,  259,  474,  238,  238, 1002,
       141853  + /*  1280 */   473,  189,  484,  318,  327,  238,  238,  484,  190,  481,
       141854  + /*  1290 */   446,  140,  188, 1343,  238,  238, 1038,  481,  148,  175,
       141855  + /*  1300 */   238,  238,  484,   49,   49,  219,  481,  484,   35,   35,
       141856  + /*  1310 */  1317, 1021,  481,  484, 1035,  484, 1315,  484, 1002, 1003,
       141857  + /*  1320 */  1004,  484,   66,   36,   36,  194,  352,  484,   38,   38,
       141858  + /*  1330 */   484,  259,  474,   69,   50,   50,   51,   51,   52,   52,
       141859  + /*  1340 */   359,  484,   12,   12,  484, 1198,  484,  158,   53,   53,
       141860  + /*  1350 */   405,  112,  112,  385,  389,  484,   26,  484,  143,  484,
       141861  + /*  1360 */   150,  484,   54,   54,  397,   40,   40,   55,   55,  484,
       141862  + /*  1370 */    79,  484,  153, 1190,  484,  154,   56,   56,   41,   41,
       141863  + /*  1380 */    58,   58,  133,  133,  484,  398,  484,  429,  484,  155,
       141864  + /*  1390 */   134,  134,  135,  135,  484,   63,   63,  484,  341,  484,
       141865  + /*  1400 */   339,  484,  196,  484,  156,   42,   42,  113,  113,   60,
       141866  + /*  1410 */    60,  484,  404,  484,   27,  114,  114, 1204,  115,  115,
       141867  + /*  1420 */   111,  111,  132,  132,  131,  131, 1266,  418,  484,  162,
       141868  + /*  1430 */   484,  200,  119,  119,  118,  118,  484,   74,  424,  484,
       141869  + /*  1440 */  1286,  484,  231,  484,  202,  484,  167,  286,  427,  116,
       141870  + /*  1450 */   116,  117,  117,  290,  203,  442, 1062,   62,   62,  204,
       141871  + /*  1460 */    64,   64,   61,   61,   33,   33,   37,   37,  344,  372,
       141872  + /*  1470 */  1114, 1105,  748, 1113,  374, 1112,  254,  458, 1086,  255,
       141873  + /*  1480 */   345, 1085,  302, 1084, 1355,   78, 1154,  311, 1104,  449,
       141874  + /*  1490 */   452, 1155, 1153,  218,    7,  313,  315,  320, 1152,   85,
       141875  + /*  1500 */  1252,  317,  109,   80,  463,  225,  461, 1068,   25,  487,
       141876  + /*  1510 */   997,  323,  257,  226,  229,  228, 1136,  324,  325,  326,
       141877  + /*  1520 */   488,  136, 1057, 1052, 1302, 1303, 1301,  706, 1300,  137,
       141878  + /*  1530 */   122,  138,  383,  173, 1082,  261,  186,  252, 1081,   65,
       141879  + /*  1540 */   387,  120,  938,  936,  855,  353,  149, 1079,  139,  151,
       141880  + /*  1550 */   192,  780,  195,  276,  952,  157,  141,  361,   70,  363,
       141881  + /*  1560 */   859,  159,   71,   72,  142,   73,  955,  354,  147,  197,
       141882  + /*  1570 */   198,  951,  130,   16,  199,  285,  216, 1032,  201,  423,
       141883  + /*  1580 */   164,  944,  163,   28,  721,  428,  304,  165,  205,  759,
       141884  + /*  1590 */    75,  432,  298,   17,   18,  437,   76,  253,  878,  144,
       141885  + /*  1600 */   877,  906,   77,  986,   30,  448,  987,   31,  451,  181,
       141886  + /*  1610 */   234,  236,  168,  828,  823,   89,  910,  921,   81,  907,
       141887  + /*  1620 */   215,  905,  909,  961,  960,   19,  221,   20,  220,   22,
       141888  + /*  1630 */    32,  331,  876,  731,   94,  790,  794,    8,  992,  222,
       141889  + /*  1640 */   480,  328, 1048, 1048, 1048, 1048, 1048, 1048, 1048, 1048,
       141890  + /*  1650 */   223, 1048, 1048, 1048, 1048, 1348, 1347,
140271 141891   };
140272 141892   static const YYCODETYPE yy_lookahead[] = {
140273         - /*     0 */   152,  144,  145,  146,  147,  152,  152,  172,  152,  180,
140274         - /*    10 */   181,  152,  223,  224,  225,  180,  152,  164,  189,   19,
140275         - /*    20 */   223,  224,  225,  168,  169,  170,  163,  173,  174,  173,
140276         - /*    30 */   174,   31,  173,  174,  168,  169,  170,  173,  174,   39,
140277         - /*    40 */   243,  191,  192,   43,   44,   45,   46,   47,   48,   49,
140278         - /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  195,  196,
140279         - /*    60 */    22,   23,  208,  209,  208,  209,  218,  208,  209,  176,
140280         - /*    70 */   207,   19,  208,  209,   23,  212,  213,   26,   26,  152,
140281         - /*    80 */    46,   47,   48,   49,   84,   85,   86,   87,   88,   89,
140282         - /*    90 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
140283         - /*   100 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
140284         - /*   110 */    90,   91,   92,   93,   94,  188,  223,  224,  225,  171,
140285         - /*   120 */    68,   83,  152,   19,   84,   85,   86,   87,   88,   89,
140286         - /*   130 */    90,   91,   92,   93,   94,  101,   84,   85,   86,   87,
140287         - /*   140 */    88,   89,   90,   91,   92,   93,   94,   43,   44,   45,
140288         - /*   150 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
140289         - /*   160 */    56,   57,   99,   94,  194,  102,  103,  104,  109,  110,
140290         - /*   170 */    66,  223,  224,  225,  152,   19,  113,   22,   23,  152,
140291         - /*   180 */    24,   26,  160,    1,    2,   59,  164,  173,   84,   85,
140292         - /*   190 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   43,
140293         - /*   200 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
140294         - /*   210 */    54,   55,   56,   57,  244,   88,   89,   90,   91,   92,
140295         - /*   220 */    93,   94,   96,   97,   98,   99,  196,   19,  102,  103,
140296         - /*   230 */   104,   23,   88,   89,  173,   59,  163,  207,   83,  113,
140297         - /*   240 */    84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
140298         - /*   250 */    94,   43,   44,   45,   46,   47,   48,   49,   50,   51,
140299         - /*   260 */    52,   53,   54,   55,   56,   57,   90,  240,  195,  196,
140300         - /*   270 */   171,   82,   96,   97,   98,  152,  132,  176,  134,   19,
140301         - /*   280 */   207,  200,   72,   23,   93,   94,   97,  152,  173,   79,
140302         - /*   290 */   101,  210,   84,   85,   86,   87,   88,   89,   90,   91,
140303         - /*   300 */    92,   93,   94,   43,   44,   45,   46,   47,   48,   49,
140304         - /*   310 */    50,   51,   52,   53,   54,   55,   56,   57,  108,  152,
140305         - /*   320 */   152,  132,  133,  134,  223,  224,  225,  152,  186,  119,
140306         - /*   330 */   120,   19,  197,  234,   31,   23,   26,  152,  239,   59,
140307         - /*   340 */   173,  174,   39,  220,   84,   85,   86,   87,   88,   89,
140308         - /*   350 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
140309         - /*   360 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
140310         - /*   370 */   152,   22,  152,   16,  152,  208,   96,   97,   98,  194,
140311         - /*   380 */    22,   23,   11,   19,   26,   79,   72,   23,    0,    1,
140312         - /*   390 */     2,  173,  174,  173,  174,  220,   84,   85,   86,   87,
140313         - /*   400 */    88,   89,   90,   91,   92,   93,   94,   43,   44,   45,
140314         - /*   410 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
140315         - /*   420 */    56,   57,  108,  109,  110,  119,  120,  152,  208,  119,
140316         - /*   430 */   120,  137,   75,  139,   77,   19,  152,   88,   89,   23,
140317         - /*   440 */   115,   83,  117,  118,  163,  227,  163,  152,   84,   85,
140318         - /*   450 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   43,
140319         - /*   460 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
140320         - /*   470 */    54,   55,   56,   57,  195,  196,  195,  196,  195,  196,
140321         - /*   480 */   109,  110,  195,  196,   22,   23,  207,   19,  207,  152,
140322         - /*   490 */   207,  108,  109,  110,  207,  163,   22,  140,   24,   59,
140323         - /*   500 */    84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
140324         - /*   510 */    94,   43,   44,   45,   46,   47,   48,   49,   50,   51,
140325         - /*   520 */    52,   53,   54,   55,   56,   57,  152,  195,  196,  152,
140326         - /*   530 */    16,    7,    8,    9,  197,  240,   96,   97,   98,  207,
140327         - /*   540 */   249,  250,   19,  152,   22,   83,   26,  173,  174,  152,
140328         - /*   550 */   173,  174,   84,   85,   86,   87,   88,   89,   90,   91,
140329         - /*   560 */    92,   93,   94,   59,  124,  152,   43,   44,   45,   46,
140330         - /*   570 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
140331         - /*   580 */    57,   59,  169,  170,  157,  194,   82,  191,  192,   75,
140332         - /*   590 */   152,   77,  108,  109,  110,   26,  152,   19,  152,  152,
140333         - /*   600 */    96,   97,   24,  152,  152,  101,  138,   84,   85,   86,
140334         - /*   610 */    87,   88,   89,   90,   91,   92,   93,   94,   96,   97,
140335         - /*   620 */    98,   43,   44,   45,   46,   47,   48,   49,   50,   51,
140336         - /*   630 */    52,   53,   54,   55,   56,   57,  132,  133,  134,  188,
140337         - /*   640 */   194,  197,  152,  123,  197,  232,  194,  152,  182,   19,
140338         - /*   650 */   119,  120,  152,  152,  152,  152,  218,  230,  152,  163,
140339         - /*   660 */   233,  138,   84,   85,   86,   87,   88,   89,   90,   91,
140340         - /*   670 */    92,   93,   94,   43,   44,   45,   46,   47,   48,   49,
140341         - /*   680 */    50,   51,   52,   53,   54,   55,   56,   57,  152,  152,
140342         - /*   690 */    23,  195,  196,   26,  194,  194,  194,  146,  147,  130,
140343         - /*   700 */   194,   19,   46,  207,   59,   29,  166,  167,  218,   33,
140344         - /*   710 */   173,  174,   26,  218,   84,   85,   86,   87,   88,   89,
140345         - /*   720 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
140346         - /*   730 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
140347         - /*   740 */    64,  171,   97,  240,  116,  166,  167,  212,  213,  121,
140348         - /*   750 */    23,  152,   19,   26,  218,  247,  248,   23,   23,  152,
140349         - /*   760 */    26,   26,   22,  107,  163,   98,   84,   85,   86,   87,
140350         - /*   770 */    88,   89,   90,   91,   92,   93,   94,   44,   45,   46,
140351         - /*   780 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
140352         - /*   790 */    57,  124,  106,   53,  100,  101,  195,  196,  152,  152,
140353         - /*   800 */    23,   23,   19,   26,   26,   19,  152,   23,  207,  239,
140354         - /*   810 */    26,   23,  152,  163,   26,  169,  170,   84,   85,   86,
140355         - /*   820 */    87,   88,   89,   90,   91,   92,   93,   94,   45,   46,
140356         - /*   830 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
140357         - /*   840 */    57,   19,   20,  101,   22,  195,  196,   22,   19,   24,
140358         - /*   850 */   163,   19,    7,    8,  112,   59,   23,  207,   36,   26,
140359         - /*   860 */    23,  152,   59,   26,   21,   59,  152,   84,   85,   86,
140360         - /*   870 */    87,   88,   89,   90,   91,   92,   93,   94,  232,  221,
140361         - /*   880 */    94,   59,  195,  196,   59,   99,  100,  101,  102,  103,
140362         - /*   890 */   104,  105,   70,   97,  207,   59,   19,   20,  112,   22,
140363         - /*   900 */    97,   79,  152,   97,   82,  152,  152,   71,  221,   90,
140364         - /*   910 */    88,   89,   23,   36,   12,   26,  163,   95,   96,   97,
140365         - /*   920 */    98,   78,   97,  101,  152,   96,  173,  174,   96,   27,
140366         - /*   930 */   182,   22,   96,   97,   98,  116,   59,  148,  149,  152,
140367         - /*   940 */   121,  119,  120,  154,   42,  156,  103,   70,  195,  196,
140368         - /*   950 */    22,   59,  163,  152,  132,  133,  134,  135,  136,   82,
140369         - /*   960 */   207,  208,  209,   71,   62,   88,   89,   90,   59,  152,
140370         - /*   970 */   152,   12,   95,   96,   97,   98,   19,   20,  101,   22,
140371         - /*   980 */    22,  182,  152,  140,  195,  196,   27,   59,   96,   97,
140372         - /*   990 */    98,   59,  132,   36,  134,   22,  207,   19,   20,   24,
140373         - /*  1000 */    22,   42,  152,  173,  174,   96,   97,   98,  219,  132,
140374         - /*  1010 */   133,  134,  135,  136,   36,  152,   59,  187,  132,  152,
140375         - /*  1020 */   134,   62,  152,  152,   96,   97,   98,   70,   96,   97,
140376         - /*  1030 */    98,   72,   59,  152,   59,  246,   26,   59,  214,   82,
140377         - /*  1040 */   152,  192,  152,  173,  174,   88,   89,   79,   70,  152,
140378         - /*  1050 */   152,   19,   95,   96,   97,   98,  124,  187,  101,   23,
140379         - /*  1060 */    82,  164,   26,  173,  174,  152,   88,   89,  100,   96,
140380         - /*  1070 */    97,   98,   97,   95,   96,   97,   98,  187,   46,  101,
140381         - /*  1080 */   122,  184,  152,  186,  211,  152,  152,  119,  120,  132,
140382         - /*  1090 */   133,  134,  135,  136,  152,    5,   22,  152,  152,   35,
140383         - /*  1100 */    10,   11,   12,   13,   14,  152,  152,   17,   98,  235,
140384         - /*  1110 */   132,  133,  134,  135,  136,   54,   55,   56,   57,   58,
140385         - /*  1120 */    30,  152,   32,  152,  152,  198,  173,  174,  152,   65,
140386         - /*  1130 */    40,  152,  152,   59,  124,  152,  236,   73,  199,  107,
140387         - /*  1140 */   187,  171,  173,  174,  112,   84,   85,   86,   87,   88,
140388         - /*  1150 */    89,   90,   91,   92,   93,   94,  173,  174,  152,   69,
140389         - /*  1160 */   152,  211,   54,   55,   56,   57,   76,  152,  150,   79,
140390         - /*  1170 */    80,   97,  211,  211,  211,  111,   59,  241,  241,  173,
140391         - /*  1180 */   174,  173,  174,  202,  202,  185,  177,  176,  173,  174,
140392         - /*  1190 */   176,  201,   84,   85,   86,   87,   88,   89,   90,   91,
140393         - /*  1200 */    92,   93,   94,  215,  114,   88,   89,  152,  215,  119,
140394         - /*  1210 */   120,  152,  181,   96,   97,   98,  176,  100,  215,  152,
140395         - /*  1220 */   229,  152,  163,  152,  107,  199,  109,  155,  173,  174,
140396         - /*  1230 */   245,  141,  173,  174,   60,  159,  152,  122,  159,  242,
140397         - /*  1240 */   173,  174,  173,  174,  173,  174,   38,  242,  152,  132,
140398         - /*  1250 */   159,  134,  152,   22,  195,  196,  152,  173,  174,  222,
140399         - /*  1260 */    43,  130,  202,  152,   18,  152,  207,  208,  152,  173,
140400         - /*  1270 */   174,  152,  190,  173,  174,  152,  193,  173,  174,  152,
140401         - /*  1280 */   193,  193,  152,  159,  173,  174,  173,  174,  152,  173,
140402         - /*  1290 */   174,  193,  173,  174,   18,  152,  173,  174,  152,  158,
140403         - /*  1300 */   173,  174,  152,  173,  174,  152,  159,  152,  202,  173,
140404         - /*  1310 */   174,  152,  190,  152,  222,  152,  173,  174,  152,  173,
140405         - /*  1320 */   174,  137,  152,  173,  174,  190,  173,  174,  173,  174,
140406         - /*  1330 */   202,  158,  173,  174,  173,  174,  173,  174,   61,  173,
140407         - /*  1340 */   174,  152,  237,  173,  174,  152,  159,  152,  238,  152,
140408         - /*  1350 */   158,  152,   22,  152,  178,  152,  158,  152,  158,  152,
140409         - /*  1360 */   178,  159,  173,  174,  152,  159,  173,  174,  173,  174,
140410         - /*  1370 */   173,  174,  173,  174,  173,  174,  173,  174,  173,  174,
140411         - /*  1380 */   173,  174,  152,   63,  152,  173,  174,  107,  175,  175,
140412         - /*  1390 */   175,  106,  183,  175,  178,  175,  177,  175,  175,  178,
140413         - /*  1400 */    94,  107,  231,  173,  174,  173,  174,  183,  231,  125,
140414         - /*  1410 */   216,  178,  159,  217,   22,  159,  226,  129,  137,  228,
140415         - /*  1420 */   128,  217,  126,   25,  127,  162,  216,   26,  217,  161,
140416         - /*  1430 */    13,  153,  153,  206,  205,    6,  251,  202,  204,  203,
140417         - /*  1440 */   151,  216,  151,  217,  216,  171,  151,  165,  179,  179,
140418         - /*  1450 */     4,    3,   22,  142,  171,  165,   15,  171,  171,   81,
140419         - /*  1460 */    16,   23,   23,  120,  171,  131,  165,  111,  123,  171,
140420         - /*  1470 */   171,  171,   20,  125,   16,    1,  123,  131,   53,   53,
140421         - /*  1480 */    53,   53,  111,   96,   34,  122,  248,    1,  251,    5,
140422         - /*  1490 */    22,  107,  140,   26,   74,   41,  122,  107,   67,   67,
140423         - /*  1500 */    24,   20,   19,  112,  105,   23,   66,   22,   66,   22,
140424         - /*  1510 */    22,   22,   37,   22,   22,   66,   23,   23,   28,   23,
140425         - /*  1520 */    23,   26,   24,   23,   22,   24,  122,   23,   23,   96,
140426         - /*  1530 */    22,  124,   26,   34,   23,   26,   23,   34,   23,   23,
140427         - /*  1540 */    23,   34,   23,   22,   26,   11,   22,   22,   26,   23,
140428         - /*  1550 */    23,   22,  116,   23,   22,   15,  122,  122,   23,  122,
140429         - /*  1560 */     1,  252,  252,  252,  252,  122,  252,  252,  252,  252,
140430         - /*  1570 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140431         - /*  1580 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140432         - /*  1590 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140433         - /*  1600 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140434         - /*  1610 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140435         - /*  1620 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140436         - /*  1630 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140437         - /*  1640 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140438         - /*  1650 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140439         - /*  1660 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140440         - /*  1670 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140441         - /*  1680 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140442         - /*  1690 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140443         - /*  1700 */   252,  252,  252,  252,  252,  252,  252,  252,  252,
140444         -};
140445         -#define YY_SHIFT_COUNT    (471)
       141893  + /*     0 */   174,  226,  227,  228,  226,  227,  228,  172,  145,  146,
       141894  + /*    10 */   147,  148,  149,  150,  153,  169,  170,  171,  155,   19,
       141895  + /*    20 */   157,  246,  192,  193,  177,  181,  182,  164,  169,  170,
       141896  + /*    30 */   171,   31,  164,  153,  190,  174,  175,  187,  153,   39,
       141897  + /*    40 */     7,    8,    9,   43,   44,   45,   46,   47,   48,   49,
       141898  + /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  174,  196,
       141899  + /*    60 */   197,  226,  227,  228,  196,  197,   46,   47,   48,   49,
       141900  + /*    70 */   209,  208,   19,  226,  227,  228,  208,  174,  177,   26,
       141901  + /*    80 */   195,  213,  214,   22,  221,   85,   86,   87,   88,   89,
       141902  + /*    90 */    90,   91,   92,   93,   94,   95,   43,   44,   45,   46,
       141903  + /*   100 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       141904  + /*   110 */    57,  172,  249,  153,   53,  153,  147,  148,  149,  150,
       141905  + /*   120 */    22,   23,   69,  103,  155,   19,  157,  226,  227,  228,
       141906  + /*   130 */    94,   95,  247,  164,  174,  175,  174,  175,   85,   86,
       141907  + /*   140 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   43,
       141908  + /*   150 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       141909  + /*   160 */    54,   55,   56,   57,  153,  196,  197,  153,  153,  209,
       141910  + /*   170 */   210,  209,  210,   67,   95,  161,  237,  208,   19,  165,
       141911  + /*   180 */   165,  242,   84,   24,   91,   92,   93,   94,   95,  223,
       141912  + /*   190 */   221,   85,   86,   87,   88,   89,   90,   91,   92,   93,
       141913  + /*   200 */    94,   95,   43,   44,   45,   46,   47,   48,   49,   50,
       141914  + /*   210 */    51,   52,   53,   54,   55,   56,   57,  153,  249,   85,
       141915  + /*   220 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
       141916  + /*   230 */   219,   19,  109,  110,  111,   23,   89,   90,   91,   92,
       141917  + /*   240 */    93,   94,   95,   73,   85,   86,   87,   88,   89,   90,
       141918  + /*   250 */    91,   92,   93,   94,   95,   43,   44,   45,   46,   47,
       141919  + /*   260 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       141920  + /*   270 */   153,   22,   23,  101,  173,   26,  104,  105,  106,  109,
       141921  + /*   280 */   110,  111,  181,   11,   19,   59,  114,   73,   23,  110,
       141922  + /*   290 */   111,  174,  175,  116,   80,  118,  119,   85,   86,   87,
       141923  + /*   300 */    88,   89,   90,   91,   92,   93,   94,   95,   43,   44,
       141924  + /*   310 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       141925  + /*   320 */    55,   56,   57,  109,   98,   99,  100,  101,   83,  153,
       141926  + /*   330 */   104,  105,  106,   84,  120,  121,  153,   19,  192,  193,
       141927  + /*   340 */   114,   23,   89,   90,   99,   59,   23,  230,  103,   26,
       141928  + /*   350 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
       141929  + /*   360 */    95,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       141930  + /*   370 */    52,   53,   54,   55,   56,   57,  153,   91,  153,  134,
       141931  + /*   380 */   135,  136,  110,  111,   98,   99,  100,  134,  153,  136,
       141932  + /*   390 */    19,   22,   23,   26,   23,   26,   80,  174,  175,  174,
       141933  + /*   400 */   175,   59,  219,   85,   86,   87,   88,   89,   90,   91,
       141934  + /*   410 */    92,   93,   94,   95,   43,   44,   45,   46,   47,   48,
       141935  + /*   420 */    49,   50,   51,   52,   53,   54,   55,   56,   57,   16,
       141936  + /*   430 */   153,   22,  209,  210,  209,  210,  120,  121,  196,  197,
       141937  + /*   440 */    98,   99,  100,   19,   46,   22,   23,   23,  252,  253,
       141938  + /*   450 */   208,  174,  175,   84,  219,  153,   85,   86,   87,   88,
       141939  + /*   460 */    89,   90,   91,   92,   93,   94,   95,   43,   44,   45,
       141940  + /*   470 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       141941  + /*   480 */    56,   57,  153,  153,  153,  153,  209,  120,  121,   76,
       141942  + /*   490 */   153,   78,  109,  110,  111,   97,   19,  153,   89,   90,
       141943  + /*   500 */   198,   59,  183,  174,  175,  174,  175,   84,  153,   85,
       141944  + /*   510 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
       141945  + /*   520 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       141946  + /*   530 */    53,   54,   55,   56,   57,   16,  197,  153,   22,  153,
       141947  + /*   540 */   153,   99,  198,   12,  153,  196,  197,  208,  153,  153,
       141948  + /*   550 */   195,  183,   19,   23,  222,  142,   26,  208,   27,  153,
       141949  + /*   560 */   174,  175,   85,   86,   87,   88,   89,   90,   91,   92,
       141950  + /*   570 */    93,   94,   95,   42,  188,   59,   43,   44,   45,   46,
       141951  + /*   580 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       141952  + /*   590 */    57,  195,   22,  198,   63,   76,  153,   78,  167,  168,
       141953  + /*   600 */   153,  195,  167,  168,   73,  153,  222,  153,   19,  222,
       141954  + /*   610 */   153,  220,  153,   24,   98,   99,  100,  140,   85,   86,
       141955  + /*   620 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   59,
       141956  + /*   630 */   100,  153,   43,   44,   45,   46,   47,   48,   49,   50,
       141957  + /*   640 */    51,   52,   53,   54,   55,   56,   57,  195,  153,  195,
       141958  + /*   650 */   153,  153,  174,  175,   26,  125,  120,  121,  153,  213,
       141959  + /*   660 */   214,   19,  153,  220,  153,  153,  188,  220,   98,   99,
       141960  + /*   670 */   100,  174,  175,  140,   85,   86,   87,   88,   89,   90,
       141961  + /*   680 */    91,   92,   93,   94,   95,   43,   44,   45,   46,   47,
       141962  + /*   690 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       141963  + /*   700 */   243,  189,  243,  198,  172,  250,  251,  117,   31,  201,
       141964  + /*   710 */    26,  139,  122,  141,   19,  220,   39,   29,  220,  211,
       141965  + /*   720 */    24,   33,  153,  164,  153,  164,   19,   85,   86,   87,
       141966  + /*   730 */    88,   89,   90,   91,   92,   93,   94,   95,   43,   44,
       141967  + /*   740 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       141968  + /*   750 */    55,   56,   57,   65,  243,  196,  197,  196,  197,  131,
       141969  + /*   760 */   189,   22,  103,   24,  153,   23,   19,  208,   26,  208,
       141970  + /*   770 */   102,  103,  113,   23,  242,   22,   26,  134,  164,  136,
       141971  + /*   780 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
       141972  + /*   790 */    95,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       141973  + /*   800 */    53,   54,   55,   56,   57,   98,  153,  153,  124,  153,
       141974  + /*   810 */   196,  197,   23,   23,   61,   26,   26,   19,   23,  123,
       141975  + /*   820 */    23,   26,  208,   26,    7,    8,  153,   22,  174,  175,
       141976  + /*   830 */   174,  175,   85,   86,   87,   88,   89,   90,   91,   92,
       141977  + /*   840 */    93,   94,   95,   45,   46,   47,   48,   49,   50,   51,
       141978  + /*   850 */    52,   53,   54,   55,   56,   57,   19,   20,   59,   22,
       141979  + /*   860 */   111,   59,  164,   23,   59,   23,   26,  153,   26,   59,
       141980  + /*   870 */   153,   72,   23,   36,   72,   26,   35,   23,   59,  134,
       141981  + /*   880 */    26,  136,  133,   85,   86,   87,   88,   89,   90,   91,
       141982  + /*   890 */    92,   93,   94,   95,  196,  197,   59,   98,   99,  100,
       141983  + /*   900 */    98,   99,  100,   98,   99,  100,  208,   66,   71,   99,
       141984  + /*   910 */    54,   55,   56,   57,   58,   74,  153,   80,   99,   19,
       141985  + /*   920 */    83,  223,   23,   26,  153,   26,   89,   90,   54,   55,
       141986  + /*   930 */    56,   57,  153,   96,  153,   98,   99,  100,   22,  153,
       141987  + /*   940 */   103,   85,   86,   87,   88,   89,   90,   91,   92,   93,
       141988  + /*   950 */    94,   95,  183,  112,  158,  174,  175,  120,  121,   85,
       141989  + /*   960 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
       141990  + /*   970 */   215,  134,  135,  136,  137,  138,    0,    1,    2,   23,
       141991  + /*   980 */    21,    5,   26,  153,   22,   59,   10,   11,   12,   13,
       141992  + /*   990 */    14,    1,    2,   17,  212,    5,  153,  153,   98,  153,
       141993  + /*  1000 */    10,   11,   12,   13,   14,  108,   30,   17,   32,  193,
       141994  + /*  1010 */   153,   59,  153,  153,  170,  171,   40,  174,  175,  153,
       141995  + /*  1020 */    30,   59,   32,   19,   20,   99,   22,  170,  171,  233,
       141996  + /*  1030 */    40,  188,  236,  174,  175,  153,  153,  153,   79,  123,
       141997  + /*  1040 */    36,   89,   90,  153,  153,  153,   70,  188,  153,   97,
       141998  + /*  1050 */    98,   99,  100,   77,  102,  153,   80,   81,  174,  175,
       141999  + /*  1060 */    70,   99,  110,   59,  105,  174,  175,   77,  153,  238,
       142000  + /*  1070 */    80,   81,  188,   19,   20,   71,   22,  153,  153,  235,
       142001  + /*  1080 */    19,   22,  164,   24,   59,  153,  134,   83,  136,  153,
       142002  + /*  1090 */    36,  115,  235,   89,   90,   91,  120,  121,  153,  153,
       142003  + /*  1100 */    96,  142,   98,   99,  100,  115,   59,  103,   83,  239,
       142004  + /*  1110 */   120,  121,  199,   59,  196,  197,  153,  153,   59,  143,
       142005  + /*  1120 */   174,  175,  153,   98,   99,   71,  208,  153,  103,  165,
       142006  + /*  1130 */   153,   19,   20,  143,   22,  153,  153,   83,  134,  135,
       142007  + /*  1140 */   136,  137,  138,   89,   90,   98,   99,  100,   36,  185,
       142008  + /*  1150 */    96,  187,   98,   99,  100,   91,   95,  103,   99,  134,
       142009  + /*  1160 */   135,  136,  101,  102,  103,  104,  105,  106,  107,  153,
       142010  + /*  1170 */    26,   59,  125,  164,  113,  153,  153,  153,  212,   12,
       142011  + /*  1180 */    19,  117,  153,   71,  153,  212,  122,  164,  134,  135,
       142012  + /*  1190 */   136,  137,  138,  212,   27,   83,  212,  174,  175,   59,
       142013  + /*  1200 */   200,   89,   90,  174,  175,  196,  197,   46,   96,   42,
       142014  + /*  1210 */    98,   99,  100,  172,  151,  103,    5,  208,  203,  196,
       142015  + /*  1220 */   197,   10,   11,   12,   13,   14,  216,  216,   17,  244,
       142016  + /*  1230 */    63,  208,  209,  210,  153,   80,   24,  203,   98,   99,
       142017  + /*  1240 */   100,   30,   22,   32,  100,  164,  134,  135,  136,  137,
       142018  + /*  1250 */   138,   40,  148,  164,  150,  174,  175,  102,   97,  155,
       142019  + /*  1260 */   203,  157,  164,  244,  178,  125,  186,  182,  164,  125,
       142020  + /*  1270 */   177,   59,  177,  177,  113,  120,  121,  196,  197,   59,
       142021  + /*  1280 */   232,   70,  153,  216,  202,  196,  197,  153,   77,  208,
       142022  + /*  1290 */   209,   80,   81,  156,  196,  197,   60,  208,  248,  200,
       142023  + /*  1300 */   196,  197,  153,  174,  175,  123,  208,  153,  174,  175,
       142024  + /*  1310 */   160,   99,  208,  153,   38,  153,  160,  153,   98,   99,
       142025  + /*  1320 */   100,  153,  245,  174,  175,  221,  115,  153,  174,  175,
       142026  + /*  1330 */   153,  120,  121,  245,  174,  175,  174,  175,  174,  175,
       142027  + /*  1340 */   160,  153,  174,  175,  153,  225,  153,   22,  174,  175,
       142028  + /*  1350 */    97,  174,  175,  249,  143,  153,  224,  153,   43,  153,
       142029  + /*  1360 */   191,  153,  174,  175,   18,  174,  175,  174,  175,  153,
       142030  + /*  1370 */   131,  153,  194,  203,  153,  194,  174,  175,  174,  175,
       142031  + /*  1380 */   174,  175,  174,  175,  153,  160,  153,   18,  153,  194,
       142032  + /*  1390 */   174,  175,  174,  175,  153,  174,  175,  153,  225,  153,
       142033  + /*  1400 */   203,  153,  159,  153,  194,  174,  175,  174,  175,  174,
       142034  + /*  1410 */   175,  153,  203,  153,  224,  174,  175,  191,  174,  175,
       142035  + /*  1420 */   174,  175,  174,  175,  174,  175,  203,  160,  153,  191,
       142036  + /*  1430 */   153,  159,  174,  175,  174,  175,  153,  139,   62,  153,
       142037  + /*  1440 */   241,  153,  160,  153,  159,  153,   22,  240,  179,  174,
       142038  + /*  1450 */   175,  174,  175,  160,  159,   97,  160,  174,  175,  159,
       142039  + /*  1460 */   174,  175,  174,  175,  174,  175,  174,  175,  179,   64,
       142040  + /*  1470 */   176,  184,  108,  176,   95,  176,  234,  126,  176,  234,
       142041  + /*  1480 */   179,  178,  176,  176,  176,   97,  218,  217,  184,  179,
       142042  + /*  1490 */   179,  218,  218,  160,   22,  217,  217,  160,  218,  139,
       142043  + /*  1500 */   229,  217,  130,  129,  127,   25,  128,  163,   26,  162,
       142044  + /*  1510 */    13,  206,  231,  154,    6,  154,  207,  205,  204,  203,
       142045  + /*  1520 */   152,  166,  152,  152,  172,  172,  172,    4,  172,  166,
       142046  + /*  1530 */   180,  166,    3,   22,  172,  144,   15,  180,  172,  172,
       142047  + /*  1540 */    82,   16,   23,   23,  121,  254,  132,  172,  112,  124,
       142048  + /*  1550 */    24,   20,  126,   16,    1,  124,  112,   61,   53,   37,
       142049  + /*  1560 */   133,  132,   53,   53,  112,   53,   98,  254,  251,   34,
       142050  + /*  1570 */   123,    1,    5,   22,   97,  142,   26,   75,  123,   41,
       142051  + /*  1580 */    97,   68,   68,   24,   20,   19,  113,   22,  107,   28,
       142052  + /*  1590 */    22,   67,   23,   22,   22,   67,   22,   67,   23,   37,
       142053  + /*  1600 */    23,   23,   26,   23,   22,   24,   23,   22,   24,  123,
       142054  + /*  1610 */    23,   23,   22,   98,  125,   26,   11,   23,   26,   23,
       142055  + /*  1620 */    34,   23,   23,   23,   23,   34,   22,   34,   26,   22,
       142056  + /*  1630 */    22,   15,   23,   23,   22,  117,   23,   22,    1,  123,
       142057  + /*  1640 */    26,   23,  255,  255,  255,  255,  255,  255,  255,  255,
       142058  + /*  1650 */   123,  255,  255,  255,  255,  123,  123,  255,  255,  255,
       142059  + /*  1660 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142060  + /*  1670 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142061  + /*  1680 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142062  + /*  1690 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142063  + /*  1700 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142064  + /*  1710 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142065  + /*  1720 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142066  + /*  1730 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142067  + /*  1740 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142068  + /*  1750 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142069  + /*  1760 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142070  + /*  1770 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142071  + /*  1780 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142072  + /*  1790 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142073  + /*  1800 */   255,  255,
       142074  +};
       142075  +#define YY_SHIFT_COUNT    (489)
140446 142076   #define YY_SHIFT_MIN      (0)
140447         -#define YY_SHIFT_MAX      (1559)
       142077  +#define YY_SHIFT_MAX      (1637)
140448 142078   static const unsigned short int yy_shift_ofst[] = {
140449         - /*     0 */   182, 1090,  822,  822,  306,  957,  957,  957,  957,  210,
140450         - /*    10 */     0,    0,  104,  630,  957,  957,  957,  957,  957,  957,
140451         - /*    20 */   957, 1117, 1117,  126,  968,  306,  306,  306,  306,  306,
140452         - /*    30 */   306,   52,  156,  208,  260,  312,  364,  416,  468,  523,
140453         - /*    40 */   578,  630,  630,  630,  630,  630,  630,  630,  630,  630,
140454         - /*    50 */   630,  630,  630,  630,  630,  630,  630,  630,  682,  630,
140455         - /*    60 */   733,  783,  783,  877,  957,  957,  957,  957,  957,  957,
140456         - /*    70 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
140457         - /*    80 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
140458         - /*    90 */   957,  957,  957,  957,  957,  978,  957,  957,  957,  957,
140459         - /*   100 */   957,  957,  957,  957,  957,  957,  957,  957,  957, 1061,
140460         - /*   110 */  1108, 1108, 1108, 1108, 1108,   40,  127,   20,  280,  843,
140461         - /*   120 */  1032,  144,  144,  280,  310,  310,  310,  310,   59,  191,
140462         - /*   130 */    69, 1566, 1566, 1566,  786,  786,  786,  522,  836,  522,
140463         - /*   140 */   959,  959,  892,  155,  358,  280,  280,  280,  280,  280,
140464         - /*   150 */   280,  280,  280,  280,  280,  280,  280,  280,  280,  280,
140465         - /*   160 */   280,  280,  280,  280,  280,  280,  371,  388,  645,  645,
140466         - /*   170 */   531, 1566, 1566, 1566,  504,  189,  189,  909,   63,  176,
140467         - /*   180 */   928,  440,  932,  973,  280,  280,  280,  280,  280,  314,
140468         - /*   190 */   280,  280,  280,  280,  280,  280,  280,  280,  280,  280,
140469         - /*   200 */   280,  280, 1064, 1064, 1064,  280,  280,  280,  280,  667,
140470         - /*   210 */   280,  280,  280,  825,  280,  280,  902,  280,  280,  280,
140471         - /*   220 */   280,  280,  280,  280,  280,  383,  676,  325,  975,  975,
140472         - /*   230 */   975,  975, 1010,  325,  325,  819,  349,  524,  569,  829,
140473         - /*   240 */   829,  832,  569,  832,  686,   51,  656,  303,  303,  303,
140474         - /*   250 */   829,  294,  520,  628,  474, 1174, 1115, 1115, 1208, 1208,
140475         - /*   260 */  1115, 1231, 1217, 1131, 1246, 1246, 1246, 1246, 1115, 1276,
140476         - /*   270 */  1131, 1231, 1217, 1217, 1131, 1115, 1276, 1184, 1277, 1115,
140477         - /*   280 */  1276, 1330, 1115, 1276, 1115, 1276, 1330, 1280, 1280, 1280,
140478         - /*   290 */  1320, 1330, 1280, 1285, 1280, 1320, 1280, 1280, 1330, 1306,
140479         - /*   300 */  1306, 1330, 1284, 1294, 1284, 1294, 1284, 1294, 1284, 1294,
140480         - /*   310 */  1115, 1392, 1115, 1281, 1288, 1296, 1292, 1297, 1131, 1398,
140481         - /*   320 */  1401, 1417, 1417, 1429, 1429, 1429, 1566, 1566, 1566, 1566,
140482         - /*   330 */  1566, 1566, 1566, 1566, 1566, 1566, 1566, 1566, 1566, 1566,
140483         - /*   340 */  1566, 1566,   34,  357,   38,  462,  514,  484, 1074,  727,
140484         - /*   350 */   740,  734,  735,  777,  778,  784,  788,  803,  694,  845,
140485         - /*   360 */   742,  796,  833,  837,  889,  860,  886, 1036,  806,  958,
140486         - /*   370 */  1446, 1448, 1430, 1311, 1441, 1378, 1444, 1438, 1439, 1343,
140487         - /*   380 */  1334, 1356, 1345, 1452, 1348, 1458, 1474, 1353, 1346, 1425,
140488         - /*   390 */  1426, 1427, 1428, 1371, 1387, 1450, 1363, 1486, 1484, 1468,
140489         - /*   400 */  1384, 1352, 1431, 1467, 1432, 1420, 1454, 1374, 1390, 1476,
140490         - /*   410 */  1481, 1483, 1391, 1399, 1485, 1440, 1487, 1488, 1482, 1489,
140491         - /*   420 */  1442, 1490, 1491, 1449, 1475, 1493, 1494, 1496, 1495, 1497,
140492         - /*   430 */  1492, 1498, 1500, 1502, 1501, 1404, 1504, 1505, 1433, 1499,
140493         - /*   440 */  1508, 1407, 1506, 1503, 1509, 1507, 1511, 1513, 1515, 1506,
140494         - /*   450 */  1516, 1517, 1518, 1519, 1521, 1534, 1524, 1525, 1526, 1527,
140495         - /*   460 */  1529, 1530, 1532, 1522, 1436, 1434, 1435, 1437, 1443, 1535,
140496         - /*   470 */  1540, 1559,
140497         -};
140498         -#define YY_REDUCE_COUNT (341)
140499         -#define YY_REDUCE_MIN   (-211)
140500         -#define YY_REDUCE_MAX   (1301)
       142079  + /*     0 */   990,  976, 1211,  837,  837,  316, 1054, 1054, 1054, 1054,
       142080  + /*    10 */   214,    0,    0,  106,  642, 1054, 1054, 1054, 1054, 1054,
       142081  + /*    20 */  1054, 1054, 1054,  952,  952,  226, 1155,  316,  316,  316,
       142082  + /*    30 */   316,  316,  316,   53,  159,  212,  265,  318,  371,  424,
       142083  + /*    40 */   477,  533,  589,  642,  642,  642,  642,  642,  642,  642,
       142084  + /*    50 */   642,  642,  642,  642,  642,  642,  642,  642,  642,  642,
       142085  + /*    60 */   695,  642,  747,  798,  798, 1004, 1054, 1054, 1054, 1054,
       142086  + /*    70 */  1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054,
       142087  + /*    80 */  1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054,
       142088  + /*    90 */  1054, 1054, 1054, 1054, 1054, 1054, 1054, 1112, 1054, 1054,
       142089  + /*   100 */  1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054,
       142090  + /*   110 */  1054,  856,  874,  874,  874,  874,  874,  134,  147,   93,
       142091  + /*   120 */   342,  959, 1161,  253,  253,  342,  367,  367,  367,  367,
       142092  + /*   130 */   179,   36,   79, 1657, 1657, 1657, 1061, 1061, 1061,  516,
       142093  + /*   140 */   799,  516,  516,  531,  531,  802,  249,  369,  342,  342,
       142094  + /*   150 */   342,  342,  342,  342,  342,  342,  342,  342,  342,  342,
       142095  + /*   160 */   342,  342,  342,  342,  342,  342,  342,  342,  342,  272,
       142096  + /*   170 */   442,  442,  536, 1657, 1657, 1657, 1025,  245,  245,  570,
       142097  + /*   180 */   172,  286,  805, 1047, 1140, 1220,  342,  342,  342,  342,
       142098  + /*   190 */   342,  342,  342,  342,  170,  342,  342,  342,  342,  342,
       142099  + /*   200 */   342,  342,  342,  342,  342,  342,  342,  841,  841,  841,
       142100  + /*   210 */   342,  342,  342,  342,  530,  342,  342,  342, 1059,  342,
       142101  + /*   220 */   342, 1167,  342,  342,  342,  342,  342,  342,  342,  342,
       142102  + /*   230 */   123,  688,  177, 1212, 1212, 1212, 1212, 1144,  177,  177,
       142103  + /*   240 */  1064,  409,   33,  628,  707,  707,  900,  628,  628,  900,
       142104  + /*   250 */   897,  323,  398,  677,  677,  677,  707,  572,  684,  590,
       142105  + /*   260 */   739, 1236, 1182, 1182, 1276, 1276, 1182, 1253, 1325, 1315,
       142106  + /*   270 */  1239, 1346, 1346, 1346, 1346, 1182, 1369, 1239, 1239, 1253,
       142107  + /*   280 */  1325, 1315, 1315, 1239, 1182, 1369, 1298, 1376, 1182, 1369,
       142108  + /*   290 */  1424, 1182, 1369, 1182, 1369, 1424, 1358, 1358, 1358, 1405,
       142109  + /*   300 */  1424, 1358, 1364, 1358, 1405, 1358, 1358, 1424, 1379, 1379,
       142110  + /*   310 */  1424, 1351, 1388, 1351, 1388, 1351, 1388, 1351, 1388, 1182,
       142111  + /*   320 */  1472, 1182, 1360, 1372, 1377, 1374, 1378, 1239, 1480, 1482,
       142112  + /*   330 */  1497, 1497, 1508, 1508, 1508, 1657, 1657, 1657, 1657, 1657,
       142113  + /*   340 */  1657, 1657, 1657, 1657, 1657, 1657, 1657, 1657, 1657, 1657,
       142114  + /*   350 */  1657,   20,  413,   98,  423,  519,  383,  962,  742,   61,
       142115  + /*   360 */   696,  749,  750,  753,  789,  790,  795,  797,  840,  842,
       142116  + /*   370 */   810,  668,  817,  659,  819,  849,  854,  899,  643,  745,
       142117  + /*   380 */   956,  926,  916, 1523, 1529, 1511, 1391, 1521, 1458, 1525,
       142118  + /*   390 */  1519, 1520, 1423, 1414, 1436, 1526, 1425, 1531, 1426, 1537,
       142119  + /*   400 */  1553, 1431, 1427, 1444, 1496, 1522, 1429, 1505, 1509, 1510,
       142120  + /*   410 */  1512, 1452, 1468, 1535, 1447, 1570, 1567, 1551, 1477, 1433,
       142121  + /*   420 */  1513, 1550, 1514, 1502, 1538, 1455, 1483, 1559, 1564, 1566,
       142122  + /*   430 */  1473, 1481, 1565, 1524, 1568, 1571, 1569, 1572, 1528, 1561,
       142123  + /*   440 */  1574, 1530, 1562, 1575, 1577, 1578, 1576, 1580, 1582, 1581,
       142124  + /*   450 */  1583, 1585, 1584, 1486, 1587, 1588, 1515, 1586, 1590, 1489,
       142125  + /*   460 */  1589, 1591, 1592, 1593, 1594, 1596, 1598, 1589, 1599, 1600,
       142126  + /*   470 */  1602, 1601, 1604, 1605, 1607, 1608, 1609, 1610, 1612, 1613,
       142127  + /*   480 */  1615, 1614, 1518, 1516, 1527, 1532, 1533, 1618, 1616, 1637,
       142128  +};
       142129  +#define YY_REDUCE_COUNT (350)
       142130  +#define YY_REDUCE_MIN   (-225)
       142131  +#define YY_REDUCE_MAX   (1375)
140501 142132   static const short yy_reduce_ofst[] = {
140502         - /*     0 */  -143,  789,  753, 1059, -137, -146, -144, -141, -136,  687,
140503         - /*    10 */  -107,  101, -203,  -52,  830,  870,  890,  167,  953,  218,
140504         - /*    20 */   220,  413,  646,  897,   73,  281,  283,  332,  496,  601,
140505         - /*    30 */   650, -211, -211, -211, -211, -211, -211, -211, -211, -211,
140506         - /*    40 */  -211, -211, -211, -211, -211, -211, -211, -211, -211, -211,
140507         - /*    50 */  -211, -211, -211, -211, -211, -211, -211, -211, -211, -211,
140508         - /*    60 */  -211, -211, -211,  374,  377,  537,  969,  983, 1006, 1008,
140509         - /*    70 */  1015, 1055, 1067, 1069, 1071, 1084, 1096, 1100, 1104, 1111,
140510         - /*    80 */  1113, 1116, 1119, 1123, 1127, 1130, 1136, 1143, 1146, 1150,
140511         - /*    90 */  1153, 1155, 1159, 1161, 1163, 1166, 1170, 1189, 1193, 1195,
140512         - /*   100 */  1197, 1199, 1201, 1203, 1205, 1207, 1212, 1230, 1232, -211,
140513         - /*   110 */  -211, -211, -211, -211, -211, -211, -211, -211,  -30,  427,
140514         - /*   120 */  -171, -145, -134,   22,  279,  287,  279,  287,   99, -211,
140515         - /*   130 */  -211, -211, -211, -211, -165, -165, -165,  123,  135,  175,
140516         - /*   140 */  -150,  396,  337,  291,  291, -147,  185,  391,  446,  444,
140517         - /*   150 */   452,  500,  501,  502,   27, -152,  295,  438,  490,  503,
140518         - /*   160 */   495,  506,  -73,  447,  451,  536,  570,  551,  540,  579,
140519         - /*   170 */    30,  508,  535,   81,   14,   61,  115,  168,  142,  222,
140520         - /*   180 */   275,  284,  397,  599,  607,  647,  654,  660,  709,  658,
140521         - /*   190 */   714,  750,  754,  772,  787,  801,  817,  818,  850,  863,
140522         - /*   200 */   867,  871,  466,  748,  799,  881,  888,  898,  913,  824,
140523         - /*   210 */   930,  933,  934,  873,  942,  945,  849,  946,  222,  954,
140524         - /*   220 */   971,  972,  976,  979,  980,  900,  874,  927,  950,  961,
140525         - /*   230 */   962,  963,  824,  927,  927,  939,  970, 1018,  981,  988,
140526         - /*   240 */   993,  936,  982,  937, 1009, 1000, 1031, 1011, 1014, 1040,
140527         - /*   250 */  1003,  991,  990, 1026, 1072,  985, 1076, 1079,  997, 1005,
140528         - /*   260 */  1091, 1037, 1082, 1060, 1083, 1087, 1088, 1098, 1124, 1141,
140529         - /*   270 */  1106, 1092, 1122, 1135, 1128, 1147, 1173, 1110, 1105, 1187,
140530         - /*   280 */  1192, 1176, 1202, 1198, 1206, 1200, 1182, 1213, 1214, 1215,
140531         - /*   290 */  1209, 1216, 1218, 1219, 1220, 1224, 1222, 1223, 1221, 1171,
140532         - /*   300 */  1177, 1233, 1196, 1194, 1204, 1210, 1211, 1225, 1226, 1228,
140533         - /*   310 */  1253, 1190, 1256, 1191, 1227, 1229, 1234, 1236, 1235, 1263,
140534         - /*   320 */  1268, 1278, 1279, 1289, 1291, 1295, 1185, 1237, 1238, 1282,
140535         - /*   330 */  1274, 1283, 1286, 1287, 1290, 1269, 1270, 1293, 1298, 1299,
140536         - /*   340 */  1300, 1301,
       142133  + /*     0 */  -137,  -31, 1104, 1023, 1081, -132,  -40,  -38,  223,  225,
       142134  + /*    10 */   698, -153,  -99, -225, -165,  386,  478,  843,  859, -139,
       142135  + /*    20 */   884,  117,  277,  844,  857,  964,  559,  561,  614,  918,
       142136  + /*    30 */  1009, 1089, 1098, -222, -222, -222, -222, -222, -222, -222,
       142137  + /*    40 */  -222, -222, -222, -222, -222, -222, -222, -222, -222, -222,
       142138  + /*    50 */  -222, -222, -222, -222, -222, -222, -222, -222, -222, -222,
       142139  + /*    60 */  -222, -222, -222, -222, -222,  329,  331,  497,  654,  656,
       142140  + /*    70 */   781,  891,  946, 1029, 1129, 1134, 1149, 1154, 1160, 1162,
       142141  + /*    80 */  1164, 1168, 1174, 1177, 1188, 1191, 1193, 1202, 1204, 1206,
       142142  + /*    90 */  1208, 1216, 1218, 1221, 1231, 1233, 1235, 1241, 1244, 1246,
       142143  + /*   100 */  1248, 1250, 1258, 1260, 1275, 1277, 1283, 1286, 1288, 1290,
       142144  + /*   110 */  1292, -222, -222, -222, -222, -222, -222, -222, -222, -222,
       142145  + /*   120 */  -115,  796, -156, -154, -141,   14,  242,  349,  242,  349,
       142146  + /*   130 */   -61, -222, -222, -222, -222, -222,  101,  101,  101,  332,
       142147  + /*   140 */   302,  384,  387, -170,  146,  344,  196,  196,   15,   11,
       142148  + /*   150 */   183,  235,  395,  355,  396,  406,  452,  457,  391,  459,
       142149  + /*   160 */   443,  447,  511,  495,  454,  512,  505,  571,  498,  532,
       142150  + /*   170 */   431,  435,  339,  455,  446,  508, -174, -116,  -97, -120,
       142151  + /*   180 */  -150,   64,  176,  330,  337,  509,  569,  611,  653,  673,
       142152  + /*   190 */   714,  717,  763,  771,  -34,  779,  786,  830,  846,  860,
       142153  + /*   200 */   866,  882,  883,  890,  892,  895,  902,  319,  368,  769,
       142154  + /*   210 */   915,  924,  925,  932,  755,  936,  945,  963,  782,  969,
       142155  + /*   220 */   974,  816,  977,   64,  982,  983, 1016, 1022, 1024, 1031,
       142156  + /*   230 */   870,  831,  913,  966,  973,  981,  984,  755,  913,  913,
       142157  + /*   240 */  1000, 1041, 1063, 1015, 1010, 1011,  985, 1034, 1057, 1019,
       142158  + /*   250 */  1086, 1080, 1085, 1093, 1095, 1096, 1067, 1048, 1082, 1099,
       142159  + /*   260 */  1137, 1050, 1150, 1156, 1077, 1088, 1180, 1120, 1132, 1169,
       142160  + /*   270 */  1170, 1178, 1181, 1195, 1210, 1225, 1243, 1197, 1209, 1173,
       142161  + /*   280 */  1190, 1226, 1238, 1223, 1267, 1272, 1199, 1207, 1282, 1285,
       142162  + /*   290 */  1269, 1293, 1295, 1296, 1300, 1289, 1294, 1297, 1299, 1287,
       142163  + /*   300 */  1301, 1302, 1303, 1306, 1304, 1307, 1308, 1310, 1242, 1245,
       142164  + /*   310 */  1311, 1268, 1270, 1273, 1278, 1274, 1279, 1280, 1284, 1333,
       142165  + /*   320 */  1271, 1337, 1281, 1309, 1305, 1312, 1314, 1316, 1344, 1347,
       142166  + /*   330 */  1359, 1361, 1368, 1370, 1371, 1291, 1313, 1317, 1355, 1352,
       142167  + /*   340 */  1353, 1354, 1356, 1363, 1350, 1357, 1362, 1366, 1367, 1375,
       142168  + /*   350 */  1365,
140537 142169   };
140538 142170   static const YYACTIONTYPE yy_default[] = {
140539         - /*     0 */  1297, 1349, 1221, 1014, 1119, 1221, 1221, 1221, 1221, 1014,
140540         - /*    10 */  1145, 1145, 1272, 1045, 1014, 1014, 1014, 1014, 1014, 1220,
140541         - /*    20 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140542         - /*    30 */  1014, 1151, 1014, 1014, 1014, 1014, 1222, 1223, 1014, 1014,
140543         - /*    40 */  1014, 1271, 1273, 1161, 1160, 1159, 1158, 1254, 1132, 1156,
140544         - /*    50 */  1149, 1153, 1216, 1217, 1215, 1219, 1222, 1223, 1014, 1152,
140545         - /*    60 */  1186, 1200, 1185, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140546         - /*    70 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140547         - /*    80 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140548         - /*    90 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140549         - /*   100 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1194,
140550         - /*   110 */  1199, 1206, 1198, 1195, 1188, 1187, 1189, 1190, 1014, 1035,
140551         - /*   120 */  1084, 1014, 1014, 1014, 1289, 1288, 1014, 1014, 1045, 1191,
140552         - /*   130 */  1192, 1203, 1202, 1201, 1279, 1305, 1304, 1014, 1014, 1014,
140553         - /*   140 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140554         - /*   150 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140555         - /*   160 */  1014, 1014, 1014, 1014, 1014, 1014, 1045, 1297, 1041, 1041,
140556         - /*   170 */  1014, 1284, 1119, 1110, 1014, 1014, 1014, 1014, 1014, 1014,
140557         - /*   180 */  1014, 1014, 1014, 1014, 1014, 1276, 1274, 1014, 1236, 1014,
140558         - /*   190 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140559         - /*   200 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140560         - /*   210 */  1014, 1014, 1014, 1115, 1014, 1014, 1014, 1014, 1014, 1014,
140561         - /*   220 */  1014, 1014, 1014, 1014, 1299, 1014, 1249, 1098, 1115, 1115,
140562         - /*   230 */  1115, 1115, 1117, 1099, 1097, 1109, 1045, 1021, 1155, 1134,
140563         - /*   240 */  1134, 1338, 1155, 1338, 1059, 1319, 1056, 1145, 1145, 1145,
140564         - /*   250 */  1134, 1218, 1116, 1109, 1014, 1341, 1124, 1124, 1340, 1340,
140565         - /*   260 */  1124, 1166, 1087, 1155, 1093, 1093, 1093, 1093, 1124, 1032,
140566         - /*   270 */  1155, 1166, 1087, 1087, 1155, 1124, 1032, 1253, 1335, 1124,
140567         - /*   280 */  1032, 1229, 1124, 1032, 1124, 1032, 1229, 1085, 1085, 1085,
140568         - /*   290 */  1074, 1229, 1085, 1059, 1085, 1074, 1085, 1085, 1229, 1233,
140569         - /*   300 */  1233, 1229, 1138, 1133, 1138, 1133, 1138, 1133, 1138, 1133,
140570         - /*   310 */  1124, 1224, 1124, 1014, 1150, 1139, 1148, 1146, 1155, 1038,
140571         - /*   320 */  1077, 1302, 1302, 1298, 1298, 1298, 1346, 1346, 1284, 1314,
140572         - /*   330 */  1045, 1045, 1045, 1045, 1314, 1061, 1061, 1045, 1045, 1045,
140573         - /*   340 */  1045, 1314, 1014, 1014, 1014, 1014, 1014, 1014, 1309, 1014,
140574         - /*   350 */  1238, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140575         - /*   360 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1171,
140576         - /*   370 */  1014, 1017, 1281, 1014, 1014, 1280, 1014, 1014, 1014, 1014,
140577         - /*   380 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140578         - /*   390 */  1014, 1014, 1014, 1014, 1014, 1014, 1337, 1014, 1014, 1014,
140579         - /*   400 */  1014, 1014, 1014, 1252, 1251, 1014, 1014, 1126, 1014, 1014,
140580         - /*   410 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140581         - /*   420 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140582         - /*   430 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140583         - /*   440 */  1014, 1014, 1147, 1014, 1140, 1014, 1014, 1014, 1014, 1328,
140584         - /*   450 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140585         - /*   460 */  1014, 1014, 1014, 1323, 1101, 1173, 1014, 1172, 1176, 1014,
140586         - /*   470 */  1026, 1014,
       142171  + /*     0 */  1389, 1389, 1389, 1261, 1046, 1151, 1261, 1261, 1261, 1261,
       142172  + /*    10 */  1046, 1181, 1181, 1312, 1077, 1046, 1046, 1046, 1046, 1046,
       142173  + /*    20 */  1046, 1260, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142174  + /*    30 */  1046, 1046, 1046, 1187, 1046, 1046, 1046, 1046, 1262, 1263,
       142175  + /*    40 */  1046, 1046, 1046, 1311, 1313, 1197, 1196, 1195, 1194, 1294,
       142176  + /*    50 */  1168, 1192, 1185, 1189, 1256, 1257, 1255, 1259, 1262, 1263,
       142177  + /*    60 */  1046, 1188, 1226, 1240, 1225, 1046, 1046, 1046, 1046, 1046,
       142178  + /*    70 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142179  + /*    80 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142180  + /*    90 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142181  + /*   100 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142182  + /*   110 */  1046, 1234, 1239, 1246, 1238, 1235, 1228, 1227, 1229, 1230,
       142183  + /*   120 */  1046, 1067, 1116, 1046, 1046, 1046, 1329, 1328, 1046, 1046,
       142184  + /*   130 */  1077, 1231, 1232, 1243, 1242, 1241, 1319, 1345, 1344, 1046,
       142185  + /*   140 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142186  + /*   150 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142187  + /*   160 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1077,
       142188  + /*   170 */  1073, 1073, 1046, 1324, 1151, 1142, 1046, 1046, 1046, 1046,
       142189  + /*   180 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1316, 1314, 1046,
       142190  + /*   190 */  1276, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142191  + /*   200 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142192  + /*   210 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1147, 1046,
       142193  + /*   220 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1339,
       142194  + /*   230 */  1046, 1289, 1130, 1147, 1147, 1147, 1147, 1149, 1131, 1129,
       142195  + /*   240 */  1141, 1077, 1053, 1191, 1170, 1170, 1378, 1191, 1191, 1378,
       142196  + /*   250 */  1091, 1359, 1088, 1181, 1181, 1181, 1170, 1258, 1148, 1141,
       142197  + /*   260 */  1046, 1381, 1156, 1156, 1380, 1380, 1156, 1200, 1206, 1119,
       142198  + /*   270 */  1191, 1125, 1125, 1125, 1125, 1156, 1064, 1191, 1191, 1200,
       142199  + /*   280 */  1206, 1119, 1119, 1191, 1156, 1064, 1293, 1375, 1156, 1064,
       142200  + /*   290 */  1269, 1156, 1064, 1156, 1064, 1269, 1117, 1117, 1117, 1106,
       142201  + /*   300 */  1269, 1117, 1091, 1117, 1106, 1117, 1117, 1269, 1273, 1273,
       142202  + /*   310 */  1269, 1174, 1169, 1174, 1169, 1174, 1169, 1174, 1169, 1156,
       142203  + /*   320 */  1264, 1156, 1046, 1186, 1175, 1184, 1182, 1191, 1070, 1109,
       142204  + /*   330 */  1342, 1342, 1338, 1338, 1338, 1386, 1386, 1324, 1354, 1077,
       142205  + /*   340 */  1077, 1077, 1077, 1354, 1093, 1093, 1077, 1077, 1077, 1077,
       142206  + /*   350 */  1354, 1046, 1046, 1046, 1046, 1046, 1046, 1349, 1046, 1278,
       142207  + /*   360 */  1160, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142208  + /*   370 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142209  + /*   380 */  1046, 1046, 1211, 1046, 1049, 1321, 1046, 1046, 1320, 1046,
       142210  + /*   390 */  1046, 1046, 1046, 1046, 1046, 1161, 1046, 1046, 1046, 1046,
       142211  + /*   400 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142212  + /*   410 */  1046, 1046, 1046, 1046, 1377, 1046, 1046, 1046, 1046, 1046,
       142213  + /*   420 */  1046, 1292, 1291, 1046, 1046, 1158, 1046, 1046, 1046, 1046,
       142214  + /*   430 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142215  + /*   440 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142216  + /*   450 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142217  + /*   460 */  1183, 1046, 1176, 1046, 1046, 1046, 1046, 1368, 1046, 1046,
       142218  + /*   470 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142219  + /*   480 */  1046, 1363, 1133, 1213, 1046, 1212, 1216, 1046, 1058, 1046,
140587 142220   };
140588 142221   /********** End of lemon-generated parsing tables *****************************/
140589 142222   
140590 142223   /* The next table maps tokens (terminal symbols) into fallback tokens.  
140591 142224   ** If a construct like the following:
140592 142225   ** 
140593 142226   **      %fallback ID X Y Z.
................................................................................
140660 142293       0,  /*         GT => nothing */
140661 142294       0,  /*         LE => nothing */
140662 142295       0,  /*         LT => nothing */
140663 142296       0,  /*         GE => nothing */
140664 142297       0,  /*     ESCAPE => nothing */
140665 142298       0,  /*         ID => nothing */
140666 142299      59,  /*   COLUMNKW => ID */
       142300  +   59,  /*         DO => ID */
140667 142301      59,  /*        FOR => ID */
140668 142302      59,  /*     IGNORE => ID */
140669 142303      59,  /*  INITIALLY => ID */
140670 142304      59,  /*    INSTEAD => ID */
140671 142305      59,  /*         NO => ID */
140672 142306      59,  /*        KEY => ID */
140673 142307      59,  /*         OF => ID */
................................................................................
140721 142355   #ifdef YYTRACKMAXSTACKDEPTH
140722 142356     int yyhwm;                    /* High-water mark of the stack */
140723 142357   #endif
140724 142358   #ifndef YYNOERRORRECOVERY
140725 142359     int yyerrcnt;                 /* Shifts left before out of the error */
140726 142360   #endif
140727 142361     sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
       142362  +  sqlite3ParserCTX_SDECL                /* A place to hold %extra_context */
140728 142363   #if YYSTACKDEPTH<=0
140729 142364     int yystksz;                  /* Current side of the stack */
140730 142365     yyStackEntry *yystack;        /* The parser's stack */
140731 142366     yyStackEntry yystk0;          /* First stack entry */
140732 142367   #else
140733 142368     yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
140734 142369     yyStackEntry *yystackEnd;            /* Last entry in the stack */
................................................................................
140829 142464     /*   54 */ "GT",
140830 142465     /*   55 */ "LE",
140831 142466     /*   56 */ "LT",
140832 142467     /*   57 */ "GE",
140833 142468     /*   58 */ "ESCAPE",
140834 142469     /*   59 */ "ID",
140835 142470     /*   60 */ "COLUMNKW",
140836         -  /*   61 */ "FOR",
140837         -  /*   62 */ "IGNORE",
140838         -  /*   63 */ "INITIALLY",
140839         -  /*   64 */ "INSTEAD",
140840         -  /*   65 */ "NO",
140841         -  /*   66 */ "KEY",
140842         -  /*   67 */ "OF",
140843         -  /*   68 */ "OFFSET",
140844         -  /*   69 */ "PRAGMA",
140845         -  /*   70 */ "RAISE",
140846         -  /*   71 */ "RECURSIVE",
140847         -  /*   72 */ "REPLACE",
140848         -  /*   73 */ "RESTRICT",
140849         -  /*   74 */ "ROW",
140850         -  /*   75 */ "TRIGGER",
140851         -  /*   76 */ "VACUUM",
140852         -  /*   77 */ "VIEW",
140853         -  /*   78 */ "VIRTUAL",
140854         -  /*   79 */ "WITH",
140855         -  /*   80 */ "REINDEX",
140856         -  /*   81 */ "RENAME",
140857         -  /*   82 */ "CTIME_KW",
140858         -  /*   83 */ "ANY",
140859         -  /*   84 */ "BITAND",
140860         -  /*   85 */ "BITOR",
140861         -  /*   86 */ "LSHIFT",
140862         -  /*   87 */ "RSHIFT",
140863         -  /*   88 */ "PLUS",
140864         -  /*   89 */ "MINUS",
140865         -  /*   90 */ "STAR",
140866         -  /*   91 */ "SLASH",
140867         -  /*   92 */ "REM",
140868         -  /*   93 */ "CONCAT",
140869         -  /*   94 */ "COLLATE",
140870         -  /*   95 */ "BITNOT",
140871         -  /*   96 */ "INDEXED",
140872         -  /*   97 */ "STRING",
140873         -  /*   98 */ "JOIN_KW",
140874         -  /*   99 */ "CONSTRAINT",
140875         -  /*  100 */ "DEFAULT",
140876         -  /*  101 */ "NULL",
140877         -  /*  102 */ "PRIMARY",
140878         -  /*  103 */ "UNIQUE",
140879         -  /*  104 */ "CHECK",
140880         -  /*  105 */ "REFERENCES",
140881         -  /*  106 */ "AUTOINCR",
140882         -  /*  107 */ "ON",
140883         -  /*  108 */ "INSERT",
140884         -  /*  109 */ "DELETE",
140885         -  /*  110 */ "UPDATE",
140886         -  /*  111 */ "SET",
140887         -  /*  112 */ "DEFERRABLE",
140888         -  /*  113 */ "FOREIGN",
140889         -  /*  114 */ "DROP",
140890         -  /*  115 */ "UNION",
140891         -  /*  116 */ "ALL",