System.Data.SQLite
Check-in [1da3d937ca]
Not logged in

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

Overview
Comment:Update SQLite core library to the latest trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 1da3d937cac12246c234ccdd778eaa8d7e6eab95
User & Date: mistachkin 2016-10-13 17:31:09
Context
2016-10-13
18:09
Test changes for Mono 4.6.x. check-in: 5955cd2f21 user: mistachkin tags: trunk
17:31
Update SQLite core library to the latest trunk. check-in: 1da3d937ca user: mistachkin tags: trunk
2016-10-10
20:48
Enhance the 'getRowsFromDataTable' test suite helper procedure. check-in: cd91b8e6bf user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

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

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

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

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

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.14.2.  By combining all the individual C code files into this
            3  +** version 3.15.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
................................................................................
   364    364   ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
   365    365   ** numbers used in [SQLITE_VERSION].)^
   366    366   ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
   367    367   ** be larger than the release from which it is derived.  Either Y will
   368    368   ** be held constant and Z will be incremented or else Y will be incremented
   369    369   ** and Z will be reset to zero.
   370    370   **
   371         -** Since version 3.6.18, SQLite source code has been stored in the
          371  +** Since [version 3.6.18] ([dateof:3.6.18]), 
          372  +** SQLite source code has been stored in the
   372    373   ** <a href="http://www.fossil-scm.org/">Fossil configuration management
   373    374   ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
   374    375   ** a string which identifies a particular check-in of SQLite
   375    376   ** within its configuration management system.  ^The SQLITE_SOURCE_ID
   376    377   ** string contains the date and time of the check-in (UTC) and an SHA1
   377    378   ** hash of the entire source tree.
   378    379   **
   379    380   ** See also: [sqlite3_libversion()],
   380    381   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   381    382   ** [sqlite_version()] and [sqlite_source_id()].
   382    383   */
   383         -#define SQLITE_VERSION        "3.14.2"
   384         -#define SQLITE_VERSION_NUMBER 3014002
   385         -#define SQLITE_SOURCE_ID      "2016-09-12 18:50:49 29dbef4b8585f753861a36d6dd102ca634197bd6"
          384  +#define SQLITE_VERSION        "3.15.0"
          385  +#define SQLITE_VERSION_NUMBER 3015000
          386  +#define SQLITE_SOURCE_ID      "2016-10-13 12:56:18 4d66ac98deaa85218be7ff0eb254f78b96d8e8d4"
   386    387   
   387    388   /*
   388    389   ** CAPI3REF: Run-Time Library Version Numbers
   389    390   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   390    391   **
   391    392   ** These interfaces provide the same information as the [SQLITE_VERSION],
   392    393   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   708    709   ** CAPI3REF: Extended Result Codes
   709    710   ** KEYWORDS: {extended result code definitions}
   710    711   **
   711    712   ** In its default configuration, SQLite API routines return one of 30 integer
   712    713   ** [result codes].  However, experience has shown that many of
   713    714   ** these result codes are too coarse-grained.  They do not provide as
   714    715   ** much information about problems as programmers might like.  In an effort to
   715         -** address this, newer versions of SQLite (version 3.3.8 and later) include
          716  +** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
          717  +** and later) include
   716    718   ** support for additional result codes that provide more detailed information
   717    719   ** about errors. These [extended result codes] are enabled or disabled
   718    720   ** on a per database connection basis using the
   719    721   ** [sqlite3_extended_result_codes()] API.  Or, the extended code for
   720    722   ** the most recent error can be obtained using
   721    723   ** [sqlite3_extended_errcode()].
   722    724   */
................................................................................
  2225   2227   ** C-API or the SQL function.
  2226   2228   ** The second parameter is a pointer to an integer into which
  2227   2229   ** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
  2228   2230   ** is disabled or enabled following this call.  The second parameter may
  2229   2231   ** be a NULL pointer, in which case the new setting is not reported back.
  2230   2232   ** </dd>
  2231   2233   **
         2234  +** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
         2235  +** <dd> ^This option is used to change the name of the "main" database
         2236  +** schema.  ^The sole argument is a pointer to a constant UTF8 string
         2237  +** which will become the new schema name in place of "main".  ^SQLite
         2238  +** does not make a copy of the new main schema name string, so the application
         2239  +** must ensure that the argument passed into this DBCONFIG option is unchanged
         2240  +** until after the database connection closes.
         2241  +** </dd>
         2242  +**
  2232   2243   ** </dl>
  2233   2244   */
         2245  +#define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  2234   2246   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  2235   2247   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  2236   2248   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  2237   2249   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  2238   2250   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  2239   2251   
  2240   2252   
................................................................................
  4297   4309   ** more threads at the same moment in time.
  4298   4310   **
  4299   4311   ** For all versions of SQLite up to and including 3.6.23.1, a call to
  4300   4312   ** [sqlite3_reset()] was required after sqlite3_step() returned anything
  4301   4313   ** other than [SQLITE_ROW] before any subsequent invocation of
  4302   4314   ** sqlite3_step().  Failure to reset the prepared statement using 
  4303   4315   ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
  4304         -** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
         4316  +** sqlite3_step().  But after [version 3.6.23.1] ([dateof:3.6.23.1],
         4317  +** sqlite3_step() began
  4305   4318   ** calling [sqlite3_reset()] automatically in this circumstance rather
  4306   4319   ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
  4307   4320   ** break because any application that ever receives an SQLITE_MISUSE error
  4308   4321   ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
  4309   4322   ** can be used to restore the legacy behavior.
  4310   4323   **
  4311   4324   ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
................................................................................
  5660   5673   **
  5661   5674   ** ^(This routine enables or disables the sharing of the database cache
  5662   5675   ** and schema data structures between [database connection | connections]
  5663   5676   ** to the same database. Sharing is enabled if the argument is true
  5664   5677   ** and disabled if the argument is false.)^
  5665   5678   **
  5666   5679   ** ^Cache sharing is enabled and disabled for an entire process.
  5667         -** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
         5680  +** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]). 
         5681  +** In prior versions of SQLite,
  5668   5682   ** sharing was enabled or disabled for each thread separately.
  5669   5683   **
  5670   5684   ** ^(The cache sharing mode set by this interface effects all subsequent
  5671   5685   ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
  5672   5686   ** Existing database connections continue use the sharing mode
  5673   5687   ** that was in effect at the time they were opened.)^
  5674   5688   **
................................................................................
  5754   5768   ** <li> An alternative page cache implementation is specified using
  5755   5769   **      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
  5756   5770   ** <li> The page cache allocates from its own memory pool supplied
  5757   5771   **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
  5758   5772   **      from the heap.
  5759   5773   ** </ul>)^
  5760   5774   **
  5761         -** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
         5775  +** Beginning with SQLite [version 3.7.3] ([dateof:3.7.3]), 
         5776  +** the soft heap limit is enforced
  5762   5777   ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
  5763   5778   ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
  5764   5779   ** the soft heap limit is enforced on every memory allocation.  Without
  5765   5780   ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
  5766   5781   ** when memory is allocated by the page cache.  Testing suggests that because
  5767   5782   ** the page cache is the predominate memory user in SQLite, most
  5768   5783   ** applications will achieve adequate soft heap limit enforcement without
................................................................................
  6148   6163   ** any database changes. In other words, if the xUpdate() returns
  6149   6164   ** SQLITE_CONSTRAINT, the database contents must be exactly as they were
  6150   6165   ** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
  6151   6166   ** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
  6152   6167   ** the xUpdate method are automatically rolled back by SQLite.
  6153   6168   **
  6154   6169   ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
  6155         -** structure for SQLite version 3.8.2. If a virtual table extension is
         6170  +** structure for SQLite [version 3.8.2] ([dateof:3.8.2]). 
         6171  +** If a virtual table extension is
  6156   6172   ** used with an SQLite version earlier than 3.8.2, the results of attempting 
  6157   6173   ** to read or write the estimatedRows field are undefined (but are likely 
  6158   6174   ** to included crashing the application). The estimatedRows field should
  6159   6175   ** therefore only be used if [sqlite3_libversion_number()] returns a
  6160   6176   ** value greater than or equal to 3008002. Similarly, the idxFlags field
  6161         -** was added for version 3.9.0. It may therefore only be used if
         6177  +** was added for [version 3.9.0] ([dateof:3.9.0]). 
         6178  +** It may therefore only be used if
  6162   6179   ** sqlite3_libversion_number() returns a value greater than or equal to
  6163   6180   ** 3009000.
  6164   6181   */
  6165   6182   struct sqlite3_index_info {
  6166   6183     /* Inputs */
  6167   6184     int nConstraint;           /* Number of entries in aConstraint */
  6168   6185     struct sqlite3_index_constraint {
................................................................................
  6852   6869   */
  6853   6870   #define SQLITE_MUTEX_FAST             0
  6854   6871   #define SQLITE_MUTEX_RECURSIVE        1
  6855   6872   #define SQLITE_MUTEX_STATIC_MASTER    2
  6856   6873   #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
  6857   6874   #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
  6858   6875   #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
  6859         -#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
         6876  +#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_randomness() */
  6860   6877   #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
  6861   6878   #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
  6862   6879   #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
  6863   6880   #define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
  6864   6881   #define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
  6865   6882   #define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
  6866   6883   #define SQLITE_MUTEX_STATIC_VFS1     11  /* For use by built-in VFS */
................................................................................
  6956   6973   #define SQLITE_TESTCTRL_ALWAYS                  13
  6957   6974   #define SQLITE_TESTCTRL_RESERVE                 14
  6958   6975   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  6959   6976   #define SQLITE_TESTCTRL_ISKEYWORD               16
  6960   6977   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  6961   6978   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  6962   6979   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
         6980  +#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  6963   6981   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  6964   6982   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  6965   6983   #define SQLITE_TESTCTRL_BYTEORDER               22
  6966   6984   #define SQLITE_TESTCTRL_ISINIT                  23
  6967   6985   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  6968   6986   #define SQLITE_TESTCTRL_IMPOSTER                25
  6969   6987   #define SQLITE_TESTCTRL_LAST                    25
................................................................................
  8895   8913     const char *zTab                /* Table name */
  8896   8914   );
  8897   8915   
  8898   8916   /*
  8899   8917   ** CAPI3REF: Set a table filter on a Session Object.
  8900   8918   **
  8901   8919   ** The second argument (xFilter) is the "filter callback". For changes to rows 
  8902         -** in tables that are not attached to the Session oject, the filter is called
         8920  +** in tables that are not attached to the Session object, the filter is called
  8903   8921   ** to determine whether changes to the table's rows should be tracked or not. 
  8904   8922   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
  8905   8923   ** attached, xFilter will not be called again.
  8906   8924   */
  8907   8925   void sqlite3session_table_filter(
  8908   8926     sqlite3_session *pSession,      /* Session object */
  8909   8927     int(*xFilter)(
................................................................................
  9161   9179   ** destroyed.
  9162   9180   **
  9163   9181   ** Assuming the changeset blob was created by one of the
  9164   9182   ** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
  9165   9183   ** [sqlite3changeset_invert()] functions, all changes within the changeset 
  9166   9184   ** that apply to a single table are grouped together. This means that when 
  9167   9185   ** an application iterates through a changeset using an iterator created by 
  9168         -** this function, all changes that relate to a single table are visted 
         9186  +** this function, all changes that relate to a single table are visited 
  9169   9187   ** consecutively. There is no chance that the iterator will visit a change 
  9170   9188   ** the applies to table X, then one for table Y, and then later on visit 
  9171   9189   ** another change for table X.
  9172   9190   */
  9173   9191   int sqlite3changeset_start(
  9174   9192     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
  9175   9193     int nChangeset,                 /* Size of changeset blob in bytes */
................................................................................
  9248   9266   ** This function is used to find which columns comprise the PRIMARY KEY of
  9249   9267   ** the table modified by the change that iterator pIter currently points to.
  9250   9268   ** If successful, *pabPK is set to point to an array of nCol entries, where
  9251   9269   ** nCol is the number of columns in the table. Elements of *pabPK are set to
  9252   9270   ** 0x01 if the corresponding column is part of the tables primary key, or
  9253   9271   ** 0x00 if it is not.
  9254   9272   **
  9255         -** If argumet pnCol is not NULL, then *pnCol is set to the number of columns
         9273  +** If argument pnCol is not NULL, then *pnCol is set to the number of columns
  9256   9274   ** in the table.
  9257   9275   **
  9258   9276   ** If this function is called when the iterator does not point to a valid
  9259   9277   ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
  9260   9278   ** SQLITE_OK is returned and the output variables populated as described
  9261   9279   ** above.
  9262   9280   */
................................................................................
  9465   9483     void *pB,                       /* Pointer to buffer containing changeset B */
  9466   9484     int *pnOut,                     /* OUT: Number of bytes in output changeset */
  9467   9485     void **ppOut                    /* OUT: Buffer containing output changeset */
  9468   9486   );
  9469   9487   
  9470   9488   
  9471   9489   /*
  9472         -** Changegroup handle.
         9490  +** CAPI3REF: Changegroup Handle
  9473   9491   */
  9474   9492   typedef struct sqlite3_changegroup sqlite3_changegroup;
  9475   9493   
  9476   9494   /*
  9477         -** CAPI3REF: Combine two or more changesets into a single changeset.
         9495  +** CAPI3REF: Create A New Changegroup Object
  9478   9496   **
  9479   9497   ** An sqlite3_changegroup object is used to combine two or more changesets
  9480   9498   ** (or patchsets) into a single changeset (or patchset). A single changegroup
  9481   9499   ** object may combine changesets or patchsets, but not both. The output is
  9482   9500   ** always in the same format as the input.
  9483   9501   **
  9484   9502   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
  9507   9525   ** As well as the regular sqlite3changegroup_add() and 
  9508   9526   ** sqlite3changegroup_output() functions, also available are the streaming
  9509   9527   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
  9510   9528   */
  9511   9529   int sqlite3changegroup_new(sqlite3_changegroup **pp);
  9512   9530   
  9513   9531   /*
         9532  +** CAPI3REF: Add A Changeset To A Changegroup
         9533  +**
  9514   9534   ** Add all changes within the changeset (or patchset) in buffer pData (size
  9515   9535   ** nData bytes) to the changegroup. 
  9516   9536   **
  9517   9537   ** If the buffer contains a patchset, then all prior calls to this function
  9518   9538   ** on the same changegroup object must also have specified patchsets. Or, if
  9519   9539   ** the buffer contains a changeset, so must have the earlier calls to this
  9520   9540   ** function. Otherwise, SQLITE_ERROR is returned and no changes are added
................................................................................
  9521   9541   ** to the changegroup.
  9522   9542   **
  9523   9543   ** Rows within the changeset and changegroup are identified by the values in
  9524   9544   ** their PRIMARY KEY columns. A change in the changeset is considered to
  9525   9545   ** apply to the same row as a change already present in the changegroup if
  9526   9546   ** the two rows have the same primary key.
  9527   9547   **
  9528         -** Changes to rows that that do not already appear in the changegroup are
         9548  +** Changes to rows that do not already appear in the changegroup are
  9529   9549   ** simply copied into it. Or, if both the new changeset and the changegroup
  9530   9550   ** contain changes that apply to a single row, the final contents of the
  9531   9551   ** changegroup depends on the type of each change, as follows:
  9532   9552   **
  9533   9553   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
  9534   9554   **   <tr><th style="white-space:pre">Existing Change  </th>
  9535   9555   **       <th style="white-space:pre">New Change       </th>
................................................................................
  9582   9602   ** final contents of the changegroup is undefined.
  9583   9603   **
  9584   9604   ** If no error occurs, SQLITE_OK is returned.
  9585   9605   */
  9586   9606   int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
  9587   9607   
  9588   9608   /*
         9609  +** CAPI3REF: Obtain A Composite Changeset From A Changegroup
         9610  +**
  9589   9611   ** Obtain a buffer containing a changeset (or patchset) representing the
  9590   9612   ** current contents of the changegroup. If the inputs to the changegroup
  9591   9613   ** were themselves changesets, the output is a changeset. Or, if the
  9592   9614   ** inputs were patchsets, the output is also a patchset.
  9593   9615   **
  9594   9616   ** As with the output of the sqlite3session_changeset() and
  9595   9617   ** sqlite3session_patchset() functions, all changes related to a single
................................................................................
  9610   9632   int sqlite3changegroup_output(
  9611   9633     sqlite3_changegroup*,
  9612   9634     int *pnData,                    /* OUT: Size of output buffer in bytes */
  9613   9635     void **ppData                   /* OUT: Pointer to output buffer */
  9614   9636   );
  9615   9637   
  9616   9638   /*
  9617         -** Delete a changegroup object.
         9639  +** CAPI3REF: Delete A Changegroup Object
  9618   9640   */
  9619   9641   void sqlite3changegroup_delete(sqlite3_changegroup*);
  9620   9642   
  9621   9643   /*
  9622   9644   ** CAPI3REF: Apply A Changeset To A Database
  9623   9645   **
  9624   9646   ** Apply a changeset to a database. This function attempts to update the
................................................................................
 11387  11409   #define TK_USING                          125
 11388  11410   #define TK_ORDER                          126
 11389  11411   #define TK_GROUP                          127
 11390  11412   #define TK_HAVING                         128
 11391  11413   #define TK_LIMIT                          129
 11392  11414   #define TK_WHERE                          130
 11393  11415   #define TK_INTO                           131
 11394         -#define TK_INTEGER                        132
 11395         -#define TK_FLOAT                          133
 11396         -#define TK_BLOB                           134
        11416  +#define TK_FLOAT                          132
        11417  +#define TK_BLOB                           133
        11418  +#define TK_INTEGER                        134
 11397  11419   #define TK_VARIABLE                       135
 11398  11420   #define TK_CASE                           136
 11399  11421   #define TK_WHEN                           137
 11400  11422   #define TK_THEN                           138
 11401  11423   #define TK_ELSE                           139
 11402  11424   #define TK_INDEX                          140
 11403  11425   #define TK_ALTER                          141
................................................................................
 11413  11435   #define TK_FUNCTION                       151
 11414  11436   #define TK_COLUMN                         152
 11415  11437   #define TK_AGG_FUNCTION                   153
 11416  11438   #define TK_AGG_COLUMN                     154
 11417  11439   #define TK_UMINUS                         155
 11418  11440   #define TK_UPLUS                          156
 11419  11441   #define TK_REGISTER                       157
 11420         -#define TK_ASTERISK                       158
 11421         -#define TK_SPAN                           159
 11422         -#define TK_SPACE                          160
 11423         -#define TK_ILLEGAL                        161
        11442  +#define TK_VECTOR                         158
        11443  +#define TK_SELECT_COLUMN                  159
        11444  +#define TK_ASTERISK                       160
        11445  +#define TK_SPAN                           161
        11446  +#define TK_SPACE                          162
        11447  +#define TK_ILLEGAL                        163
 11424  11448   
 11425  11449   /* The token codes above must all fit in 8 bits */
 11426  11450   #define TKFLG_MASK           0xff  
 11427  11451   
 11428  11452   /* Flags that can be added to a token code when it is not
 11429  11453   ** being stored in a u8: */
 11430  11454   #define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
................................................................................
 12075  12099   SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
 12076  12100   SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
 12077  12101   SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
 12078  12102   SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
 12079  12103   SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
 12080  12104   SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
 12081  12105   SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
        12106  +#ifndef SQLITE_OMIT_SHARED_CACHE
 12082  12107   SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
        12108  +#endif
 12083  12109   SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
 12084  12110   
 12085  12111   SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
 12086  12112   SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
 12087  12113   SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
 12088  12114   
 12089  12115   SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
................................................................................
 12278  12304   SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
 12279  12305   SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
 12280  12306   SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
 12281  12307   
 12282  12308   SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
 12283  12309   SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
 12284  12310   
        12311  +#ifndef SQLITE_OMIT_INCRBLOB
 12285  12312   SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
 12286  12313   SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
        12314  +#endif
 12287  12315   SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
 12288  12316   SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
 12289  12317   SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
 12290  12318   SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
 12291  12319   SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
 12292  12320   
 12293  12321   #ifndef NDEBUG
................................................................................
 12440  12468   ** A sub-routine used to implement a trigger program.
 12441  12469   */
 12442  12470   struct SubProgram {
 12443  12471     VdbeOp *aOp;                  /* Array of opcodes for sub-program */
 12444  12472     int nOp;                      /* Elements in aOp[] */
 12445  12473     int nMem;                     /* Number of memory cells required */
 12446  12474     int nCsr;                     /* Number of cursors required */
 12447         -  int nOnce;                    /* Number of OP_Once instructions */
 12448  12475     void *token;                  /* id that may be used to recursive triggers */
 12449  12476     SubProgram *pNext;            /* Next sub-program already visited */
 12450  12477   };
 12451  12478   
 12452  12479   /*
 12453  12480   ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
 12454  12481   ** it takes up less space.
................................................................................
 12563  12590   #define OP_NotNull        35 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
 12564  12591   #define OP_Ne             36 /* same as TK_NE, synopsis: IF r[P3]!=r[P1]   */
 12565  12592   #define OP_Eq             37 /* same as TK_EQ, synopsis: IF r[P3]==r[P1]   */
 12566  12593   #define OP_Gt             38 /* same as TK_GT, synopsis: IF r[P3]>r[P1]    */
 12567  12594   #define OP_Le             39 /* same as TK_LE, synopsis: IF r[P3]<=r[P1]   */
 12568  12595   #define OP_Lt             40 /* same as TK_LT, synopsis: IF r[P3]<r[P1]    */
 12569  12596   #define OP_Ge             41 /* same as TK_GE, synopsis: IF r[P3]>=r[P1]   */
 12570         -#define OP_Last           42
        12597  +#define OP_ElseNotEq      42 /* same as TK_ESCAPE                          */
 12571  12598   #define OP_BitAnd         43 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 12572  12599   #define OP_BitOr          44 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 12573  12600   #define OP_ShiftLeft      45 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 12574  12601   #define OP_ShiftRight     46 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 12575  12602   #define OP_Add            47 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 12576  12603   #define OP_Subtract       48 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 12577  12604   #define OP_Multiply       49 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 12578  12605   #define OP_Divide         50 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 12579  12606   #define OP_Remainder      51 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 12580  12607   #define OP_Concat         52 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 12581         -#define OP_SorterSort     53
        12608  +#define OP_Last           53
 12582  12609   #define OP_BitNot         54 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 12583         -#define OP_Sort           55
 12584         -#define OP_Rewind         56
 12585         -#define OP_IdxLE          57 /* synopsis: key=r[P3@P4]                     */
 12586         -#define OP_IdxGT          58 /* synopsis: key=r[P3@P4]                     */
 12587         -#define OP_IdxLT          59 /* synopsis: key=r[P3@P4]                     */
 12588         -#define OP_IdxGE          60 /* synopsis: key=r[P3@P4]                     */
 12589         -#define OP_RowSetRead     61 /* synopsis: r[P3]=rowset(P1)                 */
 12590         -#define OP_RowSetTest     62 /* synopsis: if r[P3] in rowset(P1) goto P2   */
 12591         -#define OP_Program        63
 12592         -#define OP_FkIfZero       64 /* synopsis: if fkctr[P1]==0 goto P2          */
 12593         -#define OP_IfPos          65 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
 12594         -#define OP_IfNotZero      66 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
 12595         -#define OP_DecrJumpZero   67 /* synopsis: if (--r[P1])==0 goto P2          */
 12596         -#define OP_IncrVacuum     68
 12597         -#define OP_VNext          69
 12598         -#define OP_Init           70 /* synopsis: Start at P2                      */
 12599         -#define OP_Return         71
 12600         -#define OP_EndCoroutine   72
 12601         -#define OP_HaltIfNull     73 /* synopsis: if r[P3]=null halt               */
 12602         -#define OP_Halt           74
 12603         -#define OP_Integer        75 /* synopsis: r[P2]=P1                         */
 12604         -#define OP_Int64          76 /* synopsis: r[P2]=P4                         */
 12605         -#define OP_String         77 /* synopsis: r[P2]='P4' (len=P1)              */
 12606         -#define OP_Null           78 /* synopsis: r[P2..P3]=NULL                   */
 12607         -#define OP_SoftNull       79 /* synopsis: r[P1]=NULL                       */
 12608         -#define OP_Blob           80 /* synopsis: r[P2]=P4 (len=P1)                */
 12609         -#define OP_Variable       81 /* synopsis: r[P2]=parameter(P1,P4)           */
 12610         -#define OP_Move           82 /* synopsis: r[P2@P3]=r[P1@P3]                */
 12611         -#define OP_Copy           83 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
 12612         -#define OP_SCopy          84 /* synopsis: r[P2]=r[P1]                      */
 12613         -#define OP_IntCopy        85 /* synopsis: r[P2]=r[P1]                      */
 12614         -#define OP_ResultRow      86 /* synopsis: output=r[P1@P2]                  */
 12615         -#define OP_CollSeq        87
 12616         -#define OP_Function0      88 /* synopsis: r[P3]=func(r[P2@P5])             */
 12617         -#define OP_Function       89 /* synopsis: r[P3]=func(r[P2@P5])             */
 12618         -#define OP_AddImm         90 /* synopsis: r[P1]=r[P1]+P2                   */
 12619         -#define OP_RealAffinity   91
 12620         -#define OP_Cast           92 /* synopsis: affinity(r[P1])                  */
 12621         -#define OP_Permutation    93
 12622         -#define OP_Compare        94 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 12623         -#define OP_Column         95 /* synopsis: r[P3]=PX                         */
 12624         -#define OP_Affinity       96 /* synopsis: affinity(r[P1@P2])               */
        12610  +#define OP_SorterSort     55
        12611  +#define OP_Sort           56
        12612  +#define OP_Rewind         57
        12613  +#define OP_IdxLE          58 /* synopsis: key=r[P3@P4]                     */
        12614  +#define OP_IdxGT          59 /* synopsis: key=r[P3@P4]                     */
        12615  +#define OP_IdxLT          60 /* synopsis: key=r[P3@P4]                     */
        12616  +#define OP_IdxGE          61 /* synopsis: key=r[P3@P4]                     */
        12617  +#define OP_RowSetRead     62 /* synopsis: r[P3]=rowset(P1)                 */
        12618  +#define OP_RowSetTest     63 /* synopsis: if r[P3] in rowset(P1) goto P2   */
        12619  +#define OP_Program        64
        12620  +#define OP_FkIfZero       65 /* synopsis: if fkctr[P1]==0 goto P2          */
        12621  +#define OP_IfPos          66 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
        12622  +#define OP_IfNotZero      67 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
        12623  +#define OP_DecrJumpZero   68 /* synopsis: if (--r[P1])==0 goto P2          */
        12624  +#define OP_IncrVacuum     69
        12625  +#define OP_VNext          70
        12626  +#define OP_Init           71 /* synopsis: Start at P2                      */
        12627  +#define OP_Return         72
        12628  +#define OP_EndCoroutine   73
        12629  +#define OP_HaltIfNull     74 /* synopsis: if r[P3]=null halt               */
        12630  +#define OP_Halt           75
        12631  +#define OP_Integer        76 /* synopsis: r[P2]=P1                         */
        12632  +#define OP_Int64          77 /* synopsis: r[P2]=P4                         */
        12633  +#define OP_String         78 /* synopsis: r[P2]='P4' (len=P1)              */
        12634  +#define OP_Null           79 /* synopsis: r[P2..P3]=NULL                   */
        12635  +#define OP_SoftNull       80 /* synopsis: r[P1]=NULL                       */
        12636  +#define OP_Blob           81 /* synopsis: r[P2]=P4 (len=P1)                */
        12637  +#define OP_Variable       82 /* synopsis: r[P2]=parameter(P1,P4)           */
        12638  +#define OP_Move           83 /* synopsis: r[P2@P3]=r[P1@P3]                */
        12639  +#define OP_Copy           84 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
        12640  +#define OP_SCopy          85 /* synopsis: r[P2]=r[P1]                      */
        12641  +#define OP_IntCopy        86 /* synopsis: r[P2]=r[P1]                      */
        12642  +#define OP_ResultRow      87 /* synopsis: output=r[P1@P2]                  */
        12643  +#define OP_CollSeq        88
        12644  +#define OP_Function0      89 /* synopsis: r[P3]=func(r[P2@P5])             */
        12645  +#define OP_Function       90 /* synopsis: r[P3]=func(r[P2@P5])             */
        12646  +#define OP_AddImm         91 /* synopsis: r[P1]=r[P1]+P2                   */
        12647  +#define OP_RealAffinity   92
        12648  +#define OP_Cast           93 /* synopsis: affinity(r[P1])                  */
        12649  +#define OP_Permutation    94
        12650  +#define OP_Compare        95 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
        12651  +#define OP_Column         96 /* synopsis: r[P3]=PX                         */
 12625  12652   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 12626         -#define OP_MakeRecord     98 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 12627         -#define OP_Count          99 /* synopsis: r[P2]=count()                    */
 12628         -#define OP_ReadCookie    100
 12629         -#define OP_SetCookie     101
 12630         -#define OP_ReopenIdx     102 /* synopsis: root=P2 iDb=P3                   */
 12631         -#define OP_OpenRead      103 /* synopsis: root=P2 iDb=P3                   */
 12632         -#define OP_OpenWrite     104 /* synopsis: root=P2 iDb=P3                   */
 12633         -#define OP_OpenAutoindex 105 /* synopsis: nColumn=P2                       */
 12634         -#define OP_OpenEphemeral 106 /* synopsis: nColumn=P2                       */
 12635         -#define OP_SorterOpen    107
 12636         -#define OP_SequenceTest  108 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 12637         -#define OP_OpenPseudo    109 /* synopsis: P3 columns in r[P2]              */
 12638         -#define OP_Close         110
 12639         -#define OP_ColumnsUsed   111
 12640         -#define OP_Sequence      112 /* synopsis: r[P2]=cursor[P1].ctr++           */
 12641         -#define OP_NewRowid      113 /* synopsis: r[P2]=rowid                      */
 12642         -#define OP_Insert        114 /* synopsis: intkey=r[P3] data=r[P2]          */
 12643         -#define OP_InsertInt     115 /* synopsis: intkey=P3 data=r[P2]             */
 12644         -#define OP_Delete        116
 12645         -#define OP_ResetCount    117
 12646         -#define OP_SorterCompare 118 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 12647         -#define OP_SorterData    119 /* synopsis: r[P2]=data                       */
 12648         -#define OP_RowKey        120 /* synopsis: r[P2]=key                        */
 12649         -#define OP_RowData       121 /* synopsis: r[P2]=data                       */
 12650         -#define OP_Rowid         122 /* synopsis: r[P2]=rowid                      */
 12651         -#define OP_NullRow       123
 12652         -#define OP_SorterInsert  124
 12653         -#define OP_IdxInsert     125 /* synopsis: key=r[P2]                        */
 12654         -#define OP_IdxDelete     126 /* synopsis: key=r[P2@P3]                     */
 12655         -#define OP_Seek          127 /* synopsis: Move P3 to P1.rowid              */
 12656         -#define OP_IdxRowid      128 /* synopsis: r[P2]=rowid                      */
 12657         -#define OP_Destroy       129
 12658         -#define OP_Clear         130
 12659         -#define OP_ResetSorter   131
 12660         -#define OP_CreateIndex   132 /* synopsis: r[P2]=root iDb=P1                */
 12661         -#define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 12662         -#define OP_CreateTable   134 /* synopsis: r[P2]=root iDb=P1                */
 12663         -#define OP_ParseSchema   135
 12664         -#define OP_LoadAnalysis  136
 12665         -#define OP_DropTable     137
 12666         -#define OP_DropIndex     138
 12667         -#define OP_DropTrigger   139
 12668         -#define OP_IntegrityCk   140
 12669         -#define OP_RowSetAdd     141 /* synopsis: rowset(P1)=r[P2]                 */
 12670         -#define OP_Param         142
 12671         -#define OP_FkCounter     143 /* synopsis: fkctr[P1]+=P2                    */
 12672         -#define OP_MemMax        144 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 12673         -#define OP_OffsetLimit   145 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 12674         -#define OP_AggStep0      146 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 12675         -#define OP_AggStep       147 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 12676         -#define OP_AggFinal      148 /* synopsis: accum=r[P1] N=P2                 */
 12677         -#define OP_Expire        149
 12678         -#define OP_TableLock     150 /* synopsis: iDb=P1 root=P2 write=P3          */
 12679         -#define OP_VBegin        151
 12680         -#define OP_VCreate       152
 12681         -#define OP_VDestroy      153
 12682         -#define OP_VOpen         154
 12683         -#define OP_VColumn       155 /* synopsis: r[P3]=vcolumn(P2)                */
 12684         -#define OP_VRename       156
 12685         -#define OP_Pagecount     157
 12686         -#define OP_MaxPgcnt      158
 12687         -#define OP_CursorHint    159
 12688         -#define OP_Noop          160
 12689         -#define OP_Explain       161
        12653  +#define OP_Affinity       98 /* synopsis: affinity(r[P1@P2])               */
        12654  +#define OP_MakeRecord     99 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        12655  +#define OP_Count         100 /* synopsis: r[P2]=count()                    */
        12656  +#define OP_ReadCookie    101
        12657  +#define OP_SetCookie     102
        12658  +#define OP_ReopenIdx     103 /* synopsis: root=P2 iDb=P3                   */
        12659  +#define OP_OpenRead      104 /* synopsis: root=P2 iDb=P3                   */
        12660  +#define OP_OpenWrite     105 /* synopsis: root=P2 iDb=P3                   */
        12661  +#define OP_OpenAutoindex 106 /* synopsis: nColumn=P2                       */
        12662  +#define OP_OpenEphemeral 107 /* synopsis: nColumn=P2                       */
        12663  +#define OP_SorterOpen    108
        12664  +#define OP_SequenceTest  109 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
        12665  +#define OP_OpenPseudo    110 /* synopsis: P3 columns in r[P2]              */
        12666  +#define OP_Close         111
        12667  +#define OP_ColumnsUsed   112
        12668  +#define OP_Sequence      113 /* synopsis: r[P2]=cursor[P1].ctr++           */
        12669  +#define OP_NewRowid      114 /* synopsis: r[P2]=rowid                      */
        12670  +#define OP_Insert        115 /* synopsis: intkey=r[P3] data=r[P2]          */
        12671  +#define OP_InsertInt     116 /* synopsis: intkey=P3 data=r[P2]             */
        12672  +#define OP_Delete        117
        12673  +#define OP_ResetCount    118
        12674  +#define OP_SorterCompare 119 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
        12675  +#define OP_SorterData    120 /* synopsis: r[P2]=data                       */
        12676  +#define OP_RowKey        121 /* synopsis: r[P2]=key                        */
        12677  +#define OP_RowData       122 /* synopsis: r[P2]=data                       */
        12678  +#define OP_Rowid         123 /* synopsis: r[P2]=rowid                      */
        12679  +#define OP_NullRow       124
        12680  +#define OP_SorterInsert  125
        12681  +#define OP_IdxInsert     126 /* synopsis: key=r[P2]                        */
        12682  +#define OP_IdxDelete     127 /* synopsis: key=r[P2@P3]                     */
        12683  +#define OP_Seek          128 /* synopsis: Move P3 to P1.rowid              */
        12684  +#define OP_IdxRowid      129 /* synopsis: r[P2]=rowid                      */
        12685  +#define OP_Destroy       130
        12686  +#define OP_Clear         131
        12687  +#define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
        12688  +#define OP_ResetSorter   133
        12689  +#define OP_CreateIndex   134 /* synopsis: r[P2]=root iDb=P1                */
        12690  +#define OP_CreateTable   135 /* synopsis: r[P2]=root iDb=P1                */
        12691  +#define OP_ParseSchema   136
        12692  +#define OP_LoadAnalysis  137
        12693  +#define OP_DropTable     138
        12694  +#define OP_DropIndex     139
        12695  +#define OP_DropTrigger   140
        12696  +#define OP_IntegrityCk   141
        12697  +#define OP_RowSetAdd     142 /* synopsis: rowset(P1)=r[P2]                 */
        12698  +#define OP_Param         143
        12699  +#define OP_FkCounter     144 /* synopsis: fkctr[P1]+=P2                    */
        12700  +#define OP_MemMax        145 /* synopsis: r[P1]=max(r[P1],r[P2])           */
        12701  +#define OP_OffsetLimit   146 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
        12702  +#define OP_AggStep0      147 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        12703  +#define OP_AggStep       148 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        12704  +#define OP_AggFinal      149 /* synopsis: accum=r[P1] N=P2                 */
        12705  +#define OP_Expire        150
        12706  +#define OP_TableLock     151 /* synopsis: iDb=P1 root=P2 write=P3          */
        12707  +#define OP_VBegin        152
        12708  +#define OP_VCreate       153
        12709  +#define OP_VDestroy      154
        12710  +#define OP_VOpen         155
        12711  +#define OP_VColumn       156 /* synopsis: r[P3]=vcolumn(P2)                */
        12712  +#define OP_VRename       157
        12713  +#define OP_Pagecount     158
        12714  +#define OP_MaxPgcnt      159
        12715  +#define OP_CursorHint    160
        12716  +#define OP_Noop          161
        12717  +#define OP_Explain       162
 12690  12718   
 12691  12719   /* Properties such as "out2" or "jump" that are specified in
 12692  12720   ** comments following the "case" for each opcode in the vdbe.c
 12693  12721   ** are encoded into bitvectors as follows:
 12694  12722   */
 12695  12723   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 12696  12724   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 12702  12730   /*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\
 12703  12731   /*   8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,\
 12704  12732   /*  16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x03, 0x03, 0x09,\
 12705  12733   /*  24 */ 0x09, 0x09, 0x09, 0x26, 0x26, 0x09, 0x09, 0x09,\
 12706  12734   /*  32 */ 0x09, 0x09, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 12707  12735   /*  40 */ 0x0b, 0x0b, 0x01, 0x26, 0x26, 0x26, 0x26, 0x26,\
 12708  12736   /*  48 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x01, 0x12, 0x01,\
 12709         -/*  56 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x23, 0x0b, 0x01,\
 12710         -/*  64 */ 0x01, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x02,\
 12711         -/*  72 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
 12712         -/*  80 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 12713         -/*  88 */ 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00,\
 12714         -/*  96 */ 0x00, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
        12737  +/*  56 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x23, 0x0b,\
        12738  +/*  64 */ 0x01, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01,\
        12739  +/*  72 */ 0x02, 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10,\
        12740  +/*  80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00,\
        12741  +/*  88 */ 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
        12742  +/*  96 */ 0x00, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 12715  12743   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 12716         -/* 112 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 12717         -/* 120 */ 0x00, 0x00, 0x10, 0x00, 0x04, 0x04, 0x00, 0x00,\
 12718         -/* 128 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
 12719         -/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00,\
 12720         -/* 144 */ 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 12721         -/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
 12722         -/* 160 */ 0x00, 0x00,}
        12744  +/* 112 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
        12745  +/* 120 */ 0x00, 0x00, 0x00, 0x10, 0x00, 0x04, 0x04, 0x00,\
        12746  +/* 128 */ 0x00, 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10,\
        12747  +/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10,\
        12748  +/* 144 */ 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00,\
        12749  +/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
        12750  +/* 160 */ 0x00, 0x00, 0x00,}
 12723  12751   
 12724  12752   /* The sqlite3P2Values() routine is able to run faster if it knows
 12725  12753   ** the value of the largest JUMP opcode.  The smaller the maximum
 12726  12754   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 12727  12755   ** generated this include file strives to group all JUMP opcodes
 12728  12756   ** together near the beginning of the list.
 12729  12757   */
 12730         -#define SQLITE_MX_JUMP_OPCODE  70  /* Maximum JUMP opcode */
        12758  +#define SQLITE_MX_JUMP_OPCODE  71  /* Maximum JUMP opcode */
 12731  12759   
 12732  12760   /************** End of opcodes.h *********************************************/
 12733  12761   /************** Continuing where we left off in vdbe.h ***********************/
 12734  12762   
 12735  12763   /*
 12736  12764   ** Prototypes for the VDBE interface.  See comments on the implementation
 12737  12765   ** for a description of what each of these routines does.
................................................................................
 13692  13720   ** Each database file to be accessed by the system is an instance
 13693  13721   ** of the following structure.  There are normally two of these structures
 13694  13722   ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
 13695  13723   ** aDb[1] is the database file used to hold temporary tables.  Additional
 13696  13724   ** databases may be attached.
 13697  13725   */
 13698  13726   struct Db {
 13699         -  char *zName;         /* Name of this database */
        13727  +  char *zDbSName;      /* Name of this database. (schema name, not filename) */
 13700  13728     Btree *pBt;          /* The B*Tree structure for this database file */
 13701  13729     u8 safety_level;     /* How aggressive at syncing data to disk */
 13702  13730     u8 bSyncSet;         /* True if "PRAGMA synchronous=N" has been run */
 13703  13731     Schema *pSchema;     /* Pointer to database schema (possibly shared) */
 13704  13732   };
 13705  13733   
 13706  13734   /*
................................................................................
 14328  14356   ** changing the affinity.
 14329  14357   **
 14330  14358   ** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
 14331  14359   ** It causes an assert() to fire if either operand to a comparison
 14332  14360   ** operator is NULL.  It is added to certain comparison operators to
 14333  14361   ** prove that the operands are always NOT NULL.
 14334  14362   */
        14363  +#define SQLITE_KEEPNULL     0x08  /* Used by vector == or <> */
 14335  14364   #define SQLITE_JUMPIFNULL   0x10  /* jumps if either operand is NULL */
 14336  14365   #define SQLITE_STOREP2      0x20  /* Store result in reg[P2] rather than jump */
 14337  14366   #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
 14338  14367   #define SQLITE_NOTNULL      0x90  /* Assert that operands are never NULL */
 14339  14368   
 14340  14369   /*
 14341  14370   ** An object of this type is created for each virtual table present in
................................................................................
 14892  14921   
 14893  14922   #if SQLITE_MAX_EXPR_DEPTH>0
 14894  14923     int nHeight;           /* Height of the tree headed by this node */
 14895  14924   #endif
 14896  14925     int iTable;            /* TK_COLUMN: cursor number of table holding column
 14897  14926                            ** TK_REGISTER: register number
 14898  14927                            ** TK_TRIGGER: 1 -> new, 0 -> old
 14899         -                         ** EP_Unlikely:  134217728 times likelihood */
        14928  +                         ** EP_Unlikely:  134217728 times likelihood
        14929  +                         ** TK_SELECT: 1st register of result vector */
 14900  14930     ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
 14901         -                         ** TK_VARIABLE: variable number (always >= 1). */
        14931  +                         ** TK_VARIABLE: variable number (always >= 1).
        14932  +                         ** TK_SELECT_COLUMN: column of the result vector */
 14902  14933     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 14903  14934     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 14904  14935     u8 op2;                /* TK_REGISTER: original value of Expr.op
 14905  14936                            ** TK_COLUMN: the value of p5 for OP_Column
 14906  14937                            ** TK_AGG_FUNCTION: nesting depth */
 14907  14938     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 14908  14939     Table *pTab;           /* Table for TK_COLUMN expressions. */
................................................................................
 14930  14961   #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
 14931  14962   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
 14932  14963   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
 14933  14964   #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
 14934  14965   #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
 14935  14966   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
 14936  14967   #define EP_Alias     0x400000 /* Is an alias for a result set column */
        14968  +#define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
 14937  14969   
 14938  14970   /*
 14939  14971   ** Combinations of two or more EP_* flags
 14940  14972   */
 14941  14973   #define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */
 14942  14974   
 14943  14975   /*
................................................................................
 15375  15407   
 15376  15408   /*
 15377  15409   ** An instance of this object describes where to put of the results of
 15378  15410   ** a SELECT statement.
 15379  15411   */
 15380  15412   struct SelectDest {
 15381  15413     u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
 15382         -  char affSdst;        /* Affinity used when eDest==SRT_Set */
        15414  +  char *zAffSdst;      /* Affinity used when eDest==SRT_Set */
 15383  15415     int iSDParm;         /* A parameter used by the eDest disposal method */
 15384  15416     int iSdst;           /* Base register where results are written */
 15385  15417     int nSdst;           /* Number of registers allocated */
 15386  15418     ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
 15387  15419   };
 15388  15420   
 15389  15421   /*
................................................................................
 15481  15513     u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
 15482  15514     u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
 15483  15515     u8 mayAbort;         /* True if statement may throw an ABORT exception */
 15484  15516     u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
 15485  15517     u8 okConstFactor;    /* OK to factor out constants */
 15486  15518     u8 disableLookaside; /* Number of times lookaside has been disabled */
 15487  15519     u8 nColCache;        /* Number of entries in aColCache[] */
 15488         -  int aTempReg[8];     /* Holding area for temporary registers */
 15489  15520     int nRangeReg;       /* Size of the temporary register block */
 15490  15521     int iRangeReg;       /* First register in temporary register block */
 15491  15522     int nErr;            /* Number of errors seen */
 15492  15523     int nTab;            /* Number of previously allocated VDBE cursors */
 15493  15524     int nMem;            /* Number of memory cells used so far */
 15494         -  int nSet;            /* Number of sets used so far */
 15495         -  int nOnce;           /* Number of OP_Once instructions so far */
 15496  15525     int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
 15497  15526     int szOpAlloc;       /* Bytes of memory space allocated for Vdbe.aOp[] */
 15498         -  int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
 15499  15527     int ckBase;          /* Base register of data during check constraints */
 15500  15528     int iSelfTab;        /* Table of an index whose exprs are being coded */
 15501  15529     int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
 15502  15530     int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
 15503  15531     int nLabel;          /* Number of labels used */
 15504  15532     int *aLabel;         /* Space to hold the labels */
 15505         -  struct yColCache {
 15506         -    int iTable;           /* Table cursor number */
 15507         -    i16 iColumn;          /* Table column number */
 15508         -    u8 tempReg;           /* iReg is a temp register that needs to be freed */
 15509         -    int iLevel;           /* Nesting level */
 15510         -    int iReg;             /* Reg with value of this column. 0 means none. */
 15511         -    int lru;              /* Least recently used entry has the smallest value */
 15512         -  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
 15513  15533     ExprList *pConstExpr;/* Constant expressions */
 15514  15534     Token constraintName;/* Name of the constraint currently being parsed */
 15515  15535     yDbMask writeMask;   /* Start a write transaction on these databases */
 15516  15536     yDbMask cookieMask;  /* Bitmask of schema verified databases */
 15517         -  int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
 15518  15537     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 15519  15538     int regRoot;         /* Register holding root page number for new objects */
 15520  15539     int nMaxArg;         /* Max args passed to user function by sub-program */
 15521  15540   #if SELECTTRACE_ENABLED
 15522  15541     int nSelect;         /* Number of SELECT statements seen */
 15523  15542     int nSelectIndent;   /* How far to indent SELECTTRACE() output */
 15524  15543   #endif
 15525  15544   #ifndef SQLITE_OMIT_SHARED_CACHE
 15526  15545     int nTableLock;        /* Number of locks in aTableLock */
 15527  15546     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 15528  15547   #endif
 15529  15548     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 15530         -
 15531         -  /* Information used while coding trigger programs. */
 15532  15549     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
 15533  15550     Table *pTriggerTab;  /* Table triggers are being coded for */
 15534  15551     int addrCrTab;       /* Address of OP_CreateTable opcode on CREATE TABLE */
 15535  15552     u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
 15536  15553     u32 oldmask;         /* Mask of old.* columns referenced */
 15537  15554     u32 newmask;         /* Mask of new.* columns referenced */
 15538  15555     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 15539  15556     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 15540  15557     u8 disableTriggers;  /* True to disable triggers */
        15558  +
        15559  +  /**************************************************************************
        15560  +  ** Fields above must be initialized to zero.  The fields that follow,
        15561  +  ** down to the beginning of the recursive section, do not need to be
        15562  +  ** initialized as they will be set before being used.  The boundary is
        15563  +  ** determined by offsetof(Parse,aColCache).
        15564  +  **************************************************************************/
        15565  +
        15566  +  struct yColCache {
        15567  +    int iTable;           /* Table cursor number */
        15568  +    i16 iColumn;          /* Table column number */
        15569  +    u8 tempReg;           /* iReg is a temp register that needs to be freed */
        15570  +    int iLevel;           /* Nesting level */
        15571  +    int iReg;             /* Reg with value of this column. 0 means none. */
        15572  +    int lru;              /* Least recently used entry has the smallest value */
        15573  +  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
        15574  +  int aTempReg[8];        /* Holding area for temporary registers */
        15575  +  Token sNameToken;       /* Token with unqualified schema object name */
        15576  +  Token sLastToken;       /* The last token parsed */
 15541  15577   
 15542  15578     /************************************************************************
 15543  15579     ** Above is constant between recursions.  Below is reset before and after
 15544  15580     ** each recursion.  The boundary between these two regions is determined
 15545  15581     ** using offsetof(Parse,nVar) so the nVar field must be the first field
 15546  15582     ** in the recursive region.
 15547  15583     ************************************************************************/
................................................................................
 15550  15586     int nzVar;                /* Number of available slots in azVar[] */
 15551  15587     u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
 15552  15588     u8 explain;               /* True if the EXPLAIN flag is found on the query */
 15553  15589   #ifndef SQLITE_OMIT_VIRTUALTABLE
 15554  15590     u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
 15555  15591     int nVtabLock;            /* Number of virtual tables to lock */
 15556  15592   #endif
 15557         -  int nAlias;               /* Number of aliased result set columns */
 15558  15593     int nHeight;              /* Expression tree height of current sub-select */
 15559  15594   #ifndef SQLITE_OMIT_EXPLAIN
 15560  15595     int iSelectId;            /* ID of current select for EXPLAIN output */
 15561  15596     int iNextSelectId;        /* Next available select ID for EXPLAIN output */
 15562  15597   #endif
 15563  15598     char **azVar;             /* Pointers to names of parameters */
 15564  15599     Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
 15565  15600     const char *zTail;        /* All SQL text past the last semicolon parsed */
 15566  15601     Table *pNewTable;         /* A table being constructed by CREATE TABLE */
 15567  15602     Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
 15568  15603     const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
 15569         -  Token sNameToken;         /* Token with unqualified schema object name */
 15570         -  Token sLastToken;         /* The last token parsed */
 15571  15604   #ifndef SQLITE_OMIT_VIRTUALTABLE
 15572  15605     Token sArg;               /* Complete text of a module argument */
 15573  15606     Table **apVtabLock;       /* Pointer to virtual tables needing locking */
 15574  15607   #endif
 15575  15608     Table *pZombieTab;        /* List of Table objects to delete after code gen */
 15576  15609     TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
 15577  15610     With *pWith;              /* Current WITH clause, or NULL */
 15578  15611     With *pWithToFree;        /* Free this WITH object at the end of the parse */
 15579  15612   };
 15580  15613   
        15614  +/*
        15615  +** Sizes and pointers of various parts of the Parse object.
        15616  +*/
        15617  +#define PARSE_HDR_SZ offsetof(Parse,aColCache) /* Recursive part w/o aColCache*/
        15618  +#define PARSE_RECURSE_SZ offsetof(Parse,nVar)  /* Recursive part */
        15619  +#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
        15620  +#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ)  /* Pointer to tail */
        15621  +
 15581  15622   /*
 15582  15623   ** Return true if currently inside an sqlite3_declare_vtab() call.
 15583  15624   */
 15584  15625   #ifdef SQLITE_OMIT_VIRTUALTABLE
 15585  15626     #define IN_DECLARE_VTAB 0
 15586  15627   #else
 15587  15628     #define IN_DECLARE_VTAB (pParse->declareVtab)
................................................................................
 15822  15863     void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
 15823  15864     void *pVdbeBranchArg;                                     /* 1st argument */
 15824  15865   #endif
 15825  15866   #ifndef SQLITE_OMIT_BUILTIN_TEST
 15826  15867     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
 15827  15868   #endif
 15828  15869     int bLocaltimeFault;              /* True to fail localtime() calls */
        15870  +  int iOnceResetThreshold;          /* When to reset OP_Once counters */
 15829  15871   };
 15830  15872   
 15831  15873   /*
 15832  15874   ** This macro is used inside of assert() statements to indicate that
 15833  15875   ** the assert is only valid on a well-formed database.  Instead of:
 15834  15876   **
 15835  15877   **     assert( X );
................................................................................
 16107  16149   #endif
 16108  16150   #if defined(SQLITE_TEST)
 16109  16151   SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 16110  16152   #endif
 16111  16153   
 16112  16154   #if defined(SQLITE_DEBUG)
 16113  16155   SQLITE_PRIVATE   void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
        16156  +SQLITE_PRIVATE   void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
 16114  16157   SQLITE_PRIVATE   void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
 16115  16158   SQLITE_PRIVATE   void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
 16116  16159   SQLITE_PRIVATE   void sqlite3TreeViewWith(TreeView*, const With*, u8);
 16117  16160   #endif
 16118  16161   
 16119  16162   
 16120  16163   SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
................................................................................
 16135  16178   SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
 16136  16179   SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
 16137  16180   SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
 16138  16181   SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
 16139  16182   SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
 16140  16183   SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
 16141  16184   SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
 16142         -SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
        16185  +SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
 16143  16186   SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
 16144  16187   SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
        16188  +SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
 16145  16189   SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int);
 16146  16190   SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
 16147  16191   SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
 16148  16192   SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
 16149  16193   SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
 16150  16194   SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
 16151  16195   SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
................................................................................
 16173  16217   SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
 16174  16218   SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
 16175  16219   SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
 16176  16220   SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
 16177  16221   SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
 16178  16222                       sqlite3_vfs**,char**,char **);
 16179  16223   SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
 16180         -SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
 16181  16224   
 16182  16225   #ifdef SQLITE_OMIT_BUILTIN_TEST
 16183  16226   # define sqlite3FaultSim(X) SQLITE_OK
 16184  16227   #else
 16185  16228   SQLITE_PRIVATE   int sqlite3FaultSim(int);
 16186  16229   #endif
 16187  16230   
................................................................................
 16296  16339   #define LOCATE_VIEW    0x01
 16297  16340   #define LOCATE_NOERR   0x02
 16298  16341   SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
 16299  16342   SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
 16300  16343   SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
 16301  16344   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
 16302  16345   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
 16303         -SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
 16304         -SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
        16346  +SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*);
        16347  +SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int);
 16305  16348   SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 16306  16349   SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int);
 16307  16350   SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
 16308  16351   SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
 16309  16352   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 16310  16353   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 16311  16354   SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
................................................................................
 16473  16516   #define putVarint    sqlite3PutVarint
 16474  16517   
 16475  16518   
 16476  16519   SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
 16477  16520   SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
 16478  16521   SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
 16479  16522   SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
        16523  +SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
 16480  16524   SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
 16481  16525   SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
 16482  16526   SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
 16483  16527   SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
 16484  16528   SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
 16485  16529   SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
 16486  16530   SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
................................................................................
 16538  16582   SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 16539  16583   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 16540  16584   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 16541  16585   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 16542  16586   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 16543  16587   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 16544  16588   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
 16545         -SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
        16589  +SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr *, int, int);
 16546  16590   SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 16547  16591   SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
 16548  16592   SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
 16549  16593   SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
 16550  16594   SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
 16551  16595   SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
 16552  16596   SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
................................................................................
 16592  16636   SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
 16593  16637   SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
 16594  16638   SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
 16595  16639   SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
 16596  16640   
 16597  16641   SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
 16598  16642   SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
        16643  +
        16644  +#ifndef SQLITE_OMIT_SUBQUERY
        16645  +SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
        16646  +#else
        16647  +# define sqlite3ExprCheckIN(x,y) SQLITE_OK
        16648  +#endif
 16599  16649   
 16600  16650   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 16601  16651   SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
 16602         -SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
        16652  +SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
        16653  +    Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
 16603  16654   SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
 16604  16655   SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
 16605  16656   SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
        16657  +SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
 16606  16658   #endif
 16607  16659   
 16608  16660   /*
 16609  16661   ** The interface to the LEMON-generated parser
 16610  16662   */
 16611  16663   SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
 16612  16664   SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
................................................................................
 16751  16803   #define IN_INDEX_NOOP         5   /* No table available. Use comparisons */
 16752  16804   /*
 16753  16805   ** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
 16754  16806   */
 16755  16807   #define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
 16756  16808   #define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
 16757  16809   #define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
 16758         -SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
        16810  +SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*);
 16759  16811   
 16760  16812   SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
 16761  16813   SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
 16762  16814   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 16763  16815   SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
 16764  16816   #endif
 16765  16817   
................................................................................
 16856  16908   SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
 16857  16909   #endif
 16858  16910   
 16859  16911   #if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
 16860  16912   SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
 16861  16913   #endif
 16862  16914   
        16915  +SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr);
        16916  +SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr);
        16917  +SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
        16918  +SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
        16919  +
 16863  16920   #endif /* SQLITEINT_H */
 16864  16921   
 16865  16922   /************** End of sqliteInt.h *******************************************/
 16866  16923   /************** Begin file global.c ******************************************/
 16867  16924   /*
 16868  16925   ** 2008 June 13
 16869  16926   **
................................................................................
 16941  16998   ** Bit 0x20 is set if the mapped character requires translation to upper
 16942  16999   ** case. i.e. if the character is a lower-case ASCII character.
 16943  17000   ** If x is a lower-case ASCII character, then its upper-case equivalent
 16944  17001   ** is (x - 0x20). Therefore toupper() can be implemented as:
 16945  17002   **
 16946  17003   **   (x & ~(map[x]&0x20))
 16947  17004   **
 16948         -** Standard function tolower() is implemented using the sqlite3UpperToLower[]
        17005  +** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
 16949  17006   ** array. tolower() is used more often than toupper() by SQLite.
 16950  17007   **
 16951         -** Bit 0x40 is set if the character non-alphanumeric and can be used in an 
        17008  +** Bit 0x40 is set if the character is non-alphanumeric and can be used in an 
 16952  17009   ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
 16953  17010   ** non-ASCII UTF character. Hence the test for whether or not a character is
 16954  17011   ** part of an identifier is 0x46.
 16955         -**
 16956         -** SQLite's versions are identical to the standard versions assuming a
 16957         -** locale of "C". They are implemented as macros in sqliteInt.h.
 16958  17012   */
 16959  17013   #ifdef SQLITE_ASCII
 16960  17014   SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
 16961  17015     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
 16962  17016     0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
 16963  17017     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
 16964  17018     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
................................................................................
 17023  17077   ** page size in bytes.
 17024  17078   */
 17025  17079   #ifndef SQLITE_SORTER_PMASZ
 17026  17080   # define SQLITE_SORTER_PMASZ 250
 17027  17081   #endif
 17028  17082   
 17029  17083   /* Statement journals spill to disk when their size exceeds the following
 17030         -** threashold (in bytes). 0 means that statement journals are created and
        17084  +** threshold (in bytes). 0 means that statement journals are created and
 17031  17085   ** written to disk immediately (the default behavior for SQLite versions
 17032  17086   ** before 3.12.0).  -1 means always keep the entire statement journal in
 17033  17087   ** memory.  (The statement journal is also always held entirely in memory
 17034  17088   ** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
 17035  17089   ** setting.)
 17036  17090   */
 17037  17091   #ifndef SQLITE_STMTJRNL_SPILL 
................................................................................
 17087  17141   #ifdef SQLITE_VDBE_COVERAGE
 17088  17142      0,                         /* xVdbeBranch */
 17089  17143      0,                         /* pVbeBranchArg */
 17090  17144   #endif
 17091  17145   #ifndef SQLITE_OMIT_BUILTIN_TEST
 17092  17146      0,                         /* xTestCallback */
 17093  17147   #endif
 17094         -   0                          /* bLocaltimeFault */
        17148  +   0,                         /* bLocaltimeFault */
        17149  +   0x7ffffffe                 /* iOnceResetThreshold */
 17095  17150   };
 17096  17151   
 17097  17152   /*
 17098  17153   ** Hash table for global functions - functions common to all
 17099  17154   ** database connections.  After initialization, this table is
 17100  17155   ** read-only.
 17101  17156   */
................................................................................
 17110  17165   };
 17111  17166   
 17112  17167   
 17113  17168   /*
 17114  17169   ** The value of the "pending" byte must be 0x40000000 (1 byte past the
 17115  17170   ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
 17116  17171   ** the database page that contains the pending byte.  It never attempts
 17117         -** to read or write that page.  The pending byte page is set assign
        17172  +** to read or write that page.  The pending byte page is set aside
 17118  17173   ** for use by the VFS layers as space for managing file locks.
 17119  17174   **
 17120  17175   ** During testing, it is often desirable to move the pending byte to
 17121  17176   ** a different position in the file.  This allows code that has to
 17122  17177   ** deal with the pending byte to run on files that are much smaller
 17123  17178   ** than 1 GiB.  The sqlite3_test_control() interface can be used to
 17124  17179   ** move the pending byte.
................................................................................
 17670  17725   ** Boolean values
 17671  17726   */
 17672  17727   typedef unsigned Bool;
 17673  17728   
 17674  17729   /* Opaque type used by code in vdbesort.c */
 17675  17730   typedef struct VdbeSorter VdbeSorter;
 17676  17731   
 17677         -/* Opaque type used by the explainer */
 17678         -typedef struct Explain Explain;
 17679         -
 17680  17732   /* Elements of the linked list at Vdbe.pAuxData */
 17681  17733   typedef struct AuxData AuxData;
 17682  17734   
 17683  17735   /* Types of VDBE cursors */
 17684  17736   #define CURTYPE_BTREE       0
 17685  17737   #define CURTYPE_SORTER      1
 17686  17738   #define CURTYPE_VTAB        2
................................................................................
 17747  17799     u32 *aOffset;         /* Pointer to aType[nField] */
 17748  17800     u32 aType[1];         /* Type values for all entries in the record */
 17749  17801     /* 2*nField extra array elements allocated for aType[], beyond the one
 17750  17802     ** static element declared in the structure.  nField total array slots for
 17751  17803     ** aType[] and nField+1 array slots for aOffset[] */
 17752  17804   };
 17753  17805   
        17806  +
        17807  +/*
        17808  +** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
        17809  +*/
        17810  +#define CACHE_STALE 0
        17811  +
 17754  17812   /*
 17755  17813   ** When a sub-program is executed (OP_Program), a structure of this type
 17756  17814   ** is allocated to store the current value of the program counter, as
 17757  17815   ** well as the current memory cell array and various other frame specific
 17758  17816   ** values stored in the Vdbe struct. When the sub-program is finished, 
 17759  17817   ** these values are copied back to the Vdbe from the VdbeFrame structure,
 17760  17818   ** restoring the state of the VM to as it was before the sub-program
................................................................................
 17775  17833   typedef struct VdbeFrame VdbeFrame;
 17776  17834   struct VdbeFrame {
 17777  17835     Vdbe *v;                /* VM this frame belongs to */
 17778  17836     VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
 17779  17837     Op *aOp;                /* Program instructions for parent frame */
 17780  17838     i64 *anExec;            /* Event counters from parent frame */
 17781  17839     Mem *aMem;              /* Array of memory cells for parent frame */
 17782         -  u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
 17783  17840     VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
 17784  17841     void *token;            /* Copy of SubProgram.token */
 17785  17842     i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
 17786  17843     AuxData *pAuxData;      /* Linked list of auxdata allocations */
 17787  17844     int nCursor;            /* Number of entries in apCsr */
 17788  17845     int pc;                 /* Program Counter in parent (calling) frame */
 17789  17846     int nOp;                /* Size of aOp array */
 17790  17847     int nMem;               /* Number of entries in aMem */
 17791         -  int nOnceFlag;          /* Number of entries in aOnceFlag */
 17792  17848     int nChildMem;          /* Number of memory cells for child frame */
 17793  17849     int nChildCsr;          /* Number of cursors for child frame */
 17794  17850     int nChange;            /* Statement changes (Vdbe.nChange)     */
 17795  17851     int nDbChange;          /* Value of db->nChange */
 17796  17852   };
 17797  17853   
 17798  17854   #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
 17799  17855   
 17800         -/*
 17801         -** A value for VdbeCursor.cacheValid that means the cache is always invalid.
 17802         -*/
 17803         -#define CACHE_STALE 0
 17804         -
 17805  17856   /*
 17806  17857   ** Internally, the vdbe manipulates nearly all SQL values as Mem
 17807  17858   ** structures. Each Mem struct may cache multiple representations (string,
 17808  17859   ** integer etc.) of the same value.
 17809  17860   */
 17810  17861   struct Mem {
 17811  17862     union MemValue {
................................................................................
 17938  17989     int isError;            /* Error code returned by the function. */
 17939  17990     u8 skipFlag;            /* Skip accumulator loading if true */
 17940  17991     u8 fErrorOrAux;         /* isError!=0 or pVdbe->pAuxData modified */
 17941  17992     u8 argc;                /* Number of arguments */
 17942  17993     sqlite3_value *argv[1]; /* Argument set */
 17943  17994   };
 17944  17995   
 17945         -/*
 17946         -** An Explain object accumulates indented output which is helpful
 17947         -** in describing recursive data structures.
 17948         -*/
 17949         -struct Explain {
 17950         -  Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
 17951         -  StrAccum str;      /* The string being accumulated */
 17952         -  int nIndent;       /* Number of elements in aIndent */
 17953         -  u16 aIndent[100];  /* Levels of indentation */
 17954         -  char zBase[100];   /* Initial space */
 17955         -};
 17956         -
 17957  17996   /* A bitfield type for use inside of structures.  Always follow with :N where
 17958  17997   ** N is the number of bits.
 17959  17998   */
 17960  17999   typedef unsigned bft;  /* Bit Field Type */
 17961  18000   
 17962  18001   typedef struct ScanStatus ScanStatus;
 17963  18002   struct ScanStatus {
................................................................................
 17974  18013   ** state of the virtual machine.
 17975  18014   **
 17976  18015   ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
 17977  18016   ** is really a pointer to an instance of this structure.
 17978  18017   */
 17979  18018   struct Vdbe {
 17980  18019     sqlite3 *db;            /* The database connection that owns this statement */
        18020  +  Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
        18021  +  Parse *pParse;          /* Parsing context used to create this Vdbe */
        18022  +  ynVar nVar;             /* Number of entries in aVar[] */
        18023  +  ynVar nzVar;            /* Number of entries in azVar[] */
        18024  +  u32 magic;              /* Magic number for sanity checking */
        18025  +  int nMem;               /* Number of memory locations currently allocated */
        18026  +  int nCursor;            /* Number of slots in apCsr[] */
        18027  +  u32 cacheCtr;           /* VdbeCursor row cache generation counter */
        18028  +  int pc;                 /* The program counter */
        18029  +  int rc;                 /* Value to return */
        18030  +  int nChange;            /* Number of db changes made since last reset */
        18031  +  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
        18032  +  i64 iCurrentTime;       /* Value of julianday('now') for this statement */
        18033  +  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
        18034  +  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
        18035  +  i64 nStmtDefImmCons;    /* Number of def. imm constraints when stmt started */
        18036  +
        18037  +  /* When allocating a new Vdbe object, all of the fields below should be
        18038  +  ** initialized to zero or NULL */
        18039  +
 17981  18040     Op *aOp;                /* Space to hold the virtual machine's program */
 17982  18041     Mem *aMem;              /* The memory locations */
 17983  18042     Mem **apArg;            /* Arguments to currently executing user function */
 17984  18043     Mem *aColName;          /* Column names to return */
 17985  18044     Mem *pResultSet;        /* Pointer to an array of results */
 17986         -  Parse *pParse;          /* Parsing context used to create this Vdbe */
 17987         -  int nMem;               /* Number of memory locations currently allocated */
 17988         -  int nOp;                /* Number of instructions in the program */
 17989         -  int nCursor;            /* Number of slots in apCsr[] */
 17990         -  u32 magic;              /* Magic number for sanity checking */
 17991  18045     char *zErrMsg;          /* Error message written here */
 17992         -  Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
 17993  18046     VdbeCursor **apCsr;     /* One element of this array for each open cursor */
 17994  18047     Mem *aVar;              /* Values for the OP_Variable opcode. */
 17995  18048     char **azVar;           /* Name of variables */
 17996         -  ynVar nVar;             /* Number of entries in aVar[] */
 17997         -  ynVar nzVar;            /* Number of entries in azVar[] */
 17998         -  u32 cacheCtr;           /* VdbeCursor row cache generation counter */
 17999         -  int pc;                 /* The program counter */
 18000         -  int rc;                 /* Value to return */
        18049  +#ifndef SQLITE_OMIT_TRACE
        18050  +  i64 startTime;          /* Time when query started - used for profiling */
        18051  +#endif
        18052  +  int nOp;                /* Number of instructions in the program */
 18001  18053   #ifdef SQLITE_DEBUG
 18002  18054     int rcApp;              /* errcode set by sqlite3_result_error_code() */
 18003  18055   #endif
 18004  18056     u16 nResColumn;         /* Number of columns in one row of the result set */
 18005  18057     u8 errorAction;         /* Recovery action to do in case of an error */
        18058  +  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
 18006  18059     bft expired:1;          /* True if the VM needs to be recompiled */
 18007  18060     bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
 18008         -  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
 18009  18061     bft explain:2;          /* True if EXPLAIN present on SQL command */
 18010  18062     bft changeCntOn:1;      /* True to update the change-counter */
 18011  18063     bft runOnlyOnce:1;      /* Automatically expire on reset */
 18012  18064     bft usesStmtJournal:1;  /* True if uses a statement journal */
 18013  18065     bft readOnly:1;         /* True for statements that do not write */
 18014  18066     bft bIsReader:1;        /* True for statements that read */
 18015  18067     bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
 18016         -  int nChange;            /* Number of db changes made since last reset */
 18017  18068     yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
 18018  18069     yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
 18019         -  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
 18020  18070     u32 aCounter[5];        /* Counters used by sqlite3_stmt_status() */
 18021         -#ifndef SQLITE_OMIT_TRACE
 18022         -  i64 startTime;          /* Time when query started - used for profiling */
 18023         -#endif
 18024         -  i64 iCurrentTime;       /* Value of julianday('now') for this statement */
 18025         -  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
 18026         -  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
 18027         -  i64 nStmtDefImmCons;    /* Number of def. imm constraints when stmt started */
 18028  18071     char *zSql;             /* Text of the SQL statement that generated this */
 18029  18072     void *pFree;            /* Free this when deleting the vdbe */
 18030  18073     VdbeFrame *pFrame;      /* Parent frame */
 18031  18074     VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
 18032  18075     int nFrame;             /* Number of frames in pFrame list */
 18033  18076     u32 expmask;            /* Binding to these vars invalidates VM */
 18034  18077     SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
 18035         -  int nOnceFlag;          /* Size of array aOnceFlag[] */
 18036         -  u8 *aOnceFlag;          /* Flags for OP_Once */
 18037  18078     AuxData *pAuxData;      /* Linked list of auxdata allocations */
 18038  18079   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 18039  18080     i64 *anExec;            /* Number of times each op has been executed */
 18040  18081     int nScan;              /* Entries in aScan[] */
 18041  18082     ScanStatus *aScan;      /* Scan definitions for sqlite3_stmt_scanstatus() */
 18042  18083   #endif
 18043  18084   };
 18044  18085   
 18045  18086   /*
 18046  18087   ** The following are allowed values for Vdbe.magic
 18047  18088   */
 18048         -#define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
 18049         -#define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
 18050         -#define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
 18051         -#define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
        18089  +#define VDBE_MAGIC_INIT     0x16bceaa5    /* Building a VDBE program */
        18090  +#define VDBE_MAGIC_RUN      0x2df20da3    /* VDBE is ready to execute */
        18091  +#define VDBE_MAGIC_HALT     0x319c2973    /* VDBE has completed execution */
        18092  +#define VDBE_MAGIC_RESET    0x48fa9f76    /* Reset and ready to run again */
        18093  +#define VDBE_MAGIC_DEAD     0x5606c3c8    /* The VDBE has been deallocated */
 18052  18094   
 18053  18095   /*
 18054  18096   ** Structure used to store the context required by the 
 18055  18097   ** sqlite3_preupdate_*() API functions.
 18056  18098   */
 18057  18099   struct PreUpdate {
 18058  18100     Vdbe *v;
................................................................................
 25870  25912       }
 25871  25913       sqlite3TreeViewPop(pView);
 25872  25914     }
 25873  25915   }
 25874  25916   
 25875  25917   
 25876  25918   /*
 25877         -** Generate a human-readable description of a the Select object.
        25919  +** Generate a human-readable description of a Select object.
 25878  25920   */
 25879  25921   SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
 25880  25922     int n = 0;
 25881  25923     int cnt = 0;
 25882  25924     pView = sqlite3TreeViewPush(pView, moreToFollow);
 25883  25925     if( p->pWith ){
 25884  25926       sqlite3TreeViewWith(pView, p->pWith, 1);
................................................................................
 26200  26242       }
 26201  26243   #endif
 26202  26244       case TK_MATCH: {
 26203  26245         sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
 26204  26246                             pExpr->iTable, pExpr->iColumn, zFlgs);
 26205  26247         sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
 26206  26248         break;
        26249  +    }
        26250  +    case TK_VECTOR: {
        26251  +      sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
        26252  +      break;
        26253  +    }
        26254  +    case TK_SELECT_COLUMN: {
        26255  +      sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
        26256  +      sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
        26257  +      break;
 26207  26258       }
 26208  26259       default: {
 26209  26260         sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
 26210  26261         break;
 26211  26262       }
 26212  26263     }
 26213  26264     if( zBinOp ){
................................................................................
 26216  26267       sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
 26217  26268     }else if( zUniOp ){
 26218  26269       sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
 26219  26270       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 26220  26271     }
 26221  26272     sqlite3TreeViewPop(pView);
 26222  26273   }
        26274  +
 26223  26275   
 26224  26276   /*
 26225  26277   ** Generate a human-readable explanation of an expression list.
 26226  26278   */
 26227         -SQLITE_PRIVATE void sqlite3TreeViewExprList(
        26279  +SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
 26228  26280     TreeView *pView,
 26229  26281     const ExprList *pList,
 26230         -  u8 moreToFollow,
 26231  26282     const char *zLabel
 26232  26283   ){
 26233         -  int i;
 26234         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
 26235  26284     if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
 26236  26285     if( pList==0 ){
 26237  26286       sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
 26238  26287     }else{
        26288  +    int i;
 26239  26289       sqlite3TreeViewLine(pView, "%s", zLabel);
 26240  26290       for(i=0; i<pList->nExpr; i++){
 26241  26291         int j = pList->a[i].u.x.iOrderByCol;
 26242  26292         if( j ){
 26243  26293           sqlite3TreeViewPush(pView, 0);
 26244  26294           sqlite3TreeViewLine(pView, "iOrderByCol=%d", j);
 26245  26295         }
 26246  26296         sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
 26247  26297         if( j ) sqlite3TreeViewPop(pView);
 26248  26298       }
 26249  26299     }
        26300  +}
        26301  +SQLITE_PRIVATE void sqlite3TreeViewExprList(
        26302  +  TreeView *pView,
        26303  +  const ExprList *pList,
        26304  +  u8 moreToFollow,
        26305  +  const char *zLabel
        26306  +){
        26307  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
        26308  +  sqlite3TreeViewBareExprList(pView, pList, zLabel);
 26250  26309     sqlite3TreeViewPop(pView);
 26251  26310   }
 26252  26311   
 26253  26312   #endif /* SQLITE_DEBUG */
 26254  26313   
 26255  26314   /************** End of treeview.c ********************************************/
 26256  26315   /************** Begin file random.c ******************************************/
................................................................................
 28504  28563       if( (*pA)>=0 ) return 1;
 28505  28564       *pA -= iB;
 28506  28565       return 0;
 28507  28566     }else{
 28508  28567       return sqlite3AddInt64(pA, -iB);
 28509  28568     }
 28510  28569   }
 28511         -#define TWOPOWER32 (((i64)1)<<32)
 28512         -#define TWOPOWER31 (((i64)1)<<31)
 28513  28570   SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
 28514  28571     i64 iA = *pA;
 28515         -  i64 iA1, iA0, iB1, iB0, r;
 28516         -
 28517         -  iA1 = iA/TWOPOWER32;
 28518         -  iA0 = iA % TWOPOWER32;
 28519         -  iB1 = iB/TWOPOWER32;
 28520         -  iB0 = iB % TWOPOWER32;
 28521         -  if( iA1==0 ){
 28522         -    if( iB1==0 ){
 28523         -      *pA *= iB;
 28524         -      return 0;
        28572  +  if( iB>0 ){
        28573  +    if( iA>LARGEST_INT64/iB ) return 1;
        28574  +    if( iA<SMALLEST_INT64/iB ) return 1;
        28575  +  }else if( iB<0 ){
        28576  +    if( iA>0 ){
        28577  +      if( iB<SMALLEST_INT64/iA ) return 1;
        28578  +    }else if( iA<0 ){
        28579  +      if( iB==SMALLEST_INT64 ) return 1;
        28580  +      if( iA==SMALLEST_INT64 ) return 1;
        28581  +      if( -iA>LARGEST_INT64/-iB ) return 1;
 28525  28582       }
 28526         -    r = iA0*iB1;
 28527         -  }else if( iB1==0 ){
 28528         -    r = iA1*iB0;
 28529         -  }else{
 28530         -    /* If both iA1 and iB1 are non-zero, overflow will result */
 28531         -    return 1;
 28532  28583     }
 28533         -  testcase( r==(-TWOPOWER31)-1 );
 28534         -  testcase( r==(-TWOPOWER31) );
 28535         -  testcase( r==TWOPOWER31 );
 28536         -  testcase( r==TWOPOWER31-1 );
 28537         -  if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
 28538         -  r *= TWOPOWER32;
 28539         -  if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
 28540         -  *pA = r;
        28584  +  *pA = iA*iB;
 28541  28585     return 0;
 28542  28586   }
 28543  28587   
 28544  28588   /*
 28545  28589   ** Compute the absolute value of a 32-bit signed integer, of possible.  Or 
 28546  28590   ** if the integer has a value of -2147483648, return +2147483647
 28547  28591   */
................................................................................
 28728  28772   /*
 28729  28773   ** The hashing function.
 28730  28774   */
 28731  28775   static unsigned int strHash(const char *z){
 28732  28776     unsigned int h = 0;
 28733  28777     unsigned char c;
 28734  28778     while( (c = (unsigned char)*z++)!=0 ){     /*OPTIMIZATION-IF-TRUE*/
 28735         -    h = (h<<3) ^ h ^ sqlite3UpperToLower[c];
        28779  +    /* Knuth multiplicative hashing.  (Sorting & Searching, p. 510).
        28780  +    ** 0x9e3779b1 is 2654435761 which is the closest prime number to
        28781  +    ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
        28782  +    h += sqlite3UpperToLower[c];
        28783  +    h *= 0x9e3779b1;
 28736  28784     }
 28737  28785     return h;
 28738  28786   }
 28739  28787   
 28740  28788   
 28741  28789   /* Link pNew element into the hash table pH.  If pEntry!=0 then also
 28742  28790   ** insert pNew into the pEntry hash bucket.
................................................................................
 28994  29042       /*  35 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
 28995  29043       /*  36 */ "Ne"               OpHelp("IF r[P3]!=r[P1]"),
 28996  29044       /*  37 */ "Eq"               OpHelp("IF r[P3]==r[P1]"),
 28997  29045       /*  38 */ "Gt"               OpHelp("IF r[P3]>r[P1]"),
 28998  29046       /*  39 */ "Le"               OpHelp("IF r[P3]<=r[P1]"),
 28999  29047       /*  40 */ "Lt"               OpHelp("IF r[P3]<r[P1]"),
 29000  29048       /*  41 */ "Ge"               OpHelp("IF r[P3]>=r[P1]"),
 29001         -    /*  42 */ "Last"             OpHelp(""),
        29049  +    /*  42 */ "ElseNotEq"        OpHelp(""),
 29002  29050       /*  43 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 29003  29051       /*  44 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 29004  29052       /*  45 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 29005  29053       /*  46 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 29006  29054       /*  47 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 29007  29055       /*  48 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 29008  29056       /*  49 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 29009  29057       /*  50 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 29010  29058       /*  51 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 29011  29059       /*  52 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 29012         -    /*  53 */ "SorterSort"       OpHelp(""),
        29060  +    /*  53 */ "Last"             OpHelp(""),
 29013  29061       /*  54 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 29014         -    /*  55 */ "Sort"             OpHelp(""),
 29015         -    /*  56 */ "Rewind"           OpHelp(""),
 29016         -    /*  57 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
 29017         -    /*  58 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
 29018         -    /*  59 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
 29019         -    /*  60 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
 29020         -    /*  61 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
 29021         -    /*  62 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
 29022         -    /*  63 */ "Program"          OpHelp(""),
 29023         -    /*  64 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
 29024         -    /*  65 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
 29025         -    /*  66 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
 29026         -    /*  67 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
 29027         -    /*  68 */ "IncrVacuum"       OpHelp(""),
 29028         -    /*  69 */ "VNext"            OpHelp(""),
 29029         -    /*  70 */ "Init"             OpHelp("Start at P2"),
 29030         -    /*  71 */ "Return"           OpHelp(""),
 29031         -    /*  72 */ "EndCoroutine"     OpHelp(""),
 29032         -    /*  73 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
 29033         -    /*  74 */ "Halt"             OpHelp(""),
 29034         -    /*  75 */ "Integer"          OpHelp("r[P2]=P1"),
 29035         -    /*  76 */ "Int64"            OpHelp("r[P2]=P4"),
 29036         -    /*  77 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
 29037         -    /*  78 */ "Null"             OpHelp("r[P2..P3]=NULL"),
 29038         -    /*  79 */ "SoftNull"         OpHelp("r[P1]=NULL"),
 29039         -    /*  80 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
 29040         -    /*  81 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
 29041         -    /*  82 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
 29042         -    /*  83 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
 29043         -    /*  84 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
 29044         -    /*  85 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
 29045         -    /*  86 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
 29046         -    /*  87 */ "CollSeq"          OpHelp(""),
 29047         -    /*  88 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 29048         -    /*  89 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 29049         -    /*  90 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
 29050         -    /*  91 */ "RealAffinity"     OpHelp(""),
 29051         -    /*  92 */ "Cast"             OpHelp("affinity(r[P1])"),
 29052         -    /*  93 */ "Permutation"      OpHelp(""),
 29053         -    /*  94 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 29054         -    /*  95 */ "Column"           OpHelp("r[P3]=PX"),
 29055         -    /*  96 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        29062  +    /*  55 */ "SorterSort"       OpHelp(""),
        29063  +    /*  56 */ "Sort"             OpHelp(""),
        29064  +    /*  57 */ "Rewind"           OpHelp(""),
        29065  +    /*  58 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
        29066  +    /*  59 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
        29067  +    /*  60 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
        29068  +    /*  61 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
        29069  +    /*  62 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
        29070  +    /*  63 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
        29071  +    /*  64 */ "Program"          OpHelp(""),
        29072  +    /*  65 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
        29073  +    /*  66 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
        29074  +    /*  67 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
        29075  +    /*  68 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
        29076  +    /*  69 */ "IncrVacuum"       OpHelp(""),
        29077  +    /*  70 */ "VNext"            OpHelp(""),
        29078  +    /*  71 */ "Init"             OpHelp("Start at P2"),
        29079  +    /*  72 */ "Return"           OpHelp(""),
        29080  +    /*  73 */ "EndCoroutine"     OpHelp(""),
        29081  +    /*  74 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
        29082  +    /*  75 */ "Halt"             OpHelp(""),
        29083  +    /*  76 */ "Integer"          OpHelp("r[P2]=P1"),
        29084  +    /*  77 */ "Int64"            OpHelp("r[P2]=P4"),
        29085  +    /*  78 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
        29086  +    /*  79 */ "Null"             OpHelp("r[P2..P3]=NULL"),
        29087  +    /*  80 */ "SoftNull"         OpHelp("r[P1]=NULL"),
        29088  +    /*  81 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
        29089  +    /*  82 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
        29090  +    /*  83 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
        29091  +    /*  84 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
        29092  +    /*  85 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
        29093  +    /*  86 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
        29094  +    /*  87 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
        29095  +    /*  88 */ "CollSeq"          OpHelp(""),
        29096  +    /*  89 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
        29097  +    /*  90 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
        29098  +    /*  91 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
        29099  +    /*  92 */ "RealAffinity"     OpHelp(""),
        29100  +    /*  93 */ "Cast"             OpHelp("affinity(r[P1])"),
        29101  +    /*  94 */ "Permutation"      OpHelp(""),
        29102  +    /*  95 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
        29103  +    /*  96 */ "Column"           OpHelp("r[P3]=PX"),
 29056  29104       /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 29057         -    /*  98 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 29058         -    /*  99 */ "Count"            OpHelp("r[P2]=count()"),
 29059         -    /* 100 */ "ReadCookie"       OpHelp(""),
 29060         -    /* 101 */ "SetCookie"        OpHelp(""),
 29061         -    /* 102 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 29062         -    /* 103 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 29063         -    /* 104 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 29064         -    /* 105 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 29065         -    /* 106 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 29066         -    /* 107 */ "SorterOpen"       OpHelp(""),
 29067         -    /* 108 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 29068         -    /* 109 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 29069         -    /* 110 */ "Close"            OpHelp(""),
 29070         -    /* 111 */ "ColumnsUsed"      OpHelp(""),
 29071         -    /* 112 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 29072         -    /* 113 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 29073         -    /* 114 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 29074         -    /* 115 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 29075         -    /* 116 */ "Delete"           OpHelp(""),
 29076         -    /* 117 */ "ResetCount"       OpHelp(""),
 29077         -    /* 118 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 29078         -    /* 119 */ "SorterData"       OpHelp("r[P2]=data"),
 29079         -    /* 120 */ "RowKey"           OpHelp("r[P2]=key"),
 29080         -    /* 121 */ "RowData"          OpHelp("r[P2]=data"),
 29081         -    /* 122 */ "Rowid"            OpHelp("r[P2]=rowid"),
 29082         -    /* 123 */ "NullRow"          OpHelp(""),
 29083         -    /* 124 */ "SorterInsert"     OpHelp(""),
 29084         -    /* 125 */ "IdxInsert"        OpHelp("key=r[P2]"),
 29085         -    /* 126 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 29086         -    /* 127 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
 29087         -    /* 128 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 29088         -    /* 129 */ "Destroy"          OpHelp(""),
 29089         -    /* 130 */ "Clear"            OpHelp(""),
 29090         -    /* 131 */ "ResetSorter"      OpHelp(""),
 29091         -    /* 132 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
 29092         -    /* 133 */ "Real"             OpHelp("r[P2]=P4"),
 29093         -    /* 134 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
 29094         -    /* 135 */ "ParseSchema"      OpHelp(""),
 29095         -    /* 136 */ "LoadAnalysis"     OpHelp(""),
 29096         -    /* 137 */ "DropTable"        OpHelp(""),
 29097         -    /* 138 */ "DropIndex"        OpHelp(""),
 29098         -    /* 139 */ "DropTrigger"      OpHelp(""),
 29099         -    /* 140 */ "IntegrityCk"      OpHelp(""),
 29100         -    /* 141 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 29101         -    /* 142 */ "Param"            OpHelp(""),
 29102         -    /* 143 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 29103         -    /* 144 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 29104         -    /* 145 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 29105         -    /* 146 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 29106         -    /* 147 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 29107         -    /* 148 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 29108         -    /* 149 */ "Expire"           OpHelp(""),
 29109         -    /* 150 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 29110         -    /* 151 */ "VBegin"           OpHelp(""),
 29111         -    /* 152 */ "VCreate"          OpHelp(""),
 29112         -    /* 153 */ "VDestroy"         OpHelp(""),
 29113         -    /* 154 */ "VOpen"            OpHelp(""),
 29114         -    /* 155 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 29115         -    /* 156 */ "VRename"          OpHelp(""),
 29116         -    /* 157 */ "Pagecount"        OpHelp(""),
 29117         -    /* 158 */ "MaxPgcnt"         OpHelp(""),
 29118         -    /* 159 */ "CursorHint"       OpHelp(""),
 29119         -    /* 160 */ "Noop"             OpHelp(""),
 29120         -    /* 161 */ "Explain"          OpHelp(""),
        29105  +    /*  98 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        29106  +    /*  99 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        29107  +    /* 100 */ "Count"            OpHelp("r[P2]=count()"),
        29108  +    /* 101 */ "ReadCookie"       OpHelp(""),
        29109  +    /* 102 */ "SetCookie"        OpHelp(""),
        29110  +    /* 103 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        29111  +    /* 104 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        29112  +    /* 105 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        29113  +    /* 106 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
        29114  +    /* 107 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
        29115  +    /* 108 */ "SorterOpen"       OpHelp(""),
        29116  +    /* 109 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
        29117  +    /* 110 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
        29118  +    /* 111 */ "Close"            OpHelp(""),
        29119  +    /* 112 */ "ColumnsUsed"      OpHelp(""),
        29120  +    /* 113 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
        29121  +    /* 114 */ "NewRowid"         OpHelp("r[P2]=rowid"),
        29122  +    /* 115 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
        29123  +    /* 116 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        29124  +    /* 117 */ "Delete"           OpHelp(""),
        29125  +    /* 118 */ "ResetCount"       OpHelp(""),
        29126  +    /* 119 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        29127  +    /* 120 */ "SorterData"       OpHelp("r[P2]=data"),
        29128  +    /* 121 */ "RowKey"           OpHelp("r[P2]=key"),
        29129  +    /* 122 */ "RowData"          OpHelp("r[P2]=data"),
        29130  +    /* 123 */ "Rowid"            OpHelp("r[P2]=rowid"),
        29131  +    /* 124 */ "NullRow"          OpHelp(""),
        29132  +    /* 125 */ "SorterInsert"     OpHelp(""),
        29133  +    /* 126 */ "IdxInsert"        OpHelp("key=r[P2]"),
        29134  +    /* 127 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        29135  +    /* 128 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
        29136  +    /* 129 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        29137  +    /* 130 */ "Destroy"          OpHelp(""),
        29138  +    /* 131 */ "Clear"            OpHelp(""),
        29139  +    /* 132 */ "Real"             OpHelp("r[P2]=P4"),
        29140  +    /* 133 */ "ResetSorter"      OpHelp(""),
        29141  +    /* 134 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
        29142  +    /* 135 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
        29143  +    /* 136 */ "ParseSchema"      OpHelp(""),
        29144  +    /* 137 */ "LoadAnalysis"     OpHelp(""),
        29145  +    /* 138 */ "DropTable"        OpHelp(""),
        29146  +    /* 139 */ "DropIndex"        OpHelp(""),
        29147  +    /* 140 */ "DropTrigger"      OpHelp(""),
        29148  +    /* 141 */ "IntegrityCk"      OpHelp(""),
        29149  +    /* 142 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        29150  +    /* 143 */ "Param"            OpHelp(""),
        29151  +    /* 144 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        29152  +    /* 145 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        29153  +    /* 146 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        29154  +    /* 147 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        29155  +    /* 148 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        29156  +    /* 149 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        29157  +    /* 150 */ "Expire"           OpHelp(""),
        29158  +    /* 151 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        29159  +    /* 152 */ "VBegin"           OpHelp(""),
        29160  +    /* 153 */ "VCreate"          OpHelp(""),
        29161  +    /* 154 */ "VDestroy"         OpHelp(""),
        29162  +    /* 155 */ "VOpen"            OpHelp(""),
        29163  +    /* 156 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        29164  +    /* 157 */ "VRename"          OpHelp(""),
        29165  +    /* 158 */ "Pagecount"        OpHelp(""),
        29166  +    /* 159 */ "MaxPgcnt"         OpHelp(""),
        29167  +    /* 160 */ "CursorHint"       OpHelp(""),
        29168  +    /* 161 */ "Noop"             OpHelp(""),
        29169  +    /* 162 */ "Explain"          OpHelp(""),
 29121  29170     };
 29122  29171     return azName[i];
 29123  29172   }
 29124  29173   #endif
 29125  29174   
 29126  29175   /************** End of opcodes.c *********************************************/
 29127  29176   /************** Begin file os_unix.c *****************************************/
................................................................................
 34849  34898         }
 34850  34899       }
 34851  34900       unixLeaveMutex();
 34852  34901     }
 34853  34902   #endif    /* if !OS_VXWORKS */
 34854  34903     return pUnused;
 34855  34904   }
        34905  +
        34906  +/*
        34907  +** Find the mode, uid and gid of file zFile. 
        34908  +*/
        34909  +static int getFileMode(
        34910  +  const char *zFile,              /* File name */
        34911  +  mode_t *pMode,                  /* OUT: Permissions of zFile */
        34912  +  uid_t *pUid,                    /* OUT: uid of zFile. */
        34913  +  gid_t *pGid                     /* OUT: gid of zFile. */
        34914  +){
        34915  +  struct stat sStat;              /* Output of stat() on database file */
        34916  +  int rc = SQLITE_OK;
        34917  +  if( 0==osStat(zFile, &sStat) ){
        34918  +    *pMode = sStat.st_mode & 0777;
        34919  +    *pUid = sStat.st_uid;
        34920  +    *pGid = sStat.st_gid;
        34921  +  }else{
        34922  +    rc = SQLITE_IOERR_FSTAT;
        34923  +  }
        34924  +  return rc;
        34925  +}
 34856  34926   
 34857  34927   /*
 34858  34928   ** This function is called by unixOpen() to determine the unix permissions
 34859  34929   ** to create new files with. If no error occurs, then SQLITE_OK is returned
 34860  34930   ** and a value suitable for passing as the third argument to open(2) is
 34861  34931   ** written to *pMode. If an IO error occurs, an SQLite error code is 
 34862  34932   ** returned and the value of *pMode is not modified.
................................................................................
 34885  34955     int rc = SQLITE_OK;             /* Return Code */
 34886  34956     *pMode = 0;
 34887  34957     *pUid = 0;
 34888  34958     *pGid = 0;
 34889  34959     if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
 34890  34960       char zDb[MAX_PATHNAME+1];     /* Database file path */
 34891  34961       int nDb;                      /* Number of valid bytes in zDb */
 34892         -    struct stat sStat;            /* Output of stat() on database file */
 34893  34962   
 34894  34963       /* zPath is a path to a WAL or journal file. The following block derives
 34895  34964       ** the path to the associated database file from zPath. This block handles
 34896  34965       ** the following naming conventions:
 34897  34966       **
 34898  34967       **   "<path to db>-journal"
 34899  34968       **   "<path to db>-wal"
................................................................................
 34916  34985         if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
 34917  34986   #endif
 34918  34987         nDb--;
 34919  34988       }
 34920  34989       memcpy(zDb, zPath, nDb);
 34921  34990       zDb[nDb] = '\0';
 34922  34991   
 34923         -    if( 0==osStat(zDb, &sStat) ){
 34924         -      *pMode = sStat.st_mode & 0777;
 34925         -      *pUid = sStat.st_uid;
 34926         -      *pGid = sStat.st_gid;
 34927         -    }else{
 34928         -      rc = SQLITE_IOERR_FSTAT;
 34929         -    }
        34992  +    rc = getFileMode(zDb, pMode, pUid, pGid);
 34930  34993     }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
 34931  34994       *pMode = 0600;
        34995  +  }else if( flags & SQLITE_OPEN_URI ){
        34996  +    /* If this is a main database file and the file was opened using a URI
        34997  +    ** filename, check for the "modeof" parameter. If present, interpret
        34998  +    ** its value as a filename and try to copy the mode, uid and gid from
        34999  +    ** that file.  */
        35000  +    const char *z = sqlite3_uri_parameter(zPath, "modeof");
        35001  +    if( z ){
        35002  +      rc = getFileMode(z, pMode, pUid, pGid);
        35003  +    }
 34932  35004     }
 34933  35005     return rc;
 34934  35006   }
 34935  35007   
 34936  35008   /*
 34937  35009   ** Open the file zPath.
 34938  35010   ** 
................................................................................
 43947  44019     Pgno pgno,                  /* Page number obtained */
 43948  44020     sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
 43949  44021   ){
 43950  44022     PgHdr *pPgHdr;
 43951  44023     assert( pPage!=0 );
 43952  44024     pPgHdr = (PgHdr*)pPage->pExtra;
 43953  44025     assert( pPgHdr->pPage==0 );
 43954         -  memset(pPgHdr, 0, sizeof(PgHdr));
        44026  +  memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
 43955  44027     pPgHdr->pPage = pPage;
 43956  44028     pPgHdr->pData = pPage->pBuf;
 43957  44029     pPgHdr->pExtra = (void *)&pPgHdr[1];
 43958  44030     memset(pPgHdr->pExtra, 0, pCache->szExtra);
 43959  44031     pPgHdr->pCache = pCache;
 43960  44032     pPgHdr->pgno = pgno;
 43961  44033     pPgHdr->flags = PGHDR_CLEAN;
................................................................................
 44641  44713     sqlite3BeginBenignMalloc();
 44642  44714     if( pcache1.nInitPage>0 ){
 44643  44715       szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
 44644  44716     }else{
 44645  44717       szBulk = -1024 * (i64)pcache1.nInitPage;
 44646  44718     }
 44647  44719     if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
 44648         -    szBulk = pCache->szAlloc*pCache->nMax;
        44720  +    szBulk = pCache->szAlloc*(i64)pCache->nMax;
 44649  44721     }
 44650  44722     zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
 44651  44723     sqlite3EndBenignMalloc();
 44652  44724     if( zBulk ){
 44653  44725       int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
 44654  44726       int i;
 44655  44727       for(i=0; i<nBulk; i++){
................................................................................
 52929  53001   ** then savepoint iSavepoint is also destroyed.
 52930  53002   **
 52931  53003   ** This function may return SQLITE_NOMEM if a memory allocation fails,
 52932  53004   ** or an IO error code if an IO error occurs while rolling back a 
 52933  53005   ** savepoint. If no errors occur, SQLITE_OK is returned.
 52934  53006   */ 
 52935  53007   SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
 52936         -  int rc = pPager->errCode;       /* Return code */
        53008  +  int rc = pPager->errCode;
        53009  +  
        53010  +#ifdef SQLITE_ENABLE_ZIPVFS
        53011  +  if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK;
        53012  +#endif
 52937  53013   
 52938  53014     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 52939  53015     assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
 52940  53016   
 52941  53017     if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
 52942  53018       int ii;            /* Iterator variable */
 52943  53019       int nNew;          /* Number of remaining savepoints after this op. */
................................................................................
 52970  53046       ** the database file, so the playback operation can be skipped.
 52971  53047       */
 52972  53048       else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
 52973  53049         PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
 52974  53050         rc = pagerPlaybackSavepoint(pPager, pSavepoint);
 52975  53051         assert(rc!=SQLITE_DONE);
 52976  53052       }
        53053  +    
        53054  +#ifdef SQLITE_ENABLE_ZIPVFS
        53055  +    /* If the cache has been modified but the savepoint cannot be rolled 
        53056  +    ** back journal_mode=off, put the pager in the error state. This way,
        53057  +    ** if the VFS used by this pager includes ZipVFS, the entire transaction
        53058  +    ** can be rolled back at the ZipVFS level.  */
        53059  +    else if( 
        53060  +        pPager->journalMode==PAGER_JOURNALMODE_OFF 
        53061  +     && pPager->eState>=PAGER_WRITER_CACHEMOD
        53062  +    ){
        53063  +      pPager->errCode = SQLITE_ABORT;
        53064  +      pPager->eState = PAGER_ERROR;
        53065  +    }
        53066  +#endif
 52977  53067     }
 52978  53068   
 52979  53069     return rc;
 52980  53070   }
 52981  53071   
 52982  53072   /*
 52983  53073   ** Return the full pathname of the database file.
................................................................................
 58895  58985     const void *pKey,   /* Packed key if the btree is an index */
 58896  58986     i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
 58897  58987     int bias,           /* Bias search to the high end */
 58898  58988     int *pRes           /* Write search results here */
 58899  58989   ){
 58900  58990     int rc;                    /* Status code */
 58901  58991     UnpackedRecord *pIdxKey;   /* Unpacked index key */
 58902         -  char aSpace[200];          /* Temp space for pIdxKey - to avoid a malloc */
        58992  +  char aSpace[384];          /* Temp space for pIdxKey - to avoid a malloc */
 58903  58993     char *pFree = 0;
 58904  58994   
 58905  58995     if( pKey ){
 58906  58996       assert( nKey==(i64)(int)nKey );
 58907  58997       pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 58908  58998           pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
 58909  58999       );
................................................................................
 59737  59827     ** spot on the list where iStart should be inserted.
 59738  59828     */
 59739  59829     hdr = pPage->hdrOffset;
 59740  59830     iPtr = hdr + 1;
 59741  59831     if( data[iPtr+1]==0 && data[iPtr]==0 ){
 59742  59832       iFreeBlk = 0;  /* Shortcut for the case when the freelist is empty */
 59743  59833     }else{
 59744         -    while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlk<iStart ){
 59745         -      if( iFreeBlk<iPtr+4 ) return SQLITE_CORRUPT_BKPT;
        59834  +    while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
        59835  +      if( iFreeBlk<iPtr+4 ){
        59836  +        if( iFreeBlk==0 ) break;
        59837  +        return SQLITE_CORRUPT_BKPT;
        59838  +      }
 59746  59839         iPtr = iFreeBlk;
 59747  59840       }
 59748  59841       if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
 59749  59842       assert( iFreeBlk>iPtr || iFreeBlk==0 );
 59750  59843     
 59751  59844       /* At this point:
 59752  59845       **    iFreeBlk:   First freeblock after iStart, or zero if none
................................................................................
 64229  64322       nPayload = pX->nData + pX->nZero;
 64230  64323       pSrc = pX->pData;
 64231  64324       nSrc = pX->nData;
 64232  64325       assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
 64233  64326       nHeader += putVarint32(&pCell[nHeader], nPayload);
 64234  64327       nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
 64235  64328     }else{
 64236         -    assert( pX->nData==0 );
 64237         -    assert( pX->nZero==0 );
 64238  64329       assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
 64239  64330       nSrc = nPayload = (int)pX->nKey;
 64240  64331       pSrc = pX->pKey;
 64241  64332       nHeader += putVarint32(&pCell[nHeader], nPayload);
 64242  64333     }
 64243  64334     
 64244  64335     /* Fill in the payload */
................................................................................
 67930  68021   ** function. If an error occurs while doing so, return 0 and write an 
 67931  68022   ** error message to pErrorDb.
 67932  68023   */
 67933  68024   static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
 67934  68025     int i = sqlite3FindDbName(pDb, zDb);
 67935  68026   
 67936  68027     if( i==1 ){
 67937         -    Parse *pParse;
        68028  +    Parse sParse;
 67938  68029       int rc = 0;
 67939         -    pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
 67940         -    if( pParse==0 ){
 67941         -      sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory");
 67942         -      rc = SQLITE_NOMEM_BKPT;
 67943         -    }else{
 67944         -      pParse->db = pDb;
 67945         -      if( sqlite3OpenTempDatabase(pParse) ){
 67946         -        sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
 67947         -        rc = SQLITE_ERROR;
 67948         -      }
 67949         -      sqlite3DbFree(pErrorDb, pParse->zErrMsg);
 67950         -      sqlite3ParserReset(pParse);
 67951         -      sqlite3StackFree(pErrorDb, pParse);
        68030  +    memset(&sParse, 0, sizeof(sParse));
        68031  +    sParse.db = pDb;
        68032  +    if( sqlite3OpenTempDatabase(&sParse) ){
        68033  +      sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
        68034  +      rc = SQLITE_ERROR;
 67952  68035       }
        68036  +    sqlite3DbFree(pErrorDb, sParse.zErrMsg);
        68037  +    sqlite3ParserReset(&sParse);
 67953  68038       if( rc ){
 67954  68039         return 0;
 67955  68040       }
 67956  68041     }
 67957  68042   
 67958  68043     if( i<0 ){
 67959  68044       sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
................................................................................
 68043  68128       p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
 68044  68129       p->pDestDb = pDestDb;
 68045  68130       p->pSrcDb = pSrcDb;
 68046  68131       p->iNext = 1;
 68047  68132       p->isAttached = 0;
 68048  68133   
 68049  68134       if( 0==p->pSrc || 0==p->pDest 
 68050         -     || setDestPgsz(p)==SQLITE_NOMEM 
 68051  68135        || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK 
 68052  68136        ){
 68053  68137         /* One (or both) of the named databases did not exist or an OOM
 68054  68138         ** error was hit. Or there is a transaction open on the destination
 68055  68139         ** database. The error has already been written into the pDestDb 
 68056  68140         ** handle. All that is left to do here is free the sqlite3_backup 
 68057  68141         ** structure.  */
................................................................................
 68231  68315       */
 68232  68316       if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
 68233  68317         rc = SQLITE_BUSY;
 68234  68318       }else{
 68235  68319         rc = SQLITE_OK;
 68236  68320       }
 68237  68321   
 68238         -    /* Lock the destination database, if it is not locked already. */
 68239         -    if( SQLITE_OK==rc && p->bDestLocked==0
 68240         -     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
 68241         -    ){
 68242         -      p->bDestLocked = 1;
 68243         -      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
 68244         -    }
 68245         -
 68246  68322       /* If there is no open read-transaction on the source database, open
 68247  68323       ** one now. If a transaction is opened here, then it will be closed
 68248  68324       ** before this function exits.
 68249  68325       */
 68250  68326       if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
 68251  68327         rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
 68252  68328         bCloseTrans = 1;
 68253  68329       }
        68330  +
        68331  +    /* If the destination database has not yet been locked (i.e. if this
        68332  +    ** is the first call to backup_step() for the current backup operation),
        68333  +    ** try to set its page size to the same as the source database. This
        68334  +    ** is especially important on ZipVFS systems, as in that case it is
        68335  +    ** not possible to create a database file that uses one page size by
        68336  +    ** writing to it with another.  */
        68337  +    if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
        68338  +      rc = SQLITE_NOMEM;
        68339  +    }
        68340  +
        68341  +    /* Lock the destination database, if it is not locked already. */
        68342  +    if( SQLITE_OK==rc && p->bDestLocked==0
        68343  +     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
        68344  +    ){
        68345  +      p->bDestLocked = 1;
        68346  +      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
        68347  +    }
 68254  68348   
 68255  68349       /* Do not allow backup if the destination database is in WAL mode
 68256  68350       ** and the page sizes are different between source and destination */
 68257  68351       pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
 68258  68352       pgszDest = sqlite3BtreeGetPageSize(p->pDest);
 68259  68353       destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
 68260  68354       if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
................................................................................
 68836  68930   /*
 68837  68931   ** Change pMem so that its MEM_Str or MEM_Blob value is stored in
 68838  68932   ** MEM.zMalloc, where it can be safely written.
 68839  68933   **
 68840  68934   ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
 68841  68935   */
 68842  68936   SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
 68843         -  int f;
 68844  68937     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 68845  68938     assert( (pMem->flags&MEM_RowSet)==0 );
 68846         -  ExpandBlob(pMem);
 68847         -  f = pMem->flags;
 68848         -  if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
 68849         -    if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
 68850         -      return SQLITE_NOMEM_BKPT;
        68939  +  if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
        68940  +    if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
        68941  +    if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
        68942  +      if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
        68943  +        return SQLITE_NOMEM_BKPT;
        68944  +      }
        68945  +      pMem->z[pMem->n] = 0;
        68946  +      pMem->z[pMem->n+1] = 0;
        68947  +      pMem->flags |= MEM_Term;
 68851  68948       }
 68852         -    pMem->z[pMem->n] = 0;
 68853         -    pMem->z[pMem->n+1] = 0;
 68854         -    pMem->flags |= MEM_Term;
 68855  68949     }
 68856  68950     pMem->flags &= ~MEM_Ephem;
 68857  68951   #ifdef SQLITE_DEBUG
 68858  68952     pMem->pScopyFrom = 0;
 68859  68953   #endif
 68860  68954   
 68861  68955     return SQLITE_OK;
................................................................................
 68863  68957   
 68864  68958   /*
 68865  68959   ** If the given Mem* has a zero-filled tail, turn it into an ordinary
 68866  68960   ** blob stored in dynamically allocated space.
 68867  68961   */
 68868  68962   #ifndef SQLITE_OMIT_INCRBLOB
 68869  68963   SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
 68870         -  if( pMem->flags & MEM_Zero ){
 68871         -    int nByte;
 68872         -    assert( pMem->flags&MEM_Blob );
 68873         -    assert( (pMem->flags&MEM_RowSet)==0 );
 68874         -    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
        68964  +  int nByte;
        68965  +  assert( pMem->flags & MEM_Zero );
        68966  +  assert( pMem->flags&MEM_Blob );
        68967  +  assert( (pMem->flags&MEM_RowSet)==0 );
        68968  +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 68875  68969   
 68876         -    /* Set nByte to the number of bytes required to store the expanded blob. */
 68877         -    nByte = pMem->n + pMem->u.nZero;
 68878         -    if( nByte<=0 ){
 68879         -      nByte = 1;
 68880         -    }
 68881         -    if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
 68882         -      return SQLITE_NOMEM_BKPT;
 68883         -    }
        68970  +  /* Set nByte to the number of bytes required to store the expanded blob. */
        68971  +  nByte = pMem->n + pMem->u.nZero;
        68972  +  if( nByte<=0 ){
        68973  +    nByte = 1;
        68974  +  }
        68975  +  if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
        68976  +    return SQLITE_NOMEM_BKPT;
        68977  +  }
 68884  68978   
 68885         -    memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
 68886         -    pMem->n += pMem->u.nZero;
 68887         -    pMem->flags &= ~(MEM_Zero|MEM_Term);
 68888         -  }
        68979  +  memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
        68980  +  pMem->n += pMem->u.nZero;
        68981  +  pMem->flags &= ~(MEM_Zero|MEM_Term);
 68889  68982     return SQLITE_OK;
 68890  68983   }
 68891  68984   #endif
 68892  68985   
 68893  68986   /*
 68894  68987   ** It is already known that pMem contains an unterminated string.
 68895  68988   ** Add the zero terminator.
................................................................................
 68941  69034     assert( !(fg&(MEM_Str|MEM_Blob)) );
 68942  69035     assert( fg&(MEM_Int|MEM_Real) );
 68943  69036     assert( (pMem->flags&MEM_RowSet)==0 );
 68944  69037     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 68945  69038   
 68946  69039   
 68947  69040     if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
        69041  +    pMem->enc = 0;
 68948  69042       return SQLITE_NOMEM_BKPT;
 68949  69043     }
 68950  69044   
 68951  69045     /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
 68952  69046     ** string representation of the value. Then, if the required encoding
 68953  69047     ** is UTF-16le or UTF-16be do a translation.
 68954  69048     ** 
................................................................................
 69222  69316       }else{
 69223  69317         pMem->u.r = sqlite3VdbeRealValue(pMem);
 69224  69318         MemSetTypeFlag(pMem, MEM_Real);
 69225  69319         sqlite3VdbeIntegerAffinity(pMem);
 69226  69320       }
 69227  69321     }
 69228  69322     assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
 69229         -  pMem->flags &= ~(MEM_Str|MEM_Blob);
        69323  +  pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
 69230  69324     return SQLITE_OK;
 69231  69325   }
 69232  69326   
 69233  69327   /*
 69234  69328   ** Cast the datatype of the value in pMem according to the affinity
 69235  69329   ** "aff".  Casting is different from applying affinity in that a cast
 69236  69330   ** is forced.  In other words, the value is converted into the desired
................................................................................
 69240  69334   SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
 69241  69335     if( pMem->flags & MEM_Null ) return;
 69242  69336     switch( aff ){
 69243  69337       case SQLITE_AFF_BLOB: {   /* Really a cast to BLOB */
 69244  69338         if( (pMem->flags & MEM_Blob)==0 ){
 69245  69339           sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
 69246  69340           assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
 69247         -        MemSetTypeFlag(pMem, MEM_Blob);
        69341  +        if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
 69248  69342         }else{
 69249  69343           pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
 69250  69344         }
 69251  69345         break;
 69252  69346       }
 69253  69347       case SQLITE_AFF_NUMERIC: {
 69254  69348         sqlite3VdbeMemNumerify(pMem);
................................................................................
 69665  69759     assert( pVal!=0 );
 69666  69760     assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
 69667  69761     assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 69668  69762     assert( (pVal->flags & MEM_RowSet)==0 );
 69669  69763     assert( (pVal->flags & (MEM_Null))==0 );
 69670  69764     if( pVal->flags & (MEM_Blob|MEM_Str) ){
 69671  69765       pVal->flags |= MEM_Str;
 69672         -    if( pVal->flags & MEM_Zero ){
 69673         -      sqlite3VdbeMemExpandBlob(pVal);
 69674         -    }
 69675  69766       if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
 69676  69767         sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
 69677  69768       }
 69678  69769       if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
 69679  69770         assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
 69680  69771         if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
 69681  69772           return 0;
................................................................................
 69920  70011     int op;
 69921  70012     char *zVal = 0;
 69922  70013     sqlite3_value *pVal = 0;
 69923  70014     int negInt = 1;
 69924  70015     const char *zNeg = "";
 69925  70016     int rc = SQLITE_OK;
 69926  70017   
 69927         -  if( !pExpr ){
 69928         -    *ppVal = 0;
 69929         -    return SQLITE_OK;
 69930         -  }
        70018  +  assert( pExpr!=0 );
 69931  70019     while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
 69932  70020     if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
 69933  70021   
 69934  70022     /* Compressed expressions only appear when parsing the DEFAULT clause
 69935  70023     ** on a table column definition, and hence only when pCtx==0.  This
 69936  70024     ** check ensures that an EP_TokenOnly expression is never passed down
 69937  70025     ** into valueFromFunction(). */
................................................................................
 70047  70135   SQLITE_PRIVATE int sqlite3ValueFromExpr(
 70048  70136     sqlite3 *db,              /* The database connection */
 70049  70137     Expr *pExpr,              /* The expression to evaluate */
 70050  70138     u8 enc,                   /* Encoding to use */
 70051  70139     u8 affinity,              /* Affinity to use */
 70052  70140     sqlite3_value **ppVal     /* Write the new value here */
 70053  70141   ){
 70054         -  return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
        70142  +  return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
 70055  70143   }
 70056  70144   
 70057  70145   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 70058  70146   /*
 70059  70147   ** The implementation of the sqlite_record() function. This function accepts
 70060  70148   ** a single argument of any type. The return value is a formatted database 
 70061  70149   ** record (a blob) containing the argument value.
................................................................................
 70167  70255   }
 70168  70256   
 70169  70257   /*
 70170  70258   ** This function is used to allocate and populate UnpackedRecord 
 70171  70259   ** structures intended to be compared against sample index keys stored 
 70172  70260   ** in the sqlite_stat4 table.
 70173  70261   **
 70174         -** A single call to this function attempts to populates field iVal (leftmost 
 70175         -** is 0 etc.) of the unpacked record with a value extracted from expression
 70176         -** pExpr. Extraction of values is possible if:
        70262  +** A single call to this function populates zero or more fields of the
        70263  +** record starting with field iVal (fields are numbered from left to
        70264  +** right starting with 0). A single field is populated if:
 70177  70265   **
 70178  70266   **  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
 70179  70267   **
 70180  70268   **  * The expression is a bound variable, and this is a reprepare, or
 70181  70269   **
 70182  70270   **  * The sqlite3ValueFromExpr() function is able to extract a value 
 70183  70271   **    from the expression (i.e. the expression is a literal value).
 70184  70272   **
 70185         -** If a value can be extracted, the affinity passed as the 5th argument
 70186         -** is applied to it before it is copied into the UnpackedRecord. Output
 70187         -** parameter *pbOk is set to true if a value is extracted, or false 
 70188         -** otherwise.
        70273  +** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
        70274  +** vector components that match either of the two latter criteria listed
        70275  +** above.
        70276  +**
        70277  +** Before any value is appended to the record, the affinity of the 
        70278  +** corresponding column within index pIdx is applied to it. Before
        70279  +** this function returns, output parameter *pnExtract is set to the
        70280  +** number of values appended to the record.
 70189  70281   **
 70190  70282   ** When this function is called, *ppRec must either point to an object
 70191  70283   ** allocated by an earlier call to this function, or must be NULL. If it
 70192  70284   ** is NULL and a value can be successfully extracted, a new UnpackedRecord
 70193  70285   ** is allocated (and *ppRec set to point to it) before returning.
 70194  70286   **
 70195  70287   ** Unless an error is encountered, SQLITE_OK is returned. It is not an
................................................................................
 70197  70289   ** occur, an SQLite error code is returned.
 70198  70290   */
 70199  70291   SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
 70200  70292     Parse *pParse,                  /* Parse context */
 70201  70293     Index *pIdx,                    /* Index being probed */
 70202  70294     UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
 70203  70295     Expr *pExpr,                    /* The expression to extract a value from */
 70204         -  u8 affinity,                    /* Affinity to use */
        70296  +  int nElem,                      /* Maximum number of values to append */
 70205  70297     int iVal,                       /* Array element to populate */
 70206         -  int *pbOk                       /* OUT: True if value was extracted */
        70298  +  int *pnExtract                  /* OUT: Values appended to the record */
 70207  70299   ){
 70208         -  int rc;
 70209         -  sqlite3_value *pVal = 0;
 70210         -  struct ValueNewStat4Ctx alloc;
        70300  +  int rc = SQLITE_OK;
        70301  +  int nExtract = 0;
 70211  70302   
 70212         -  alloc.pParse = pParse;
 70213         -  alloc.pIdx = pIdx;
 70214         -  alloc.ppRec = ppRec;
 70215         -  alloc.iVal = iVal;
        70303  +  if( pExpr==0 || pExpr->op!=TK_SELECT ){
        70304  +    int i;
        70305  +    struct ValueNewStat4Ctx alloc;
 70216  70306   
 70217         -  rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal);
 70218         -  assert( pVal==0 || pVal->db==pParse->db );
 70219         -  *pbOk = (pVal!=0);
        70307  +    alloc.pParse = pParse;
        70308  +    alloc.pIdx = pIdx;
        70309  +    alloc.ppRec = ppRec;
        70310  +
        70311  +    for(i=0; i<nElem; i++){
        70312  +      sqlite3_value *pVal = 0;
        70313  +      Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
        70314  +      u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
        70315  +      alloc.iVal = iVal+i;
        70316  +      rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
        70317  +      if( !pVal ) break;
        70318  +      nExtract++;
        70319  +    }
        70320  +  }
        70321  +
        70322  +  *pnExtract = nExtract;
 70220  70323     return rc;
 70221  70324   }
 70222  70325   
 70223  70326   /*
 70224  70327   ** Attempt to extract a value from expression pExpr using the methods
 70225  70328   ** as described for sqlite3Stat4ProbeSetValue() above. 
 70226  70329   **
................................................................................
 70375  70478   
 70376  70479   /*
 70377  70480   ** Create a new virtual database engine.
 70378  70481   */
 70379  70482   SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
 70380  70483     sqlite3 *db = pParse->db;
 70381  70484     Vdbe *p;
 70382         -  p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
        70485  +  p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
 70383  70486     if( p==0 ) return 0;
        70487  +  memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
 70384  70488     p->db = db;
 70385  70489     if( db->pVdbe ){
 70386  70490       db->pVdbe->pPrev = p;
 70387  70491     }
 70388  70492     p->pNext = db->pVdbe;
 70389  70493     p->pPrev = 0;
 70390  70494     db->pVdbe = p;
................................................................................
 70538  70642   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 70539  70643     pOp->zComment = 0;
 70540  70644   #endif
 70541  70645   #ifdef SQLITE_DEBUG
 70542  70646     if( p->db->flags & SQLITE_VdbeAddopTrace ){
 70543  70647       int jj, kk;
 70544  70648       Parse *pParse = p->pParse;
 70545         -    for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
        70649  +    for(jj=kk=0; jj<pParse->nColCache; jj++){
 70546  70650         struct yColCache *x = pParse->aColCache + jj;
 70547         -      if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
 70548  70651         printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
 70549  70652         kk++;
 70550  70653       }
 70551  70654       if( kk ) printf("\n");
 70552  70655       sqlite3VdbePrintOp(0, i, &p->aOp[i]);
 70553  70656       test_addop_breakpoint();
 70554  70657     }
................................................................................
 70728  70831     int j = ADDR(x);
 70729  70832     assert( v->magic==VDBE_MAGIC_INIT );
 70730  70833     assert( j<p->nLabel );
 70731  70834     assert( j>=0 );
 70732  70835     if( p->aLabel ){
 70733  70836       p->aLabel[j] = v->nOp;
 70734  70837     }
 70735         -  p->iFixedOp = v->nOp - 1;
 70736  70838   }
 70737  70839   
 70738  70840   /*
 70739  70841   ** Mark the VDBE as one that can only be run one time.
 70740  70842   */
 70741  70843   SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
 70742  70844     p->runOnlyOnce = 1;
................................................................................
 71119  71221   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
 71120  71222     sqlite3VdbeGetOp(p,addr)->p2 = val;
 71121  71223   }
 71122  71224   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
 71123  71225     sqlite3VdbeGetOp(p,addr)->p3 = val;
 71124  71226   }
 71125  71227   SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
 71126         -  if( !p->db->mallocFailed ) p->aOp[p->nOp-1].p5 = p5;
        71228  +  assert( p->nOp>0 || p->db->mallocFailed );
        71229  +  if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
 71127  71230   }
 71128  71231   
 71129  71232   /*
 71130  71233   ** Change the P2 operand of instruction addr so that it points to
 71131  71234   ** the address of the next instruction to be coded.
 71132  71235   */
 71133  71236   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
 71134         -  p->pParse->iFixedOp = p->nOp - 1;
 71135  71237     sqlite3VdbeChangeP2(p, addr, p->nOp);
 71136  71238   }
 71137  71239   
 71138  71240   
 71139  71241   /*
 71140  71242   ** If the input FuncDef structure is ephemeral, then free it.  If
 71141  71243   ** the FuncDef is not ephermal, then do nothing.
................................................................................
 71250  71352   }
 71251  71353   
 71252  71354   /*
 71253  71355   ** If the last opcode is "op" and it is not a jump destination,
 71254  71356   ** then remove it.  Return true if and only if an opcode was removed.
 71255  71357   */
 71256  71358   SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
 71257         -  if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
        71359  +  if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
 71258  71360       return sqlite3VdbeChangeToNoop(p, p->nOp-1);
 71259  71361     }else{
 71260  71362       return 0;
 71261  71363     }
 71262  71364   }
 71263  71365   
 71264  71366   /*
................................................................................
 71812  71914     fprintf(pOut, zFormat1, pc, 
 71813  71915         sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
 71814  71916         zCom
 71815  71917     );
 71816  71918     fflush(pOut);
 71817  71919   }
 71818  71920   #endif
        71921  +
        71922  +/*
        71923  +** Initialize an array of N Mem element.
        71924  +*/
        71925  +static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
        71926  +  while( (N--)>0 ){
        71927  +    p->db = db;
        71928  +    p->flags = flags;
        71929  +    p->szMalloc = 0;
        71930  +#ifdef SQLITE_DEBUG
        71931  +    p->pScopyFrom = 0;
        71932  +#endif
        71933  +    p++;
        71934  +  }
        71935  +}
 71819  71936   
 71820  71937   /*
 71821  71938   ** Release an array of N Mem elements
 71822  71939   */
 71823  71940   static void releaseMemArray(Mem *p, int N){
 71824  71941     if( p && N ){
 71825  71942       Mem *pEnd = &p[N];
................................................................................
 72024  72141       if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
 72025  72142         assert( p->db->mallocFailed );
 72026  72143         return SQLITE_ERROR;
 72027  72144       }
 72028  72145       pMem->flags = MEM_Str|MEM_Term;
 72029  72146       zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
 72030  72147       if( zP4!=pMem->z ){
        72148  +      pMem->n = 0;
 72031  72149         sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
 72032  72150       }else{
 72033  72151         assert( pMem->z!=0 );
 72034  72152         pMem->n = sqlite3Strlen30(pMem->z);
 72035  72153         pMem->enc = SQLITE_UTF8;
 72036  72154       }
 72037  72155       pMem++;
................................................................................
 72166  72284   ** running it.
 72167  72285   */
 72168  72286   SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
 72169  72287   #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 72170  72288     int i;
 72171  72289   #endif
 72172  72290     assert( p!=0 );
 72173         -  assert( p->magic==VDBE_MAGIC_INIT );
        72291  +  assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
 72174  72292   
 72175  72293     /* There should be at least one opcode.
 72176  72294     */
 72177  72295     assert( p->nOp>0 );
 72178  72296   
 72179  72297     /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
 72180  72298     p->magic = VDBE_MAGIC_RUN;
................................................................................
 72223  72341     Parse *pParse                  /* Parsing context */
 72224  72342   ){
 72225  72343     sqlite3 *db;                   /* The database connection */
 72226  72344     int nVar;                      /* Number of parameters */
 72227  72345     int nMem;                      /* Number of VM memory registers */
 72228  72346     int nCursor;                   /* Number of cursors required */
 72229  72347     int nArg;                      /* Number of arguments in subprograms */
 72230         -  int nOnce;                     /* Number of OP_Once instructions */
 72231  72348     int n;                         /* Loop counter */
 72232  72349     struct ReusableSpace x;        /* Reusable bulk memory */
 72233  72350   
 72234  72351     assert( p!=0 );
 72235  72352     assert( p->nOp>0 );
 72236  72353     assert( pParse!=0 );
 72237  72354     assert( p->magic==VDBE_MAGIC_INIT );
................................................................................
 72238  72355     assert( pParse==p->pParse );
 72239  72356     db = p->db;
 72240  72357     assert( db->mallocFailed==0 );
 72241  72358     nVar = pParse->nVar;
 72242  72359     nMem = pParse->nMem;
 72243  72360     nCursor = pParse->nTab;
 72244  72361     nArg = pParse->nMaxArg;
 72245         -  nOnce = pParse->nOnce;
 72246         -  if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
 72247  72362     
 72248  72363     /* Each cursor uses a memory cell.  The first cursor (cursor 0) can
 72249  72364     ** use aMem[0] which is not otherwise used by the VDBE program.  Allocate
 72250  72365     ** space at the end of aMem[] for cursors 1 and greater.
 72251  72366     ** See also: allocateCursor().
 72252  72367     */
 72253  72368     nMem += nCursor;
................................................................................
 72258  72373     ** of the prepared statement.
 72259  72374     */
 72260  72375     n = ROUND8(sizeof(Op)*p->nOp);              /* Bytes of opcode memory used */
 72261  72376     x.pSpace = &((u8*)p->aOp)[n];               /* Unused opcode memory */
 72262  72377     assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
 72263  72378     x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n);  /* Bytes of unused memory */
 72264  72379     assert( x.nFree>=0 );
 72265         -  if( x.nFree>0 ){
 72266         -    memset(x.pSpace, 0, x.nFree);
 72267         -    assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
 72268         -  }
        72380  +  assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
 72269  72381   
 72270  72382     resolveP2Values(p, &nArg);
 72271  72383     p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
 72272  72384     if( pParse->explain && nMem<10 ){
 72273  72385       nMem = 10;
 72274  72386     }
 72275  72387     p->expired = 0;
................................................................................
 72286  72398     */
 72287  72399     do {
 72288  72400       x.nNeeded = 0;
 72289  72401       p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
 72290  72402       p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
 72291  72403       p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
 72292  72404       p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
 72293         -    p->aOnceFlag = allocSpace(&x, p->aOnceFlag, nOnce);
 72294  72405   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 72295  72406       p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
 72296  72407   #endif
 72297  72408       if( x.nNeeded==0 ) break;
 72298         -    x.pSpace = p->pFree = sqlite3DbMallocZero(db, x.nNeeded);
        72409  +    x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
 72299  72410       x.nFree = x.nNeeded;
 72300  72411     }while( !db->mallocFailed );
 72301  72412   
 72302         -  p->nCursor = nCursor;
 72303         -  p->nOnceFlag = nOnce;
 72304         -  if( p->aVar ){
 72305         -    p->nVar = (ynVar)nVar;
 72306         -    for(n=0; n<nVar; n++){
 72307         -      p->aVar[n].flags = MEM_Null;
 72308         -      p->aVar[n].db = db;
 72309         -    }
 72310         -  }
 72311  72413     p->nzVar = pParse->nzVar;
 72312  72414     p->azVar = pParse->azVar;
 72313  72415     pParse->nzVar =  0;
 72314  72416     pParse->azVar = 0;
 72315         -  if( p->aMem ){
 72316         -    p->nMem = nMem;
 72317         -    for(n=0; n<nMem; n++){
 72318         -      p->aMem[n].flags = MEM_Undefined;
 72319         -      p->aMem[n].db = db;
 72320         -    }
 72321         -  }
 72322  72417     p->explain = pParse->explain;
        72418  +  if( db->mallocFailed ){
        72419  +    p->nVar = 0;
        72420  +    p->nCursor = 0;
        72421  +    p->nMem = 0;
        72422  +  }else{
        72423  +    p->nCursor = nCursor;
        72424  +    p->nVar = (ynVar)nVar;
        72425  +    initMemArray(p->aVar, nVar, db, MEM_Null);
        72426  +    p->nMem = nMem;
        72427  +    initMemArray(p->aMem, nMem, db, MEM_Undefined);
        72428  +    memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
        72429  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
        72430  +    memset(p->anExec, 0, p->nOp*sizeof(i64));
        72431  +#endif
        72432  +  }
 72323  72433     sqlite3VdbeRewind(p);
 72324  72434   }
 72325  72435   
 72326  72436   /*
 72327  72437   ** Close a VDBE cursor and release all the resources that cursor 
 72328  72438   ** happens to hold.
 72329  72439   */
................................................................................
 72384  72494   */
 72385  72495   SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
 72386  72496     Vdbe *v = pFrame->v;
 72387  72497     closeCursorsInFrame(v);
 72388  72498   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 72389  72499     v->anExec = pFrame->anExec;
 72390  72500   #endif
 72391         -  v->aOnceFlag = pFrame->aOnceFlag;
 72392         -  v->nOnceFlag = pFrame->nOnceFlag;
 72393  72501     v->aOp = pFrame->aOp;
 72394  72502     v->nOp = pFrame->nOp;
 72395  72503     v->aMem = pFrame->aMem;
 72396  72504     v->nMem = pFrame->nMem;
 72397  72505     v->apCsr = pFrame->apCsr;
 72398  72506     v->nCursor = pFrame->nCursor;
 72399  72507     v->db->lastRowid = pFrame->lastRowid;
................................................................................
 72469  72577     int n;
 72470  72578     sqlite3 *db = p->db;
 72471  72579   
 72472  72580     releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
 72473  72581     sqlite3DbFree(db, p->aColName);
 72474  72582     n = nResColumn*COLNAME_N;
 72475  72583     p->nResColumn = (u16)nResColumn;
 72476         -  p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
        72584  +  p->aColName = pColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
 72477  72585     if( p->aColName==0 ) return;
 72478         -  while( n-- > 0 ){
 72479         -    pColName->flags = MEM_Null;
 72480         -    pColName->db = p->db;
 72481         -    pColName++;
 72482         -  }
        72586  +  initMemArray(p->aColName, n, p->db, MEM_Null);
 72483  72587   }
 72484  72588   
 72485  72589   /*
 72486  72590   ** Set the name of the idx'th column to be returned by the SQL statement.
 72487  72591   ** zName must be a pointer to a nul terminated string.
 72488  72592   **
 72489  72593   ** This call must be made after a call to sqlite3VdbeSetNumCols().
................................................................................
 72926  73030     ** state.  We need to rollback the statement transaction, if there is
 72927  73031     ** one, or the complete transaction if there is no statement transaction.
 72928  73032     */
 72929  73033   
 72930  73034     if( db->mallocFailed ){
 72931  73035       p->rc = SQLITE_NOMEM_BKPT;
 72932  73036     }
 72933         -  if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
 72934  73037     closeAllCursors(p);
 72935  73038     if( p->magic!=VDBE_MAGIC_RUN ){
 72936  73039       return SQLITE_OK;
 72937  73040     }
 72938  73041     checkActiveVdbeCnt(db);
 72939  73042   
 72940  73043     /* No commit or rollback needed if the program never started or if the
................................................................................
 73238  73341           sqlite3VdbePrintOp(out, i, &p->aOp[i]);
 73239  73342         }
 73240  73343         fclose(out);
 73241  73344       }
 73242  73345     }
 73243  73346   #endif
 73244  73347     p->iCurrentTime = 0;
 73245         -  p->magic = VDBE_MAGIC_INIT;
        73348  +  p->magic = VDBE_MAGIC_RESET;
 73246  73349     return p->rc & db->errMask;
 73247  73350   }
 73248  73351    
 73249  73352   /*
 73250  73353   ** Clean up and delete a VDBE after execution.  Return an integer which is
 73251  73354   ** the result code.  Write any error message text into *pzErrMsg.
 73252  73355   */
................................................................................
 73302  73405   ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
 73303  73406   ** the database connection and frees the object itself.
 73304  73407   */
 73305  73408   SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
 73306  73409     SubProgram *pSub, *pNext;
 73307  73410     int i;
 73308  73411     assert( p->db==0 || p->db==db );
 73309         -  releaseMemArray(p->aVar, p->nVar);
 73310  73412     releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
 73311  73413     for(pSub=p->pProgram; pSub; pSub=pNext){
 73312  73414       pNext = pSub->pNext;
 73313  73415       vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
 73314  73416       sqlite3DbFree(db, pSub);
 73315  73417     }
 73316         -  for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
 73317         -  sqlite3DbFree(db, p->azVar);
        73418  +  if( p->magic!=VDBE_MAGIC_INIT ){
        73419  +    releaseMemArray(p->aVar, p->nVar);
        73420  +    for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
        73421  +    sqlite3DbFree(db, p->azVar);
        73422  +    sqlite3DbFree(db, p->pFree);
        73423  +  }
 73318  73424     vdbeFreeOpArray(db, p->aOp, p->nOp);
 73319  73425     sqlite3DbFree(db, p->aColName);
 73320  73426     sqlite3DbFree(db, p->zSql);
 73321         -  sqlite3DbFree(db, p->pFree);
 73322  73427   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 73323  73428     for(i=0; i<p->nScan; i++){
 73324  73429       sqlite3DbFree(db, p->aScan[i].zName);
 73325  73430     }
 73326  73431     sqlite3DbFree(db, p->aScan);
 73327  73432   #endif
 73328  73433   }
................................................................................
 74069  74174       rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
 74070  74175       if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
 74071  74176       sqlite3VdbeMemRelease(&c1);
 74072  74177       sqlite3VdbeMemRelease(&c2);
 74073  74178       return rc;
 74074  74179     }
 74075  74180   }
        74181  +
        74182  +/*
        74183  +** The input pBlob is guaranteed to be a Blob that is not marked
        74184  +** with MEM_Zero.  Return true if it could be a zero-blob.
        74185  +*/
        74186  +static int isAllZero(const char *z, int n){
        74187  +  int i;
        74188  +  for(i=0; i<n; i++){
        74189  +    if( z[i] ) return 0;
        74190  +  }
        74191  +  return 1;
        74192  +}
 74076  74193   
 74077  74194   /*
 74078  74195   ** Compare two blobs.  Return negative, zero, or positive if the first
 74079  74196   ** is less than, equal to, or greater than the second, respectively.
 74080  74197   ** If one blob is a prefix of the other, then the shorter is the lessor.
 74081  74198   */
 74082  74199   static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
 74083         -  int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
        74200  +  int c;
        74201  +  int n1 = pB1->n;
        74202  +  int n2 = pB2->n;
        74203  +
        74204  +  /* It is possible to have a Blob value that has some non-zero content
        74205  +  ** followed by zero content.  But that only comes up for Blobs formed
        74206  +  ** by the OP_MakeRecord opcode, and such Blobs never get passed into
        74207  +  ** sqlite3MemCompare(). */
        74208  +  assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
        74209  +  assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
        74210  +
        74211  +  if( (pB1->flags|pB2->flags) & MEM_Zero ){
        74212  +    if( pB1->flags & pB2->flags & MEM_Zero ){
        74213  +      return pB1->u.nZero - pB2->u.nZero;
        74214  +    }else if( pB1->flags & MEM_Zero ){
        74215  +      if( !isAllZero(pB2->z, pB2->n) ) return -1;
        74216  +      return pB1->u.nZero - n2;
        74217  +    }else{
        74218  +      if( !isAllZero(pB1->z, pB1->n) ) return +1;
        74219  +      return n1 - pB2->u.nZero;
        74220  +    }
        74221  +  }
        74222  +  c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
 74084  74223     if( c ) return c;
 74085         -  return pB1->n - pB2->n;
        74224  +  return n1 - n2;
 74086  74225   }
 74087  74226   
 74088  74227   /*
 74089  74228   ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
 74090  74229   ** number.  Return negative, zero, or positive if the first (i64) is less than,
 74091  74230   ** equal to, or greater than the second (double).
 74092  74231   */
................................................................................
 74384  74523             if( rc==0 ) rc = mem1.n - pRhs->n; 
 74385  74524           }
 74386  74525         }
 74387  74526       }
 74388  74527   
 74389  74528       /* RHS is a blob */
 74390  74529       else if( pRhs->flags & MEM_Blob ){
        74530  +      assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
 74391  74531         getVarint32(&aKey1[idx1], serial_type);
 74392  74532         testcase( serial_type==12 );
 74393  74533         if( serial_type<12 || (serial_type & 0x01) ){
 74394  74534           rc = -1;
 74395  74535         }else{
 74396  74536           int nStr = (serial_type - 12) / 2;
 74397  74537           testcase( (d1+nStr)==(unsigned)nKey1 );
 74398  74538           testcase( (d1+nStr+1)==(unsigned)nKey1 );
 74399  74539           if( (d1+nStr) > (unsigned)nKey1 ){
 74400  74540             pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
 74401  74541             return 0;                /* Corruption */
        74542  +        }else if( pRhs->flags & MEM_Zero ){
        74543  +          if( !isAllZero((const char*)&aKey1[d1],nStr) ){
        74544  +            rc = 1;
        74545  +          }else{
        74546  +            rc = nStr - pRhs->u.nZero;
        74547  +          }
 74402  74548           }else{
 74403  74549             int nCmp = MIN(nStr, pRhs->n);
 74404  74550             rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
 74405  74551             if( rc==0 ) rc = nStr - pRhs->n;
 74406  74552           }
 74407  74553         }
 74408  74554       }
................................................................................
 74465  74611     UnpackedRecord *pPKey2        /* Right key */
 74466  74612   ){
 74467  74613     const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
 74468  74614     int serial_type = ((const u8*)pKey1)[1];
 74469  74615     int res;
 74470  74616     u32 y;
 74471  74617     u64 x;
 74472         -  i64 v = pPKey2->aMem[0].u.i;
        74618  +  i64 v;
 74473  74619     i64 lhs;
 74474  74620   
 74475  74621     vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
 74476  74622     assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
 74477  74623     switch( serial_type ){
 74478  74624       case 1: { /* 1-byte signed integer */
 74479  74625         lhs = ONE_BYTE_INT(aKey);
................................................................................
 74524  74670       case 0: case 7:
 74525  74671         return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
 74526  74672   
 74527  74673       default:
 74528  74674         return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
 74529  74675     }
 74530  74676   
        74677  +  v = pPKey2->aMem[0].u.i;
 74531  74678     if( v>lhs ){
 74532  74679       res = pPKey2->r1;
 74533  74680     }else if( v<lhs ){
 74534  74681       res = pPKey2->r2;
 74535  74682     }else if( pPKey2->nField>1 ){
 74536  74683       /* The first fields of the two keys are equal. Compare the trailing 
 74537  74684       ** fields.  */
................................................................................
 75118  75265   /**************************** sqlite3_value_  *******************************
 75119  75266   ** The following routines extract information from a Mem or sqlite3_value
 75120  75267   ** structure.
 75121  75268   */
 75122  75269   SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
 75123  75270     Mem *p = (Mem*)pVal;
 75124  75271     if( p->flags & (MEM_Blob|MEM_Str) ){
 75125         -    if( sqlite3VdbeMemExpandBlob(p)!=SQLITE_OK ){
        75272  +    if( ExpandBlob(p)!=SQLITE_OK ){
 75126  75273         assert( p->flags==MEM_Null && p->z==0 );
 75127  75274         return 0;
 75128  75275       }
 75129  75276       p->flags |= MEM_Blob;
 75130  75277       return p->n ? p->z : 0;
 75131  75278     }else{
 75132  75279       return sqlite3_value_text(pVal);
................................................................................
 75448  75595       Btree *pBt = db->aDb[i].pBt;
 75449  75596       if( pBt ){
 75450  75597         int nEntry;
 75451  75598         sqlite3BtreeEnter(pBt);
 75452  75599         nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
 75453  75600         sqlite3BtreeLeave(pBt);
 75454  75601         if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
 75455         -        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
        75602  +        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
 75456  75603         }
 75457  75604       }
 75458  75605     }
 75459  75606   #endif
 75460  75607     return rc;
 75461  75608   }
 75462  75609   
................................................................................
 75901  76048   ** of NULL.
 75902  76049   */
 75903  76050   static Mem *columnMem(sqlite3_stmt *pStmt, int i){
 75904  76051     Vdbe *pVm;
 75905  76052     Mem *pOut;
 75906  76053   
 75907  76054     pVm = (Vdbe *)pStmt;
 75908         -  if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
 75909         -    sqlite3_mutex_enter(pVm->db->mutex);
        76055  +  if( pVm==0 ) return (Mem*)columnNullValue();
        76056  +  assert( pVm->db );
        76057  +  sqlite3_mutex_enter(pVm->db->mutex);
        76058  +  if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
 75910  76059       pOut = &pVm->pResultSet[i];
 75911  76060     }else{
 75912         -    if( pVm && ALWAYS(pVm->db) ){
 75913         -      sqlite3_mutex_enter(pVm->db->mutex);
 75914         -      sqlite3Error(pVm->db, SQLITE_RANGE);
 75915         -    }
        76061  +    sqlite3Error(pVm->db, SQLITE_RANGE);
 75916  76062       pOut = (Mem*)columnNullValue();
 75917  76063     }
 75918  76064     return pOut;
 75919  76065   }
 75920  76066   
 75921  76067   /*
 75922  76068   ** This function is called after invoking an sqlite3_value_XXX function on a 
................................................................................
 75941  76087     /* If malloc() failed during an encoding conversion within an
 75942  76088     ** sqlite3_column_XXX API, then set the return code of the statement to
 75943  76089     ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
 75944  76090     ** and _finalize() will return NOMEM.
 75945  76091     */
 75946  76092     Vdbe *p = (Vdbe *)pStmt;
 75947  76093     if( p ){
        76094  +    assert( p->db!=0 );
        76095  +    assert( sqlite3_mutex_held(p->db->mutex) );
 75948  76096       p->rc = sqlite3ApiExit(p->db, p->rc);
 75949  76097       sqlite3_mutex_leave(p->db->mutex);
 75950  76098     }
 75951  76099   }
 75952  76100   
 75953  76101   /**************************** sqlite3_column_  *******************************
 75954  76102   ** The following routines are used to access elements of the current row
................................................................................
 76517  76665   }
 76518  76666   
 76519  76667   /*
 76520  76668   ** Return true if the prepared statement is in need of being reset.
 76521  76669   */
 76522  76670   SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
 76523  76671     Vdbe *v = (Vdbe*)pStmt;
 76524         -  return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN;
        76672  +  return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
 76525  76673   }
 76526  76674   
 76527  76675   /*
 76528  76676   ** Return a pointer to the next prepared statement after pStmt associated
 76529  76677   ** with database connection pDb.  If pStmt is NULL, return the first
 76530  76678   ** prepared statement for the database connection.  Return NULL if there
 76531  76679   ** are no more.
................................................................................
 76724  76872   
 76725  76873     if( p->op==SQLITE_INSERT ){
 76726  76874       /* For an INSERT, memory cell p->iNewReg contains the serialized record
 76727  76875       ** that is being inserted. Deserialize it. */
 76728  76876       UnpackedRecord *pUnpack = p->pNewUnpacked;
 76729  76877       if( !pUnpack ){
 76730  76878         Mem *pData = &p->v->aMem[p->iNewReg];
 76731         -      rc = sqlite3VdbeMemExpandBlob(pData);
        76879  +      rc = ExpandBlob(pData);
 76732  76880         if( rc!=SQLITE_OK ) goto preupdate_new_out;
 76733  76881         pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
 76734  76882         if( !pUnpack ){
 76735  76883           rc = SQLITE_NOMEM;
 76736  76884           goto preupdate_new_out;
 76737  76885         }
 76738  76886         p->pNewUnpacked = pUnpack;
................................................................................
 77706  77854   #ifdef SQLITE_DEBUG
 77707  77855     int nExtraDelete = 0;      /* Verifies FORDELETE and AUXDELETE flags */
 77708  77856   #endif
 77709  77857     int rc = SQLITE_OK;        /* Value to return */
 77710  77858     sqlite3 *db = p->db;       /* The database */
 77711  77859     u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
 77712  77860     u8 encoding = ENC(db);     /* The database encoding */
 77713         -  int iCompare = 0;          /* Result of last OP_Compare operation */
        77861  +  int iCompare = 0;          /* Result of last comparison */
 77714  77862     unsigned nVmStep = 0;      /* Number of virtual machine steps */
 77715  77863   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 77716  77864     unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
 77717  77865   #endif
 77718  77866     Mem *aMem = p->aMem;       /* Copy of p->aMem */
 77719  77867     Mem *pIn1 = 0;             /* 1st input operand */
 77720  77868     Mem *pIn2 = 0;             /* 2nd input operand */
................................................................................
 78038  78186     pIn1->u.i = (int)(pOp - aOp);
 78039  78187     REGISTER_TRACE(pOp->p1, pIn1);
 78040  78188     pOp = &aOp[pcDest];
 78041  78189     break;
 78042  78190   }
 78043  78191   
 78044  78192   /* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
 78045         -** Synopsis:  if r[P3]=null halt
        78193  +** Synopsis: if r[P3]=null halt
 78046  78194   **
 78047  78195   ** Check the value in register P3.  If it is NULL then Halt using
 78048  78196   ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
 78049  78197   ** value in register P3 is not NULL, then this routine is a no-op.
 78050  78198   ** The P5 parameter should be 1.
 78051  78199   */
 78052  78200   case OP_HaltIfNull: {      /* in3 */
................................................................................
 78251  78399       if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
 78252  78400     }
 78253  78401   #endif
 78254  78402     break;
 78255  78403   }
 78256  78404   
 78257  78405   /* Opcode: Null P1 P2 P3 * *
 78258         -** Synopsis:  r[P2..P3]=NULL
        78406  +** Synopsis: r[P2..P3]=NULL
 78259  78407   **
 78260  78408   ** Write a NULL into registers P2.  If P3 greater than P2, then also write
 78261  78409   ** NULL into register P3 and every register in between P2 and P3.  If P3
 78262  78410   ** is less than P2 (typically P3 is zero) then only register P2 is
 78263  78411   ** set to NULL.
 78264  78412   **
 78265  78413   ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
................................................................................
 78269  78417   case OP_Null: {           /* out2 */
 78270  78418     int cnt;
 78271  78419     u16 nullFlag;
 78272  78420     pOut = out2Prerelease(p, pOp);
 78273  78421     cnt = pOp->p3-pOp->p2;
 78274  78422     assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
 78275  78423     pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
        78424  +  pOut->n = 0;
 78276  78425     while( cnt>0 ){
 78277  78426       pOut++;
 78278  78427       memAboutToChange(p, pOut);
 78279  78428       sqlite3VdbeMemSetNull(pOut);
 78280  78429       pOut->flags = nullFlag;
        78430  +    pOut->n = 0;
 78281  78431       cnt--;
 78282  78432     }
 78283  78433     break;
 78284  78434   }
 78285  78435   
 78286  78436   /* Opcode: SoftNull P1 * * * *
 78287         -** Synopsis:  r[P1]=NULL
        78437  +** Synopsis: r[P1]=NULL
 78288  78438   **
 78289  78439   ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
 78290  78440   ** instruction, but do not free any string or blob memory associated with
 78291  78441   ** the register, so that if the value was a string or blob that was
 78292  78442   ** previously copied using OP_SCopy, the copies will continue to be valid.
 78293  78443   */
 78294  78444   case OP_SoftNull: {
................................................................................
 78333  78483     pOut = out2Prerelease(p, pOp);
 78334  78484     sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
 78335  78485     UPDATE_MAX_BLOBSIZE(pOut);
 78336  78486     break;
 78337  78487   }
 78338  78488   
 78339  78489   /* Opcode: Move P1 P2 P3 * *
 78340         -** Synopsis:  r[P2@P3]=r[P1@P3]
        78490  +** Synopsis: r[P2@P3]=r[P1@P3]
 78341  78491   **
 78342  78492   ** Move the P3 values in register P1..P1+P3-1 over into
 78343  78493   ** registers P2..P2+P3-1.  Registers P1..P1+P3-1 are
 78344  78494   ** left holding a NULL.  It is an error for register ranges
 78345  78495   ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.  It is an error
 78346  78496   ** for P3 to be less than 1.
 78347  78497   */
................................................................................
 78443  78593     assert( (pIn1->flags & MEM_Int)!=0 );
 78444  78594     pOut = &aMem[pOp->p2];
 78445  78595     sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
 78446  78596     break;
 78447  78597   }
 78448  78598   
 78449  78599   /* Opcode: ResultRow P1 P2 * * *
 78450         -** Synopsis:  output=r[P1@P2]
        78600  +** Synopsis: output=r[P1@P2]
 78451  78601   **
 78452  78602   ** The registers P1 through P1+P2-1 contain a single row of
 78453  78603   ** results. This opcode causes the sqlite3_step() call to terminate
 78454  78604   ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
 78455  78605   ** structure to provide access to the r(P1)..r(P1+P2-1) values as
 78456  78606   ** the result row.
 78457  78607   */
................................................................................
 78576  78726     pOut->n = (int)nByte;
 78577  78727     pOut->enc = encoding;
 78578  78728     UPDATE_MAX_BLOBSIZE(pOut);
 78579  78729     break;
 78580  78730   }
 78581  78731   
 78582  78732   /* Opcode: Add P1 P2 P3 * *
 78583         -** Synopsis:  r[P3]=r[P1]+r[P2]
        78733  +** Synopsis: r[P3]=r[P1]+r[P2]
 78584  78734   **
 78585  78735   ** Add the value in register P1 to the value in register P2
 78586  78736   ** and store the result in register P3.
 78587  78737   ** If either input is NULL, the result is NULL.
 78588  78738   */
 78589  78739   /* Opcode: Multiply P1 P2 P3 * *
 78590         -** Synopsis:  r[P3]=r[P1]*r[P2]
        78740  +** Synopsis: r[P3]=r[P1]*r[P2]
 78591  78741   **
 78592  78742   **
 78593  78743   ** Multiply the value in register P1 by the value in register P2
 78594  78744   ** and store the result in register P3.
 78595  78745   ** If either input is NULL, the result is NULL.
 78596  78746   */
 78597  78747   /* Opcode: Subtract P1 P2 P3 * *
 78598         -** Synopsis:  r[P3]=r[P2]-r[P1]
        78748  +** Synopsis: r[P3]=r[P2]-r[P1]
 78599  78749   **
 78600  78750   ** Subtract the value in register P1 from the value in register P2
 78601  78751   ** and store the result in register P3.
 78602  78752   ** If either input is NULL, the result is NULL.
 78603  78753   */
 78604  78754   /* Opcode: Divide P1 P2 P3 * *
 78605         -** Synopsis:  r[P3]=r[P2]/r[P1]
        78755  +** Synopsis: r[P3]=r[P2]/r[P1]
 78606  78756   **
 78607  78757   ** Divide the value in register P1 by the value in register P2
 78608  78758   ** and store the result in register P3 (P3=P2/P1). If the value in 
 78609  78759   ** register P1 is zero, then the result is NULL. If either input is 
 78610  78760   ** NULL, the result is NULL.
 78611  78761   */
 78612  78762   /* Opcode: Remainder P1 P2 P3 * *
 78613         -** Synopsis:  r[P3]=r[P2]%r[P1]
        78763  +** Synopsis: r[P3]=r[P2]%r[P1]
 78614  78764   **
 78615  78765   ** Compute the remainder after integer register P2 is divided by 
 78616  78766   ** register P1 and store the result in register P3. 
 78617  78767   ** If the value in register P1 is zero the result is NULL.
 78618  78768   ** If either operand is NULL, the result is NULL.
 78619  78769   */
 78620  78770   case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
................................................................................
 78839  78989   
 78840  78990     REGISTER_TRACE(pOp->p3, pCtx->pOut);
 78841  78991     UPDATE_MAX_BLOBSIZE(pCtx->pOut);
 78842  78992     break;
 78843  78993   }
 78844  78994   
 78845  78995   /* Opcode: BitAnd P1 P2 P3 * *
 78846         -** Synopsis:  r[P3]=r[P1]&r[P2]
        78996  +** Synopsis: r[P3]=r[P1]&r[P2]
 78847  78997   **
 78848  78998   ** Take the bit-wise AND of the values in register P1 and P2 and
 78849  78999   ** store the result in register P3.
 78850  79000   ** If either input is NULL, the result is NULL.
 78851  79001   */
 78852  79002   /* Opcode: BitOr P1 P2 P3 * *
 78853         -** Synopsis:  r[P3]=r[P1]|r[P2]
        79003  +** Synopsis: r[P3]=r[P1]|r[P2]
 78854  79004   **
 78855  79005   ** Take the bit-wise OR of the values in register P1 and P2 and
 78856  79006   ** store the result in register P3.
 78857  79007   ** If either input is NULL, the result is NULL.
 78858  79008   */
 78859  79009   /* Opcode: ShiftLeft P1 P2 P3 * *
 78860         -** Synopsis:  r[P3]=r[P2]<<r[P1]
        79010  +** Synopsis: r[P3]=r[P2]<<r[P1]
 78861  79011   **
 78862  79012   ** Shift the integer value in register P2 to the left by the
 78863  79013   ** number of bits specified by the integer in register P1.
 78864  79014   ** Store the result in register P3.
 78865  79015   ** If either input is NULL, the result is NULL.
 78866  79016   */
 78867  79017   /* Opcode: ShiftRight P1 P2 P3 * *
 78868         -** Synopsis:  r[P3]=r[P2]>>r[P1]
        79018  +** Synopsis: r[P3]=r[P2]>>r[P1]
 78869  79019   **
 78870  79020   ** Shift the integer value in register P2 to the right by the
 78871  79021   ** number of bits specified by the integer in register P1.
 78872  79022   ** Store the result in register P3.
 78873  79023   ** If either input is NULL, the result is NULL.
 78874  79024   */
 78875  79025   case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
................................................................................
 78921  79071     }
 78922  79072     pOut->u.i = iA;
 78923  79073     MemSetTypeFlag(pOut, MEM_Int);
 78924  79074     break;
 78925  79075   }
 78926  79076   
 78927  79077   /* Opcode: AddImm  P1 P2 * * *
 78928         -** Synopsis:  r[P1]=r[P1]+P2
        79078  +** Synopsis: r[P1]=r[P1]+P2
 78929  79079   ** 
 78930  79080   ** Add the constant P2 to the value in register P1.
 78931  79081   ** The result is always an integer.
 78932  79082   **
 78933  79083   ** To force any register to be an integer, just add 0.
 78934  79084   */
 78935  79085   case OP_AddImm: {            /* in1 */
................................................................................
 79013  79163     sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
 79014  79164     UPDATE_MAX_BLOBSIZE(pIn1);
 79015  79165     if( rc ) goto abort_due_to_error;
 79016  79166     break;
 79017  79167   }
 79018  79168   #endif /* SQLITE_OMIT_CAST */
 79019  79169   
        79170  +/* Opcode: Eq P1 P2 P3 P4 P5
        79171  +** Synopsis: IF r[P3]==r[P1]
        79172  +**
        79173  +** Compare the values in register P1 and P3.  If reg(P3)==reg(P1) then
        79174  +** jump to address P2.  Or if the SQLITE_STOREP2 flag is set in P5, then
        79175  +** store the result of comparison in register P2.
        79176  +**
        79177  +** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
        79178  +** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
        79179  +** to coerce both inputs according to this affinity before the
        79180  +** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
        79181  +** affinity is used. Note that the affinity conversions are stored
        79182  +** back into the input registers P1 and P3.  So this opcode can cause
        79183  +** persistent changes to registers P1 and P3.
        79184  +**
        79185  +** Once any conversions have taken place, and neither value is NULL, 
        79186  +** the values are compared. If both values are blobs then memcmp() is
        79187  +** used to determine the results of the comparison.  If both values
        79188  +** are text, then the appropriate collating function specified in
        79189  +** P4 is used to do the comparison.  If P4 is not specified then
        79190  +** memcmp() is used to compare text string.  If both values are
        79191  +** numeric, then a numeric comparison is used. If the two values
        79192  +** are of different types, then numbers are considered less than
        79193  +** strings and strings are considered less than blobs.
        79194  +**
        79195  +** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
        79196  +** true or false and is never NULL.  If both operands are NULL then the result
        79197  +** of comparison is true.  If either operand is NULL then the result is false.
        79198  +** If neither operand is NULL the result is the same as it would be if
        79199  +** the SQLITE_NULLEQ flag were omitted from P5.
        79200  +**
        79201  +** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
        79202  +** content of r[P2] is only changed if the new value is NULL or 0 (false).
        79203  +** In other words, a prior r[P2] value will not be overwritten by 1 (true).
        79204  +*/
        79205  +/* Opcode: Ne P1 P2 P3 P4 P5
        79206  +** Synopsis: IF r[P3]!=r[P1]
        79207  +**
        79208  +** This works just like the Eq opcode except that the jump is taken if
        79209  +** the operands in registers P1 and P3 are not equal.  See the Eq opcode for
        79210  +** additional information.
        79211  +**
        79212  +** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
        79213  +** content of r[P2] is only changed if the new value is NULL or 1 (true).
        79214  +** In other words, a prior r[P2] value will not be overwritten by 0 (false).
        79215  +*/
 79020  79216   /* Opcode: Lt P1 P2 P3 P4 P5
 79021  79217   ** Synopsis: IF r[P3]<r[P1]
 79022  79218   **
 79023  79219   ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
 79024         -** jump to address P2.  
        79220  +** jump to address P2.  Or if the SQLITE_STOREP2 flag is set in P5 store
        79221  +** the result of comparison (0 or 1 or NULL) into register P2.
 79025  79222   **
 79026  79223   ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
 79027         -** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
        79224  +** reg(P3) is NULL then the take the jump.  If the SQLITE_JUMPIFNULL 
 79028  79225   ** bit is clear then fall through if either operand is NULL.
 79029  79226   **
 79030  79227   ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
 79031  79228   ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
 79032  79229   ** to coerce both inputs according to this affinity before the
 79033  79230   ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
 79034  79231   ** affinity is used. Note that the affinity conversions are stored
................................................................................
 79040  79237   ** used to determine the results of the comparison.  If both values
 79041  79238   ** are text, then the appropriate collating function specified in
 79042  79239   ** P4 is  used to do the comparison.  If P4 is not specified then
 79043  79240   ** memcmp() is used to compare text string.  If both values are
 79044  79241   ** numeric, then a numeric comparison is used. If the two values
 79045  79242   ** are of different types, then numbers are considered less than
 79046  79243   ** strings and strings are considered less than blobs.
 79047         -**
 79048         -** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
 79049         -** store a boolean result (either 0, or 1, or NULL) in register P2.
 79050         -**
 79051         -** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
 79052         -** equal to one another, provided that they do not have their MEM_Cleared
 79053         -** bit set.
 79054         -*/
 79055         -/* Opcode: Ne P1 P2 P3 P4 P5
 79056         -** Synopsis: IF r[P3]!=r[P1]
 79057         -**
 79058         -** This works just like the Lt opcode except that the jump is taken if
 79059         -** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
 79060         -** additional information.
 79061         -**
 79062         -** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 79063         -** true or false and is never NULL.  If both operands are NULL then the result
 79064         -** of comparison is false.  If either operand is NULL then the result is true.
 79065         -** If neither operand is NULL the result is the same as it would be if
 79066         -** the SQLITE_NULLEQ flag were omitted from P5.
 79067         -*/
 79068         -/* Opcode: Eq P1 P2 P3 P4 P5
 79069         -** Synopsis: IF r[P3]==r[P1]
 79070         -**
 79071         -** This works just like the Lt opcode except that the jump is taken if
 79072         -** the operands in registers P1 and P3 are equal.
 79073         -** See the Lt opcode for additional information.
 79074         -**
 79075         -** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 79076         -** true or false and is never NULL.  If both operands are NULL then the result
 79077         -** of comparison is true.  If either operand is NULL then the result is false.
 79078         -** If neither operand is NULL the result is the same as it would be if
 79079         -** the SQLITE_NULLEQ flag were omitted from P5.
 79080  79244   */
 79081  79245   /* Opcode: Le P1 P2 P3 P4 P5
 79082  79246   ** Synopsis: IF r[P3]<=r[P1]
 79083  79247   **
 79084  79248   ** This works just like the Lt opcode except that the jump is taken if
 79085  79249   ** the content of register P3 is less than or equal to the content of
 79086  79250   ** register P1.  See the Lt opcode for additional information.
................................................................................
 79101  79265   */
 79102  79266   case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
 79103  79267   case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
 79104  79268   case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
 79105  79269   case OP_Le:               /* same as TK_LE, jump, in1, in3 */
 79106  79270   case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
 79107  79271   case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
 79108         -  int res;            /* Result of the comparison of pIn1 against pIn3 */
        79272  +  int res, res2;      /* Result of the comparison of pIn1 against pIn3 */
 79109  79273     char affinity;      /* Affinity to use for comparison */
 79110  79274     u16 flags1;         /* Copy of initial value of pIn1->flags */
 79111  79275     u16 flags3;         /* Copy of initial value of pIn3->flags */
 79112  79276   
 79113  79277     pIn1 = &aMem[pOp->p1];
 79114  79278     pIn3 = &aMem[pOp->p3];
 79115  79279     flags1 = pIn1->flags;
................................................................................
 79124  79288         assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
 79125  79289         assert( (flags1 & MEM_Cleared)==0 );
 79126  79290         assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
 79127  79291         if( (flags1&MEM_Null)!=0
 79128  79292          && (flags3&MEM_Null)!=0
 79129  79293          && (flags3&MEM_Cleared)==0
 79130  79294         ){
 79131         -        res = 0;  /* Results are equal */
        79295  +        res = 0;  /* Operands are equal */
 79132  79296         }else{
 79133         -        res = 1;  /* Results are not equal */
        79297  +        res = 1;  /* Operands are not equal */
 79134  79298         }
 79135  79299       }else{
 79136  79300         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
 79137  79301         ** then the result is always NULL.
 79138  79302         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 79139  79303         */
 79140  79304         if( pOp->p5 & SQLITE_STOREP2 ){
 79141  79305           pOut = &aMem[pOp->p2];
        79306  +        iCompare = 1;    /* Operands are not equal */
 79142  79307           memAboutToChange(p, pOut);
 79143  79308           MemSetTypeFlag(pOut, MEM_Null);
 79144  79309           REGISTER_TRACE(pOp->p2, pOut);
 79145  79310         }else{
 79146  79311           VdbeBranchTaken(2,3);
 79147  79312           if( pOp->p5 & SQLITE_JUMPIFNULL ){
 79148  79313             goto jump_to_p2;
................................................................................
 79153  79318     }else{
 79154  79319       /* Neither operand is NULL.  Do a comparison. */
 79155  79320       affinity = pOp->p5 & SQLITE_AFF_MASK;
 79156  79321       if( affinity>=SQLITE_AFF_NUMERIC ){
 79157  79322         if( (flags1 | flags3)&MEM_Str ){
 79158  79323           if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
 79159  79324             applyNumericAffinity(pIn1,0);
        79325  +          testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
 79160  79326             flags3 = pIn3->flags;
 79161  79327           }
 79162  79328           if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
 79163  79329             applyNumericAffinity(pIn3,0);
 79164  79330           }
        79331  +      }
        79332  +      /* Handle the common case of integer comparison here, as an
        79333  +      ** optimization, to avoid a call to sqlite3MemCompare() */
        79334  +      if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
        79335  +        if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
        79336  +        if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
        79337  +        res = 0;
        79338  +        goto compare_op;
 79165  79339         }
 79166  79340       }else if( affinity==SQLITE_AFF_TEXT ){
 79167  79341         if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
 79168  79342           testcase( pIn1->flags & MEM_Int );
 79169  79343           testcase( pIn1->flags & MEM_Real );
 79170  79344           sqlite3VdbeMemStringify(pIn1, encoding, 1);
 79171  79345           testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
 79172  79346           flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
 79173         -        flags3 = pIn3->flags;
        79347  +        assert( pIn1!=pIn3 );
 79174  79348         }
 79175  79349         if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
 79176  79350           testcase( pIn3->flags & MEM_Int );
 79177  79351           testcase( pIn3->flags & MEM_Real );
 79178  79352           sqlite3VdbeMemStringify(pIn3, encoding, 1);
 79179  79353           testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
 79180  79354           flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
 79181  79355         }
 79182  79356       }
 79183  79357       assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
 79184         -    if( flags1 & MEM_Zero ){
 79185         -      sqlite3VdbeMemExpandBlob(pIn1);
 79186         -      flags1 &= ~MEM_Zero;
 79187         -    }
 79188         -    if( flags3 & MEM_Zero ){
 79189         -      sqlite3VdbeMemExpandBlob(pIn3);
 79190         -      flags3 &= ~MEM_Zero;
 79191         -    }
 79192  79358       res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
 79193  79359     }
        79360  +compare_op:
 79194  79361     switch( pOp->opcode ){
 79195         -    case OP_Eq:    res = res==0;     break;
 79196         -    case OP_Ne:    res = res!=0;     break;
 79197         -    case OP_Lt:    res = res<0;      break;
 79198         -    case OP_Le:    res = res<=0;     break;
 79199         -    case OP_Gt:    res = res>0;      break;
 79200         -    default:       res = res>=0;     break;
        79362  +    case OP_Eq:    res2 = res==0;     break;
        79363  +    case OP_Ne:    res2 = res;        break;
        79364  +    case OP_Lt:    res2 = res<0;      break;
        79365  +    case OP_Le:    res2 = res<=0;     break;
        79366  +    case OP_Gt:    res2 = res>0;      break;
        79367  +    default:       res2 = res>=0;     break;
 79201  79368     }
 79202  79369   
 79203  79370     /* Undo any changes made by applyAffinity() to the input registers. */
 79204  79371     assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
 79205  79372     pIn1->flags = flags1;
 79206  79373     assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
 79207  79374     pIn3->flags = flags3;
 79208  79375   
 79209  79376     if( pOp->p5 & SQLITE_STOREP2 ){
 79210  79377       pOut = &aMem[pOp->p2];
        79378  +    iCompare = res;
        79379  +    res2 = res2!=0;  /* For this path res2 must be exactly 0 or 1 */
        79380  +    if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
        79381  +      /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
        79382  +      ** and prevents OP_Ne from overwriting NULL with 0.  This flag
        79383  +      ** is only used in contexts where either:
        79384  +      **   (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
        79385  +      **   (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
        79386  +      ** Therefore it is not necessary to check the content of r[P2] for
        79387  +      ** NULL. */
        79388  +      assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
        79389  +      assert( res2==0 || res2==1 );
        79390  +      testcase( res2==0 && pOp->opcode==OP_Eq );
        79391  +      testcase( res2==1 && pOp->opcode==OP_Eq );
        79392  +      testcase( res2==0 && pOp->opcode==OP_Ne );
        79393  +      testcase( res2==1 && pOp->opcode==OP_Ne );
        79394  +      if( (pOp->opcode==OP_Eq)==res2 ) break;
        79395  +    }
 79211  79396       memAboutToChange(p, pOut);
 79212  79397       MemSetTypeFlag(pOut, MEM_Int);
 79213         -    pOut->u.i = res;
        79398  +    pOut->u.i = res2;
 79214  79399       REGISTER_TRACE(pOp->p2, pOut);
 79215  79400     }else{
 79216  79401       VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
 79217         -    if( res ){
        79402  +    if( res2 ){
 79218  79403         goto jump_to_p2;
 79219  79404       }
 79220  79405     }
 79221  79406     break;
 79222  79407   }
        79408  +
        79409  +/* Opcode: ElseNotEq * P2 * * *
        79410  +**
        79411  +** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
        79412  +** If result of an OP_Eq comparison on the same two operands
        79413  +** would have be NULL or false (0), then then jump to P2. 
        79414  +** If the result of an OP_Eq comparison on the two previous operands
        79415  +** would have been true (1), then fall through.
        79416  +*/
        79417  +case OP_ElseNotEq: {       /* same as TK_ESCAPE, jump */
        79418  +  assert( pOp>aOp );
        79419  +  assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
        79420  +  assert( pOp[-1].p5 & SQLITE_STOREP2 );
        79421  +  VdbeBranchTaken(iCompare!=0, 2);
        79422  +  if( iCompare!=0 ) goto jump_to_p2;
        79423  +  break;
        79424  +}
        79425  +
 79223  79426   
 79224  79427   /* Opcode: Permutation * * * P4 *
 79225  79428   **
 79226  79429   ** Set the permutation used by the OP_Compare operator to be the array
 79227  79430   ** of integers in P4.
 79228  79431   **
 79229  79432   ** The permutation is only valid until the next OP_Compare that has
................................................................................
 79412  79615       pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
 79413  79616     }
 79414  79617     break;
 79415  79618   }
 79416  79619   
 79417  79620   /* Opcode: Once P1 P2 * * *
 79418  79621   **
 79419         -** Check the "once" flag number P1. If it is set, jump to instruction P2. 
 79420         -** Otherwise, set the flag and fall through to the next instruction.
 79421         -** In other words, this opcode causes all following opcodes up through P2
 79422         -** (but not including P2) to run just once and to be skipped on subsequent
 79423         -** times through the loop.
 79424         -**
 79425         -** All "once" flags are initially cleared whenever a prepared statement
 79426         -** first begins to run.
        79622  +** If the P1 value is equal to the P1 value on the OP_Init opcode at
        79623  +** instruction 0, then jump to P2.  If the two P1 values differ, then
        79624  +** set the P1 value on this opcode to equal the P1 value on the OP_Init
        79625  +** and fall through.
 79427  79626   */
 79428  79627   case OP_Once: {             /* jump */
 79429         -  assert( pOp->p1<p->nOnceFlag );
 79430         -  VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
 79431         -  if( p->aOnceFlag[pOp->p1] ){
        79628  +  assert( p->aOp[0].opcode==OP_Init );
        79629  +  VdbeBranchTaken(p->aOp[0].p1==pOp->p1, 2);
        79630  +  if( p->aOp[0].p1==pOp->p1 ){
 79432  79631       goto jump_to_p2;
 79433  79632     }else{
 79434         -    p->aOnceFlag[pOp->p1] = 1;
        79633  +    pOp->p1 = p->aOp[0].p1;
 79435  79634     }
 79436  79635     break;
 79437  79636   }
 79438  79637   
 79439  79638   /* Opcode: If P1 P2 P3 * *
 79440  79639   **
 79441  79640   ** Jump to P2 if the value in register P1 is true.  The value
................................................................................
 79466  79665     if( c ){
 79467  79666       goto jump_to_p2;
 79468  79667     }
 79469  79668     break;
 79470  79669   }
 79471  79670   
 79472  79671   /* Opcode: IsNull P1 P2 * * *
 79473         -** Synopsis:  if r[P1]==NULL goto P2
        79672  +** Synopsis: if r[P1]==NULL goto P2
 79474  79673   **
 79475  79674   ** Jump to P2 if the value in register P1 is NULL.
 79476  79675   */
 79477  79676   case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
 79478  79677     pIn1 = &aMem[pOp->p1];
 79479  79678     VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
 79480  79679     if( (pIn1->flags & MEM_Null)!=0 ){
................................................................................
 79494  79693     if( (pIn1->flags & MEM_Null)==0 ){
 79495  79694       goto jump_to_p2;
 79496  79695     }
 79497  79696     break;
 79498  79697   }
 79499  79698   
 79500  79699   /* Opcode: Column P1 P2 P3 P4 P5
 79501         -** Synopsis:  r[P3]=PX
        79700  +** Synopsis: r[P3]=PX
 79502  79701   **
 79503  79702   ** Interpret the data that cursor P1 points to as a structure built using
 79504  79703   ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
 79505  79704   ** information about the format of the data.)  Extract the P2-th column
 79506  79705   ** from this record.  If there are less that (P2+1) 
 79507  79706   ** values in the record, extract a NULL.
 79508  79707   **
................................................................................
 80260  80459     }
 80261  80460     pBt = db->aDb[pOp->p1].pBt;
 80262  80461   
 80263  80462     if( pBt ){
 80264  80463       rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
 80265  80464       testcase( rc==SQLITE_BUSY_SNAPSHOT );
 80266  80465       testcase( rc==SQLITE_BUSY_RECOVERY );
 80267         -    if( (rc&0xff)==SQLITE_BUSY ){
 80268         -      p->pc = (int)(pOp - aOp);
 80269         -      p->rc = rc;
 80270         -      goto vdbe_return;
 80271         -    }
 80272  80466       if( rc!=SQLITE_OK ){
        80467  +      if( (rc&0xff)==SQLITE_BUSY ){
        80468  +        p->pc = (int)(pOp - aOp);
        80469  +        p->rc = rc;
        80470  +        goto vdbe_return;
        80471  +      }
 80273  80472         goto abort_due_to_error;
 80274  80473       }
 80275  80474   
 80276  80475       if( pOp->p2 && p->usesStmtJournal 
 80277  80476        && (db->autoCommit==0 || db->nVdbeRead>1) 
 80278  80477       ){
 80279  80478         assert( sqlite3BtreeIsInTrans(pBt) );
................................................................................
 80292  80491         ** counter. If the statement transaction needs to be rolled back,
 80293  80492         ** the value of this counter needs to be restored too.  */
 80294  80493         p->nStmtDefCons = db->nDeferredCons;
 80295  80494         p->nStmtDefImmCons = db->nDeferredImmCons;
 80296  80495       }
 80297  80496   
 80298  80497       /* Gather the schema version number for checking:
 80299         -    ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite
 80300         -    ** each time a query is executed to ensure that the internal cache of the
 80301         -    ** schema used when compiling the SQL query matches the schema of the
 80302         -    ** database against which the compiled query is actually executed.
        80498  +    ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
        80499  +    ** version is checked to ensure that the schema has not changed since the
        80500  +    ** SQL statement was prepared.
 80303  80501       */
 80304  80502       sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
 80305  80503       iGen = db->aDb[pOp->p1].pSchema->iGeneration;
 80306  80504     }else{
 80307  80505       iGen = iMeta = 0;
 80308  80506     }
 80309  80507     assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
................................................................................
 80956  81154       assert( oc!=OP_SeekGE || r.default_rc==+1 );
 80957  81155       assert( oc!=OP_SeekLT || r.default_rc==+1 );
 80958  81156   
 80959  81157       r.aMem = &aMem[pOp->p3];
 80960  81158   #ifdef SQLITE_DEBUG
 80961  81159       { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
 80962  81160   #endif
 80963         -    ExpandBlob(r.aMem);
 80964  81161       r.eqSeen = 0;
 80965  81162       rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
 80966  81163       if( rc!=SQLITE_OK ){
 80967  81164         goto abort_due_to_error;
 80968  81165       }
 80969  81166       if( eqOnly && r.eqSeen==0 ){
 80970  81167         assert( res!=0 );
................................................................................
 81004  81201       goto jump_to_p2;
 81005  81202     }else if( eqOnly ){
 81006  81203       assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
 81007  81204       pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
 81008  81205     }
 81009  81206     break;
 81010  81207   }
 81011         -  
 81012  81208   
 81013  81209   /* Opcode: Found P1 P2 P3 P4 *
 81014  81210   ** Synopsis: key=r[P3@P4]
 81015  81211   **
 81016  81212   ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
 81017  81213   ** P4>0 then register P3 is the first of P4 registers that form an unpacked
 81018  81214   ** record.
................................................................................
 81098  81294     assert( pC->uc.pCursor!=0 );
 81099  81295     assert( pC->isTable==0 );
 81100  81296     pFree = 0;
 81101  81297     if( pOp->p4.i>0 ){
 81102  81298       r.pKeyInfo = pC->pKeyInfo;
 81103  81299       r.nField = (u16)pOp->p4.i;
 81104  81300       r.aMem = pIn3;
        81301  +#ifdef SQLITE_DEBUG
 81105  81302       for(ii=0; ii<r.nField; ii++){
 81106  81303         assert( memIsValid(&r.aMem[ii]) );
 81107         -      ExpandBlob(&r.aMem[ii]);
 81108         -#ifdef SQLITE_DEBUG
        81304  +      assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
 81109  81305         if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
        81306  +    }
 81110  81307   #endif
 81111         -    }
 81112  81308       pIdxKey = &r;
 81113  81309     }else{
 81114  81310       pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 81115  81311           pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
 81116  81312       );
 81117  81313       if( pIdxKey==0 ) goto no_mem;
 81118  81314       assert( pIn3->flags & MEM_Blob );
 81119         -    ExpandBlob(pIn3);
        81315  +    (void)ExpandBlob(pIn3);
 81120  81316       sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
 81121  81317     }
 81122  81318     pIdxKey->default_rc = 0;
 81123  81319     takeJump = 0;
 81124  81320     if( pOp->opcode==OP_NoConflict ){
 81125  81321       /* For the OP_NoConflict opcode, take the jump if any of the
 81126  81322       ** input fields are NULL, since any key with a NULL will not
................................................................................
 81437  81633   ** value of register P2 will then change.  Make sure this does not
 81438  81634   ** cause any problems.)
 81439  81635   **
 81440  81636   ** This instruction only works on tables.  The equivalent instruction
 81441  81637   ** for indices is OP_IdxInsert.
 81442  81638   */
 81443  81639   /* Opcode: InsertInt P1 P2 P3 P4 P5
 81444         -** Synopsis:  intkey=P3 data=r[P2]
        81640  +** Synopsis: intkey=P3 data=r[P2]
 81445  81641   **
 81446  81642   ** This works exactly like OP_Insert except that the key is the
 81447  81643   ** integer value P3, not the value of the integer stored in register P3.
 81448  81644   */
 81449  81645   case OP_Insert: 
 81450  81646   case OP_InsertInt: {
 81451  81647     Mem *pData;       /* MEM cell holding data for the record to be inserted */
................................................................................
 81479  81675       assert( pOp->opcode==OP_InsertInt );
 81480  81676       x.nKey = pOp->p3;
 81481  81677     }
 81482  81678   
 81483  81679     if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 81484  81680       assert( pC->isTable );
 81485  81681       assert( pC->iDb>=0 );
 81486         -    zDb = db->aDb[pC->iDb].zName;
        81682  +    zDb = db->aDb[pC->iDb].zDbSName;
 81487  81683       pTab = pOp->p4.pTab;
 81488  81684       assert( HasRowid(pTab) );
 81489  81685       op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
 81490  81686     }else{
 81491  81687       pTab = 0; /* Not needed.  Silence a comiler warning. */
 81492  81688       zDb = 0;  /* Not needed.  Silence a compiler warning. */
 81493  81689     }
................................................................................
 81553  81749   **
 81554  81750   ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
 81555  81751   ** change count is incremented (otherwise not).
 81556  81752   **
 81557  81753   ** P1 must not be pseudo-table.  It has to be a real table with
 81558  81754   ** multiple rows.
 81559  81755   **
 81560         -** If P4 is not NULL then it points to a Table struture. In this case either 
        81756  +** If P4 is not NULL then it points to a Table object. In this case either 
 81561  81757   ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
 81562  81758   ** have been positioned using OP_NotFound prior to invoking this opcode in 
 81563  81759   ** this case. Specifically, if one is configured, the pre-update hook is 
 81564  81760   ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, 
 81565  81761   ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
 81566  81762   **
 81567  81763   ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
................................................................................
 81596  81792     ** the name of the db to pass as to it. Also set local pTab to a copy
 81597  81793     ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
 81598  81794     ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set 
 81599  81795     ** VdbeCursor.movetoTarget to the current rowid.  */
 81600  81796     if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 81601  81797       assert( pC->iDb>=0 );
 81602  81798       assert( pOp->p4.pTab!=0 );
 81603         -    zDb = db->aDb[pC->iDb].zName;
        81799  +    zDb = db->aDb[pC->iDb].zDbSName;
 81604  81800       pTab = pOp->p4.pTab;
 81605  81801       if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
 81606  81802         pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
 81607  81803       }
 81608  81804     }else{
 81609  81805       zDb = 0;   /* Not needed.  Silence a compiler warning. */
 81610  81806       pTab = 0;  /* Not needed.  Silence a compiler warning. */
................................................................................
 81668  81864   case OP_ResetCount: {
 81669  81865     sqlite3VdbeSetChanges(db, p->nChange);
 81670  81866     p->nChange = 0;
 81671  81867     break;
 81672  81868   }
 81673  81869   
 81674  81870   /* Opcode: SorterCompare P1 P2 P3 P4
 81675         -** Synopsis:  if key(P1)!=trim(r[P3],P4) goto P2
        81871  +** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
 81676  81872   **
 81677  81873   ** P1 is a sorter cursor. This instruction compares a prefix of the
 81678  81874   ** record blob in register P3 against a prefix of the entry that 
 81679  81875   ** the sorter cursor currently points to.  Only the first P4 fields
 81680  81876   ** of r[P3] and the sorter record are compared.
 81681  81877   **
 81682  81878   ** If either P3 or the sorter contains a NULL in one of their significant
................................................................................
 82144  82340     rc = ExpandBlob(pIn2);
 82145  82341     if( rc ) goto abort_due_to_error;
 82146  82342     if( pOp->opcode==OP_SorterInsert ){
 82147  82343       rc = sqlite3VdbeSorterWrite(pC, pIn2);
 82148  82344     }else{
 82149  82345       x.nKey = pIn2->n;
 82150  82346       x.pKey = pIn2->z;
 82151         -    x.nData = 0;
 82152         -    x.nZero = 0;
 82153         -    x.pData = 0;
 82154  82347       rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, pOp->p3, 
 82155  82348           ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
 82156  82349           );
 82157  82350       assert( pC->deferredMoveto==0 );
 82158  82351       pC->cacheStatus = CACHE_STALE;
 82159  82352     }
 82160  82353     if( rc) goto abort_due_to_error;
................................................................................
 82195  82388     }
 82196  82389     assert( pC->deferredMoveto==0 );
 82197  82390     pC->cacheStatus = CACHE_STALE;
 82198  82391     break;
 82199  82392   }
 82200  82393   
 82201  82394   /* Opcode: Seek P1 * P3 P4 *
 82202         -** Synopsis:  Move P3 to P1.rowid
        82395  +** Synopsis: Move P3 to P1.rowid
 82203  82396   **
 82204  82397   ** P1 is an open index cursor and P3 is a cursor on the corresponding
 82205  82398   ** table.  This opcode does a deferred seek of the P3 table cursor
 82206  82399   ** to the row that corresponds to the current row of P1.
 82207  82400   **
 82208  82401   ** This is a deferred seek.  Nothing actually happens until
 82209  82402   ** the cursor is used to read a record.  That way, if no reads
................................................................................
 82566  82759     /* Used to be a conditional */ {
 82567  82760       zMaster = SCHEMA_TABLE(iDb);
 82568  82761       initData.db = db;
 82569  82762       initData.iDb = pOp->p1;
 82570  82763       initData.pzErrMsg = &p->zErrMsg;
 82571  82764       zSql = sqlite3MPrintf(db,
 82572  82765          "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 82573         -       db->aDb[iDb].zName, zMaster, pOp->p4.z);
        82766  +       db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
 82574  82767       if( zSql==0 ){
 82575  82768         rc = SQLITE_NOMEM_BKPT;
 82576  82769       }else{
 82577  82770         assert( db->init.busy==0 );
 82578  82771         db->init.busy = 1;
 82579  82772         initData.rc = SQLITE_OK;
 82580  82773         assert( !db->mallocFailed );
................................................................................
 82702  82895     UPDATE_MAX_BLOBSIZE(pIn1);
 82703  82896     sqlite3VdbeChangeEncoding(pIn1, encoding);
 82704  82897     break;
 82705  82898   }
 82706  82899   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 82707  82900   
 82708  82901   /* Opcode: RowSetAdd P1 P2 * * *
 82709         -** Synopsis:  rowset(P1)=r[P2]
        82902  +** Synopsis: rowset(P1)=r[P2]
 82710  82903   **
 82711  82904   ** Insert the integer value held by register P2 into a boolean index
 82712  82905   ** held in register P1.
 82713  82906   **
 82714  82907   ** An assertion fails if P2 is not an integer.
 82715  82908   */
 82716  82909   case OP_RowSetAdd: {       /* in1, in2 */
................................................................................
 82722  82915       if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
 82723  82916     }
 82724  82917     sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
 82725  82918     break;
 82726  82919   }
 82727  82920   
 82728  82921   /* Opcode: RowSetRead P1 P2 P3 * *
 82729         -** Synopsis:  r[P3]=rowset(P1)
        82922  +** Synopsis: r[P3]=rowset(P1)
 82730  82923   **
 82731  82924   ** Extract the smallest value from boolean index P1 and put that value into
 82732  82925   ** register P3.  Or, if boolean index P1 is initially empty, leave P3
 82733  82926   ** unchanged and jump to instruction P2.
 82734  82927   */
 82735  82928   case OP_RowSetRead: {       /* jump, in1, out3 */
 82736  82929     i64 val;
................................................................................
 82871  83064       ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
 82872  83065       */
 82873  83066       nMem = pProgram->nMem + pProgram->nCsr;
 82874  83067       assert( nMem>0 );
 82875  83068       if( pProgram->nCsr==0 ) nMem++;
 82876  83069       nByte = ROUND8(sizeof(VdbeFrame))
 82877  83070                 + nMem * sizeof(Mem)
 82878         -              + pProgram->nCsr * sizeof(VdbeCursor *)
 82879         -              + pProgram->nOnce * sizeof(u8);
        83071  +              + pProgram->nCsr * sizeof(VdbeCursor *);
 82880  83072       pFrame = sqlite3DbMallocZero(db, nByte);
 82881  83073       if( !pFrame ){
 82882  83074         goto no_mem;
 82883  83075       }
 82884  83076       sqlite3VdbeMemRelease(pRt);
 82885  83077       pRt->flags = MEM_Frame;
 82886  83078       pRt->u.pFrame = pFrame;
................................................................................
 82892  83084       pFrame->aMem = p->aMem;
 82893  83085       pFrame->nMem = p->nMem;
 82894  83086       pFrame->apCsr = p->apCsr;
 82895  83087       pFrame->nCursor = p->nCursor;
 82896  83088       pFrame->aOp = p->aOp;
 82897  83089       pFrame->nOp = p->nOp;
 82898  83090       pFrame->token = pProgram->token;
 82899         -    pFrame->aOnceFlag = p->aOnceFlag;
 82900         -    pFrame->nOnceFlag = p->nOnceFlag;
 82901  83091   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 82902  83092       pFrame->anExec = p->anExec;
 82903  83093   #endif
 82904  83094   
 82905  83095       pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
 82906  83096       for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
 82907  83097         pMem->flags = MEM_Undefined;
................................................................................
 82927  83117     p->pFrame = pFrame;
 82928  83118     p->aMem = aMem = VdbeFrameMem(pFrame);
 82929  83119     p->nMem = pFrame->nChildMem;
 82930  83120     p->nCursor = (u16)pFrame->nChildCsr;
 82931  83121     p->apCsr = (VdbeCursor **)&aMem[p->nMem];
 82932  83122     p->aOp = aOp = pProgram->aOp;
 82933  83123     p->nOp = pProgram->nOp;
 82934         -  p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
 82935         -  p->nOnceFlag = pProgram->nOnce;
 82936  83124   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 82937  83125     p->anExec = 0;
 82938  83126   #endif
 82939  83127     pOp = &aOp[-1];
 82940         -  memset(p->aOnceFlag, 0, p->nOnceFlag);
 82941  83128   
 82942  83129     break;
 82943  83130   }
 82944  83131   
 82945  83132   /* Opcode: Param P1 P2 * * *
 82946  83133   **
 82947  83134   ** This opcode is only ever present in sub-programs called via the 
................................................................................
 83395  83582     sqlite3VdbeChangeEncoding(pOut, encoding);
 83396  83583     if( rc ) goto abort_due_to_error;
 83397  83584     break;
 83398  83585   };
 83399  83586   #endif /* SQLITE_OMIT_PRAGMA */
 83400  83587   
 83401  83588   #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
 83402         -/* Opcode: Vacuum * * * * *
        83589  +/* Opcode: Vacuum P1 * * * *
 83403  83590   **
 83404         -** Vacuum the entire database.  This opcode will cause other virtual
 83405         -** machines to be created and run.  It may not be called from within
 83406         -** a transaction.
        83591  +** Vacuum the entire database P1.  P1 is 0 for "main", and 2 or more
        83592  +** for an attached database.  The "temp" database may not be vacuumed.
 83407  83593   */
 83408  83594   case OP_Vacuum: {
 83409  83595     assert( p->readOnly==0 );
 83410         -  rc = sqlite3RunVacuum(&p->zErrMsg, db);
        83596  +  rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
 83411  83597     if( rc ) goto abort_due_to_error;
 83412  83598     break;
 83413  83599   }
 83414  83600   #endif
 83415  83601   
 83416  83602   #if !defined(SQLITE_OMIT_AUTOVACUUM)
 83417  83603   /* Opcode: IncrVacuum P1 P2 * * *
................................................................................
 83901  84087     }
 83902  84088     pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
 83903  84089     break;
 83904  84090   }
 83905  84091   #endif
 83906  84092   
 83907  84093   
 83908         -/* Opcode: Init * P2 * P4 *
 83909         -** Synopsis:  Start at P2
        84094  +/* Opcode: Init P1 P2 * P4 *
        84095  +** Synopsis: Start at P2
 83910  84096   **
 83911  84097   ** Programs contain a single instance of this opcode as the very first
 83912  84098   ** opcode.
 83913  84099   **
 83914  84100   ** If tracing is enabled (by the sqlite3_trace()) interface, then
 83915  84101   ** the UTF-8 string contained in P4 is emitted on the trace callback.
 83916  84102   ** Or if P4 is blank, use the string returned by sqlite3_sql().
 83917  84103   **
 83918  84104   ** If P2 is not zero, jump to instruction P2.
        84105  +**
        84106  +** Increment the value of P1 so that OP_Once opcodes will jump the
        84107  +** first time they are evaluated for this run.
 83919  84108   */
 83920  84109   case OP_Init: {          /* jump */
 83921  84110     char *zTrace;
        84111  +  int i;
 83922  84112   
 83923  84113     /* If the P4 argument is not NULL, then it must be an SQL comment string.
 83924  84114     ** The "--" string is broken up to prevent false-positives with srcck1.c.
 83925  84115     **
 83926  84116     ** This assert() provides evidence for:
 83927  84117     ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
 83928  84118     ** would have been returned by the legacy sqlite3_trace() interface by
 83929  84119     ** using the X argument when X begins with "--" and invoking
 83930  84120     ** sqlite3_expanded_sql(P) otherwise.
 83931  84121     */
 83932  84122     assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
        84123  +  assert( pOp==p->aOp );  /* Always instruction 0 */
 83933  84124   
 83934  84125   #ifndef SQLITE_OMIT_TRACE
 83935  84126     if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
 83936  84127      && !p->doingRerun
 83937  84128      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 83938  84129     ){
 83939  84130   #ifndef SQLITE_OMIT_DEPRECATED
................................................................................
 83947  84138       {
 83948  84139         (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
 83949  84140       }
 83950  84141     }
 83951  84142   #ifdef SQLITE_USE_FCNTL_TRACE
 83952  84143     zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
 83953  84144     if( zTrace ){
 83954         -    int i;
 83955         -    for(i=0; i<db->nDb; i++){
 83956         -      if( DbMaskTest(p->btreeMask, i)==0 ) continue;
 83957         -      sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
        84145  +    int j;
        84146  +    for(j=0; j<db->nDb; j++){
        84147  +      if( DbMaskTest(p->btreeMask, j)==0 ) continue;
        84148  +      sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
 83958  84149       }
 83959  84150     }
 83960  84151   #endif /* SQLITE_USE_FCNTL_TRACE */
 83961  84152   #ifdef SQLITE_DEBUG
 83962  84153     if( (db->flags & SQLITE_SqlTrace)!=0
 83963  84154      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 83964  84155     ){
 83965  84156       sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
 83966  84157     }
 83967  84158   #endif /* SQLITE_DEBUG */
 83968  84159   #endif /* SQLITE_OMIT_TRACE */
 83969         -  if( pOp->p2 ) goto jump_to_p2;
 83970         -  break;
        84160  +  assert( pOp->p2>0 );
        84161  +  if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
        84162  +    for(i=1; i<p->nOp; i++){
        84163  +      if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
        84164  +    }
        84165  +    pOp->p1 = 0;
        84166  +  }
        84167  +  pOp->p1++;
        84168  +  goto jump_to_p2;
 83971  84169   }
 83972  84170   
 83973  84171   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 83974  84172   /* Opcode: CursorHint P1 * * P4 *
 83975  84173   **
 83976  84174   ** Provide a hint to cursor P1 that it only needs to return rows that
 83977  84175   ** satisfy the Expr in P4.  TK_REGISTER terms in the P4 expression refer
................................................................................
 84286  84484           pParse->zErrMsg = 0;
 84287  84485         }
 84288  84486         rc = SQLITE_ERROR;
 84289  84487         sqlite3BtreeLeaveAll(db);
 84290  84488         goto blob_open_out;
 84291  84489       }
 84292  84490       pBlob->pTab = pTab;
 84293         -    pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zName;
        84491  +    pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
 84294  84492   
 84295  84493       /* Now search pTab for the exact column. */
 84296  84494       for(iCol=0; iCol<pTab->nCol; iCol++) {
 84297  84495         if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
 84298  84496           break;
 84299  84497         }
 84300  84498       }
................................................................................
 87838  88036   ** and WRC_Continue to continue.
 87839  88037   */
 87840  88038   static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
 87841  88039     int rc;
 87842  88040     testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
 87843  88041     testcase( ExprHasProperty(pExpr, EP_Reduced) );
 87844  88042     rc = pWalker->xExprCallback(pWalker, pExpr);
 87845         -  if( rc==WRC_Continue
 87846         -              && !ExprHasProperty(pExpr,EP_TokenOnly) ){
 87847         -    if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
 87848         -    if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
 87849         -    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 87850         -      if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
 87851         -    }else{
 87852         -      if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
 87853         -    }
        88043  +  if( rc || ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
        88044  +    return rc & WRC_Abort;
 87854  88045     }
 87855         -  return rc & WRC_Abort;
        88046  +  if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
        88047  +  if( pExpr->pRight && walkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
        88048  +  if( ExprHasProperty(pExpr, EP_xIsSelect) ){
        88049  +    if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
        88050  +  }else if( pExpr->x.pList ){
        88051  +    if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
        88052  +  }
        88053  +  return WRC_Continue;
 87856  88054   }
 87857  88055   SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
 87858  88056     return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
 87859  88057   }
 87860  88058   
 87861  88059   /*
 87862  88060   ** Call sqlite3WalkExpr() for every expression in list p or until
................................................................................
 88182  88380         /* Silently ignore database qualifiers inside CHECK constraints and
 88183  88381         ** partial indices.  Do not raise errors because that might break
 88184  88382         ** legacy and because it does not hurt anything to just ignore the
 88185  88383         ** database name. */
 88186  88384         zDb = 0;
 88187  88385       }else{
 88188  88386         for(i=0; i<db->nDb; i++){
 88189         -        assert( db->aDb[i].zName );
 88190         -        if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
        88387  +        assert( db->aDb[i].zDbSName );
        88388  +        if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
 88191  88389             pSchema = db->aDb[i].pSchema;
 88192  88390             break;
 88193  88391           }
 88194  88392         }
 88195  88393       }
 88196  88394     }
 88197  88395   
................................................................................
 88584  88782         const char *zColumn;
 88585  88783         const char *zTable;
 88586  88784         const char *zDb;
 88587  88785         Expr *pRight;
 88588  88786   
 88589  88787         /* if( pSrcList==0 ) break; */
 88590  88788         notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
 88591         -      /*notValid(pParse, pNC, "the \".\" operator", NC_PartIdx|NC_IsCheck, 1);*/
 88592  88789         pRight = pExpr->pRight;
 88593  88790         if( pRight->op==TK_ID ){
 88594  88791           zDb = 0;
 88595  88792           zTable = pExpr->pLeft->u.zToken;
 88596  88793           zColumn = pRight->u.zToken;
 88597  88794         }else{
 88598  88795           assert( pRight->op==TK_DOT );
................................................................................
 88607  88804       */
 88608  88805       case TK_FUNCTION: {
 88609  88806         ExprList *pList = pExpr->x.pList;    /* The argument list */
 88610  88807         int n = pList ? pList->nExpr : 0;    /* Number of arguments */
 88611  88808         int no_such_func = 0;       /* True if no such function exists */
 88612  88809         int wrong_num_args = 0;     /* True if wrong number of arguments */
 88613  88810         int is_agg = 0;             /* True if is an aggregate function */
 88614         -      int auth;                   /* Authorization to use the function */
 88615  88811         int nId;                    /* Number of characters in function name */
 88616  88812         const char *zId;            /* The function name. */
 88617  88813         FuncDef *pDef;              /* Information about the function */
 88618  88814         u8 enc = ENC(pParse->db);   /* The database encoding */
 88619  88815   
 88620  88816         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 88621         -      notValid(pParse, pNC, "functions", NC_PartIdx);
 88622  88817         zId = pExpr->u.zToken;
 88623  88818         nId = sqlite3Strlen30(zId);
 88624  88819         pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
 88625  88820         if( pDef==0 ){
 88626  88821           pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
 88627  88822           if( pDef==0 ){
 88628  88823             no_such_func = 1;
................................................................................
 88651  88846               ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
 88652  88847               ** to likelihood(X,0.9375). */
 88653  88848               /* TUNING: unlikely() probability is 0.0625.  likely() is 0.9375 */
 88654  88849               pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
 88655  88850             }             
 88656  88851           }
 88657  88852   #ifndef SQLITE_OMIT_AUTHORIZATION
 88658         -        auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
 88659         -        if( auth!=SQLITE_OK ){
 88660         -          if( auth==SQLITE_DENY ){
 88661         -            sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
 88662         -                                    pDef->zName);
 88663         -            pNC->nErr++;
        88853  +        {
        88854  +          int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
        88855  +          if( auth!=SQLITE_OK ){
        88856  +            if( auth==SQLITE_DENY ){
        88857  +              sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
        88858  +                                      pDef->zName);
        88859  +              pNC->nErr++;
        88860  +            }
        88861  +            pExpr->op = TK_NULL;
        88862  +            return WRC_Prune;
 88664  88863             }
 88665         -          pExpr->op = TK_NULL;
 88666         -          return WRC_Prune;
 88667  88864           }
 88668  88865   #endif
 88669  88866           if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
 88670  88867             /* For the purposes of the EP_ConstFunc flag, date and time
 88671  88868             ** functions and other functions that change slowly are considered
 88672  88869             ** constant because they are constant for the duration of one query */
 88673  88870             ExprSetProperty(pExpr,EP_ConstFunc);
 88674  88871           }
 88675  88872           if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
 88676  88873             /* Date/time functions that use 'now', and other functions like
 88677  88874             ** sqlite_version() that might change over time cannot be used
 88678  88875             ** in an index. */
 88679         -          notValid(pParse, pNC, "non-deterministic functions", NC_IdxExpr);
        88876  +          notValid(pParse, pNC, "non-deterministic functions",
        88877  +                   NC_IdxExpr|NC_PartIdx);
 88680  88878           }
 88681  88879         }
 88682  88880         if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
 88683  88881           sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
 88684  88882           pNC->nErr++;
 88685  88883           is_agg = 0;
 88686  88884         }else if( no_such_func && pParse->db->init.busy==0
................................................................................
 88736  88934           }
 88737  88935         }
 88738  88936         break;
 88739  88937       }
 88740  88938       case TK_VARIABLE: {
 88741  88939         notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
 88742  88940         break;
        88941  +    }
        88942  +    case TK_EQ:
        88943  +    case TK_NE:
        88944  +    case TK_LT:
        88945  +    case TK_LE:
        88946  +    case TK_GT:
        88947  +    case TK_GE:
        88948  +    case TK_IS:
        88949  +    case TK_ISNOT: {
        88950  +      int nLeft, nRight;
        88951  +      if( pParse->db->mallocFailed ) break;
        88952  +      assert( pExpr->pRight!=0 );
        88953  +      assert( pExpr->pLeft!=0 );
        88954  +      nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
        88955  +      nRight = sqlite3ExprVectorSize(pExpr->pRight);
        88956  +      if( nLeft!=nRight ){
        88957  +        testcase( pExpr->op==TK_EQ );
        88958  +        testcase( pExpr->op==TK_NE );
        88959  +        testcase( pExpr->op==TK_LT );
        88960  +        testcase( pExpr->op==TK_LE );
        88961  +        testcase( pExpr->op==TK_GT );
        88962  +        testcase( pExpr->op==TK_GE );
        88963  +        testcase( pExpr->op==TK_IS );
        88964  +        testcase( pExpr->op==TK_ISNOT );
        88965  +        sqlite3ErrorMsg(pParse, "row value misused");
        88966  +      }
        88967  +      break; 
 88743  88968       }
 88744  88969     }
 88745  88970     return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
 88746  88971   }
 88747  88972   
 88748  88973   /*
 88749  88974   ** pEList is a list of expressions which are really the result set of the
................................................................................
 89478  89703   **    May you share freely, never taking more than you give.
 89479  89704   **
 89480  89705   *************************************************************************
 89481  89706   ** This file contains routines used for analyzing expressions and
 89482  89707   ** for generating VDBE code that evaluates expressions in SQLite.
 89483  89708   */
 89484  89709   /* #include "sqliteInt.h" */
        89710  +
        89711  +/* Forward declarations */
        89712  +static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
        89713  +static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
        89714  +
        89715  +/*
        89716  +** Return the affinity character for a single column of a table.
        89717  +*/
        89718  +SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table *pTab, int iCol){
        89719  +  assert( iCol<pTab->nCol );
        89720  +  return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
        89721  +}
 89485  89722   
 89486  89723   /*
 89487  89724   ** Return the 'affinity' of the expression pExpr if any.
 89488  89725   **
 89489  89726   ** If pExpr is a column, a reference to a column via an 'AS' alias,
 89490  89727   ** or a sub-select with a column as the return value, then the 
 89491  89728   ** affinity of that column is returned. Otherwise, 0x00 is returned,
................................................................................
 89504  89741     pExpr = sqlite3ExprSkipCollate(pExpr);
 89505  89742     if( pExpr->flags & EP_Generic ) return 0;
 89506  89743     op = pExpr->op;
 89507  89744     if( op==TK_SELECT ){
 89508  89745       assert( pExpr->flags&EP_xIsSelect );
 89509  89746       return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
 89510  89747     }
        89748  +  if( op==TK_REGISTER ) op = pExpr->op2;
 89511  89749   #ifndef SQLITE_OMIT_CAST
 89512  89750     if( op==TK_CAST ){
 89513  89751       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 89514  89752       return sqlite3AffinityType(pExpr->u.zToken, 0);
 89515  89753     }
 89516  89754   #endif
 89517         -  if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
 89518         -   && pExpr->pTab!=0
 89519         -  ){
 89520         -    /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
 89521         -    ** a TK_COLUMN but was previously evaluated and cached in a register */
 89522         -    int j = pExpr->iColumn;
 89523         -    if( j<0 ) return SQLITE_AFF_INTEGER;
 89524         -    assert( pExpr->pTab && j<pExpr->pTab->nCol );
 89525         -    return pExpr->pTab->aCol[j].affinity;
        89755  +  if( op==TK_AGG_COLUMN || op==TK_COLUMN ){
        89756  +    return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
        89757  +  }
        89758  +  if( op==TK_SELECT_COLUMN ){
        89759  +    assert( pExpr->pLeft->flags&EP_xIsSelect );
        89760  +    return sqlite3ExprAffinity(
        89761  +        pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
        89762  +    );
 89526  89763     }
 89527  89764     return pExpr->affinity;
 89528  89765   }
 89529  89766   
 89530  89767   /*
 89531  89768   ** Set the collating sequence for expression pExpr to be the collating
 89532  89769   ** sequence named by pToken.   Return a pointer to a new Expr node that
................................................................................
 89684  89921             pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
 89685  89922     assert( pExpr->pLeft );
 89686  89923     aff = sqlite3ExprAffinity(pExpr->pLeft);
 89687  89924     if( pExpr->pRight ){
 89688  89925       aff = sqlite3CompareAffinity(pExpr->pRight, aff);
 89689  89926     }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 89690  89927       aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
 89691         -  }else if( !aff ){
        89928  +  }else if( NEVER(aff==0) ){
 89692  89929       aff = SQLITE_AFF_BLOB;
 89693  89930     }
 89694  89931     return aff;
 89695  89932   }
 89696  89933   
 89697  89934   /*
 89698  89935   ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
................................................................................
 89773  90010     p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
 89774  90011     p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
 89775  90012     addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
 89776  90013                              (void*)p4, P4_COLLSEQ);
 89777  90014     sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
 89778  90015     return addr;
 89779  90016   }
        90017  +
        90018  +/*
        90019  +** Return true if expression pExpr is a vector, or false otherwise.
        90020  +**
        90021  +** A vector is defined as any expression that results in two or more
        90022  +** columns of result.  Every TK_VECTOR node is an vector because the
        90023  +** parser will not generate a TK_VECTOR with fewer than two entries.
        90024  +** But a TK_SELECT might be either a vector or a scalar. It is only
        90025  +** considered a vector if it has two or more result columns.
        90026  +*/
        90027  +SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr){
        90028  +  return sqlite3ExprVectorSize(pExpr)>1;
        90029  +}
        90030  +
        90031  +/*
        90032  +** If the expression passed as the only argument is of type TK_VECTOR 
        90033  +** return the number of expressions in the vector. Or, if the expression
        90034  +** is a sub-select, return the number of columns in the sub-select. For
        90035  +** any other type of expression, return 1.
        90036  +*/
        90037  +SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr){
        90038  +  u8 op = pExpr->op;
        90039  +  if( op==TK_REGISTER ) op = pExpr->op2;
        90040  +  if( op==TK_VECTOR ){
        90041  +    return pExpr->x.pList->nExpr;
        90042  +  }else if( op==TK_SELECT ){
        90043  +    return pExpr->x.pSelect->pEList->nExpr;
        90044  +  }else{
        90045  +    return 1;
        90046  +  }
        90047  +}
        90048  +
        90049  +#ifndef SQLITE_OMIT_SUBQUERY
        90050  +/*
        90051  +** Return a pointer to a subexpression of pVector that is the i-th
        90052  +** column of the vector (numbered starting with 0).  The caller must
        90053  +** ensure that i is within range.
        90054  +**
        90055  +** If pVector is really a scalar (and "scalar" here includes subqueries
        90056  +** that return a single column!) then return pVector unmodified.
        90057  +**
        90058  +** pVector retains ownership of the returned subexpression.
        90059  +**
        90060  +** If the vector is a (SELECT ...) then the expression returned is
        90061  +** just the expression for the i-th term of the result set, and may
        90062  +** not be ready for evaluation because the table cursor has not yet
        90063  +** been positioned.
        90064  +*/
        90065  +SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
        90066  +  assert( i<sqlite3ExprVectorSize(pVector) );
        90067  +  if( sqlite3ExprIsVector(pVector) ){
        90068  +    assert( pVector->op2==0 || pVector->op==TK_REGISTER );
        90069  +    if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
        90070  +      return pVector->x.pSelect->pEList->a[i].pExpr;
        90071  +    }else{
        90072  +      return pVector->x.pList->a[i].pExpr;
        90073  +    }
        90074  +  }
        90075  +  return pVector;
        90076  +}
        90077  +#endif /* !defined(SQLITE_OMIT_SUBQUERY) */
        90078  +
        90079  +#ifndef SQLITE_OMIT_SUBQUERY
        90080  +/*
        90081  +** Compute and return a new Expr object which when passed to
        90082  +** sqlite3ExprCode() will generate all necessary code to compute
        90083  +** the iField-th column of the vector expression pVector.
        90084  +**
        90085  +** It is ok for pVector to be a scalar (as long as iField==0).  
        90086  +** In that case, this routine works like sqlite3ExprDup().
        90087  +**
        90088  +** The caller owns the returned Expr object and is responsible for
        90089  +** ensuring that the returned value eventually gets freed.
        90090  +**
        90091  +** The caller retains ownership of pVector.  If pVector is a TK_SELECT,
        90092  +** then the returned object will reference pVector and so pVector must remain
        90093  +** valid for the life of the returned object.  If pVector is a TK_VECTOR
        90094  +** or a scalar expression, then it can be deleted as soon as this routine
        90095  +** returns.
        90096  +**
        90097  +** A trick to cause a TK_SELECT pVector to be deleted together with
        90098  +** the returned Expr object is to attach the pVector to the pRight field
        90099  +** of the returned TK_SELECT_COLUMN Expr object.
        90100  +*/
        90101  +SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
        90102  +  Parse *pParse,       /* Parsing context */
        90103  +  Expr *pVector,       /* The vector.  List of expressions or a sub-SELECT */
        90104  +  int iField           /* Which column of the vector to return */
        90105  +){
        90106  +  Expr *pRet;
        90107  +  if( pVector->op==TK_SELECT ){
        90108  +    assert( pVector->flags & EP_xIsSelect );
        90109  +    /* The TK_SELECT_COLUMN Expr node:
        90110  +    **
        90111  +    ** pLeft:           pVector containing TK_SELECT
        90112  +    ** pRight:          not used.  But recursively deleted.
        90113  +    ** iColumn:         Index of a column in pVector
        90114  +    ** pLeft->iTable:   First in an array of register holding result, or 0
        90115  +    **                  if the result is not yet computed.
        90116  +    **
        90117  +    ** sqlite3ExprDelete() specifically skips the recursive delete of
        90118  +    ** pLeft on TK_SELECT_COLUMN nodes.  But pRight is followed, so pVector
        90119  +    ** can be attached to pRight to cause this node to take ownership of
        90120  +    ** pVector.  Typically there will be multiple TK_SELECT_COLUMN nodes
        90121  +    ** with the same pLeft pointer to the pVector, but only one of them
        90122  +    ** will own the pVector.
        90123  +    */
        90124  +    pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0, 0);
        90125  +    if( pRet ){
        90126  +      pRet->iColumn = iField;
        90127  +      pRet->pLeft = pVector;
        90128  +    }
        90129  +    assert( pRet==0 || pRet->iTable==0 );
        90130  +  }else{
        90131  +    if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
        90132  +    pRet = sqlite3ExprDup(pParse->db, pVector, 0);
        90133  +  }
        90134  +  return pRet;
        90135  +}
        90136  +#endif /* !define(SQLITE_OMIT_SUBQUERY) */
        90137  +
        90138  +/*
        90139  +** If expression pExpr is of type TK_SELECT, generate code to evaluate
        90140  +** it. Return the register in which the result is stored (or, if the 
        90141  +** sub-select returns more than one column, the first in an array
        90142  +** of registers in which the result is stored).
        90143  +**
        90144  +** If pExpr is not a TK_SELECT expression, return 0.
        90145  +*/
        90146  +static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
        90147  +  int reg = 0;
        90148  +#ifndef SQLITE_OMIT_SUBQUERY
        90149  +  if( pExpr->op==TK_SELECT ){
        90150  +    reg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
        90151  +  }
        90152  +#endif
        90153  +  return reg;
        90154  +}
        90155  +
        90156  +/*
        90157  +** Argument pVector points to a vector expression - either a TK_VECTOR
        90158  +** or TK_SELECT that returns more than one column. This function returns
        90159  +** the register number of a register that contains the value of
        90160  +** element iField of the vector.
        90161  +**
        90162  +** If pVector is a TK_SELECT expression, then code for it must have 
        90163  +** already been generated using the exprCodeSubselect() routine. In this
        90164  +** case parameter regSelect should be the first in an array of registers
        90165  +** containing the results of the sub-select. 
        90166  +**
        90167  +** If pVector is of type TK_VECTOR, then code for the requested field
        90168  +** is generated. In this case (*pRegFree) may be set to the number of
        90169  +** a temporary register to be freed by the caller before returning.
        90170  +**
        90171  +** Before returning, output parameter (*ppExpr) is set to point to the
        90172  +** Expr object corresponding to element iElem of the vector.
        90173  +*/
        90174  +static int exprVectorRegister(
        90175  +  Parse *pParse,                  /* Parse context */
        90176  +  Expr *pVector,                  /* Vector to extract element from */
        90177  +  int iField,                     /* Field to extract from pVector */
        90178  +  int regSelect,                  /* First in array of registers */
        90179  +  Expr **ppExpr,                  /* OUT: Expression element */
        90180  +  int *pRegFree                   /* OUT: Temp register to free */
        90181  +){
        90182  +  u8 op = pVector->op;
        90183  +  assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT );
        90184  +  if( op==TK_REGISTER ){
        90185  +    *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
        90186  +    return pVector->iTable+iField;
        90187  +  }
        90188  +  if( op==TK_SELECT ){
        90189  +    *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
        90190  +     return regSelect+iField;
        90191  +  }
        90192  +  *ppExpr = pVector->x.pList->a[iField].pExpr;
        90193  +  return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
        90194  +}
        90195  +
        90196  +/*
        90197  +** Expression pExpr is a comparison between two vector values. Compute
        90198  +** the result of the comparison (1, 0, or NULL) and write that
        90199  +** result into register dest.
        90200  +**
        90201  +** The caller must satisfy the following preconditions:
        90202  +**
        90203  +**    if pExpr->op==TK_IS:      op==TK_EQ and p5==SQLITE_NULLEQ
        90204  +**    if pExpr->op==TK_ISNOT:   op==TK_NE and p5==SQLITE_NULLEQ
        90205  +**    otherwise:                op==pExpr->op and p5==0
        90206  +*/
        90207  +static void codeVectorCompare(
        90208  +  Parse *pParse,        /* Code generator context */
        90209  +  Expr *pExpr,          /* The comparison operation */
        90210  +  int dest,             /* Write results into this register */
        90211  +  u8 op,                /* Comparison operator */
        90212  +  u8 p5                 /* SQLITE_NULLEQ or zero */
        90213  +){
        90214  +  Vdbe *v = pParse->pVdbe;
        90215  +  Expr *pLeft = pExpr->pLeft;
        90216  +  Expr *pRight = pExpr->pRight;
        90217  +  int nLeft = sqlite3ExprVectorSize(pLeft);
        90218  +  int i;
        90219  +  int regLeft = 0;
        90220  +  int regRight = 0;
        90221  +  u8 opx = op;
        90222  +  int addrDone = sqlite3VdbeMakeLabel(v);
        90223  +
        90224  +  assert( nLeft==sqlite3ExprVectorSize(pRight) );
        90225  +  assert( pExpr->op==TK_EQ || pExpr->op==TK_NE 
        90226  +       || pExpr->op==TK_IS || pExpr->op==TK_ISNOT 
        90227  +       || pExpr->op==TK_LT || pExpr->op==TK_GT 
        90228  +       || pExpr->op==TK_LE || pExpr->op==TK_GE 
        90229  +  );
        90230  +  assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
        90231  +            || (pExpr->op==TK_ISNOT && op==TK_NE) );
        90232  +  assert( p5==0 || pExpr->op!=op );
        90233  +  assert( p5==SQLITE_NULLEQ || pExpr->op==op );
        90234  +
        90235  +  p5 |= SQLITE_STOREP2;
        90236  +  if( opx==TK_LE ) opx = TK_LT;
        90237  +  if( opx==TK_GE ) opx = TK_GT;
        90238  +
        90239  +  regLeft = exprCodeSubselect(pParse, pLeft);
        90240  +  regRight = exprCodeSubselect(pParse, pRight);
        90241  +
        90242  +  for(i=0; 1 /*Loop exits by "break"*/; i++){
        90243  +    int regFree1 = 0, regFree2 = 0;
        90244  +    Expr *pL, *pR; 
        90245  +    int r1, r2;
        90246  +    assert( i>=0 && i<nLeft );
        90247  +    if( i>0 ) sqlite3ExprCachePush(pParse);
        90248  +    r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
        90249  +    r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
        90250  +    codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5);
        90251  +    testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
        90252  +    testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
        90253  +    testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
        90254  +    testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
        90255  +    testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
        90256  +    testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
        90257  +    sqlite3ReleaseTempReg(pParse, regFree1);
        90258  +    sqlite3ReleaseTempReg(pParse, regFree2);
        90259  +    if( i>0 ) sqlite3ExprCachePop(pParse);
        90260  +    if( i==nLeft-1 ){
        90261  +      break;
        90262  +    }
        90263  +    if( opx==TK_EQ ){
        90264  +      sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
        90265  +      p5 |= SQLITE_KEEPNULL;
        90266  +    }else if( opx==TK_NE ){
        90267  +      sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v);
        90268  +      p5 |= SQLITE_KEEPNULL;
        90269  +    }else{
        90270  +      assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
        90271  +      sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone);
        90272  +      VdbeCoverageIf(v, op==TK_LT);
        90273  +      VdbeCoverageIf(v, op==TK_GT);
        90274  +      VdbeCoverageIf(v, op==TK_LE);
        90275  +      VdbeCoverageIf(v, op==TK_GE);
        90276  +      if( i==nLeft-2 ) opx = op;
        90277  +    }
        90278  +  }
        90279  +  sqlite3VdbeResolveLabel(v, addrDone);
        90280  +}
 89780  90281   
 89781  90282   #if SQLITE_MAX_EXPR_DEPTH>0
 89782  90283   /*
 89783  90284   ** Check that argument nHeight is less than or equal to the maximum
 89784  90285   ** expression depth allowed. If it is not, leave an error message in
 89785  90286   ** pParse.
 89786  90287   */
................................................................................
 89909  90410   ** Special case:  If op==TK_INTEGER and pToken points to a string that
 89910  90411   ** can be translated into a 32-bit integer, then the token is not
 89911  90412   ** stored in u.zToken.  Instead, the integer values is written
 89912  90413   ** into u.iValue and the EP_IntValue flag is set.  No extra storage
 89913  90414   ** is allocated to hold the integer text and the dequote flag is ignored.
 89914  90415   */
 89915  90416   SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
 89916         -  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
        90417  +  sqlite3 *db,            /* Handle for sqlite3DbMallocRawNN() */
 89917  90418     int op,                 /* Expression opcode */
 89918  90419     const Token *pToken,    /* Token argument.  Might be NULL */
 89919  90420     int dequote             /* True to dequote */
 89920  90421   ){
 89921  90422     Expr *pNew;
 89922  90423     int nExtra = 0;
 89923  90424     int iValue = 0;
................................................................................
 90127  90628   ** the SQL statement comes from an external source.
 90128  90629   **
 90129  90630   ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
 90130  90631   ** as the previous instance of the same wildcard.  Or if this is the first
 90131  90632   ** instance of the wildcard, the next sequential variable number is
 90132  90633   ** assigned.
 90133  90634   */
 90134         -SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
        90635  +SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
 90135  90636     sqlite3 *db = pParse->db;
 90136  90637     const char *z;
 90137  90638   
 90138  90639     if( pExpr==0 ) return;
 90139  90640     assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
 90140  90641     z = pExpr->u.zToken;
 90141  90642     assert( z!=0 );
 90142  90643     assert( z[0]!=0 );
        90644  +  assert( n==sqlite3Strlen30(z) );
 90143  90645     if( z[1]==0 ){
 90144  90646       /* Wildcard of the form "?".  Assign the next variable number */
 90145  90647       assert( z[0]=='?' );
 90146  90648       pExpr->iColumn = (ynVar)(++pParse->nVar);
 90147  90649     }else{
 90148         -    ynVar x = 0;
 90149         -    u32 n = sqlite3Strlen30(z);
        90650  +    ynVar x;
 90150  90651       if( z[0]=='?' ){
 90151  90652         /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
 90152  90653         ** use it as the variable number */
 90153  90654         i64 i;
 90154  90655         int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
 90155         -      pExpr->iColumn = x = (ynVar)i;
        90656  +      x = (ynVar)i;
 90156  90657         testcase( i==0 );
 90157  90658         testcase( i==1 );
 90158  90659         testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
 90159  90660         testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
 90160  90661         if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 90161  90662           sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
 90162  90663               db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
 90163         -        x = 0;
        90664  +        return;
 90164  90665         }
 90165  90666         if( i>pParse->nVar ){
 90166  90667           pParse->nVar = (int)i;
 90167  90668         }
 90168  90669       }else{
 90169  90670         /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
 90170  90671         ** number as the prior appearance of the same name, or if the name
 90171  90672         ** has never appeared before, reuse the same variable number
 90172  90673         */
 90173  90674         ynVar i;
 90174         -      for(i=0; i<pParse->nzVar; i++){
        90675  +      for(i=x=0; i<pParse->nzVar; i++){
 90175  90676           if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
 90176         -          pExpr->iColumn = x = (ynVar)i+1;
 90177         -          break;
 90178         -        }
 90179         -      }
 90180         -      if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
 90181         -    }
 90182         -    if( x>0 ){
 90183         -      if( x>pParse->nzVar ){
 90184         -        char **a;
 90185         -        a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
 90186         -        if( a==0 ){
 90187         -          assert( db->mallocFailed ); /* Error reported through mallocFailed */
 90188         -          return;
 90189         -        }
 90190         -        pParse->azVar = a;
 90191         -        memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
 90192         -        pParse->nzVar = x;
 90193         -      }
 90194         -      if( z[0]!='?' || pParse->azVar[x-1]==0 ){
 90195         -        sqlite3DbFree(db, pParse->azVar[x-1]);
 90196         -        pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
 90197         -      }
        90677  +          x = (ynVar)i+1;
        90678  +          break;
        90679  +        }
        90680  +      }
        90681  +      if( x==0 ) x = (ynVar)(++pParse->nVar);
        90682  +    }
        90683  +    pExpr->iColumn = x;
        90684  +    if( x>pParse->nzVar ){
        90685  +      char **a;
        90686  +      a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
        90687  +      if( a==0 ){
        90688  +        assert( db->mallocFailed ); /* Error reported through mallocFailed */
        90689  +        return;
        90690  +      }
        90691  +      pParse->azVar = a;
        90692  +      memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
        90693  +      pParse->nzVar = x;
        90694  +    }
        90695  +    if( pParse->azVar[x-1]==0 ){
        90696  +      pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
 90198  90697       }
 90199  90698     } 
 90200         -  if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
        90699  +  if( pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 90201  90700       sqlite3ErrorMsg(pParse, "too many SQL variables");
 90202  90701     }
 90203  90702   }
 90204  90703   
 90205  90704   /*
 90206  90705   ** Recursively delete an expression tree.
 90207  90706   */
 90208  90707   static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
 90209  90708     assert( p!=0 );
 90210  90709     /* Sanity check: Assert that the IntValue is non-negative if it exists */
 90211  90710     assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
 90212         -  if( !ExprHasProperty(p, EP_TokenOnly) ){
        90711  +#ifdef SQLITE_DEBUG
        90712  +  if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
        90713  +    assert( p->pLeft==0 );
        90714  +    assert( p->pRight==0 );
        90715  +    assert( p->x.pSelect==0 );
        90716  +  }
        90717  +#endif
        90718  +  if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
 90213  90719       /* The Expr.x union is never used at the same time as Expr.pRight */
 90214  90720       assert( p->x.pList==0 || p->pRight==0 );
 90215         -    sqlite3ExprDelete(db, p->pLeft);
        90721  +    if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
 90216  90722       sqlite3ExprDelete(db, p->pRight);
 90217         -    if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
 90218  90723       if( ExprHasProperty(p, EP_xIsSelect) ){
 90219  90724         sqlite3SelectDelete(db, p->x.pSelect);
 90220  90725       }else{
 90221  90726         sqlite3ExprListDelete(db, p->x.pList);
 90222  90727       }
 90223  90728     }
        90729  +  if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
 90224  90730     if( !ExprHasProperty(p, EP_Static) ){
 90225  90731       sqlite3DbFree(db, p);
 90226  90732     }
 90227  90733   }
 90228  90734   SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
 90229  90735     if( p ) sqlite3ExprDeleteNN(db, p);
 90230  90736   }
................................................................................
 90393  90899   
 90394  90900       /* Copy the p->u.zToken string, if any. */
 90395  90901       if( nToken ){
 90396  90902         char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
 90397  90903         memcpy(zToken, p->u.zToken, nToken);
 90398  90904       }
 90399  90905   
 90400         -    if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
        90906  +    if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
 90401  90907         /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
 90402  90908         if( ExprHasProperty(p, EP_xIsSelect) ){
 90403  90909           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
 90404  90910         }else{
 90405  90911           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
 90406  90912         }
 90407  90913       }
 90408  90914   
 90409  90915       /* Fill in pNew->pLeft and pNew->pRight. */
 90410  90916       if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
 90411  90917         zAlloc += dupedExprNodeSize(p, dupFlags);
 90412         -      if( ExprHasProperty(pNew, EP_Reduced) ){
        90918  +      if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
 90413  90919           pNew->pLeft = p->pLeft ?
 90414  90920                         exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
 90415  90921           pNew->pRight = p->pRight ?
 90416  90922                          exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
 90417  90923         }
 90418  90924         if( pzBuffer ){
 90419  90925           *pzBuffer = zAlloc;
 90420  90926         }
 90421  90927       }else{
 90422         -      if( !ExprHasProperty(p, EP_TokenOnly) ){
 90423         -        pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
        90928  +      if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
        90929  +        if( pNew->op==TK_SELECT_COLUMN ){
        90930  +          pNew->pLeft = p->pLeft;
        90931  +        }else{
        90932  +          pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
        90933  +        }
 90424  90934           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
 90425  90935         }
 90426  90936       }
 90427  90937     }
 90428  90938     return pNew;
 90429  90939   }
 90430  90940   
................................................................................
 90656  91166   
 90657  91167   no_mem:     
 90658  91168     /* Avoid leaking memory if malloc has failed. */
 90659  91169     sqlite3ExprDelete(db, pExpr);
 90660  91170     sqlite3ExprListDelete(db, pList);
 90661  91171     return 0;
 90662  91172   }
        91173  +
        91174  +/*
        91175  +** pColumns and pExpr form a vector assignment which is part of the SET
        91176  +** clause of an UPDATE statement.  Like this:
        91177  +**
        91178  +**        (a,b,c) = (expr1,expr2,expr3)
        91179  +** Or:    (a,b,c) = (SELECT x,y,z FROM ....)
        91180  +**
        91181  +** For each term of the vector assignment, append new entries to the
        91182  +** expression list pList.  In the case of a subquery on the LHS, append
        91183  +** TK_SELECT_COLUMN expressions.
        91184  +*/
        91185  +SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
        91186  +  Parse *pParse,         /* Parsing context */
        91187  +  ExprList *pList,       /* List to which to append. Might be NULL */
        91188  +  IdList *pColumns,      /* List of names of LHS of the assignment */
        91189  +  Expr *pExpr            /* Vector expression to be appended. Might be NULL */
        91190  +){
        91191  +  sqlite3 *db = pParse->db;
        91192  +  int n;
        91193  +  int i;
        91194  +  int iFirst = pList ? pList->nExpr : 0;
        91195  +  /* pColumns can only be NULL due to an OOM but an OOM will cause an
        91196  +  ** exit prior to this routine being invoked */
        91197  +  if( NEVER(pColumns==0) ) goto vector_append_error;
        91198  +  if( pExpr==0 ) goto vector_append_error;
        91199  +  n = sqlite3ExprVectorSize(pExpr);
        91200  +  if( pColumns->nId!=n ){
        91201  +    sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
        91202  +                    pColumns->nId, n);
        91203  +    goto vector_append_error;
        91204  +  }
        91205  +  for(i=0; i<n; i++){
        91206  +    Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
        91207  +    pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
        91208  +    if( pList ){
        91209  +      assert( pList->nExpr==iFirst+i+1 );
        91210  +      pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
        91211  +      pColumns->a[i].zName = 0;
        91212  +    }
        91213  +  }
        91214  +  if( pExpr->op==TK_SELECT ){
        91215  +    if( pList && pList->a[iFirst].pExpr ){
        91216  +      assert( pList->a[iFirst].pExpr->op==TK_SELECT_COLUMN );
        91217  +      pList->a[iFirst].pExpr->pRight = pExpr;
        91218  +      pExpr = 0;
        91219  +    }
        91220  +  }
        91221  +
        91222  +vector_append_error:
        91223  +  sqlite3ExprDelete(db, pExpr);
        91224  +  sqlite3IdListDelete(db, pColumns);
        91225  +  return pList;
        91226  +}
 90663  91227   
 90664  91228   /*
 90665  91229   ** Set the sort order for the last element on the given ExprList.
 90666  91230   */
 90667  91231   SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
 90668  91232     if( p==0 ) return;
 90669  91233     assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
................................................................................
 91064  91628   ** table, then return NULL.
 91065  91629   */
 91066  91630   #ifndef SQLITE_OMIT_SUBQUERY
 91067  91631   static Select *isCandidateForInOpt(Expr *pX){
 91068  91632     Select *p;
 91069  91633     SrcList *pSrc;
 91070  91634     ExprList *pEList;
 91071         -  Expr *pRes;
 91072  91635     Table *pTab;
        91636  +  int i;
 91073  91637     if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0;  /* Not a subquery */
 91074  91638     if( ExprHasProperty(pX, EP_VarSelect)  ) return 0;  /* Correlated subq */
 91075  91639     p = pX->x.pSelect;
 91076  91640     if( p->pPrior ) return 0;              /* Not a compound SELECT */
 91077  91641     if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 91078  91642       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 91079  91643       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
................................................................................
 91088  91652     if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
 91089  91653     if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
 91090  91654     pTab = pSrc->a[0].pTab;
 91091  91655     assert( pTab!=0 );
 91092  91656     assert( pTab->pSelect==0 );            /* FROM clause is not a view */
 91093  91657     if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
 91094  91658     pEList = p->pEList;
 91095         -  if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
 91096         -  pRes = pEList->a[0].pExpr;
 91097         -  if( pRes->op!=TK_COLUMN ) return 0;    /* Result is a column */
 91098         -  assert( pRes->iTable==pSrc->a[0].iCursor );  /* Not a correlated subquery */
        91659  +  assert( pEList!=0 );
        91660  +  /* All SELECT results must be columns. */
        91661  +  for(i=0; i<pEList->nExpr; i++){
        91662  +    Expr *pRes = pEList->a[i].pExpr;
        91663  +    if( pRes->op!=TK_COLUMN ) return 0;
        91664  +    assert( pRes->iTable==pSrc->a[0].iCursor );  /* Not a correlated subquery */
        91665  +  }
 91099  91666     return p;
 91100  91667   }
 91101  91668   #endif /* SQLITE_OMIT_SUBQUERY */
 91102  91669   
 91103         -/*
 91104         -** Code an OP_Once instruction and allocate space for its flag. Return the 
 91105         -** address of the new instruction.
 91106         -*/
 91107         -SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
 91108         -  Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
 91109         -  return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
 91110         -}
 91111         -
        91670  +#ifndef SQLITE_OMIT_SUBQUERY
 91112  91671   /*
 91113  91672   ** Generate code that checks the left-most column of index table iCur to see if
 91114  91673   ** it contains any NULL entries.  Cause the register at regHasNull to be set
 91115  91674   ** to a non-NULL value if iCur contains no NULLs.  Cause register regHasNull
 91116  91675   ** to be set to NULL if iCur contains one or more NULL values.
 91117  91676   */
 91118  91677   static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
................................................................................
 91120  91679     sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
 91121  91680     addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
 91122  91681     sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
 91123  91682     sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
 91124  91683     VdbeComment((v, "first_entry_in(%d)", iCur));
 91125  91684     sqlite3VdbeJumpHere(v, addr1);
 91126  91685   }
        91686  +#endif
 91127  91687   
 91128  91688   
 91129  91689   #ifndef SQLITE_OMIT_SUBQUERY
 91130  91690   /*
 91131  91691   ** The argument is an IN operator with a list (not a subquery) on the 
 91132  91692   ** right-hand side.  Return TRUE if that list is constant.
 91133  91693   */
................................................................................
 91164  91724   **                         populated epheremal table.
 91165  91725   **   IN_INDEX_NOOP       - No cursor was allocated.  The IN operator must be
 91166  91726   **                         implemented as a sequence of comparisons.
 91167  91727   **
 91168  91728   ** An existing b-tree might be used if the RHS expression pX is a simple
 91169  91729   ** subquery such as:
 91170  91730   **
 91171         -**     SELECT <column> FROM <table>
        91731  +**     SELECT <column1>, <column2>... FROM <table>
 91172  91732   **
 91173  91733   ** If the RHS of the IN operator is a list or a more complex subquery, then
 91174  91734   ** an ephemeral table might need to be generated from the RHS and then
 91175  91735   ** pX->iTable made to point to the ephemeral table instead of an
 91176  91736   ** existing table.
 91177  91737   **
 91178  91738   ** The inFlags parameter must contain exactly one of the bits
................................................................................
 91180  91740   ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
 91181  91741   ** fast membership test.  When the IN_INDEX_LOOP bit is set, the
 91182  91742   ** IN index will be used to loop over all values of the RHS of the
 91183  91743   ** IN operator.
 91184  91744   **
 91185  91745   ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
 91186  91746   ** through the set members) then the b-tree must not contain duplicates.
 91187         -** An epheremal table must be used unless the selected <column> is guaranteed
 91188         -** to be unique - either because it is an INTEGER PRIMARY KEY or it
 91189         -** has a UNIQUE constraint or UNIQUE index.
        91747  +** An epheremal table must be used unless the selected columns are guaranteed
        91748  +** to be unique - either because it is an INTEGER PRIMARY KEY or due to
        91749  +** a UNIQUE constraint or index.
 91190  91750   **
 91191  91751   ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 
 91192  91752   ** for fast set membership tests) then an epheremal table must 
 91193         -** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
 91194         -** be found with <column> as its left-most column.
        91753  +** be used unless <columns> is a single INTEGER PRIMARY KEY column or an 
        91754  +** index can be found with the specified <columns> as its left-most.
 91195  91755   **
 91196  91756   ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
 91197  91757   ** if the RHS of the IN operator is a list (not a subquery) then this
 91198  91758   ** routine might decide that creating an ephemeral b-tree for membership
 91199  91759   ** testing is too expensive and return IN_INDEX_NOOP.  In that case, the
 91200  91760   ** calling routine should implement the IN operator using a sequence
 91201  91761   ** of Eq or Ne comparison operations.
................................................................................
 91208  91768   ** to *prRhsHasNull. If there is no chance that the (...) contains a
 91209  91769   ** NULL value, then *prRhsHasNull is left unchanged.
 91210  91770   **
 91211  91771   ** If a register is allocated and its location stored in *prRhsHasNull, then
 91212  91772   ** the value in that register will be NULL if the b-tree contains one or more
 91213  91773   ** NULL values, and it will be some non-NULL value if the b-tree contains no
 91214  91774   ** NULL values.
        91775  +**
        91776  +** If the aiMap parameter is not NULL, it must point to an array containing
        91777  +** one element for each column returned by the SELECT statement on the RHS
        91778  +** of the IN(...) operator. The i'th entry of the array is populated with the
        91779  +** offset of the index column that matches the i'th column returned by the
        91780  +** SELECT. For example, if the expression and selected index are:
        91781  +**
        91782  +**   (?,?,?) IN (SELECT a, b, c FROM t1)
        91783  +**   CREATE INDEX i1 ON t1(b, c, a);
        91784  +**
        91785  +** then aiMap[] is populated with {2, 0, 1}.
 91215  91786   */
 91216  91787   #ifndef SQLITE_OMIT_SUBQUERY
 91217         -SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
        91788  +SQLITE_PRIVATE int sqlite3FindInIndex(
        91789  +  Parse *pParse,             /* Parsing context */
        91790  +  Expr *pX,                  /* The right-hand side (RHS) of the IN operator */
        91791  +  u32 inFlags,               /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
        91792  +  int *prRhsHasNull,         /* Register holding NULL status.  See notes */
        91793  +  int *aiMap                 /* Mapping from Index fields to RHS fields */
        91794  +){
 91218  91795     Select *p;                            /* SELECT to the right of IN operator */
 91219  91796     int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
 91220  91797     int iTab = pParse->nTab++;            /* Cursor of the RHS table */
 91221  91798     int mustBeUnique;                     /* True if RHS must be unique */
 91222  91799     Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
 91223  91800   
 91224  91801     assert( pX->op==TK_IN );
 91225  91802     mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
        91803  +
        91804  +  /* If the RHS of this IN(...) operator is a SELECT, and if it matters 
        91805  +  ** whether or not the SELECT result contains NULL values, check whether
        91806  +  ** or not NULL is actually possible (it may not be, for example, due 
        91807  +  ** to NOT NULL constraints in the schema). If no NULL values are possible,
        91808  +  ** set prRhsHasNull to 0 before continuing.  */
        91809  +  if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
        91810  +    int i;
        91811  +    ExprList *pEList = pX->x.pSelect->pEList;
        91812  +    for(i=0; i<pEList->nExpr; i++){
        91813  +      if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
        91814  +    }
        91815  +    if( i==pEList->nExpr ){
        91816  +      prRhsHasNull = 0;
        91817  +    }
        91818  +  }
 91226  91819   
 91227  91820     /* Check to see if an existing table or index can be used to
 91228  91821     ** satisfy the query.  This is preferable to generating a new 
 91229         -  ** ephemeral table.
 91230         -  */
        91822  +  ** ephemeral table.  */
 91231  91823     if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
 91232  91824       sqlite3 *db = pParse->db;              /* Database connection */
 91233  91825       Table *pTab;                           /* Table <table>. */
 91234         -    Expr *pExpr;                           /* Expression <column> */
 91235         -    i16 iCol;                              /* Index of column <column> */
 91236  91826       i16 iDb;                               /* Database idx for pTab */
        91827  +    ExprList *pEList = p->pEList;
        91828  +    int nExpr = pEList->nExpr;
 91237  91829   
 91238  91830       assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
 91239  91831       assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
 91240  91832       assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
 91241  91833       pTab = p->pSrc->a[0].pTab;
 91242         -    pExpr = p->pEList->a[0].pExpr;
 91243         -    iCol = (i16)pExpr->iColumn;
 91244         -   
        91834  +
 91245  91835       /* Code an OP_Transaction and OP_TableLock for <table>. */
 91246  91836       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 91247  91837       sqlite3CodeVerifySchema(pParse, iDb);
 91248  91838       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 91249  91839   
 91250         -    /* This function is only called from two places. In both cases the vdbe
 91251         -    ** has already been allocated. So assume sqlite3GetVdbe() is always
 91252         -    ** successful here.
 91253         -    */
 91254         -    assert(v);
 91255         -    if( iCol<0 ){
 91256         -      int iAddr = sqlite3CodeOnce(pParse);
        91840  +    assert(v);  /* sqlite3GetVdbe() has always been previously called */
        91841  +    if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
        91842  +      /* The "x IN (SELECT rowid FROM table)" case */
        91843  +      int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
 91257  91844         VdbeCoverage(v);
 91258  91845   
 91259  91846         sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 91260  91847         eType = IN_INDEX_ROWID;
 91261  91848   
 91262  91849         sqlite3VdbeJumpHere(v, iAddr);
 91263  91850       }else{
 91264  91851         Index *pIdx;                         /* Iterator variable */
 91265         -
 91266         -      /* The collation sequence used by the comparison. If an index is to
 91267         -      ** be used in place of a temp-table, it must be ordered according
 91268         -      ** to this collation sequence.  */
 91269         -      CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
 91270         -
 91271         -      /* Check that the affinity that will be used to perform the 
 91272         -      ** comparison is the same as the affinity of the column. If
 91273         -      ** it is not, it is not possible to use any index.
 91274         -      */
 91275         -      int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
 91276         -
 91277         -      for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
 91278         -        if( (pIdx->aiColumn[0]==iCol)
 91279         -         && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
 91280         -         && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
 91281         -        ){
 91282         -          int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
 91283         -          sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
 91284         -          sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 91285         -          VdbeComment((v, "%s", pIdx->zName));
 91286         -          assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
 91287         -          eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
 91288         -
 91289         -          if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
        91852  +      int affinity_ok = 1;
        91853  +      int i;
        91854  +
        91855  +      /* Check that the affinity that will be used to perform each 
        91856  +      ** comparison is the same as the affinity of each column in table
        91857  +      ** on the RHS of the IN operator.  If it not, it is not possible to
        91858  +      ** use any index of the RHS table.  */
        91859  +      for(i=0; i<nExpr && affinity_ok; i++){
        91860  +        Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
        91861  +        int iCol = pEList->a[i].pExpr->iColumn;
        91862  +        char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
        91863  +        char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
        91864  +        testcase( cmpaff==SQLITE_AFF_BLOB );
        91865  +        testcase( cmpaff==SQLITE_AFF_TEXT );
        91866  +        switch( cmpaff ){
        91867  +          case SQLITE_AFF_BLOB:
        91868  +            break;
        91869  +          case SQLITE_AFF_TEXT:
        91870  +            /* sqlite3CompareAffinity() only returns TEXT if one side or the
        91871  +            ** other has no affinity and the other side is TEXT.  Hence,
        91872  +            ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
        91873  +            ** and for the term on the LHS of the IN to have no affinity. */
        91874  +            assert( idxaff==SQLITE_AFF_TEXT );
        91875  +            break;
        91876  +          default:
        91877  +            affinity_ok = sqlite3IsNumericAffinity(idxaff);
        91878  +        }
        91879  +      }
        91880  +
        91881  +      if( affinity_ok ){
        91882  +        /* Search for an existing index that will work for this IN operator */
        91883  +        for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
        91884  +          Bitmask colUsed;      /* Columns of the index used */
        91885  +          Bitmask mCol;         /* Mask for the current column */
        91886  +          if( pIdx->nColumn<nExpr ) continue;
        91887  +          /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
        91888  +          ** BITMASK(nExpr) without overflowing */
        91889  +          testcase( pIdx->nColumn==BMS-2 );
        91890  +          testcase( pIdx->nColumn==BMS-1 );
        91891  +          if( pIdx->nColumn>=BMS-1 ) continue;
        91892  +          if( mustBeUnique ){
        91893  +            if( pIdx->nKeyCol>nExpr
        91894  +             ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
        91895  +            ){
        91896  +              continue;  /* This index is not unique over the IN RHS columns */
        91897  +            }
        91898  +          }
        91899  +  
        91900  +          colUsed = 0;   /* Columns of index used so far */
        91901  +          for(i=0; i<nExpr; i++){
        91902  +            Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
        91903  +            Expr *pRhs = pEList->a[i].pExpr;
        91904  +            CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
        91905  +            int j;
        91906  +  
        91907  +            assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
        91908  +            for(j=0; j<nExpr; j++){
        91909  +              if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
        91910  +              assert( pIdx->azColl[j] );
        91911  +              if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
        91912  +                continue;
        91913  +              }
        91914  +              break;
        91915  +            }
        91916  +            if( j==nExpr ) break;
        91917  +            mCol = MASKBIT(j);
        91918  +            if( mCol & colUsed ) break; /* Each column used only once */
        91919  +            colUsed |= mCol;
        91920  +            if( aiMap ) aiMap[i] = j;
        91921  +          }
        91922  +  
        91923  +          assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
        91924  +          if( colUsed==(MASKBIT(nExpr)-1) ){
        91925  +            /* If we reach this point, that means the index pIdx is usable */
        91926  +            int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
        91927  +#ifndef SQLITE_OMIT_EXPLAIN
        91928  +            sqlite3VdbeAddOp4(v, OP_Explain, 0, 0, 0,
        91929  +              sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName),
        91930  +              P4_DYNAMIC);
        91931  +#endif
        91932  +            sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
        91933  +            sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
        91934  +            VdbeComment((v, "%s", pIdx->zName));
        91935  +            assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
        91936  +            eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
        91937  +  
        91938  +            if( prRhsHasNull ){
 91290  91939   #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
 91291         -            const i64 sOne = 1;
 91292         -            sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 
 91293         -                iTab, 0, 0, (u8*)&sOne, P4_INT64);
        91940  +              i64 mask = (1<<nExpr)-1;
        91941  +              sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 
        91942  +                  iTab, 0, 0, (u8*)&mask, P4_INT64);
 91294  91943   #endif
 91295         -            *prRhsHasNull = ++pParse->nMem;
 91296         -            sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
        91944  +              *prRhsHasNull = ++pParse->nMem;
        91945  +              if( nExpr==1 ){
        91946  +                sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
        91947  +              }
        91948  +            }
        91949  +            sqlite3VdbeJumpHere(v, iAddr);
 91297  91950             }
 91298         -          sqlite3VdbeJumpHere(v, iAddr);
 91299         -        }
 91300         -      }
 91301         -    }
 91302         -  }
        91951  +        } /* End loop over indexes */
        91952  +      } /* End if( affinity_ok ) */
        91953  +    } /* End if not an rowid index */
        91954  +  } /* End attempt to optimize using an index */
 91303  91955   
 91304  91956     /* If no preexisting index is available for the IN clause
 91305  91957     ** and IN_INDEX_NOOP is an allowed reply
 91306  91958     ** and the RHS of the IN operator is a list, not a subquery
 91307  91959     ** and the RHS is not constant or has two or fewer terms,
 91308  91960     ** then it is not worth creating an ephemeral table to evaluate
 91309  91961     ** the IN operator so return IN_INDEX_NOOP.
................................................................................
 91311  91963     if( eType==0
 91312  91964      && (inFlags & IN_INDEX_NOOP_OK)
 91313  91965      && !ExprHasProperty(pX, EP_xIsSelect)
 91314  91966      && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
 91315  91967     ){
 91316  91968       eType = IN_INDEX_NOOP;
 91317  91969     }
 91318         -     
 91319  91970   
 91320  91971     if( eType==0 ){
 91321  91972       /* Could not find an existing table or index to use as the RHS b-tree.
 91322  91973       ** We will have to generate an ephemeral table to do the job.
 91323  91974       */
 91324  91975       u32 savedNQueryLoop = pParse->nQueryLoop;
 91325  91976       int rMayHaveNull = 0;
................................................................................
 91333  91984         *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
 91334  91985       }
 91335  91986       sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
 91336  91987       pParse->nQueryLoop = savedNQueryLoop;
 91337  91988     }else{
 91338  91989       pX->iTable = iTab;
 91339  91990     }
        91991  +
        91992  +  if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
        91993  +    int i, n;
        91994  +    n = sqlite3ExprVectorSize(pX->pLeft);
        91995  +    for(i=0; i<n; i++) aiMap[i] = i;
        91996  +  }
 91340  91997     return eType;
 91341  91998   }
 91342  91999   #endif
        92000  +
        92001  +#ifndef SQLITE_OMIT_SUBQUERY
        92002  +/*
        92003  +** Argument pExpr is an (?, ?...) IN(...) expression. This 
        92004  +** function allocates and returns a nul-terminated string containing 
        92005  +** the affinities to be used for each column of the comparison.
        92006  +**
        92007  +** It is the responsibility of the caller to ensure that the returned
        92008  +** string is eventually freed using sqlite3DbFree().
        92009  +*/
        92010  +static char *exprINAffinity(Parse *pParse, Expr *pExpr){
        92011  +  Expr *pLeft = pExpr->pLeft;
        92012  +  int nVal = sqlite3ExprVectorSize(pLeft);
        92013  +  Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
        92014  +  char *zRet;
        92015  +
        92016  +  assert( pExpr->op==TK_IN );
        92017  +  zRet = sqlite3DbMallocZero(pParse->db, nVal+1);
        92018  +  if( zRet ){
        92019  +    int i;
        92020  +    for(i=0; i<nVal; i++){
        92021  +      Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
        92022  +      char a = sqlite3ExprAffinity(pA);
        92023  +      if( pSelect ){
        92024  +        zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
        92025  +      }else{
        92026  +        zRet[i] = a;
        92027  +      }
        92028  +    }
        92029  +    zRet[nVal] = '\0';
        92030  +  }
        92031  +  return zRet;
        92032  +}
        92033  +#endif
        92034  +
        92035  +#ifndef SQLITE_OMIT_SUBQUERY
        92036  +/*
        92037  +** Load the Parse object passed as the first argument with an error 
        92038  +** message of the form:
        92039  +**
        92040  +**   "sub-select returns N columns - expected M"
        92041  +*/   
        92042  +SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
        92043  +  const char *zFmt = "sub-select returns %d columns - expected %d";
        92044  +  sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
        92045  +}
        92046  +#endif
 91343  92047   
 91344  92048   /*
 91345  92049   ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
 91346  92050   ** or IN operators.  Examples:
 91347  92051   **
 91348  92052   **     (SELECT a FROM b)          -- subquery
 91349  92053   **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
................................................................................
 91362  92066   ** If rMayHaveNull is non-zero, that means that the operation is an IN
 91363  92067   ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
 91364  92068   ** All this routine does is initialize the register given by rMayHaveNull
 91365  92069   ** to NULL.  Calling routines will take care of changing this register
 91366  92070   ** value to non-NULL if the RHS is NULL-free.
 91367  92071   **
 91368  92072   ** For a SELECT or EXISTS operator, return the register that holds the
 91369         -** result.  For IN operators or if an error occurs, the return value is 0.
        92073  +** result.  For a multi-column SELECT, the result is stored in a contiguous
        92074  +** array of registers and the return value is the register of the left-most
        92075  +** result column.  Return 0 for IN operators or if an error occurs.
 91370  92076   */
 91371  92077   #ifndef SQLITE_OMIT_SUBQUERY
 91372  92078   SQLITE_PRIVATE int sqlite3CodeSubselect(
 91373  92079     Parse *pParse,          /* Parsing context */
 91374  92080     Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
 91375  92081     int rHasNullFlag,       /* Register that records whether NULLs exist in RHS */
 91376  92082     int isRowid             /* If true, LHS of IN operator is a rowid */
................................................................................
 91377  92083   ){
 91378  92084     int jmpIfDynamic = -1;                      /* One-time test address */
 91379  92085     int rReg = 0;                           /* Register storing resulting */
 91380  92086     Vdbe *v = sqlite3GetVdbe(pParse);
 91381  92087     if( NEVER(v==0) ) return 0;
 91382  92088     sqlite3ExprCachePush(pParse);
 91383  92089   
 91384         -  /* This code must be run in its entirety every time it is encountered
 91385         -  ** if any of the following is true:
        92090  +  /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it
        92091  +  ** is encountered if any of the following is true:
 91386  92092     **
 91387  92093     **    *  The right-hand side is a correlated subquery
 91388  92094     **    *  The right-hand side is an expression list containing variables
 91389  92095     **    *  We are inside a trigger
 91390  92096     **
 91391  92097     ** If all of the above are false, then we can run this code just once
 91392  92098     ** save the results, and reuse the same result on subsequent invocations.
 91393  92099     */
 91394  92100     if( !ExprHasProperty(pExpr, EP_VarSelect) ){
 91395         -    jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
        92101  +    jmpIfDynamic = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
 91396  92102     }
 91397  92103   
 91398  92104   #ifndef SQLITE_OMIT_EXPLAIN
 91399  92105     if( pParse->explain==2 ){
 91400  92106       char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
 91401  92107           jmpIfDynamic>=0?"":"CORRELATED ",
 91402  92108           pExpr->op==TK_IN?"LIST":"SCALAR",
................................................................................
 91404  92110       );
 91405  92111       sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 91406  92112     }
 91407  92113   #endif
 91408  92114   
 91409  92115     switch( pExpr->op ){
 91410  92116       case TK_IN: {
 91411         -      char affinity;              /* Affinity of the LHS of the IN */
 91412  92117         int addr;                   /* Address of OP_OpenEphemeral instruction */
 91413  92118         Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
 91414  92119         KeyInfo *pKeyInfo = 0;      /* Key information */
 91415         -
 91416         -      affinity = sqlite3ExprAffinity(pLeft);
        92120  +      int nVal;                   /* Size of vector pLeft */
        92121  +      
        92122  +      nVal = sqlite3ExprVectorSize(pLeft);
        92123  +      assert( !isRowid || nVal==1 );
 91417  92124   
 91418  92125         /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
 91419  92126         ** expression it is handled the same way.  An ephemeral table is 
 91420         -      ** filled with single-field index keys representing the results
 91421         -      ** from the SELECT or the <exprlist>.
        92127  +      ** filled with index keys representing the results from the 
        92128  +      ** SELECT or the <exprlist>.
 91422  92129         **
 91423  92130         ** If the 'x' expression is a column value, or the SELECT...
 91424  92131         ** statement returns a column value, then the affinity of that
 91425  92132         ** column is used to build the index keys. If both 'x' and the
 91426  92133         ** SELECT... statement are columns, then numeric affinity is used
 91427  92134         ** if either column has NUMERIC or INTEGER affinity. If neither
 91428  92135         ** 'x' nor the SELECT... statement are columns, then numeric affinity
 91429  92136         ** is used.
 91430  92137         */
 91431  92138         pExpr->iTable = pParse->nTab++;
 91432         -      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
 91433         -      pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
        92139  +      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, 
        92140  +          pExpr->iTable, (isRowid?0:nVal));
        92141  +      pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
 91434  92142   
 91435  92143         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 91436  92144           /* Case 1:     expr IN (SELECT ...)
 91437  92145           **
 91438  92146           ** Generate code to write the results of the select into the temporary
 91439  92147           ** table allocated and opened above.
 91440  92148           */
 91441  92149           Select *pSelect = pExpr->x.pSelect;
 91442         -        SelectDest dest;
 91443         -        ExprList *pEList;
        92150  +        ExprList *pEList = pSelect->pEList;
 91444  92151   
 91445  92152           assert( !isRowid );
 91446         -        sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
 91447         -        dest.affSdst = (u8)affinity;
 91448         -        assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
 91449         -        pSelect->iLimit = 0;
 91450         -        testcase( pSelect->selFlags & SF_Distinct );
 91451         -        testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
 91452         -        if( sqlite3Select(pParse, pSelect, &dest) ){
 91453         -          sqlite3KeyInfoUnref(pKeyInfo);
 91454         -          return 0;
 91455         -        }
 91456         -        pEList = pSelect->pEList;
 91457         -        assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
 91458         -        assert( pEList!=0 );
 91459         -        assert( pEList->nExpr>0 );
 91460         -        assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 91461         -        pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
 91462         -                                                         pEList->a[0].pExpr);
        92153  +        /* If the LHS and RHS of the IN operator do not match, that
        92154  +        ** error will have been caught long before we reach this point. */
        92155  +        if( ALWAYS(pEList->nExpr==nVal) ){
        92156  +          SelectDest dest;
        92157  +          int i;
        92158  +          sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
        92159  +          dest.zAffSdst = exprINAffinity(pParse, pExpr);
        92160  +          assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
        92161  +          pSelect->iLimit = 0;
        92162  +          testcase( pSelect->selFlags & SF_Distinct );
        92163  +          testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
        92164  +          if( sqlite3Select(pParse, pSelect, &dest) ){
        92165  +            sqlite3DbFree(pParse->db, dest.zAffSdst);
        92166  +            sqlite3KeyInfoUnref(pKeyInfo);
        92167  +            return 0;
        92168  +          }
        92169  +          sqlite3DbFree(pParse->db, dest.zAffSdst);
        92170  +          assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
        92171  +          assert( pEList!=0 );
        92172  +          assert( pEList->nExpr>0 );
        92173  +          assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
        92174  +          for(i=0; i<nVal; i++){
        92175  +            Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
        92176  +            pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
        92177  +                pParse, p, pEList->a[i].pExpr
        92178  +            );
        92179  +          }
        92180  +        }
 91463  92181         }else if( ALWAYS(pExpr->x.pList!=0) ){
 91464  92182           /* Case 2:     expr IN (exprlist)
 91465  92183           **
 91466  92184           ** For each expression, build an index key from the evaluation and
 91467  92185           ** store it in the temporary table. If <expr> is a column, then use
 91468  92186           ** that columns affinity when building index keys. If <expr> is not
 91469  92187           ** a column, use numeric affinity.
 91470  92188           */
        92189  +        char affinity;            /* Affinity of the LHS of the IN */
 91471  92190           int i;
 91472  92191           ExprList *pList = pExpr->x.pList;
 91473  92192           struct ExprList_item *pItem;
 91474  92193           int r1, r2, r3;
 91475  92194   
        92195  +        affinity = sqlite3ExprAffinity(pLeft);
 91476  92196           if( !affinity ){
 91477  92197             affinity = SQLITE_AFF_BLOB;
 91478  92198           }
 91479  92199           if( pKeyInfo ){
 91480  92200             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 91481  92201             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 91482  92202           }
................................................................................
 91524  92244         }
 91525  92245         break;
 91526  92246       }
 91527  92247   
 91528  92248       case TK_EXISTS:
 91529  92249       case TK_SELECT:
 91530  92250       default: {
 91531         -      /* If this has to be a scalar SELECT.  Generate code to put the
 91532         -      ** value of this select in a memory cell and record the number
 91533         -      ** of the memory cell in iColumn.  If this is an EXISTS, write
 91534         -      ** an integer 0 (not exists) or 1 (exists) into a memory cell
 91535         -      ** and record that memory cell in iColumn.
        92251  +      /* Case 3:    (SELECT ... FROM ...)
        92252  +      **     or:    EXISTS(SELECT ... FROM ...)
        92253  +      **
        92254  +      ** For a SELECT, generate code to put the values for all columns of
        92255  +      ** the first row into an array of registers and return the index of
        92256  +      ** the first register.
        92257  +      **
        92258  +      ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
        92259  +      ** into a register and return that register number.
        92260  +      **
        92261  +      ** In both cases, the query is augmented with "LIMIT 1".  Any 
        92262  +      ** preexisting limit is discarded in place of the new LIMIT 1.
 91536  92263         */
 91537  92264         Select *pSel;                         /* SELECT statement to encode */
 91538         -      SelectDest dest;                      /* How to deal with SELECt result */
        92265  +      SelectDest dest;                      /* How to deal with SELECT result */
        92266  +      int nReg;                             /* Registers to allocate */
 91539  92267   
 91540  92268         testcase( pExpr->op==TK_EXISTS );
 91541  92269         testcase( pExpr->op==TK_SELECT );
 91542  92270         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 91543         -
 91544  92271         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
        92272  +
 91545  92273         pSel = pExpr->x.pSelect;
 91546         -      sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
        92274  +      nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
        92275  +      sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
        92276  +      pParse->nMem += nReg;
 91547  92277         if( pExpr->op==TK_SELECT ){
 91548  92278           dest.eDest = SRT_Mem;
 91549  92279           dest.iSdst = dest.iSDParm;
 91550         -        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
        92280  +        dest.nSdst = nReg;
        92281  +        sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
 91551  92282           VdbeComment((v, "Init subquery result"));
 91552  92283         }else{
 91553  92284           dest.eDest = SRT_Exists;
 91554  92285           sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 91555  92286           VdbeComment((v, "Init EXISTS result"));
 91556  92287         }
 91557  92288         sqlite3ExprDelete(pParse->db, pSel->pLimit);
 91558         -      pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
 91559         -                                  &sqlite3IntTokens[1]);
        92289  +      pSel->pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,
        92290  +                                  &sqlite3IntTokens[1], 0);
 91560  92291         pSel->iLimit = 0;
 91561  92292         pSel->selFlags &= ~SF_MultiValue;
 91562  92293         if( sqlite3Select(pParse, pSel, &dest) ){
 91563  92294           return 0;
 91564  92295         }
 91565  92296         rReg = dest.iSDParm;
 91566  92297         ExprSetVVAProperty(pExpr, EP_NoReduce);
................................................................................
 91576  92307       sqlite3VdbeJumpHere(v, jmpIfDynamic);
 91577  92308     }
 91578  92309     sqlite3ExprCachePop(pParse);
 91579  92310   
 91580  92311     return rReg;
 91581  92312   }
 91582  92313   #endif /* SQLITE_OMIT_SUBQUERY */
        92314  +
        92315  +#ifndef SQLITE_OMIT_SUBQUERY
        92316  +/*
        92317  +** Expr pIn is an IN(...) expression. This function checks that the 
        92318  +** sub-select on the RHS of the IN() operator has the same number of 
        92319  +** columns as the vector on the LHS. Or, if the RHS of the IN() is not 
        92320  +** a sub-query, that the LHS is a vector of size 1.
        92321  +*/
        92322  +SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
        92323  +  int nVector = sqlite3ExprVectorSize(pIn->pLeft);
        92324  +  if( (pIn->flags & EP_xIsSelect) ){
        92325  +    if( nVector!=pIn->x.pSelect->pEList->nExpr ){
        92326  +      sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
        92327  +      return 1;
        92328  +    }
        92329  +  }else if( nVector!=1 ){
        92330  +    if( (pIn->pLeft->flags & EP_xIsSelect) ){
        92331  +      sqlite3SubselectError(pParse, nVector, 1);
        92332  +    }else{
        92333  +      sqlite3ErrorMsg(pParse, "row value misused");
        92334  +    }
        92335  +    return 1;
        92336  +  }
        92337  +  return 0;
        92338  +}
        92339  +#endif
 91583  92340   
 91584  92341   #ifndef SQLITE_OMIT_SUBQUERY
 91585  92342   /*
 91586  92343   ** Generate code for an IN expression.
 91587  92344   **
 91588  92345   **      x IN (SELECT ...)
 91589  92346   **      x IN (value, value, ...)
 91590  92347   **
 91591         -** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
 91592         -** is an array of zero or more values.  The expression is true if the LHS is
 91593         -** contained within the RHS.  The value of the expression is unknown (NULL)
 91594         -** if the LHS is NULL or if the LHS is not contained within the RHS and the
 91595         -** RHS contains one or more NULL values.
        92348  +** The left-hand side (LHS) is a scalar or vector expression.  The 
        92349  +** right-hand side (RHS) is an array of zero or more scalar values, or a
        92350  +** subquery.  If the RHS is a subquery, the number of result columns must
        92351  +** match the number of columns in the vector on the LHS.  If the RHS is
        92352  +** a list of values, the LHS must be a scalar. 
        92353  +**
        92354  +** The IN operator is true if the LHS value is contained within the RHS.
        92355  +** The result is false if the LHS is definitely not in the RHS.  The 
        92356  +** result is NULL if the presence of the LHS in the RHS cannot be 
        92357  +** determined due to NULLs.
 91596  92358   **
 91597  92359   ** This routine generates code that jumps to destIfFalse if the LHS is not 
 91598  92360   ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
 91599  92361   ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
 91600  92362   ** within the RHS then fall through.
        92363  +**
        92364  +** See the separate in-operator.md documentation file in the canonical
        92365  +** SQLite source tree for additional information.
 91601  92366   */
 91602  92367   static void sqlite3ExprCodeIN(
 91603  92368     Parse *pParse,        /* Parsing and code generating context */
 91604  92369     Expr *pExpr,          /* The IN expression */
 91605  92370     int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
 91606  92371     int destIfNull        /* Jump here if the results are unknown due to NULLs */
 91607  92372   ){
 91608  92373     int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
 91609         -  char affinity;        /* Comparison affinity to use */
 91610  92374     int eType;            /* Type of the RHS */
 91611         -  int r1;               /* Temporary use register */
        92375  +  int rLhs;             /* Register(s) holding the LHS values */
        92376  +  int rLhsOrig;         /* LHS values prior to reordering by aiMap[] */
 91612  92377     Vdbe *v;              /* Statement under construction */
        92378  +  int *aiMap = 0;       /* Map from vector field to index column */
        92379  +  char *zAff = 0;       /* Affinity string for comparisons */
        92380  +  int nVector;          /* Size of vectors for this IN operator */
        92381  +  int iDummy;           /* Dummy parameter to exprCodeVector() */
        92382  +  Expr *pLeft;          /* The LHS of the IN operator */
        92383  +  int i;                /* loop counter */
        92384  +  int destStep2;        /* Where to jump when NULLs seen in step 2 */
        92385  +  int destStep6 = 0;    /* Start of code for Step 6 */
        92386  +  int addrTruthOp;      /* Address of opcode that determines the IN is true */
        92387  +  int destNotNull;      /* Jump here if a comparison is not true in step 6 */
        92388  +  int addrTop;          /* Top of the step-6 loop */ 
 91613  92389   
 91614         -  /* Compute the RHS.   After this step, the table with cursor
 91615         -  ** pExpr->iTable will contains the values that make up the RHS.
 91616         -  */
        92390  +  pLeft = pExpr->pLeft;
        92391  +  if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
        92392  +  zAff = exprINAffinity(pParse, pExpr);
        92393  +  nVector = sqlite3ExprVectorSize(pExpr->pLeft);
        92394  +  aiMap = (int*)sqlite3DbMallocZero(
        92395  +      pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
        92396  +  );
        92397  +  if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
        92398  +
        92399  +  /* Attempt to compute the RHS. After this step, if anything other than
        92400  +  ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable 
        92401  +  ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
        92402  +  ** the RHS has not yet been coded.  */
 91617  92403     v = pParse->pVdbe;
 91618  92404     assert( v!=0 );       /* OOM detected prior to this routine */
 91619  92405     VdbeNoopComment((v, "begin IN expr"));
 91620  92406     eType = sqlite3FindInIndex(pParse, pExpr,
 91621  92407                                IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
 91622         -                             destIfFalse==destIfNull ? 0 : &rRhsHasNull);
        92408  +                             destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap);
 91623  92409   
 91624         -  /* Figure out the affinity to use to create a key from the results
 91625         -  ** of the expression. affinityStr stores a static string suitable for
 91626         -  ** P4 of OP_MakeRecord.
 91627         -  */
 91628         -  affinity = comparisonAffinity(pExpr);
        92410  +  assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
        92411  +       || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC 
        92412  +  );
        92413  +#ifdef SQLITE_DEBUG
        92414  +  /* Confirm that aiMap[] contains nVector integer values between 0 and
        92415  +  ** nVector-1. */
        92416  +  for(i=0; i<nVector; i++){
        92417  +    int j, cnt;
        92418  +    for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
        92419  +    assert( cnt==1 );
        92420  +  }
        92421  +#endif
 91629  92422   
 91630         -  /* Code the LHS, the <expr> from "<expr> IN (...)".
        92423  +  /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a 
        92424  +  ** vector, then it is stored in an array of nVector registers starting 
        92425  +  ** at r1.
        92426  +  **
        92427  +  ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
        92428  +  ** so that the fields are in the same order as an existing index.   The
        92429  +  ** aiMap[] array contains a mapping from the original LHS field order to
        92430  +  ** the field order that matches the RHS index.
 91631  92431     */
 91632  92432     sqlite3ExprCachePush(pParse);
 91633         -  r1 = sqlite3GetTempReg(pParse);
 91634         -  sqlite3ExprCode(pParse, pExpr->pLeft, r1);
        92433  +  rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
        92434  +  for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
        92435  +  if( i==nVector ){
        92436  +    /* LHS fields are not reordered */
        92437  +    rLhs = rLhsOrig;
        92438  +  }else{
        92439  +    /* Need to reorder the LHS fields according to aiMap */
        92440  +    rLhs = sqlite3GetTempRange(pParse, nVector);
        92441  +    for(i=0; i<nVector; i++){
        92442  +      sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0);
        92443  +    }
        92444  +  }
 91635  92445   
 91636  92446     /* If sqlite3FindInIndex() did not find or create an index that is
 91637  92447     ** suitable for evaluating the IN operator, then evaluate using a
 91638  92448     ** sequence of comparisons.
        92449  +  **
        92450  +  ** This is step (1) in the in-operator.md optimized algorithm.
 91639  92451     */
 91640  92452     if( eType==IN_INDEX_NOOP ){
 91641  92453       ExprList *pList = pExpr->x.pList;
 91642  92454       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 91643  92455       int labelOk = sqlite3VdbeMakeLabel(v);
 91644  92456       int r2, regToFree;
 91645  92457       int regCkNull = 0;
 91646  92458       int ii;
 91647  92459       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 91648  92460       if( destIfNull!=destIfFalse ){
 91649  92461         regCkNull = sqlite3GetTempReg(pParse);
 91650         -      sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
        92462  +      sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
 91651  92463       }
 91652  92464       for(ii=0; ii<pList->nExpr; ii++){
 91653  92465         r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
 91654  92466         if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
 91655  92467           sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
 91656  92468         }
 91657  92469         if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
 91658         -        sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
        92470  +        sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2,
 91659  92471                             (void*)pColl, P4_COLLSEQ);
 91660  92472           VdbeCoverageIf(v, ii<pList->nExpr-1);
 91661  92473           VdbeCoverageIf(v, ii==pList->nExpr-1);
 91662         -        sqlite3VdbeChangeP5(v, affinity);
        92474  +        sqlite3VdbeChangeP5(v, zAff[0]);
 91663  92475         }else{
 91664  92476           assert( destIfNull==destIfFalse );
 91665         -        sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
        92477  +        sqlite3VdbeAddOp4(v, OP_Ne, rLhs, destIfFalse, r2,
 91666  92478                             (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
 91667         -        sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
        92479  +        sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
 91668  92480         }
 91669  92481         sqlite3ReleaseTempReg(pParse, regToFree);
 91670  92482       }
 91671  92483       if( regCkNull ){
 91672  92484         sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
 91673  92485         sqlite3VdbeGoto(v, destIfFalse);
 91674  92486       }
 91675  92487       sqlite3VdbeResolveLabel(v, labelOk);
 91676  92488       sqlite3ReleaseTempReg(pParse, regCkNull);
 91677         -  }else{
 91678         -  
 91679         -    /* If the LHS is NULL, then the result is either false or NULL depending
 91680         -    ** on whether the RHS is empty or not, respectively.
 91681         -    */
 91682         -    if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
 91683         -      if( destIfNull==destIfFalse ){
 91684         -        /* Shortcut for the common case where the false and NULL outcomes are
 91685         -        ** the same. */
 91686         -        sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
 91687         -      }else{
 91688         -        int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
 91689         -        sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
 91690         -        VdbeCoverage(v);
 91691         -        sqlite3VdbeGoto(v, destIfNull);
 91692         -        sqlite3VdbeJumpHere(v, addr1);
 91693         -      }
 91694         -    }
 91695         -  
 91696         -    if( eType==IN_INDEX_ROWID ){
 91697         -      /* In this case, the RHS is the ROWID of table b-tree
 91698         -      */
 91699         -      sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, r1);
 91700         -      VdbeCoverage(v);
 91701         -    }else{
 91702         -      /* In this case, the RHS is an index b-tree.
 91703         -      */
 91704         -      sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
 91705         -  
 91706         -      /* If the set membership test fails, then the result of the 
 91707         -      ** "x IN (...)" expression must be either 0 or NULL. If the set
 91708         -      ** contains no NULL values, then the result is 0. If the set 
 91709         -      ** contains one or more NULL values, then the result of the
 91710         -      ** expression is also NULL.
 91711         -      */
 91712         -      assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
 91713         -      if( rRhsHasNull==0 ){
 91714         -        /* This branch runs if it is known at compile time that the RHS
 91715         -        ** cannot contain NULL values. This happens as the result
 91716         -        ** of a "NOT NULL" constraint in the database schema.
 91717         -        **
 91718         -        ** Also run this branch if NULL is equivalent to FALSE
 91719         -        ** for this particular IN operator.
 91720         -        */
 91721         -        sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
 91722         -        VdbeCoverage(v);
 91723         -      }else{
 91724         -        /* In this branch, the RHS of the IN might contain a NULL and
 91725         -        ** the presence of a NULL on the RHS makes a difference in the
 91726         -        ** outcome.
 91727         -        */
 91728         -        int addr1;
 91729         -  
 91730         -        /* First check to see if the LHS is contained in the RHS.  If so,
 91731         -        ** then the answer is TRUE the presence of NULLs in the RHS does
 91732         -        ** not matter.  If the LHS is not contained in the RHS, then the
 91733         -        ** answer is NULL if the RHS contains NULLs and the answer is
 91734         -        ** FALSE if the RHS is NULL-free.
 91735         -        */
 91736         -        addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
 91737         -        VdbeCoverage(v);
 91738         -        sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
 91739         -        VdbeCoverage(v);
 91740         -        sqlite3VdbeGoto(v, destIfFalse);
 91741         -        sqlite3VdbeJumpHere(v, addr1);
 91742         -      }
 91743         -    }
 91744         -  }
 91745         -  sqlite3ReleaseTempReg(pParse, r1);
        92489  +    goto sqlite3ExprCodeIN_finished;
        92490  +  }
        92491  +
        92492  +  /* Step 2: Check to see if the LHS contains any NULL columns.  If the
        92493  +  ** LHS does contain NULLs then the result must be either FALSE or NULL.
        92494  +  ** We will then skip the binary search of the RHS.
        92495  +  */
        92496  +  if( destIfNull==destIfFalse ){
        92497  +    destStep2 = destIfFalse;
        92498  +  }else{
        92499  +    destStep2 = destStep6 = sqlite3VdbeMakeLabel(v);
        92500  +  }
        92501  +  for(i=0; i<nVector; i++){
        92502  +    Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
        92503  +    if( sqlite3ExprCanBeNull(p) ){
        92504  +      sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2);
        92505  +      VdbeCoverage(v);
        92506  +    }
        92507  +  }
        92508  +
        92509  +  /* Step 3.  The LHS is now known to be non-NULL.  Do the binary search
        92510  +  ** of the RHS using the LHS as a probe.  If found, the result is
        92511  +  ** true.
        92512  +  */
        92513  +  if( eType==IN_INDEX_ROWID ){
        92514  +    /* In this case, the RHS is the ROWID of table b-tree and so we also
        92515  +    ** know that the RHS is non-NULL.  Hence, we combine steps 3 and 4
        92516  +    ** into a single opcode. */
        92517  +    sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, rLhs);
        92518  +    VdbeCoverage(v);
        92519  +    addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto);  /* Return True */
        92520  +  }else{
        92521  +    sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector);
        92522  +    if( destIfFalse==destIfNull ){
        92523  +      /* Combine Step 3 and Step 5 into a single opcode */
        92524  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse,
        92525  +                           rLhs, nVector); VdbeCoverage(v);
        92526  +      goto sqlite3ExprCodeIN_finished;
        92527  +    }
        92528  +    /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
        92529  +    addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0,
        92530  +                                      rLhs, nVector); VdbeCoverage(v);
        92531  +  }
        92532  +
        92533  +  /* Step 4.  If the RHS is known to be non-NULL and we did not find
        92534  +  ** an match on the search above, then the result must be FALSE.
        92535  +  */
        92536  +  if( rRhsHasNull && nVector==1 ){
        92537  +    sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse);
        92538  +    VdbeCoverage(v);
        92539  +  }
        92540  +
        92541  +  /* Step 5.  If we do not care about the difference between NULL and
        92542  +  ** FALSE, then just return false. 
        92543  +  */
        92544  +  if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
        92545  +
        92546  +  /* Step 6: Loop through rows of the RHS.  Compare each row to the LHS.
        92547  +  ** If any comparison is NULL, then the result is NULL.  If all
        92548  +  ** comparisons are FALSE then the final result is FALSE.
        92549  +  **
        92550  +  ** For a scalar LHS, it is sufficient to check just the first row
        92551  +  ** of the RHS.
        92552  +  */
        92553  +  if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
        92554  +  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
        92555  +  VdbeCoverage(v);
        92556  +  if( nVector>1 ){
        92557  +    destNotNull = sqlite3VdbeMakeLabel(v);
        92558  +  }else{
        92559  +    /* For nVector==1, combine steps 6 and 7 by immediately returning
        92560  +    ** FALSE if the first comparison is not NULL */
        92561  +    destNotNull = destIfFalse;
        92562  +  }
        92563  +  for(i=0; i<nVector; i++){
        92564  +    Expr *p;
        92565  +    CollSeq *pColl;
        92566  +    int r3 = sqlite3GetTempReg(pParse);
        92567  +    p = sqlite3VectorFieldSubexpr(pLeft, i);
        92568  +    pColl = sqlite3ExprCollSeq(pParse, p);
        92569  +    sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r3);
        92570  +    sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3,
        92571  +                      (void*)pColl, P4_COLLSEQ);
        92572  +    VdbeCoverage(v);
        92573  +    sqlite3ReleaseTempReg(pParse, r3);
        92574  +  }
        92575  +  sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
        92576  +  if( nVector>1 ){
        92577  +    sqlite3VdbeResolveLabel(v, destNotNull);
        92578  +    sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrTop+1);
        92579  +    VdbeCoverage(v);
        92580  +
        92581  +    /* Step 7:  If we reach this point, we know that the result must
        92582  +    ** be false. */
        92583  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
        92584  +  }
        92585  +
        92586  +  /* Jumps here in order to return true. */
        92587  +  sqlite3VdbeJumpHere(v, addrTruthOp);
        92588  +
        92589  +sqlite3ExprCodeIN_finished:
        92590  +  if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
 91746  92591     sqlite3ExprCachePop(pParse);
 91747  92592     VdbeComment((v, "end IN expr"));
        92593  +sqlite3ExprCodeIN_oom_error:
        92594  +  sqlite3DbFree(pParse->db, aiMap);
        92595  +  sqlite3DbFree(pParse->db, zAff);
 91748  92596   }
 91749  92597   #endif /* SQLITE_OMIT_SUBQUERY */
 91750  92598   
 91751  92599   #ifndef SQLITE_OMIT_FLOATING_POINT
 91752  92600   /*
 91753  92601   ** Generate an instruction that will put the floating point
 91754  92602   ** value described by z[0..n-1] into register iMem.
................................................................................
 91804  92652           codeReal(v, z, negFlag, iMem);
 91805  92653         }
 91806  92654   #endif
 91807  92655       }
 91808  92656     }
 91809  92657   }
 91810  92658   
 91811         -#if defined(SQLITE_DEBUG)
 91812  92659   /*
 91813         -** Verify the consistency of the column cache
        92660  +** Erase column-cache entry number i
 91814  92661   */
 91815         -static int cacheIsValid(Parse *pParse){
 91816         -  int i, n;
 91817         -  for(i=n=0; i<SQLITE_N_COLCACHE; i++){
 91818         -    if( pParse->aColCache[i].iReg>0 ) n++;
 91819         -  }
 91820         -  return n==pParse->nColCache;
 91821         -}
 91822         -#endif
 91823         -
 91824         -/*
 91825         -** Clear a cache entry.
 91826         -*/
 91827         -static void cacheEntryClear(Parse *pParse, struct yColCache *p){
 91828         -  if( p->tempReg ){
        92662  +static void cacheEntryClear(Parse *pParse, int i){
        92663  +  if( pParse->aColCache[i].tempReg ){
 91829  92664       if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
 91830         -      pParse->aTempReg[pParse->nTempReg++] = p->iReg;
        92665  +      pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
 91831  92666       }
 91832         -    p->tempReg = 0;
 91833  92667     }
 91834         -  p->iReg = 0;
 91835  92668     pParse->nColCache--;
 91836         -  assert( pParse->db->mallocFailed || cacheIsValid(pParse) );
        92669  +  if( i<pParse->nColCache ){
        92670  +    pParse->aColCache[i] = pParse->aColCache[pParse->nColCache];
        92671  +  }
 91837  92672   }
 91838  92673   
 91839  92674   
 91840  92675   /*
 91841  92676   ** Record in the column cache that a particular column from a
 91842  92677   ** particular table is stored in a particular register.
 91843  92678   */
................................................................................
 91859  92694   
 91860  92695     /* First replace any existing entry.
 91861  92696     **
 91862  92697     ** Actually, the way the column cache is currently used, we are guaranteed
 91863  92698     ** that the object will never already be in cache.  Verify this guarantee.
 91864  92699     */
 91865  92700   #ifndef NDEBUG
 91866         -  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 91867         -    assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
 91868         -  }
 91869         -#endif
 91870         -
 91871         -  /* Find an empty slot and replace it */
 91872         -  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 91873         -    if( p->iReg==0 ){
 91874         -      p->iLevel = pParse->iCacheLevel;
 91875         -      p->iTable = iTab;
 91876         -      p->iColumn = iCol;
 91877         -      p->iReg = iReg;
 91878         -      p->tempReg = 0;
 91879         -      p->lru = pParse->iCacheCnt++;
 91880         -      pParse->nColCache++;
 91881         -      assert( pParse->db->mallocFailed || cacheIsValid(pParse) );
 91882         -      return;
 91883         -    }
 91884         -  }
 91885         -
 91886         -  /* Replace the last recently used */
 91887         -  minLru = 0x7fffffff;
 91888         -  idxLru = -1;
 91889         -  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 91890         -    if( p->lru<minLru ){
 91891         -      idxLru = i;
 91892         -      minLru = p->lru;
 91893         -    }
 91894         -  }
 91895         -  if( ALWAYS(idxLru>=0) ){
        92701  +  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
        92702  +    assert( p->iTable!=iTab || p->iColumn!=iCol );
        92703  +  }
        92704  +#endif
        92705  +
        92706  +  /* If the cache is already full, delete the least recently used entry */
        92707  +  if( pParse->nColCache>=SQLITE_N_COLCACHE ){
        92708  +    minLru = 0x7fffffff;
        92709  +    idxLru = -1;
        92710  +    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
        92711  +      if( p->lru<minLru ){
        92712  +        idxLru = i;
        92713  +        minLru = p->lru;
        92714  +      }
        92715  +    }
 91896  92716       p = &pParse->aColCache[idxLru];
 91897         -    p->iLevel = pParse->iCacheLevel;
 91898         -    p->iTable = iTab;
 91899         -    p->iColumn = iCol;
 91900         -    p->iReg = iReg;
 91901         -    p->tempReg = 0;
 91902         -    p->lru = pParse->iCacheCnt++;
 91903         -    assert( cacheIsValid(pParse) );
 91904         -    return;
        92717  +  }else{
        92718  +    p = &pParse->aColCache[pParse->nColCache++];
 91905  92719     }
        92720  +
        92721  +  /* Add the new entry to the end of the cache */
        92722  +  p->iLevel = pParse->iCacheLevel;
        92723  +  p->iTable = iTab;
        92724  +  p->iColumn = iCol;
        92725  +  p->iReg = iReg;
        92726  +  p->tempReg = 0;
        92727  +  p->lru = pParse->iCacheCnt++;
 91906  92728   }
 91907  92729   
 91908  92730   /*
 91909  92731   ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
 91910  92732   ** Purge the range of registers from the column cache.
 91911  92733   */
 91912  92734   SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
 91913         -  struct yColCache *p;
 91914         -  if( iReg<=0 || pParse->nColCache==0 ) return;
 91915         -  p = &pParse->aColCache[SQLITE_N_COLCACHE-1];
 91916         -  while(1){
 91917         -    if( p->iReg >= iReg && p->iReg < iReg+nReg ) cacheEntryClear(pParse, p);
 91918         -    if( p==pParse->aColCache ) break;
 91919         -    p--;
        92735  +  int i = 0;
        92736  +  while( i<pParse->nColCache ){
        92737  +    struct yColCache *p = &pParse->aColCache[i];
        92738  +    if( p->iReg >= iReg && p->iReg < iReg+nReg ){
        92739  +      cacheEntryClear(pParse, i);
        92740  +    }else{
        92741  +      i++;
        92742  +    }
 91920  92743     }
 91921  92744   }
 91922  92745   
 91923  92746   /*
 91924  92747   ** Remember the current column cache context.  Any new entries added
 91925  92748   ** added to the column cache after this call are removed when the
 91926  92749   ** corresponding pop occurs.
................................................................................
 91936  92759   
 91937  92760   /*
 91938  92761   ** Remove from the column cache any entries that were added since the
 91939  92762   ** the previous sqlite3ExprCachePush operation.  In other words, restore
 91940  92763   ** the cache to the state it was in prior the most recent Push.
 91941  92764   */
 91942  92765   SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse){
 91943         -  int i;
 91944         -  struct yColCache *p;
        92766  +  int i = 0;
 91945  92767     assert( pParse->iCacheLevel>=1 );
 91946  92768     pParse->iCacheLevel--;
 91947  92769   #ifdef SQLITE_DEBUG
 91948  92770     if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
 91949  92771       printf("POP  to %d\n", pParse->iCacheLevel);
 91950  92772     }
 91951  92773   #endif
 91952         -  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 91953         -    if( p->iReg && p->iLevel>pParse->iCacheLevel ){
 91954         -      cacheEntryClear(pParse, p);
        92774  +  while( i<pParse->nColCache ){
        92775  +    if( pParse->aColCache[i].iLevel>pParse->iCacheLevel ){
        92776  +      cacheEntryClear(pParse, i);
        92777  +    }else{
        92778  +      i++;
 91955  92779       }
 91956  92780     }
 91957  92781   }
 91958  92782   
 91959  92783   /*
 91960  92784   ** When a cached column is reused, make sure that its register is
 91961  92785   ** no longer available as a temp register.  ticket #3879:  that same
 91962  92786   ** register might be in the cache in multiple places, so be sure to
 91963  92787   ** get them all.
 91964  92788   */
 91965  92789   static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
 91966  92790     int i;
 91967  92791     struct yColCache *p;
 91968         -  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
        92792  +  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
 91969  92793       if( p->iReg==iReg ){
 91970  92794         p->tempReg = 0;
 91971  92795       }
 91972  92796     }
 91973  92797   }
 91974  92798   
 91975  92799   /* Generate code that will load into register regOut a value that is
................................................................................
 92039  92863     int iReg,        /* Store results here */
 92040  92864     u8 p5            /* P5 value for OP_Column + FLAGS */
 92041  92865   ){
 92042  92866     Vdbe *v = pParse->pVdbe;
 92043  92867     int i;
 92044  92868     struct yColCache *p;
 92045  92869   
 92046         -  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 92047         -    if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
        92870  +  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
        92871  +    if( p->iTable==iTable && p->iColumn==iColumn ){
 92048  92872         p->lru = pParse->iCacheCnt++;
 92049  92873         sqlite3ExprCachePinRegister(pParse, p->iReg);
 92050  92874         return p->iReg;
 92051  92875       }
 92052  92876     }  
 92053  92877     assert( v!=0 );
 92054  92878     sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
................................................................................
 92072  92896   
 92073  92897   
 92074  92898   /*
 92075  92899   ** Clear all column cache entries.
 92076  92900   */
 92077  92901   SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
 92078  92902     int i;
 92079         -  struct yColCache *p;
 92080  92903   
 92081  92904   #if SQLITE_DEBUG
 92082  92905     if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
 92083  92906       printf("CLEAR\n");
 92084  92907     }
 92085  92908   #endif
 92086         -  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 92087         -    if( p->iReg ){
 92088         -      cacheEntryClear(pParse, p);
        92909  +  for(i=0; i<pParse->nColCache; i++){
        92910  +    if( pParse->aColCache[i].tempReg
        92911  +     && pParse->nTempReg<ArraySize(pParse->aTempReg)
        92912  +    ){
        92913  +       pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
 92089  92914       }
 92090  92915     }
        92916  +  pParse->nColCache = 0;
 92091  92917   }
 92092  92918   
 92093  92919   /*
 92094  92920   ** Record the fact that an affinity change has occurred on iCount
 92095  92921   ** registers starting with iStart.
 92096  92922   */
 92097  92923   SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
................................................................................
 92115  92941   **
 92116  92942   ** This routine is used within assert() and testcase() macros only
 92117  92943   ** and does not appear in a normal build.
 92118  92944   */
 92119  92945   static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
 92120  92946     int i;
 92121  92947     struct yColCache *p;
 92122         -  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
        92948  +  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
 92123  92949       int r = p->iReg;
 92124  92950       if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
 92125  92951     }
 92126  92952     return 0;
 92127  92953   }
 92128  92954   #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
 92129  92955   
 92130  92956   
 92131  92957   /*
 92132         -** Convert an expression node to a TK_REGISTER
        92958  +** Convert a scalar expression node to a TK_REGISTER referencing
        92959  +** register iReg.  The caller must ensure that iReg already contains
        92960  +** the correct value for the expression.
 92133  92961   */
 92134  92962   static void exprToRegister(Expr *p, int iReg){
 92135  92963     p->op2 = p->op;
 92136  92964     p->op = TK_REGISTER;
 92137  92965     p->iTable = iReg;
 92138  92966     ExprClearProperty(p, EP_Skip);
 92139  92967   }
        92968  +
        92969  +/*
        92970  +** Evaluate an expression (either a vector or a scalar expression) and store
        92971  +** the result in continguous temporary registers.  Return the index of
        92972  +** the first register used to store the result.
        92973  +**
        92974  +** If the returned result register is a temporary scalar, then also write
        92975  +** that register number into *piFreeable.  If the returned result register
        92976  +** is not a temporary or if the expression is a vector set *piFreeable
        92977  +** to 0.
        92978  +*/
        92979  +static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
        92980  +  int iResult;
        92981  +  int nResult = sqlite3ExprVectorSize(p);
        92982  +  if( nResult==1 ){
        92983  +    iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
        92984  +  }else{
        92985  +    *piFreeable = 0;
        92986  +    if( p->op==TK_SELECT ){
        92987  +      iResult = sqlite3CodeSubselect(pParse, p, 0, 0);
        92988  +    }else{
        92989  +      int i;
        92990  +      iResult = pParse->nMem+1;
        92991  +      pParse->nMem += nResult;
        92992  +      for(i=0; i<nResult; i++){
        92993  +        sqlite3ExprCode(pParse, p->x.pList->a[i].pExpr, i+iResult);
        92994  +      }
        92995  +    }
        92996  +  }
        92997  +  return iResult;
        92998  +}
        92999  +
 92140  93000   
 92141  93001   /*
 92142  93002   ** Generate code into the current Vdbe to evaluate the given
 92143  93003   ** expression.  Attempt to store the results in register "target".
 92144  93004   ** Return the register where results are stored.
 92145  93005   **
 92146  93006   ** With this routine, there is no guarantee that results will
................................................................................
 92151  93011   */
 92152  93012   SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
 92153  93013     Vdbe *v = pParse->pVdbe;  /* The VM under construction */
 92154  93014     int op;                   /* The opcode being coded */
 92155  93015     int inReg = target;       /* Results stored in register inReg */
 92156  93016     int regFree1 = 0;         /* If non-zero free this temporary register */
 92157  93017     int regFree2 = 0;         /* If non-zero free this temporary register */
 92158         -  int r1, r2, r3, r4;       /* Various register numbers */
 92159         -  sqlite3 *db = pParse->db; /* The database connection */
        93018  +  int r1, r2;               /* Various register numbers */
 92160  93019     Expr tempX;               /* Temporary expression node */
        93020  +  int p5 = 0;
 92161  93021   
 92162  93022     assert( target>0 && target<=pParse->nMem );
 92163  93023     if( v==0 ){
 92164  93024       assert( pParse->db->mallocFailed );
 92165  93025       return 0;
 92166  93026     }
 92167  93027   
................................................................................
 92172  93032     }
 92173  93033     switch( op ){
 92174  93034       case TK_AGG_COLUMN: {
 92175  93035         AggInfo *pAggInfo = pExpr->pAggInfo;
 92176  93036         struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
 92177  93037         if( !pAggInfo->directMode ){
 92178  93038           assert( pCol->iMem>0 );
 92179         -        inReg = pCol->iMem;
 92180         -        break;
        93039  +        return pCol->iMem;
 92181  93040         }else if( pAggInfo->useSortingIdx ){
 92182  93041           sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
 92183  93042                                 pCol->iSorterColumn, target);
 92184         -        break;
        93043  +        return target;
 92185  93044         }
 92186  93045         /* Otherwise, fall thru into the TK_COLUMN case */
 92187  93046       }
 92188  93047       case TK_COLUMN: {
 92189  93048         int iTab = pExpr->iTable;
 92190  93049         if( iTab<0 ){
 92191  93050           if( pParse->ckBase>0 ){
 92192  93051             /* Generating CHECK constraints or inserting into partial index */
 92193         -          inReg = pExpr->iColumn + pParse->ckBase;
 92194         -          break;
        93052  +          return pExpr->iColumn + pParse->ckBase;
 92195  93053           }else{
 92196  93054             /* Coding an expression that is part of an index where column names
 92197  93055             ** in the index refer to the table to which the index belongs */
 92198  93056             iTab = pParse->iSelfTab;
 92199  93057           }
 92200  93058         }
 92201         -      inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
        93059  +      return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
 92202  93060                                  pExpr->iColumn, iTab, target,
 92203  93061                                  pExpr->op2);
 92204         -      break;
 92205  93062       }
 92206  93063       case TK_INTEGER: {
 92207  93064         codeInteger(pParse, pExpr, 0, target);
 92208         -      break;
        93065  +      return target;
 92209  93066       }
 92210  93067   #ifndef SQLITE_OMIT_FLOATING_POINT
 92211  93068       case TK_FLOAT: {
 92212  93069         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 92213  93070         codeReal(v, pExpr->u.zToken, 0, target);
 92214         -      break;
        93071  +      return target;
 92215  93072       }
 92216  93073   #endif
 92217  93074       case TK_STRING: {
 92218  93075         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 92219  93076         sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
 92220         -      break;
        93077  +      return target;
 92221  93078       }
 92222  93079       case TK_NULL: {
 92223  93080         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 92224         -      break;
        93081  +      return target;
 92225  93082       }
 92226  93083   #ifndef SQLITE_OMIT_BLOB_LITERAL
 92227  93084       case TK_BLOB: {
 92228  93085         int n;
 92229  93086         const char *z;
 92230  93087         char *zBlob;
 92231  93088         assert( !ExprHasProperty(pExpr, EP_IntValue) );
................................................................................
 92232  93089         assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
 92233  93090         assert( pExpr->u.zToken[1]=='\'' );
 92234  93091         z = &pExpr->u.zToken[2];
 92235  93092         n = sqlite3Strlen30(z) - 1;
 92236  93093         assert( z[n]=='\'' );
 92237  93094         zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
 92238  93095         sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
 92239         -      break;
        93096  +      return target;
 92240  93097       }
 92241  93098   #endif
 92242  93099       case TK_VARIABLE: {
 92243  93100         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 92244  93101         assert( pExpr->u.zToken!=0 );
 92245  93102         assert( pExpr->u.zToken[0]!=0 );
 92246  93103         sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
 92247  93104         if( pExpr->u.zToken[1]!=0 ){
 92248  93105           assert( pExpr->u.zToken[0]=='?' 
 92249  93106                || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
 92250  93107           sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
 92251  93108         }
 92252         -      break;
        93109  +      return target;
 92253  93110       }
 92254  93111       case TK_REGISTER: {
 92255         -      inReg = pExpr->iTable;
 92256         -      break;
        93112  +      return pExpr->iTable;
 92257  93113       }
 92258  93114   #ifndef SQLITE_OMIT_CAST
 92259  93115       case TK_CAST: {
 92260  93116         /* Expressions of the form:   CAST(pLeft AS token) */
 92261  93117         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 92262  93118         if( inReg!=target ){
 92263  93119           sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
 92264  93120           inReg = target;
 92265  93121         }
 92266  93122         sqlite3VdbeAddOp2(v, OP_Cast, target,
 92267  93123                           sqlite3AffinityType(pExpr->u.zToken, 0));
 92268  93124         testcase( usedAsColumnCache(pParse, inReg, inReg) );
 92269  93125         sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
 92270         -      break;
        93126  +      return inReg;
 92271  93127       }
 92272  93128   #endif /* SQLITE_OMIT_CAST */
        93129  +    case TK_IS:
        93130  +    case TK_ISNOT:
        93131  +      op = (op==TK_IS) ? TK_EQ : TK_NE;
        93132  +      p5 = SQLITE_NULLEQ;
        93133  +      /* fall-through */
 92273  93134       case TK_LT:
 92274  93135       case TK_LE:
 92275  93136       case TK_GT:
 92276  93137       case TK_GE:
 92277  93138       case TK_NE:
 92278  93139       case TK_EQ: {
 92279         -      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 92280         -      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 92281         -      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 92282         -                  r1, r2, inReg, SQLITE_STOREP2);
 92283         -      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
 92284         -      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
 92285         -      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
 92286         -      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
 92287         -      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
 92288         -      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
 92289         -      testcase( regFree1==0 );
 92290         -      testcase( regFree2==0 );
 92291         -      break;
 92292         -    }
 92293         -    case TK_IS:
 92294         -    case TK_ISNOT: {
 92295         -      testcase( op==TK_IS );
 92296         -      testcase( op==TK_ISNOT );
 92297         -      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 92298         -      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 92299         -      op = (op==TK_IS) ? TK_EQ : TK_NE;
 92300         -      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 92301         -                  r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
 92302         -      VdbeCoverageIf(v, op==TK_EQ);
 92303         -      VdbeCoverageIf(v, op==TK_NE);
 92304         -      testcase( regFree1==0 );
 92305         -      testcase( regFree2==0 );
        93140  +      Expr *pLeft = pExpr->pLeft;
        93141  +      if( sqlite3ExprIsVector(pLeft) ){
        93142  +        codeVectorCompare(pParse, pExpr, target, op, p5);
        93143  +      }else{
        93144  +        r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
        93145  +        r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
        93146  +        codeCompare(pParse, pLeft, pExpr->pRight, op,
        93147  +            r1, r2, inReg, SQLITE_STOREP2 | p5);
        93148  +        assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
        93149  +        assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
        93150  +        assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
        93151  +        assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
        93152  +        assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
        93153  +        assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
        93154  +        testcase( regFree1==0 );
        93155  +        testcase( regFree2==0 );
        93156  +      }
 92306  93157         break;
 92307  93158       }
 92308  93159       case TK_AND:
 92309  93160       case TK_OR:
 92310  93161       case TK_PLUS:
 92311  93162       case TK_STAR:
 92312  93163       case TK_MINUS:
................................................................................
 92336  93187         break;
 92337  93188       }
 92338  93189       case TK_UMINUS: {
 92339  93190         Expr *pLeft = pExpr->pLeft;
 92340  93191         assert( pLeft );
 92341  93192         if( pLeft->op==TK_INTEGER ){
 92342  93193           codeInteger(pParse, pLeft, 1, target);
        93194  +        return target;
 92343  93195   #ifndef SQLITE_OMIT_FLOATING_POINT
 92344  93196         }else if( pLeft->op==TK_FLOAT ){
 92345  93197           assert( !ExprHasProperty(pExpr, EP_IntValue) );
 92346  93198           codeReal(v, pLeft->u.zToken, 1, target);
        93199  +        return target;
 92347  93200   #endif
 92348  93201         }else{
 92349  93202           tempX.op = TK_INTEGER;
 92350  93203           tempX.flags = EP_IntValue|EP_TokenOnly;
 92351  93204           tempX.u.iValue = 0;
 92352  93205           r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
 92353  93206           r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
 92354  93207           sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
 92355  93208           testcase( regFree2==0 );
 92356  93209         }
 92357         -      inReg = target;
 92358  93210         break;
 92359  93211       }
 92360  93212       case TK_BITNOT:
 92361  93213       case TK_NOT: {
 92362  93214         assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
 92363  93215         assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
 92364  93216         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 92365  93217         testcase( regFree1==0 );
 92366         -      inReg = target;
 92367  93218         sqlite3VdbeAddOp2(v, op, r1, inReg);
 92368  93219         break;
 92369  93220       }
 92370  93221       case TK_ISNULL:
 92371  93222       case TK_NOTNULL: {
 92372  93223         int addr;
 92373  93224         assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
................................................................................
 92384  93235       }
 92385  93236       case TK_AGG_FUNCTION: {
 92386  93237         AggInfo *pInfo = pExpr->pAggInfo;
 92387  93238         if( pInfo==0 ){
 92388  93239           assert( !ExprHasProperty(pExpr, EP_IntValue) );
 92389  93240           sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
 92390  93241         }else{
 92391         -        inReg = pInfo->aFunc[pExpr->iAgg].iMem;
        93242  +        return pInfo->aFunc[pExpr->iAgg].iMem;
 92392  93243         }
 92393  93244         break;
 92394  93245       }
 92395  93246       case TK_FUNCTION: {
 92396  93247         ExprList *pFarg;       /* List of function arguments */
 92397  93248         int nFarg;             /* Number of function arguments */
 92398  93249         FuncDef *pDef;         /* The function definition object */
 92399  93250         const char *zId;       /* The function name */
 92400  93251         u32 constMask = 0;     /* Mask of function arguments that are constant */
 92401  93252         int i;                 /* Loop counter */
        93253  +      sqlite3 *db = pParse->db;  /* The database connection */
 92402  93254         u8 enc = ENC(db);      /* The text encoding used by this database */
 92403  93255         CollSeq *pColl = 0;    /* A collating sequence */
 92404  93256   
 92405  93257         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 92406  93258         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
 92407  93259           pFarg = 0;
 92408  93260         }else{
................................................................................
 92443  93295         }
 92444  93296   
 92445  93297         /* The UNLIKELY() function is a no-op.  The result is the value
 92446  93298         ** of the first argument.
 92447  93299         */
 92448  93300         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
 92449  93301           assert( nFarg>=1 );
 92450         -        inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
 92451         -        break;
        93302  +        return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
 92452  93303         }
 92453  93304   
 92454  93305         for(i=0; i<nFarg; i++){
 92455  93306           if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
 92456  93307             testcase( i==31 );
 92457  93308             constMask |= MASKBIT32(i);
 92458  93309           }
................................................................................
 92519  93370         }
 92520  93371         sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
 92521  93372                           (char*)pDef, P4_FUNCDEF);
 92522  93373         sqlite3VdbeChangeP5(v, (u8)nFarg);
 92523  93374         if( nFarg && constMask==0 ){
 92524  93375           sqlite3ReleaseTempRange(pParse, r1, nFarg);
 92525  93376         }
 92526         -      break;
        93377  +      return target;
 92527  93378       }
 92528  93379   #ifndef SQLITE_OMIT_SUBQUERY
 92529  93380       case TK_EXISTS:
 92530  93381       case TK_SELECT: {
        93382  +      int nCol;
 92531  93383         testcase( op==TK_EXISTS );
 92532  93384         testcase( op==TK_SELECT );
 92533         -      inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
        93385  +      if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
        93386  +        sqlite3SubselectError(pParse, nCol, 1);
        93387  +      }else{
        93388  +        return sqlite3CodeSubselect(pParse, pExpr, 0, 0);
        93389  +      }
 92534  93390         break;
 92535  93391       }
        93392  +    case TK_SELECT_COLUMN: {
        93393  +      if( pExpr->pLeft->iTable==0 ){
        93394  +        pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft, 0, 0);
        93395  +      }
        93396  +      return pExpr->pLeft->iTable + pExpr->iColumn;
        93397  +    }
 92536  93398       case TK_IN: {
 92537  93399         int destIfFalse = sqlite3VdbeMakeLabel(v);
 92538  93400         int destIfNull = sqlite3VdbeMakeLabel(v);
 92539  93401         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 92540  93402         sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 92541  93403         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
 92542  93404         sqlite3VdbeResolveLabel(v, destIfFalse);
 92543  93405         sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
 92544  93406         sqlite3VdbeResolveLabel(v, destIfNull);
 92545         -      break;
        93407  +      return target;
 92546  93408       }
 92547  93409   #endif /* SQLITE_OMIT_SUBQUERY */
 92548  93410   
 92549  93411   
 92550  93412       /*
 92551  93413       **    x BETWEEN y AND z
 92552  93414       **
................................................................................
 92555  93417       **    x>=y AND x<=z
 92556  93418       **
 92557  93419       ** X is stored in pExpr->pLeft.
 92558  93420       ** Y is stored in pExpr->pList->a[0].pExpr.
 92559  93421       ** Z is stored in pExpr->pList->a[1].pExpr.
 92560  93422       */
 92561  93423       case TK_BETWEEN: {
 92562         -      Expr *pLeft = pExpr->pLeft;
 92563         -      struct ExprList_item *pLItem = pExpr->x.pList->a;
 92564         -      Expr *pRight = pLItem->pExpr;
 92565         -
 92566         -      r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
 92567         -      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 92568         -      testcase( regFree1==0 );
 92569         -      testcase( regFree2==0 );
 92570         -      r3 = sqlite3GetTempReg(pParse);
 92571         -      r4 = sqlite3GetTempReg(pParse);
 92572         -      codeCompare(pParse, pLeft, pRight, OP_Ge,
 92573         -                  r1, r2, r3, SQLITE_STOREP2);  VdbeCoverage(v);
 92574         -      pLItem++;
 92575         -      pRight = pLItem->pExpr;
 92576         -      sqlite3ReleaseTempReg(pParse, regFree2);
 92577         -      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 92578         -      testcase( regFree2==0 );
 92579         -      codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
 92580         -      VdbeCoverage(v);
 92581         -      sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
 92582         -      sqlite3ReleaseTempReg(pParse, r3);
 92583         -      sqlite3ReleaseTempReg(pParse, r4);
 92584         -      break;
        93424  +      exprCodeBetween(pParse, pExpr, target, 0, 0);
        93425  +      return target;
 92585  93426       }
 92586  93427       case TK_SPAN:
 92587  93428       case TK_COLLATE: 
 92588  93429       case TK_UPLUS: {
 92589         -      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 92590         -      break;
        93430  +      return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 92591  93431       }
 92592  93432   
 92593  93433       case TK_TRIGGER: {
 92594  93434         /* If the opcode is TK_TRIGGER, then the expression is a reference
 92595  93435         ** to a column in the new.* or old.* pseudo-tables available to
 92596  93436         ** trigger programs. In this case Expr.iTable is set to 1 for the
 92597  93437         ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
................................................................................
 92642  93482         ){
 92643  93483           sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
 92644  93484         }
 92645  93485   #endif
 92646  93486         break;
 92647  93487       }
 92648  93488   
        93489  +    case TK_VECTOR: {
        93490  +      sqlite3ErrorMsg(pParse, "row value misused");
        93491  +      break;
        93492  +    }
 92649  93493   
 92650  93494       /*
 92651  93495       ** Form A:
 92652  93496       **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
 92653  93497       **
 92654  93498       ** Form B:
 92655  93499       **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
................................................................................
 92685  93529         pEList = pExpr->x.pList;
 92686  93530         aListelem = pEList->a;
 92687  93531         nExpr = pEList->nExpr;
 92688  93532         endLabel = sqlite3VdbeMakeLabel(v);
 92689  93533         if( (pX = pExpr->pLeft)!=0 ){
 92690  93534           tempX = *pX;
 92691  93535           testcase( pX->op==TK_COLUMN );
 92692         -        exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
        93536  +        exprToRegister(&tempX, exprCodeVector(pParse, &tempX, &regFree1));
 92693  93537           testcase( regFree1==0 );
        93538  +        memset(&opCompare, 0, sizeof(opCompare));
 92694  93539           opCompare.op = TK_EQ;
 92695  93540           opCompare.pLeft = &tempX;
 92696  93541           pTest = &opCompare;
 92697  93542           /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
 92698  93543           ** The value in regFree1 might get SCopy-ed into the file result.
 92699  93544           ** So make sure that the regFree1 register is not reused for other
 92700  93545           ** purposes and possibly overwritten.  */
................................................................................
 92720  93565         if( (nExpr&1)!=0 ){
 92721  93566           sqlite3ExprCachePush(pParse);
 92722  93567           sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
 92723  93568           sqlite3ExprCachePop(pParse);
 92724  93569         }else{
 92725  93570           sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 92726  93571         }
 92727         -      assert( db->mallocFailed || pParse->nErr>0 
        93572  +      assert( pParse->db->mallocFailed || pParse->nErr>0 
 92728  93573              || pParse->iCacheLevel==iCacheLevel );
 92729  93574         sqlite3VdbeResolveLabel(v, endLabel);
 92730  93575         break;
 92731  93576       }
 92732  93577   #ifndef SQLITE_OMIT_TRIGGER
 92733  93578       case TK_RAISE: {
 92734  93579         assert( pExpr->affinity==OE_Rollback 
................................................................................
 92965  93810   **
 92966  93811   ** The above is equivalent to 
 92967  93812   **
 92968  93813   **    x>=y AND x<=z
 92969  93814   **
 92970  93815   ** Code it as such, taking care to do the common subexpression
 92971  93816   ** elimination of x.
        93817  +**
        93818  +** The xJumpIf parameter determines details:
        93819  +**
        93820  +**    NULL:                   Store the boolean result in reg[dest]
        93821  +**    sqlite3ExprIfTrue:      Jump to dest if true
        93822  +**    sqlite3ExprIfFalse:     Jump to dest if false
        93823  +**
        93824  +** The jumpIfNull parameter is ignored if xJumpIf is NULL.
 92972  93825   */
 92973  93826   static void exprCodeBetween(
 92974  93827     Parse *pParse,    /* Parsing and code generating context */
 92975  93828     Expr *pExpr,      /* The BETWEEN expression */
 92976         -  int dest,         /* Jump here if the jump is taken */
 92977         -  int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
        93829  +  int dest,         /* Jump destination or storage location */
        93830  +  void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
 92978  93831     int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
 92979  93832   ){
 92980         -  Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
        93833  + Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
 92981  93834     Expr compLeft;    /* The  x>=y  term */
 92982  93835     Expr compRight;   /* The  x<=z  term */
 92983  93836     Expr exprX;       /* The  x  subexpression */
 92984  93837     int regFree1 = 0; /* Temporary use register */
 92985  93838   
        93839  +
        93840  +  memset(&compLeft, 0, sizeof(Expr));
        93841  +  memset(&compRight, 0, sizeof(Expr));
        93842  +  memset(&exprAnd, 0, sizeof(Expr));
        93843  +
 92986  93844     assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 92987  93845     exprX = *pExpr->pLeft;
 92988  93846     exprAnd.op = TK_AND;
 92989  93847     exprAnd.pLeft = &compLeft;
 92990  93848     exprAnd.pRight = &compRight;
 92991  93849     compLeft.op = TK_GE;
 92992  93850     compLeft.pLeft = &exprX;
 92993  93851     compLeft.pRight = pExpr->x.pList->a[0].pExpr;
 92994  93852     compRight.op = TK_LE;
 92995  93853     compRight.pLeft = &exprX;
 92996  93854     compRight.pRight = pExpr->x.pList->a[1].pExpr;
 92997         -  exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
 92998         -  if( jumpIfTrue ){
 92999         -    sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
        93855  +  exprToRegister(&exprX, exprCodeVector(pParse, &exprX, &regFree1));
        93856  +  if( xJump ){
        93857  +    xJump(pParse, &exprAnd, dest, jumpIfNull);
 93000  93858     }else{
 93001         -    sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
        93859  +    exprX.flags |= EP_FromJoin;
        93860  +    sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
 93002  93861     }
 93003  93862     sqlite3ReleaseTempReg(pParse, regFree1);
 93004  93863   
 93005  93864     /* Ensure adequate test coverage */
 93006         -  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
 93007         -  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
 93008         -  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
 93009         -  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
 93010         -  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
 93011         -  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
 93012         -  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
 93013         -  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
        93865  +  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1==0 );
        93866  +  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1!=0 );
        93867  +  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1==0 );
        93868  +  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1!=0 );
        93869  +  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
        93870  +  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
        93871  +  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
        93872  +  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
        93873  +  testcase( xJump==0 );
 93014  93874   }
 93015  93875   
 93016  93876   /*
 93017  93877   ** Generate code for a boolean expression such that a jump is made
 93018  93878   ** to the label "dest" if the expression is true but execution
 93019  93879   ** continues straight thru if the expression is false.
 93020  93880   **
................................................................................
 93071  93931         /* Fall thru */
 93072  93932       case TK_LT:
 93073  93933       case TK_LE:
 93074  93934       case TK_GT:
 93075  93935       case TK_GE:
 93076  93936       case TK_NE:
 93077  93937       case TK_EQ: {
        93938  +      if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
 93078  93939         testcase( jumpIfNull==0 );
 93079  93940         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 93080  93941         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 93081  93942         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 93082  93943                     r1, r2, dest, jumpIfNull);
 93083  93944         assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
 93084  93945         assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
................................................................................
 93103  93964         VdbeCoverageIf(v, op==TK_ISNULL);
 93104  93965         VdbeCoverageIf(v, op==TK_NOTNULL);
 93105  93966         testcase( regFree1==0 );
 93106  93967         break;
 93107  93968       }
 93108  93969       case TK_BETWEEN: {
 93109  93970         testcase( jumpIfNull==0 );
 93110         -      exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
        93971  +      exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
 93111  93972         break;
 93112  93973       }
 93113  93974   #ifndef SQLITE_OMIT_SUBQUERY
 93114  93975       case TK_IN: {
 93115  93976         int destIfFalse = sqlite3VdbeMakeLabel(v);
 93116  93977         int destIfNull = jumpIfNull ? dest : destIfFalse;
 93117  93978         sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 93118  93979         sqlite3VdbeGoto(v, dest);
 93119  93980         sqlite3VdbeResolveLabel(v, destIfFalse);
 93120  93981         break;
 93121  93982       }
 93122  93983   #endif
 93123  93984       default: {
        93985  +    default_expr:
 93124  93986         if( exprAlwaysTrue(pExpr) ){
 93125  93987           sqlite3VdbeGoto(v, dest);
 93126  93988         }else if( exprAlwaysFalse(pExpr) ){
 93127  93989           /* No-op */
 93128  93990         }else{
 93129  93991           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 93130  93992           sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
................................................................................
 93223  94085         /* Fall thru */
 93224  94086       case TK_LT:
 93225  94087       case TK_LE:
 93226  94088       case TK_GT:
 93227  94089       case TK_GE:
 93228  94090       case TK_NE:
 93229  94091       case TK_EQ: {
        94092  +      if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
 93230  94093         testcase( jumpIfNull==0 );
 93231  94094         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 93232  94095         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 93233  94096         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 93234  94097                     r1, r2, dest, jumpIfNull);
 93235  94098         assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
 93236  94099         assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
................................................................................
 93253  94116         testcase( op==TK_ISNULL );   VdbeCoverageIf(v, op==TK_ISNULL);
 93254  94117         testcase( op==TK_NOTNULL );  VdbeCoverageIf(v, op==TK_NOTNULL);
 93255  94118         testcase( regFree1==0 );
 93256  94119         break;
 93257  94120       }
 93258  94121       case TK_BETWEEN: {
 93259  94122         testcase( jumpIfNull==0 );
 93260         -      exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
        94123  +      exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
 93261  94124         break;
 93262  94125       }
 93263  94126   #ifndef SQLITE_OMIT_SUBQUERY
 93264  94127       case TK_IN: {
 93265  94128         if( jumpIfNull ){
 93266  94129           sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
 93267  94130         }else{
................................................................................
 93269  94132           sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
 93270  94133           sqlite3VdbeResolveLabel(v, destIfNull);
 93271  94134         }
 93272  94135         break;
 93273  94136       }
 93274  94137   #endif
 93275  94138       default: {
        94139  +    default_expr: 
 93276  94140         if( exprAlwaysFalse(pExpr) ){
 93277  94141           sqlite3VdbeGoto(v, dest);
 93278  94142         }else if( exprAlwaysTrue(pExpr) ){
 93279  94143           /* no-op */
 93280  94144         }else{
 93281  94145           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 93282  94146           sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
................................................................................
 93773  94637   ** the deallocation is deferred until the column cache line that uses
 93774  94638   ** the register becomes stale.
 93775  94639   */
 93776  94640   SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
 93777  94641     if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
 93778  94642       int i;
 93779  94643       struct yColCache *p;
 93780         -    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
        94644  +    for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
 93781  94645         if( p->iReg==iReg ){
 93782  94646           p->tempReg = 1;
 93783  94647           return;
 93784  94648         }
 93785  94649       }
 93786  94650       pParse->aTempReg[pParse->nTempReg++] = iReg;
 93787  94651     }
 93788  94652   }
 93789  94653   
 93790  94654   /*
 93791         -** Allocate or deallocate a block of nReg consecutive registers
        94655  +** Allocate or deallocate a block of nReg consecutive registers.
 93792  94656   */
 93793  94657   SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
 93794  94658     int i, n;
        94659  +  if( nReg==1 ) return sqlite3GetTempReg(pParse);
 93795  94660     i = pParse->iRangeReg;
 93796  94661     n = pParse->nRangeReg;
 93797  94662     if( nReg<=n ){
 93798  94663       assert( !usedAsColumnCache(pParse, i, i+n-1) );
 93799  94664       pParse->iRangeReg += nReg;
 93800  94665       pParse->nRangeReg -= nReg;
 93801  94666     }else{
 93802  94667       i = pParse->nMem+1;
 93803  94668       pParse->nMem += nReg;
 93804  94669     }
 93805  94670     return i;
 93806  94671   }
 93807  94672   SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
        94673  +  if( nReg==1 ){
        94674  +    sqlite3ReleaseTempReg(pParse, iReg);
        94675  +    return;
        94676  +  }
 93808  94677     sqlite3ExprCacheRemove(pParse, iReg, nReg);
 93809  94678     if( nReg>pParse->nRangeReg ){
 93810  94679       pParse->nRangeReg = nReg;
 93811  94680       pParse->iRangeReg = iReg;
 93812  94681     }
 93813  94682   }
 93814  94683   
................................................................................
 94256  95125     if( NEVER(db->mallocFailed) ) goto exit_rename_table;
 94257  95126     assert( pSrc->nSrc==1 );
 94258  95127     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 94259  95128   
 94260  95129     pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 94261  95130     if( !pTab ) goto exit_rename_table;
 94262  95131     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 94263         -  zDb = db->aDb[iDb].zName;
        95132  +  zDb = db->aDb[iDb].zDbSName;
 94264  95133     db->flags |= SQLITE_PreferBuiltin;
 94265  95134   
 94266  95135     /* Get a NULL terminated version of the new table name. */
 94267  95136     zName = sqlite3NameFromToken(db, pName);
 94268  95137     if( !zName ) goto exit_rename_table;
 94269  95138   
 94270  95139     /* Check that a table or index named 'zName' does not already exist
................................................................................
 94454  95323     if( pParse->nErr || db->mallocFailed ) return;
 94455  95324     assert( v!=0 );
 94456  95325     pNew = pParse->pNewTable;
 94457  95326     assert( pNew );
 94458  95327   
 94459  95328     assert( sqlite3BtreeHoldsAllMutexes(db) );
 94460  95329     iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
 94461         -  zDb = db->aDb[iDb].zName;
        95330  +  zDb = db->aDb[iDb].zDbSName;
 94462  95331     zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
 94463  95332     pCol = &pNew->aCol[pNew->nCol-1];
 94464  95333     pDflt = pCol->pDflt;
 94465  95334     pTab = sqlite3FindTable(db, zTab, zDb);
 94466  95335     assert( pTab );
 94467  95336   
 94468  95337   #ifndef SQLITE_OMIT_AUTHORIZATION
................................................................................
 94864  95733   
 94865  95734     /* Create new statistic tables if they do not exist, or clear them
 94866  95735     ** if they do already exist.
 94867  95736     */
 94868  95737     for(i=0; i<ArraySize(aTable); i++){
 94869  95738       const char *zTab = aTable[i].zName;
 94870  95739       Table *pStat;
 94871         -    if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
        95740  +    if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
 94872  95741         if( aTable[i].zCols ){
 94873  95742           /* The sqlite_statN table does not exist. Create it. Note that a 
 94874  95743           ** side-effect of the CREATE TABLE statement is to leave the rootpage 
 94875  95744           ** of the new table in register pParse->regRoot. This is important 
 94876  95745           ** because the OpenWrite opcode below will be needing it. */
 94877  95746           sqlite3NestedParse(pParse,
 94878         -            "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
        95747  +            "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
 94879  95748           );
 94880  95749           aRoot[i] = pParse->regRoot;
 94881  95750           aCreateTbl[i] = OPFLAG_P2ISREG;
 94882  95751         }
 94883  95752       }else{
 94884  95753         /* The table already exists. If zWhere is not NULL, delete all entries 
 94885  95754         ** associated with the table zWhere. If zWhere is NULL, delete the
................................................................................
 94886  95755         ** entire contents of the table. */
 94887  95756         aRoot[i] = pStat->tnum;
 94888  95757         aCreateTbl[i] = 0;
 94889  95758         sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
 94890  95759         if( zWhere ){
 94891  95760           sqlite3NestedParse(pParse,
 94892  95761              "DELETE FROM %Q.%s WHERE %s=%Q",
 94893         -           pDb->zName, zTab, zWhereType, zWhere
        95762  +           pDb->zDbSName, zTab, zWhereType, zWhere
 94894  95763           );
 94895  95764         }else{
 94896  95765           /* The sqlite_stat[134] table already exists.  Delete all rows. */
 94897  95766           sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
 94898  95767         }
 94899  95768       }
 94900  95769     }
................................................................................
 95648  96517     }
 95649  96518     assert( sqlite3BtreeHoldsAllMutexes(db) );
 95650  96519     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 95651  96520     assert( iDb>=0 );
 95652  96521     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 95653  96522   #ifndef SQLITE_OMIT_AUTHORIZATION
 95654  96523     if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
 95655         -      db->aDb[iDb].zName ) ){
        96524  +      db->aDb[iDb].zDbSName ) ){
 95656  96525       return;
 95657  96526     }
 95658  96527   #endif
 95659  96528   
 95660  96529     /* Establish a read-lock on the table at the shared-cache level. 
 95661  96530     ** Open a read-only cursor on the table. Also allocate a cursor number
 95662  96531     ** to use for scanning indexes (iIdxCur). No index cursor is opened at
................................................................................
 96038  96907           sqlite3DbFree(db, z);
 96039  96908         }
 96040  96909       }
 96041  96910     }else{
 96042  96911       /* Form 3: Analyze the fully qualified table name */
 96043  96912       iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
 96044  96913       if( iDb>=0 ){
 96045         -      zDb = db->aDb[iDb].zName;
        96914  +      zDb = db->aDb[iDb].zDbSName;
 96046  96915         z = sqlite3NameFromToken(db, pTableName);
 96047  96916         if( z ){
 96048  96917           if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
 96049  96918             analyzeTable(pParse, pIdx->pTable, pIdx);
 96050  96919           }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
 96051  96920             analyzeTable(pParse, pTab, 0);
 96052  96921           }
................................................................................
 96498  97367       sqlite3DeleteIndexSamples(db, pIdx);
 96499  97368       pIdx->aSample = 0;
 96500  97369   #endif
 96501  97370     }
 96502  97371   
 96503  97372     /* Load new statistics out of the sqlite_stat1 table */
 96504  97373     sInfo.db = db;
 96505         -  sInfo.zDatabase = db->aDb[iDb].zName;
        97374  +  sInfo.zDatabase = db->aDb[iDb].zDbSName;
 96506  97375     if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
 96507  97376       zSql = sqlite3MPrintf(db, 
 96508  97377           "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
 96509  97378       if( zSql==0 ){
 96510  97379         rc = SQLITE_NOMEM_BKPT;
 96511  97380       }else{
 96512  97381         rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
................................................................................
 96641  97510       goto attach_error;
 96642  97511     }
 96643  97512     if( !db->autoCommit ){
 96644  97513       zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
 96645  97514       goto attach_error;
 96646  97515     }
 96647  97516     for(i=0; i<db->nDb; i++){
 96648         -    char *z = db->aDb[i].zName;
        97517  +    char *z = db->aDb[i].zDbSName;
 96649  97518       assert( z && zName );
 96650  97519       if( sqlite3StrICmp(z, zName)==0 ){
 96651  97520         zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
 96652  97521         goto attach_error;
 96653  97522       }
 96654  97523     }
 96655  97524   
................................................................................
 96706  97575   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 96707  97576       sqlite3BtreeSetPagerFlags(aNew->pBt,
 96708  97577                         PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
 96709  97578   #endif
 96710  97579       sqlite3BtreeLeave(aNew->pBt);
 96711  97580     }
 96712  97581     aNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
 96713         -  aNew->zName = sqlite3DbStrDup(db, zName);
 96714         -  if( rc==SQLITE_OK && aNew->zName==0 ){
        97582  +  aNew->zDbSName = sqlite3DbStrDup(db, zName);
        97583  +  if( rc==SQLITE_OK && aNew->zDbSName==0 ){
 96715  97584       rc = SQLITE_NOMEM_BKPT;
 96716  97585     }
 96717  97586   
 96718  97587   
 96719  97588   #ifdef SQLITE_HAS_CODEC
 96720  97589     if( rc==SQLITE_OK ){
 96721  97590       extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
................................................................................
 96736  97605           zKey = (char *)sqlite3_value_blob(argv[2]);
 96737  97606           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 96738  97607           break;
 96739  97608   
 96740  97609         case SQLITE_NULL:
 96741  97610           /* No key specified.  Use the key from the main database */
 96742  97611           sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
 96743         -        if( nKey>0 || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
        97612  +        if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
 96744  97613             rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 96745  97614           }
 96746  97615           break;
 96747  97616       }
 96748  97617     }
 96749  97618   #endif
 96750  97619   
................................................................................
 96819  97688   
 96820  97689     UNUSED_PARAMETER(NotUsed);
 96821  97690   
 96822  97691     if( zName==0 ) zName = "";
 96823  97692     for(i=0; i<db->nDb; i++){
 96824  97693       pDb = &db->aDb[i];
 96825  97694       if( pDb->pBt==0 ) continue;
 96826         -    if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
        97695  +    if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
 96827  97696     }
 96828  97697   
 96829  97698     if( i>=db->nDb ){
 96830  97699       sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
 96831  97700       goto detach_error;
 96832  97701     }
 96833  97702     if( i<2 ){
................................................................................
 96977  97846     const Token *pName  /* Name of the view, trigger, or index */
 96978  97847   ){
 96979  97848     sqlite3 *db;
 96980  97849   
 96981  97850     db = pParse->db;
 96982  97851     assert( db->nDb>iDb );
 96983  97852     pFix->pParse = pParse;
 96984         -  pFix->zDb = db->aDb[iDb].zName;
        97853  +  pFix->zDb = db->aDb[iDb].zDbSName;
 96985  97854     pFix->pSchema = db->aDb[iDb].pSchema;
 96986  97855     pFix->zType = zType;
 96987  97856     pFix->pName = pName;
 96988  97857     pFix->bVarOnly = (iDb==1);
 96989  97858   }
 96990  97859   
 96991  97860   /*
................................................................................
 97074  97943         if( pFix->pParse->db->init.busy ){
 97075  97944           pExpr->op = TK_NULL;
 97076  97945         }else{
 97077  97946           sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
 97078  97947           return 1;
 97079  97948         }
 97080  97949       }
 97081         -    if( ExprHasProperty(pExpr, EP_TokenOnly) ) break;
        97950  +    if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf) ) break;
 97082  97951       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 97083  97952         if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
 97084  97953       }else{
 97085  97954         if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
 97086  97955       }
 97087  97956       if( sqlite3FixExpr(pFix, pExpr->pRight) ){
 97088  97957         return 1;
................................................................................
 97235  98104   */
 97236  98105   SQLITE_PRIVATE int sqlite3AuthReadCol(
 97237  98106     Parse *pParse,                  /* The parser context */
 97238  98107     const char *zTab,               /* Table name */
 97239  98108     const char *zCol,               /* Column name */
 97240  98109     int iDb                         /* Index of containing database. */
 97241  98110   ){
 97242         -  sqlite3 *db = pParse->db;       /* Database handle */
 97243         -  char *zDb = db->aDb[iDb].zName; /* Name of attached database */
 97244         -  int rc;                         /* Auth callback return code */
        98111  +  sqlite3 *db = pParse->db;          /* Database handle */
        98112  +  char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
        98113  +  int rc;                            /* Auth callback return code */
 97245  98114   
 97246  98115     if( db->init.busy ) return SQLITE_OK;
 97247  98116     rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
 97248  98117   #ifdef SQLITE_USER_AUTHENTICATION
 97249  98118                    ,db->auth.zAuthUser
 97250  98119   #endif
 97251  98120                   );
................................................................................
 97538  98407     /* Begin by generating some termination code at the end of the
 97539  98408     ** vdbe program
 97540  98409     */
 97541  98410     v = sqlite3GetVdbe(pParse);
 97542  98411     assert( !pParse->isMultiWrite 
 97543  98412          || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
 97544  98413     if( v ){
 97545         -    while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
 97546  98414       sqlite3VdbeAddOp0(v, OP_Halt);
 97547  98415   
 97548  98416   #if SQLITE_USER_AUTHENTICATION
 97549  98417       if( pParse->nTableLock>0 && db->init.busy==0 ){
 97550  98418         sqlite3UserAuthInit(db);
 97551  98419         if( db->auth.authLevel<UAUTH_User ){
 97552         -        pParse->rc = SQLITE_AUTH_USER;
 97553  98420           sqlite3ErrorMsg(pParse, "user not authenticated");
        98421  +        pParse->rc = SQLITE_AUTH_USER;
 97554  98422           return;
 97555  98423         }
 97556  98424       }
 97557  98425   #endif
 97558  98426   
 97559  98427       /* The cookie mask contains one bit for each database file open.
 97560  98428       ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
................................................................................
 97565  98433       if( db->mallocFailed==0 
 97566  98434        && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
 97567  98435       ){
 97568  98436         int iDb, i;
 97569  98437         assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
 97570  98438         sqlite3VdbeJumpHere(v, 0);
 97571  98439         for(iDb=0; iDb<db->nDb; iDb++){
        98440  +        Schema *pSchema;
 97572  98441           if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
 97573  98442           sqlite3VdbeUsesBtree(v, iDb);
        98443  +        pSchema = db->aDb[iDb].pSchema;
 97574  98444           sqlite3VdbeAddOp4Int(v,
 97575  98445             OP_Transaction,                    /* Opcode */
 97576  98446             iDb,                               /* P1 */
 97577  98447             DbMaskTest(pParse->writeMask,iDb), /* P2 */
 97578         -          pParse->cookieValue[iDb],          /* P3 */
 97579         -          db->aDb[iDb].pSchema->iGeneration  /* P4 */
        98448  +          pSchema->schema_cookie,            /* P3 */
        98449  +          pSchema->iGeneration               /* P4 */
 97580  98450           );
 97581  98451           if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
 97582  98452           VdbeComment((v,
 97583  98453                 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
 97584  98454         }
 97585  98455   #ifndef SQLITE_OMIT_VIRTUALTABLE
 97586  98456         for(i=0; i<pParse->nVtabLock; i++){
................................................................................
 97623  98493       *  See ticket [a696379c1f08866] */
 97624  98494       if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
 97625  98495       sqlite3VdbeMakeReady(v, pParse);
 97626  98496       pParse->rc = SQLITE_DONE;
 97627  98497     }else{
 97628  98498       pParse->rc = SQLITE_ERROR;
 97629  98499     }
 97630         -
 97631         -  /* We are done with this Parse object. There is no need to de-initialize it */
 97632         -#if 0
 97633         -  pParse->colNamesSet = 0;
 97634         -  pParse->nTab = 0;
 97635         -  pParse->nMem = 0;
 97636         -  pParse->nSet = 0;
 97637         -  pParse->nVar = 0;
 97638         -  DbMaskZero(pParse->cookieMask);
 97639         -#endif
 97640  98500   }
 97641  98501   
 97642  98502   /*
 97643  98503   ** Run the parser and code generator recursively in order to generate
 97644  98504   ** code for the SQL statement given onto the end of the pParse context
 97645  98505   ** currently under construction.  When the parser is run recursively
 97646  98506   ** this way, the final OP_Halt is not appended and other initialization
................................................................................
 97652  98512   ** care if you decide to try to use this routine for some other purposes.
 97653  98513   */
 97654  98514   SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
 97655  98515     va_list ap;
 97656  98516     char *zSql;
 97657  98517     char *zErrMsg = 0;
 97658  98518     sqlite3 *db = pParse->db;
 97659         -# define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
 97660         -  char saveBuf[SAVE_SZ];
        98519  +  char saveBuf[PARSE_TAIL_SZ];
 97661  98520   
 97662  98521     if( pParse->nErr ) return;
 97663  98522     assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
 97664  98523     va_start(ap, zFormat);
 97665  98524     zSql = sqlite3VMPrintf(db, zFormat, ap);
 97666  98525     va_end(ap);
 97667  98526     if( zSql==0 ){
 97668  98527       return;   /* A malloc must have failed */
 97669  98528     }
 97670  98529     pParse->nested++;
 97671         -  memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
 97672         -  memset(&pParse->nVar, 0, SAVE_SZ);
        98530  +  memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
        98531  +  memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
 97673  98532     sqlite3RunParser(pParse, zSql, &zErrMsg);
 97674  98533     sqlite3DbFree(db, zErrMsg);
 97675  98534     sqlite3DbFree(db, zSql);
 97676         -  memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
        98535  +  memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
 97677  98536     pParse->nested--;
 97678  98537   }
 97679  98538   
 97680  98539   #if SQLITE_USER_AUTHENTICATION
 97681  98540   /*
 97682  98541   ** Return TRUE if zTable is the name of the system table that stores the
 97683  98542   ** list of users and their access credentials.
................................................................................
 97710  98569     ** exists */
 97711  98570     if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
 97712  98571       return 0;
 97713  98572     }
 97714  98573   #endif
 97715  98574     for(i=OMIT_TEMPDB; i<db->nDb; i++){
 97716  98575       int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
 97717         -    if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
 97718         -    assert( sqlite3SchemaMutexHeld(db, j, 0) );
 97719         -    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
 97720         -    if( p ) break;
        98576  +    if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
        98577  +      assert( sqlite3SchemaMutexHeld(db, j, 0) );
        98578  +      p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
        98579  +      if( p ) break;
        98580  +    }
 97721  98581     }
 97722  98582     return p;
 97723  98583   }
 97724  98584   
 97725  98585   /*
 97726  98586   ** Locate the in-memory structure that describes a particular database
 97727  98587   ** table given the name of that table and (optionally) the name of the
................................................................................
 97787  98647     u32 flags,
 97788  98648     struct SrcList_item *p
 97789  98649   ){
 97790  98650     const char *zDb;
 97791  98651     assert( p->pSchema==0 || p->zDatabase==0 );
 97792  98652     if( p->pSchema ){
 97793  98653       int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
 97794         -    zDb = pParse->db->aDb[iDb].zName;
        98654  +    zDb = pParse->db->aDb[iDb].zDbSName;
 97795  98655     }else{
 97796  98656       zDb = p->zDatabase;
 97797  98657     }
 97798  98658     return sqlite3LocateTable(pParse, flags, p->zName, zDb);
 97799  98659   }
 97800  98660   
 97801  98661   /*
................................................................................
 97815  98675     int i;
 97816  98676     /* All mutexes are required for schema access.  Make sure we hold them. */
 97817  98677     assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 97818  98678     for(i=OMIT_TEMPDB; i<db->nDb; i++){
 97819  98679       int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
 97820  98680       Schema *pSchema = db->aDb[j].pSchema;
 97821  98681       assert( pSchema );
 97822         -    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
        98682  +    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
 97823  98683       assert( sqlite3SchemaMutexHeld(db, j, 0) );
 97824  98684       p = sqlite3HashFind(&pSchema->idxHash, zName);
 97825  98685       if( p ) break;
 97826  98686     }
 97827  98687     return p;
 97828  98688   }
 97829  98689   
................................................................................
 97884  98744   ** are never candidates for being collapsed.
 97885  98745   */
 97886  98746   SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
 97887  98747     int i, j;
 97888  98748     for(i=j=2; i<db->nDb; i++){
 97889  98749       struct Db *pDb = &db->aDb[i];
 97890  98750       if( pDb->pBt==0 ){
 97891         -      sqlite3DbFree(db, pDb->zName);
 97892         -      pDb->zName = 0;
        98751  +      sqlite3DbFree(db, pDb->zDbSName);
        98752  +      pDb->zDbSName = 0;
 97893  98753         continue;
 97894  98754       }
 97895  98755       if( j<i ){
 97896  98756         db->aDb[j] = db->aDb[i];
 97897  98757       }
 97898  98758       j++;
 97899  98759     }
................................................................................
 98105  98965   ** -1 if the named db cannot be found.
 98106  98966   */
 98107  98967   SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
 98108  98968     int i = -1;         /* Database number */
 98109  98969     if( zName ){
 98110  98970       Db *pDb;
 98111  98971       for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
 98112         -      if( 0==sqlite3StrICmp(pDb->zName, zName) ) break;
        98972  +      if( 0==sqlite3StrICmp(pDb->zDbSName, zName) ) break;
 98113  98973       }
 98114  98974     }
 98115  98975     return i;
 98116  98976   }
 98117  98977   
 98118  98978   /*
 98119  98979   ** The token *pName contains the name of a database (either "main" or
................................................................................
 98164  99024       *pUnqual = pName2;
 98165  99025       iDb = sqlite3FindDb(db, pName1);
 98166  99026       if( iDb<0 ){
 98167  99027         sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
 98168  99028         return -1;
 98169  99029       }
 98170  99030     }else{
 98171         -    assert( db->init.iDb==0 || db->init.busy );
        99031  +    assert( db->init.iDb==0 || db->init.busy || (db->flags & SQLITE_Vacuum)!=0);
 98172  99032       iDb = db->init.iDb;
 98173  99033       *pUnqual = pName1;
 98174  99034     }
 98175  99035     return iDb;
 98176  99036   }
 98177  99037   
 98178  99038   /*
................................................................................
 98275  99135     {
 98276  99136       static const u8 aCode[] = {
 98277  99137          SQLITE_CREATE_TABLE,
 98278  99138          SQLITE_CREATE_TEMP_TABLE,
 98279  99139          SQLITE_CREATE_VIEW,
 98280  99140          SQLITE_CREATE_TEMP_VIEW
 98281  99141       };
 98282         -    char *zDb = db->aDb[iDb].zName;
        99142  +    char *zDb = db->aDb[iDb].zDbSName;
 98283  99143       if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
 98284  99144         goto begin_table_error;
 98285  99145       }
 98286  99146       if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
 98287  99147                                          zName, 0, zDb) ){
 98288  99148         goto begin_table_error;
 98289  99149       }
................................................................................
 98294  99154     ** index or table name in the same database.  Issue an error message if
 98295  99155     ** it does. The exception is if the statement being parsed was passed
 98296  99156     ** to an sqlite3_declare_vtab() call. In that case only the column names
 98297  99157     ** and types will be used, so there is no need to test for namespace
 98298  99158     ** collisions.
 98299  99159     */
 98300  99160     if( !IN_DECLARE_VTAB ){
 98301         -    char *zDb = db->aDb[iDb].zName;
        99161  +    char *zDb = db->aDb[iDb].zDbSName;
 98302  99162       if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 98303  99163         goto begin_table_error;
 98304  99164       }
 98305  99165       pTable = sqlite3FindTable(db, zName, zDb);
 98306  99166       if( pTable ){
 98307  99167         if( !noErr ){
 98308  99168           sqlite3ErrorMsg(pParse, "table %T already exists", pName);
................................................................................
 98849  99709   ** since it was last read.
 98850  99710   **
 98851  99711   ** This plan is not completely bullet-proof.  It is possible for
 98852  99712   ** the schema to change multiple times and for the cookie to be
 98853  99713   ** set back to prior value.  But schema changes are infrequent
 98854  99714   ** and the probability of hitting the same cookie value is only
 98855  99715   ** 1 chance in 2^32.  So we're safe enough.
        99716  +**
        99717  +** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
        99718  +** the schema-version whenever the schema changes.
 98856  99719   */
 98857  99720   SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
 98858  99721     sqlite3 *db = pParse->db;
 98859  99722     Vdbe *v = pParse->pVdbe;
 98860  99723     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 98861  99724     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, 
 98862  99725                       db->aDb[iDb].pSchema->schema_cookie+1);
................................................................................
 99387 100250       ** SQLITE_MASTER table.  We just need to update that slot with all
 99388 100251       ** the information we've collected.
 99389 100252       */
 99390 100253       sqlite3NestedParse(pParse,
 99391 100254         "UPDATE %Q.%s "
 99392 100255            "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
 99393 100256          "WHERE rowid=#%d",
 99394         -      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
       100257  +      db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb),
 99395 100258         zType,
 99396 100259         p->zName,
 99397 100260         p->zName,
 99398 100261         pParse->regRoot,
 99399 100262         zStmt,
 99400 100263         pParse->regRowid
 99401 100264       );
................................................................................
 99402 100265       sqlite3DbFree(db, zStmt);
 99403 100266       sqlite3ChangeCookie(pParse, iDb);
 99404 100267   
 99405 100268   #ifndef SQLITE_OMIT_AUTOINCREMENT
 99406 100269       /* Check to see if we need to create an sqlite_sequence table for
 99407 100270       ** keeping track of autoincrement keys.
 99408 100271       */
 99409         -    if( p->tabFlags & TF_Autoincrement ){
       100272  +    if( (p->tabFlags & TF_Autoincrement)!=0 ){
 99410 100273         Db *pDb = &db->aDb[iDb];
 99411 100274         assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 99412 100275         if( pDb->pSchema->pSeqTab==0 ){
 99413 100276           sqlite3NestedParse(pParse,
 99414 100277             "CREATE TABLE %Q.sqlite_sequence(name,seq)",
 99415         -          pDb->zName
       100278  +          pDb->zDbSName
 99416 100279           );
 99417 100280         }
 99418 100281       }
 99419 100282   #endif
 99420 100283   
 99421 100284       /* Reparse everything to update our internal data structures */
 99422 100285       sqlite3VdbeAddParseSchemaOp(v, iDb,
................................................................................
 99532 100395   */
 99533 100396   SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
 99534 100397     Table *pSelTab;   /* A fake table from which we get the result set */
 99535 100398     Select *pSel;     /* Copy of the SELECT that implements the view */
 99536 100399     int nErr = 0;     /* Number of errors encountered */
 99537 100400     int n;            /* Temporarily holds the number of cursors assigned */
 99538 100401     sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
       100402  +#ifndef SQLITE_OMIT_AUTHORIZATION
 99539 100403     sqlite3_xauth xAuth;       /* Saved xAuth pointer */
       100404  +#endif
 99540 100405   
 99541 100406     assert( pTable );
 99542 100407   
 99543 100408   #ifndef SQLITE_OMIT_VIRTUALTABLE
 99544 100409     if( sqlite3VtabCallConnect(pParse, pTable) ){
 99545 100410       return SQLITE_ERROR;
 99546 100411     }
................................................................................
 99722 100587     **
 99723 100588     ** The "#NNN" in the SQL is a special constant that means whatever value
 99724 100589     ** is in register NNN.  See grammar rules associated with the TK_REGISTER
 99725 100590     ** token for additional information.
 99726 100591     */
 99727 100592     sqlite3NestedParse(pParse, 
 99728 100593        "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
 99729         -     pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
       100594  +     pParse->db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb), iTable, r1, r1);
 99730 100595   #endif
 99731 100596     sqlite3ReleaseTempReg(pParse, r1);
 99732 100597   }
 99733 100598   
 99734 100599   /*
 99735 100600   ** Write VDBE code to erase table pTab and all associated indices on disk.
 99736 100601   ** Code to update the sqlite_master tables and internal schema definitions
................................................................................
 99798 100663   static void sqlite3ClearStatTables(
 99799 100664     Parse *pParse,         /* The parsing context */
 99800 100665     int iDb,               /* The database number */
 99801 100666     const char *zType,     /* "idx" or "tbl" */
 99802 100667     const char *zName      /* Name of index or table */
 99803 100668   ){
 99804 100669     int i;
 99805         -  const char *zDbName = pParse->db->aDb[iDb].zName;
       100670  +  const char *zDbName = pParse->db->aDb[iDb].zDbSName;
 99806 100671     for(i=1; i<=4; i++){
 99807 100672       char zTab[24];
 99808 100673       sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
 99809 100674       if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
 99810 100675         sqlite3NestedParse(pParse,
 99811 100676           "DELETE FROM %Q.%s WHERE %s=%Q",
 99812 100677           zDbName, zTab, zType, zName
................................................................................
 99851 100716     ** the table being dropped. This is done before the table is dropped
 99852 100717     ** at the btree level, in case the sqlite_sequence table needs to
 99853 100718     ** move as a result of the drop (can happen in auto-vacuum mode).
 99854 100719     */
 99855 100720     if( pTab->tabFlags & TF_Autoincrement ){
 99856 100721       sqlite3NestedParse(pParse,
 99857 100722         "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
 99858         -      pDb->zName, pTab->zName
       100723  +      pDb->zDbSName, pTab->zName
 99859 100724       );
 99860 100725     }
 99861 100726   #endif
 99862 100727   
 99863 100728     /* Drop all SQLITE_MASTER table and index entries that refer to the
 99864 100729     ** table. The program name loops through the master table and deletes
 99865 100730     ** every row that refers to a table of the same name as the one being
 99866 100731     ** dropped. Triggers are handled separately because a trigger can be
 99867 100732     ** created in the temp database that refers to a table in another
 99868 100733     ** database.
 99869 100734     */
 99870 100735     sqlite3NestedParse(pParse, 
 99871 100736         "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
 99872         -      pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
       100737  +      pDb->zDbSName, SCHEMA_TABLE(iDb), pTab->zName);
 99873 100738     if( !isView && !IsVirtual(pTab) ){
 99874 100739       destroyTable(pParse, pTab);
 99875 100740     }
 99876 100741   
 99877 100742     /* Remove the table entry from SQLite's internal schema and modify
 99878 100743     ** the schema cookie.
 99879 100744     */
................................................................................
 99919 100784     if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
 99920 100785       goto exit_drop_table;
 99921 100786     }
 99922 100787   #ifndef SQLITE_OMIT_AUTHORIZATION
 99923 100788     {
 99924 100789       int code;
 99925 100790       const char *zTab = SCHEMA_TABLE(iDb);
 99926         -    const char *zDb = db->aDb[iDb].zName;
       100791  +    const char *zDb = db->aDb[iDb].zDbSName;
 99927 100792       const char *zArg2 = 0;
 99928 100793       if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
 99929 100794         goto exit_drop_table;
 99930 100795       }
 99931 100796       if( isView ){
 99932 100797         if( !OMIT_TEMPDB && iDb==1 ){
 99933 100798           code = SQLITE_DROP_TEMP_VIEW;
................................................................................
100160 101025     KeyInfo *pKey;                 /* KeyInfo for index */
100161 101026     int regRecord;                 /* Register holding assembled index record */
100162 101027     sqlite3 *db = pParse->db;      /* The database connection */
100163 101028     int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
100164 101029   
100165 101030   #ifndef SQLITE_OMIT_AUTHORIZATION
100166 101031     if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
100167         -      db->aDb[iDb].zName ) ){
       101032  +      db->aDb[iDb].zDbSName ) ){
100168 101033       return;
100169 101034     }
100170 101035   #endif
100171 101036   
100172 101037     /* Require a write-lock on the table to perform this operation */
100173 101038     sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
100174 101039   
................................................................................
100412 101277       }
100413 101278       if( !db->init.busy ){
100414 101279         if( sqlite3FindTable(db, zName, 0)!=0 ){
100415 101280           sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
100416 101281           goto exit_create_index;
100417 101282         }
100418 101283       }
100419         -    if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
       101284  +    if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
100420 101285         if( !ifNotExist ){
100421 101286           sqlite3ErrorMsg(pParse, "index %s already exists", zName);
100422 101287         }else{
100423 101288           assert( !db->init.busy );
100424 101289           sqlite3CodeVerifySchema(pParse, iDb);
100425 101290         }
100426 101291         goto exit_create_index;
................................................................................
100442 101307       if( IN_DECLARE_VTAB ) zName[7]++;
100443 101308     }
100444 101309   
100445 101310     /* Check for authorization to create an index.
100446 101311     */
100447 101312   #ifndef SQLITE_OMIT_AUTHORIZATION
100448 101313     {
100449         -    const char *zDb = pDb->zName;
       101314  +    const char *zDb = pDb->zDbSName;
100450 101315       if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
100451 101316         goto exit_create_index;
100452 101317       }
100453 101318       i = SQLITE_CREATE_INDEX;
100454 101319       if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
100455 101320       if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
100456 101321         goto exit_create_index;
................................................................................
100757 101622         zStmt = 0;
100758 101623       }
100759 101624   
100760 101625       /* Add an entry in sqlite_master for this index
100761 101626       */
100762 101627       sqlite3NestedParse(pParse, 
100763 101628           "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
100764         -        db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
       101629  +        db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb),
100765 101630           pIndex->zName,
100766 101631           pTab->zName,
100767 101632           iMem,
100768 101633           zStmt
100769 101634       );
100770 101635       sqlite3DbFree(db, zStmt);
100771 101636   
................................................................................
100891 101756       goto exit_drop_index;
100892 101757     }
100893 101758     iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
100894 101759   #ifndef SQLITE_OMIT_AUTHORIZATION
100895 101760     {
100896 101761       int code = SQLITE_DROP_INDEX;
100897 101762       Table *pTab = pIndex->pTable;
100898         -    const char *zDb = db->aDb[iDb].zName;
       101763  +    const char *zDb = db->aDb[iDb].zDbSName;
100899 101764       const char *zTab = SCHEMA_TABLE(iDb);
100900 101765       if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
100901 101766         goto exit_drop_index;
100902 101767       }
100903 101768       if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
100904 101769       if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
100905 101770         goto exit_drop_index;
................................................................................
100909 101774   
100910 101775     /* Generate code to remove the index and from the master table */
100911 101776     v = sqlite3GetVdbe(pParse);
100912 101777     if( v ){
100913 101778       sqlite3BeginWriteOperation(pParse, 1, iDb);
100914 101779       sqlite3NestedParse(pParse,
100915 101780          "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
100916         -       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
       101781  +       db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb), pIndex->zName
100917 101782       );
100918 101783       sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
100919 101784       sqlite3ChangeCookie(pParse, iDb);
100920 101785       destroyRootPage(pParse, pIndex->tnum, iDb);
100921 101786       sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
100922 101787     }
100923 101788   
................................................................................
101430 102295   ** Record the fact that the schema cookie will need to be verified
101431 102296   ** for database iDb.  The code to actually verify the schema cookie
101432 102297   ** will occur at the end of the top-level VDBE and will be generated
101433 102298   ** later, by sqlite3FinishCoding().
101434 102299   */
101435 102300   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
101436 102301     Parse *pToplevel = sqlite3ParseToplevel(pParse);
101437         -  sqlite3 *db = pToplevel->db;
101438 102302   
101439         -  assert( iDb>=0 && iDb<db->nDb );
101440         -  assert( db->aDb[iDb].pBt!=0 || iDb==1 );
       102303  +  assert( iDb>=0 && iDb<pParse->db->nDb );
       102304  +  assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
101441 102305     assert( iDb<SQLITE_MAX_ATTACHED+2 );
101442         -  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
       102306  +  assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
101443 102307     if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
101444 102308       DbMaskSet(pToplevel->cookieMask, iDb);
101445         -    pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
101446 102309       if( !OMIT_TEMPDB && iDb==1 ){
101447 102310         sqlite3OpenTempDatabase(pToplevel);
101448 102311       }
101449 102312     }
101450 102313   }
101451 102314   
101452 102315   /*
................................................................................
101454 102317   ** attached database. Otherwise, invoke it for the database named zDb only.
101455 102318   */
101456 102319   SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
101457 102320     sqlite3 *db = pParse->db;
101458 102321     int i;
101459 102322     for(i=0; i<db->nDb; i++){
101460 102323       Db *pDb = &db->aDb[i];
101461         -    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
       102324  +    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
101462 102325         sqlite3CodeVerifySchema(pParse, i);
101463 102326       }
101464 102327     }
101465 102328   }
101466 102329   
101467 102330   /*
101468 102331   ** Generate VDBE code that prepares for doing an operation that
................................................................................
101701 102564       }
101702 102565       sqlite3DbFree(db, zColl);
101703 102566     }
101704 102567     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
101705 102568     if( iDb<0 ) return;
101706 102569     z = sqlite3NameFromToken(db, pObjName);
101707 102570     if( z==0 ) return;
101708         -  zDb = db->aDb[iDb].zName;
       102571  +  zDb = db->aDb[iDb].zDbSName;
101709 102572     pTab = sqlite3FindTable(db, z, zDb);
101710 102573     if( pTab ){
101711 102574       reindexTable(pParse, pTab, 0);
101712 102575       sqlite3DbFree(db, z);
101713 102576       return;
101714 102577     }
101715 102578     pIndex = sqlite3FindIndex(db, z, zDb);
................................................................................
102415 103278     sqlite3 *db = pParse->db;
102416 103279     int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
102417 103280     pWhere = sqlite3ExprDup(db, pWhere, 0);
102418 103281     pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
102419 103282     if( pFrom ){
102420 103283       assert( pFrom->nSrc==1 );
102421 103284       pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
102422         -    pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
       103285  +    pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
102423 103286       assert( pFrom->a[0].pOn==0 );
102424 103287       assert( pFrom->a[0].pUsing==0 );
102425 103288     }
102426 103289     pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 
102427 103290                             SF_IncludeHidden, 0, 0);
102428 103291     sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
102429 103292     sqlite3Select(pParse, pSel, &dest);
................................................................................
102525 103388   SQLITE_PRIVATE void sqlite3DeleteFrom(
102526 103389     Parse *pParse,         /* The parser context */
102527 103390     SrcList *pTabList,     /* The table from which we should delete things */
102528 103391     Expr *pWhere           /* The WHERE clause.  May be null */
102529 103392   ){
102530 103393     Vdbe *v;               /* The virtual database engine */
102531 103394     Table *pTab;           /* The table from which records will be deleted */
102532         -  const char *zDb;       /* Name of database holding pTab */
102533 103395     int i;                 /* Loop counter */
102534 103396     WhereInfo *pWInfo;     /* Information about the WHERE clause */
102535 103397     Index *pIdx;           /* For looping over indices of the table */
102536 103398     int iTabCur;           /* Cursor number for the table */
102537 103399     int iDataCur = 0;      /* VDBE cursor for the canonical data source */
102538 103400     int iIdxCur = 0;       /* Cursor number of the first index */
102539 103401     int nIdx;              /* Number of indices */
................................................................................
102602 103464     }
102603 103465   
102604 103466     if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
102605 103467       goto delete_from_cleanup;
102606 103468     }
102607 103469     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
102608 103470     assert( iDb<db->nDb );
102609         -  zDb = db->aDb[iDb].zName;
102610         -  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
       103471  +  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, 
       103472  +                            db->aDb[iDb].zDbSName);
102611 103473     assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
102612 103474     if( rcauth==SQLITE_DENY ){
102613 103475       goto delete_from_cleanup;
102614 103476     }
102615 103477     assert(!isView || pTrigger);
102616 103478   
102617 103479     /* Assign cursor numbers to the table and all its indices.
................................................................................
102787 103649       ** deleting from and all its indices. If this is a view, then the
102788 103650       ** only effect this statement has is to fire the INSTEAD OF 
102789 103651       ** triggers.
102790 103652       */
102791 103653       if( !isView ){
102792 103654         int iAddrOnce = 0;
102793 103655         if( eOnePass==ONEPASS_MULTI ){
102794         -        iAddrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
       103656  +        iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
102795 103657         }
102796 103658         testcase( IsVirtual(pTab) );
102797 103659         sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
102798 103660                                    iTabCur, aToOpen, &iDataCur, &iIdxCur);
102799 103661         assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
102800 103662         assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
102801 103663         if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
................................................................................
105934 106796     /* Exactly one of regOld and regNew should be non-zero. */
105935 106797     assert( (regOld==0)!=(regNew==0) );
105936 106798   
105937 106799     /* If foreign-keys are disabled, this function is a no-op. */
105938 106800     if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
105939 106801   
105940 106802     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
105941         -  zDb = db->aDb[iDb].zName;
       106803  +  zDb = db->aDb[iDb].zDbSName;
105942 106804   
105943 106805     /* Loop through all the foreign key constraints for which pTab is the
105944 106806     ** child table (the table that the foreign key definition is part of).  */
105945 106807     for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
105946 106808       Table *pTo;                   /* Parent table of foreign key pFKey */
105947 106809       Index *pIdx = 0;              /* Index on key columns in pTo */
105948 106810       int *aiFree = 0;
................................................................................
106305 107167               sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
106306 107168             , 0);
106307 107169           }else if( action==OE_SetDflt ){
106308 107170             Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
106309 107171             if( pDflt ){
106310 107172               pNew = sqlite3ExprDup(db, pDflt, 0);
106311 107173             }else{
106312         -            pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
       107174  +            pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
106313 107175             }
106314 107176           }else{
106315         -          pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
       107177  +          pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
106316 107178           }
106317 107179           pList = sqlite3ExprListAppend(pParse, pList, pNew);
106318 107180           sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
106319 107181         }
106320 107182       }
106321 107183       sqlite3DbFree(db, aiCol);
106322 107184   
................................................................................
106674 107536     return 0;
106675 107537   }
106676 107538   
106677 107539   #ifndef SQLITE_OMIT_AUTOINCREMENT
106678 107540   /*
106679 107541   ** Locate or create an AutoincInfo structure associated with table pTab
106680 107542   ** which is in database iDb.  Return the register number for the register
106681         -** that holds the maximum rowid.
       107543  +** that holds the maximum rowid.  Return zero if pTab is not an AUTOINCREMENT
       107544  +** table.  (Also return zero when doing a VACUUM since we do not want to
       107545  +** update the AUTOINCREMENT counters during a VACUUM.)
106682 107546   **
106683 107547   ** There is at most one AutoincInfo structure per table even if the
106684 107548   ** same table is autoincremented multiple times due to inserts within
106685 107549   ** triggers.  A new AutoincInfo structure is created if this is the
106686 107550   ** first use of table pTab.  On 2nd and subsequent uses, the original
106687 107551   ** AutoincInfo structure is used.
106688 107552   **
................................................................................
106697 107561   */
106698 107562   static int autoIncBegin(
106699 107563     Parse *pParse,      /* Parsing context */
106700 107564     int iDb,            /* Index of the database holding pTab */
106701 107565     Table *pTab         /* The table we are writing to */
106702 107566   ){
106703 107567     int memId = 0;      /* Register holding maximum rowid */
106704         -  if( pTab->tabFlags & TF_Autoincrement ){
       107568  +  if( (pTab->tabFlags & TF_Autoincrement)!=0
       107569  +   && (pParse->db->flags & SQLITE_Vacuum)==0
       107570  +  ){
106705 107571       Parse *pToplevel = sqlite3ParseToplevel(pParse);
106706 107572       AutoincInfo *pInfo;
106707 107573   
106708 107574       pInfo = pToplevel->pAinc;
106709 107575       while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
106710 107576       if( pInfo==0 ){
106711 107577         pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
................................................................................
106955 107821     Select *pSelect,      /* A SELECT statement to use as the data source */
106956 107822     IdList *pColumn,      /* Column names corresponding to IDLIST. */
106957 107823     int onError           /* How to handle constraint errors */
106958 107824   ){
106959 107825     sqlite3 *db;          /* The main database structure */
106960 107826     Table *pTab;          /* The table to insert into.  aka TABLE */
106961 107827     char *zTab;           /* Name of the table into which we are inserting */
106962         -  const char *zDb;      /* Name of the database holding this table */
106963 107828     int i, j, idx;        /* Loop counters */
106964 107829     Vdbe *v;              /* Generate code into this virtual machine */
106965 107830     Index *pIdx;          /* For looping over indices of the table */
106966 107831     int nColumn;          /* Number of columns in the data */
106967 107832     int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
106968 107833     int iDataCur = 0;     /* VDBE cursor that is the main data repository */
106969 107834     int iIdxCur = 0;      /* First index cursor */
................................................................................
106970 107835     int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
106971 107836     int endOfLoop;        /* Label for the end of the insertion loop */
106972 107837     int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
106973 107838     int addrInsTop = 0;   /* Jump to label "D" */
106974 107839     int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
106975 107840     SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
106976 107841     int iDb;              /* Index of database holding TABLE */
106977         -  Db *pDb;              /* The database containing table being inserted into */
106978 107842     u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
106979 107843     u8 appendFlag = 0;    /* True if the insert is likely to be an append */
106980 107844     u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
106981 107845     u8 bIdListInOrder;    /* True if IDLIST is in table order */
106982 107846     ExprList *pList = 0;  /* List of VALUES() to be inserted  */
106983 107847   
106984 107848     /* Register allocations */
................................................................................
107020 107884     if( NEVER(zTab==0) ) goto insert_cleanup;
107021 107885     pTab = sqlite3SrcListLookup(pParse, pTabList);
107022 107886     if( pTab==0 ){
107023 107887       goto insert_cleanup;
107024 107888     }
107025 107889     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
107026 107890     assert( iDb<db->nDb );
107027         -  pDb = &db->aDb[iDb];
107028         -  zDb = pDb->zName;
107029         -  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
       107891  +  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
       107892  +                       db->aDb[iDb].zDbSName) ){
107030 107893       goto insert_cleanup;
107031 107894     }
107032 107895     withoutRowid = !HasRowid(pTab);
107033 107896   
107034 107897     /* Figure out if we have any triggers and if the table being
107035 107898     ** inserted into is a view
107036 107899     */
................................................................................
108650 109513       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
108651 109514       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
108652 109515     }
108653 109516     if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
108654 109517     sqlite3ReleaseTempReg(pParse, regRowid);
108655 109518     sqlite3ReleaseTempReg(pParse, regData);
108656 109519     if( emptyDestTest ){
       109520  +    sqlite3AutoincrementEnd(pParse);
108657 109521       sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
108658 109522       sqlite3VdbeJumpHere(v, emptyDestTest);
108659 109523       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
108660 109524       return 0;
108661 109525     }else{
108662 109526       return 1;
108663 109527     }
................................................................................
109995 110859     }else{
109996 110860       db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
109997 110861     }
109998 110862     sqlite3_mutex_leave(db->mutex);
109999 110863     return SQLITE_OK;
110000 110864   }
110001 110865   
110002         -#endif /* SQLITE_OMIT_LOAD_EXTENSION */
110003         -
110004         -/*
110005         -** The auto-extension code added regardless of whether or not extension
110006         -** loading is supported.  We need a dummy sqlite3Apis pointer for that
110007         -** code if regular extension loading is not available.  This is that
110008         -** dummy pointer.
110009         -*/
110010         -#ifdef SQLITE_OMIT_LOAD_EXTENSION
110011         -static const sqlite3_api_routines sqlite3Apis = { 0 };
110012         -#endif
110013         -
       110866  +#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
110014 110867   
110015 110868   /*
110016 110869   ** The following object holds the list of automatically loaded
110017 110870   ** extensions.
110018 110871   **
110019 110872   ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
110020 110873   ** mutex must be held while accessing this list.
................................................................................
110151 111004       return;
110152 111005     }
110153 111006     for(i=0; go; i++){
110154 111007       char *zErrmsg;
110155 111008   #if SQLITE_THREADSAFE
110156 111009       sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
110157 111010   #endif
       111011  +#ifdef SQLITE_OMIT_LOAD_EXTENSION
       111012  +    const sqlite3_api_routines *pThunk = 0;
       111013  +#else
       111014  +    const sqlite3_api_routines *pThunk = &sqlite3Apis;
       111015  +#endif
110158 111016       sqlite3_mutex_enter(mutex);
110159 111017       if( i>=wsdAutoext.nExt ){
110160 111018         xInit = 0;
110161 111019         go = 0;
110162 111020       }else{
110163 111021         xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
110164 111022       }
110165 111023       sqlite3_mutex_leave(mutex);
110166 111024       zErrmsg = 0;
110167         -    if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
       111025  +    if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
110168 111026         sqlite3ErrorWithMsg(db, rc,
110169 111027               "automatic extension loading failed: %s", zErrmsg);
110170 111028         go = 0;
110171 111029       }
110172 111030       sqlite3_free(zErrmsg);
110173 111031     }
110174 111032   }
................................................................................
110979 111837     if( minusFlag ){
110980 111838       zRight = sqlite3MPrintf(db, "-%T", pValue);
110981 111839     }else{
110982 111840       zRight = sqlite3NameFromToken(db, pValue);
110983 111841     }
110984 111842   
110985 111843     assert( pId2 );
110986         -  zDb = pId2->n>0 ? pDb->zName : 0;
       111844  +  zDb = pId2->n>0 ? pDb->zDbSName : 0;
110987 111845     if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
110988 111846       goto pragma_out;
110989 111847     }
110990 111848   
110991 111849     /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
110992 111850     ** connection.  If it returns SQLITE_OK, then assume that the VFS
110993 111851     ** handled the pragma and generate a no-op prepared statement.
................................................................................
111832 112690     case PragTyp_DATABASE_LIST: {
111833 112691       static const char *azCol[] = { "seq", "name", "file" };
111834 112692       int i;
111835 112693       pParse->nMem = 3;
111836 112694       setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) );
111837 112695       for(i=0; i<db->nDb; i++){
111838 112696         if( db->aDb[i].pBt==0 ) continue;
111839         -      assert( db->aDb[i].zName!=0 );
       112697  +      assert( db->aDb[i].zDbSName!=0 );
111840 112698         sqlite3VdbeMultiLoad(v, 1, "iss",
111841 112699            i,
111842         -         db->aDb[i].zName,
       112700  +         db->aDb[i].zDbSName,
111843 112701            sqlite3BtreeGetFilename(db->aDb[i].pBt));
111844 112702         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
111845 112703       }
111846 112704     }
111847 112705     break;
111848 112706   
111849 112707     case PragTyp_COLLATION_LIST: {
................................................................................
112124 112982         pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
112125 112983   
112126 112984         /* Do the b-tree integrity checks */
112127 112985         sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
112128 112986         sqlite3VdbeChangeP5(v, (u8)i);
112129 112987         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
112130 112988         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
112131         -         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
       112989  +         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
112132 112990            P4_DYNAMIC);
112133 112991         sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
112134 112992         sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
112135 112993         sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
112136 112994         sqlite3VdbeJumpHere(v, addr);
112137 112995   
112138 112996         /* Make sure all the indices are constructed correctly.
................................................................................
112563 113421       int i;
112564 113422       setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) );
112565 113423       pParse->nMem = 2;
112566 113424       for(i=0; i<db->nDb; i++){
112567 113425         Btree *pBt;
112568 113426         const char *zState = "unknown";
112569 113427         int j;
112570         -      if( db->aDb[i].zName==0 ) continue;
       113428  +      if( db->aDb[i].zDbSName==0 ) continue;
112571 113429         pBt = db->aDb[i].pBt;
112572 113430         if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
112573 113431           zState = "closed";
112574         -      }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
       113432  +      }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0, 
112575 113433                                        SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
112576 113434            zState = azLockName[j];
112577 113435         }
112578         -      sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zName, zState);
       113436  +      sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
112579 113437         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
112580 113438       }
112581 113439       break;
112582 113440     }
112583 113441   #endif
112584 113442   
112585 113443   #ifdef SQLITE_HAS_CODEC
................................................................................
112707 113565     }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
112708 113566       /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
112709 113567       ** But because db->init.busy is set to 1, no VDBE code is generated
112710 113568       ** or executed.  All the parser does is build the internal data
112711 113569       ** structures that describe the table, index, or view.
112712 113570       */
112713 113571       int rc;
       113572  +    u8 saved_iDb = db->init.iDb;
112714 113573       sqlite3_stmt *pStmt;
112715 113574       TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
112716 113575   
112717 113576       assert( db->init.busy );
112718 113577       db->init.iDb = iDb;
112719 113578       db->init.newTnum = sqlite3Atoi(argv[1]);
112720 113579       db->init.orphanTrigger = 0;
112721 113580       TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
112722 113581       rc = db->errCode;
112723 113582       assert( (rc&0xFF)==(rcp&0xFF) );
112724         -    db->init.iDb = 0;
       113583  +    db->init.iDb = saved_iDb;
       113584  +    assert( saved_iDb==0 || (db->flags & SQLITE_Vacuum)!=0 );
112725 113585       if( SQLITE_OK!=rc ){
112726 113586         if( db->init.orphanTrigger ){
112727 113587           assert( iDb==1 );
112728 113588         }else{
112729 113589           pData->rc = rc;
112730 113590           if( rc==SQLITE_NOMEM ){
112731 113591             sqlite3OomFault(db);
................................................................................
112741 113601       /* If the SQL column is blank it means this is an index that
112742 113602       ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
112743 113603       ** constraint for a CREATE TABLE.  The index should have already
112744 113604       ** been created when we processed the CREATE TABLE.  All we have
112745 113605       ** to do here is record the root page number for that index.
112746 113606       */
112747 113607       Index *pIndex;
112748         -    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
       113608  +    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
112749 113609       if( pIndex==0 ){
112750 113610         /* This can occur if there exists an index on a TEMP table which
112751 113611         ** has the same name as another index on a permanent index.  Since
112752 113612         ** the permanent table is hidden by the TEMP table, we can also
112753 113613         ** safely ignore the index on the permanent table.
112754 113614         */
112755 113615         /* Do Nothing */;
................................................................................
112920 113780     /* Read the schema information out of the schema tables
112921 113781     */
112922 113782     assert( db->init.busy );
112923 113783     {
112924 113784       char *zSql;
112925 113785       zSql = sqlite3MPrintf(db, 
112926 113786           "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
112927         -        db->aDb[iDb].zName, zMasterName);
       113787  +        db->aDb[iDb].zDbSName, zMasterName);
112928 113788   #ifndef SQLITE_OMIT_AUTHORIZATION
112929 113789       {
112930 113790         sqlite3_xauth xAuth;
112931 113791         xAuth = db->xAuth;
112932 113792         db->xAuth = 0;
112933 113793   #endif
112934 113794         rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
................................................................................
113150 114010     const char *zSql,         /* UTF-8 encoded SQL statement. */
113151 114011     int nBytes,               /* Length of zSql in bytes. */
113152 114012     int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
113153 114013     Vdbe *pReprepare,         /* VM being reprepared */
113154 114014     sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
113155 114015     const char **pzTail       /* OUT: End of parsed string */
113156 114016   ){
113157         -  Parse *pParse;            /* Parsing context */
113158 114017     char *zErrMsg = 0;        /* Error message */
113159 114018     int rc = SQLITE_OK;       /* Result code */
113160 114019     int i;                    /* Loop counter */
       114020  +  Parse sParse;             /* Parsing context */
113161 114021   
113162         -  /* Allocate the parsing context */
113163         -  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
113164         -  if( pParse==0 ){
113165         -    rc = SQLITE_NOMEM_BKPT;
113166         -    goto end_prepare;
113167         -  }
113168         -  pParse->pReprepare = pReprepare;
       114022  +  memset(&sParse, 0, PARSE_HDR_SZ);
       114023  +  memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
       114024  +  sParse.pReprepare = pReprepare;
113169 114025     assert( ppStmt && *ppStmt==0 );
113170 114026     /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
113171 114027     assert( sqlite3_mutex_held(db->mutex) );
113172 114028   
113173 114029     /* Check to verify that it is possible to get a read lock on all
113174 114030     ** database schemas.  The inability to get a read lock indicates that
113175 114031     ** some other database connection is holding a write-lock, which in
................................................................................
113195 114051     */
113196 114052     for(i=0; i<db->nDb; i++) {
113197 114053       Btree *pBt = db->aDb[i].pBt;
113198 114054       if( pBt ){
113199 114055         assert( sqlite3BtreeHoldsMutex(pBt) );
113200 114056         rc = sqlite3BtreeSchemaLocked(pBt);
113201 114057         if( rc ){
113202         -        const char *zDb = db->aDb[i].zName;
       114058  +        const char *zDb = db->aDb[i].zDbSName;
113203 114059           sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
113204 114060           testcase( db->flags & SQLITE_ReadUncommitted );
113205 114061           goto end_prepare;
113206 114062         }
113207 114063       }
113208 114064     }
113209 114065   
113210 114066     sqlite3VtabUnlockList(db);
113211 114067   
113212         -  pParse->db = db;
113213         -  pParse->nQueryLoop = 0;  /* Logarithmic, so 0 really means 1 */
       114068  +  sParse.db = db;
113214 114069     if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
113215 114070       char *zSqlCopy;
113216 114071       int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
113217 114072       testcase( nBytes==mxLen );
113218 114073       testcase( nBytes==mxLen+1 );
113219 114074       if( nBytes>mxLen ){
113220 114075         sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
113221 114076         rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
113222 114077         goto end_prepare;
113223 114078       }
113224 114079       zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
113225 114080       if( zSqlCopy ){
113226         -      sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
113227         -      pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
       114081  +      sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
       114082  +      sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
113228 114083         sqlite3DbFree(db, zSqlCopy);
113229 114084       }else{
113230         -      pParse->zTail = &zSql[nBytes];
       114085  +      sParse.zTail = &zSql[nBytes];
113231 114086       }
113232 114087     }else{
113233         -    sqlite3RunParser(pParse, zSql, &zErrMsg);
       114088  +    sqlite3RunParser(&sParse, zSql, &zErrMsg);
113234 114089     }
113235         -  assert( 0==pParse->nQueryLoop );
       114090  +  assert( 0==sParse.nQueryLoop );
113236 114091   
113237         -  if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
113238         -  if( pParse->checkSchema ){
113239         -    schemaIsValid(pParse);
       114092  +  if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
       114093  +  if( sParse.checkSchema ){
       114094  +    schemaIsValid(&sParse);
113240 114095     }
113241 114096     if( db->mallocFailed ){
113242         -    pParse->rc = SQLITE_NOMEM_BKPT;
       114097  +    sParse.rc = SQLITE_NOMEM_BKPT;
113243 114098     }
113244 114099     if( pzTail ){
113245         -    *pzTail = pParse->zTail;
       114100  +    *pzTail = sParse.zTail;
113246 114101     }
113247         -  rc = pParse->rc;
       114102  +  rc = sParse.rc;
113248 114103   
113249 114104   #ifndef SQLITE_OMIT_EXPLAIN
113250         -  if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
       114105  +  if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
113251 114106       static const char * const azColName[] = {
113252 114107          "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
113253 114108          "selectid", "order", "from", "detail"
113254 114109       };
113255 114110       int iFirst, mx;
113256         -    if( pParse->explain==2 ){
113257         -      sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
       114111  +    if( sParse.explain==2 ){
       114112  +      sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
113258 114113         iFirst = 8;
113259 114114         mx = 12;
113260 114115       }else{
113261         -      sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
       114116  +      sqlite3VdbeSetNumCols(sParse.pVdbe, 8);
113262 114117         iFirst = 0;
113263 114118         mx = 8;
113264 114119       }
113265 114120       for(i=iFirst; i<mx; i++){
113266         -      sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
       114121  +      sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
113267 114122                               azColName[i], SQLITE_STATIC);
113268 114123       }
113269 114124     }
113270 114125   #endif
113271 114126   
113272 114127     if( db->init.busy==0 ){
113273         -    Vdbe *pVdbe = pParse->pVdbe;
113274         -    sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
       114128  +    Vdbe *pVdbe = sParse.pVdbe;
       114129  +    sqlite3VdbeSetSql(pVdbe, zSql, (int)(sParse.zTail-zSql), saveSqlFlag);
113275 114130     }
113276         -  if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
113277         -    sqlite3VdbeFinalize(pParse->pVdbe);
       114131  +  if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
       114132  +    sqlite3VdbeFinalize(sParse.pVdbe);
113278 114133       assert(!(*ppStmt));
113279 114134     }else{
113280         -    *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
       114135  +    *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
113281 114136     }
113282 114137   
113283 114138     if( zErrMsg ){
113284 114139       sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
113285 114140       sqlite3DbFree(db, zErrMsg);
113286 114141     }else{
113287 114142       sqlite3Error(db, rc);
113288 114143     }
113289 114144   
113290 114145     /* Delete any TriggerPrg structures allocated while parsing this statement. */
113291         -  while( pParse->pTriggerPrg ){
113292         -    TriggerPrg *pT = pParse->pTriggerPrg;
113293         -    pParse->pTriggerPrg = pT->pNext;
       114146  +  while( sParse.pTriggerPrg ){
       114147  +    TriggerPrg *pT = sParse.pTriggerPrg;
       114148  +    sParse.pTriggerPrg = pT->pNext;
113294 114149       sqlite3DbFree(db, pT);
113295 114150     }
113296 114151   
113297 114152   end_prepare:
113298 114153   
113299         -  sqlite3ParserReset(pParse);
113300         -  sqlite3StackFree(db, pParse);
       114154  +  sqlite3ParserReset(&sParse);
113301 114155     rc = sqlite3ApiExit(db, rc);
113302 114156     assert( (rc&db->errMask)==rc );
113303 114157     return rc;
113304 114158   }
113305 114159   static int sqlite3LockAndPrepare(
113306 114160     sqlite3 *db,              /* Database handle. */
113307 114161     const char *zSql,         /* UTF-8 encoded SQL statement. */
................................................................................
113581 114435   
113582 114436   /*
113583 114437   ** Initialize a SelectDest structure.
113584 114438   */
113585 114439   SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
113586 114440     pDest->eDest = (u8)eDest;
113587 114441     pDest->iSDParm = iParm;
113588         -  pDest->affSdst = 0;
       114442  +  pDest->zAffSdst = 0;
113589 114443     pDest->iSdst = 0;
113590 114444     pDest->nSdst = 0;
113591 114445   }
113592 114446   
113593 114447   
113594 114448   /*
113595 114449   ** Allocate a new Select structure and return a pointer to that
................................................................................
114152 115006     r1 = sqlite3GetTempReg(pParse);
114153 115007     sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
114154 115008     sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
114155 115009     sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
114156 115010     sqlite3ReleaseTempReg(pParse, r1);
114157 115011   }
114158 115012   
114159         -#ifndef SQLITE_OMIT_SUBQUERY
114160         -/*
114161         -** Generate an error message when a SELECT is used within a subexpression
114162         -** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
114163         -** column.  We do this in a subroutine because the error used to occur
114164         -** in multiple places.  (The error only occurs in one place now, but we
114165         -** retain the subroutine to minimize code disruption.)
114166         -*/
114167         -static int checkForMultiColumnSelectError(
114168         -  Parse *pParse,       /* Parse context. */
114169         -  SelectDest *pDest,   /* Destination of SELECT results */
114170         -  int nExpr            /* Number of result columns returned by SELECT */
114171         -){
114172         -  int eDest = pDest->eDest;
114173         -  if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
114174         -    sqlite3ErrorMsg(pParse, "only a single result allowed for "
114175         -       "a SELECT that is part of an expression");
114176         -    return 1;
114177         -  }else{
114178         -    return 0;
114179         -  }
114180         -}
114181         -#endif
114182         -
114183 115013   /*
114184 115014   ** This routine generates the code for the inside of the inner loop
114185 115015   ** of a SELECT.
114186 115016   **
114187 115017   ** If srcTab is negative, then the pEList expressions
114188 115018   ** are evaluated in order to get the data for this row.  If srcTab is
114189 115019   ** zero or more, then data is pulled from srcTab and pEList is used only 
................................................................................
114385 115215   
114386 115216   #ifndef SQLITE_OMIT_SUBQUERY
114387 115217       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
114388 115218       ** then there should be a single item on the stack.  Write this
114389 115219       ** item into the set table with bogus data.
114390 115220       */
114391 115221       case SRT_Set: {
114392         -      assert( nResultCol==1 );
114393         -      pDest->affSdst =
114394         -                  sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
114395 115222         if( pSort ){
114396 115223           /* At first glance you would think we could optimize out the
114397 115224           ** ORDER BY in this case since the order of entries in the set
114398 115225           ** does not matter.  But there might be a LIMIT clause, in which
114399 115226           ** case the order does matter */
114400         -        pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg);
       115227  +        pushOntoSorter(
       115228  +            pParse, pSort, p, regResult, regResult, nResultCol, nPrefixReg);
114401 115229         }else{
114402 115230           int r1 = sqlite3GetTempReg(pParse);
114403         -        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
114404         -        sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
       115231  +        assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
       115232  +        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol, 
       115233  +            r1, pDest->zAffSdst, nResultCol);
       115234  +        sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
114405 115235           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
114406 115236           sqlite3ReleaseTempReg(pParse, r1);
114407 115237         }
114408 115238         break;
114409 115239       }
114410 115240   
114411 115241       /* If any row exist in the result set, record that fact and abort.
................................................................................
114413 115243       case SRT_Exists: {
114414 115244         sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
114415 115245         /* The LIMIT clause will terminate the loop for us */
114416 115246         break;
114417 115247       }
114418 115248   
114419 115249       /* If this is a scalar select that is part of an expression, then
114420         -    ** store the results in the appropriate memory cell and break out
114421         -    ** of the scan loop.
       115250  +    ** store the results in the appropriate memory cell or array of 
       115251  +    ** memory cells and break out of the scan loop.
114422 115252       */
114423 115253       case SRT_Mem: {
114424         -      assert( nResultCol==1 );
       115254  +      assert( nResultCol==pDest->nSdst );
114425 115255         if( pSort ){
114426         -        pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg);
       115256  +        pushOntoSorter(
       115257  +            pParse, pSort, p, regResult, regResult, nResultCol, nPrefixReg);
114427 115258         }else{
114428 115259           assert( regResult==iParm );
114429 115260           /* The LIMIT clause will jump out of the loop for us */
114430 115261         }
114431 115262         break;
114432 115263       }
114433 115264   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
................................................................................
114521 115352   
114522 115353   /*
114523 115354   ** Allocate a KeyInfo object sufficient for an index of N key columns and
114524 115355   ** X extra columns.
114525 115356   */
114526 115357   SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
114527 115358     int nExtra = (N+X)*(sizeof(CollSeq*)+1);
114528         -  KeyInfo *p = sqlite3DbMallocRaw(db, sizeof(KeyInfo) + nExtra);
       115359  +  KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
114529 115360     if( p ){
114530 115361       p->aSortOrder = (u8*)&p->aColl[N+X];
114531 115362       p->nField = (u16)N;
114532 115363       p->nXField = (u16)X;
114533 115364       p->enc = ENC(db);
114534 115365       p->db = db;
114535 115366       p->nRef = 1;
................................................................................
114734 115565     assert( addrBreak<0 );
114735 115566     if( pSort->labelBkOut ){
114736 115567       sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
114737 115568       sqlite3VdbeGoto(v, addrBreak);
114738 115569       sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
114739 115570     }
114740 115571     iTab = pSort->iECursor;
114741         -  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
       115572  +  if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
114742 115573       regRowid = 0;
114743 115574       regRow = pDest->iSdst;
114744 115575       nSortData = nColumn;
114745 115576     }else{
114746 115577       regRowid = sqlite3GetTempReg(pParse);
114747         -    regRow = sqlite3GetTempReg(pParse);
114748         -    nSortData = 1;
       115578  +    regRow = sqlite3GetTempRange(pParse, nColumn);
       115579  +    nSortData = nColumn;
114749 115580     }
114750 115581     nKey = pOrderBy->nExpr - pSort->nOBSat;
114751 115582     if( pSort->sortFlags & SORTFLAG_UseSorter ){
114752 115583       int regSortOut = ++pParse->nMem;
114753 115584       iSortTab = pParse->nTab++;
114754 115585       if( pSort->labelBkOut ){
114755         -      addrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
       115586  +      addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
114756 115587       }
114757 115588       sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData);
114758 115589       if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
114759 115590       addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
114760 115591       VdbeCoverage(v);
114761 115592       codeOffset(v, p->iOffset, addrContinue);
114762 115593       sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
................................................................................
114776 115607         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
114777 115608         sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
114778 115609         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
114779 115610         break;
114780 115611       }
114781 115612   #ifndef SQLITE_OMIT_SUBQUERY
114782 115613       case SRT_Set: {
114783         -      assert( nColumn==1 );
114784         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
114785         -                        &pDest->affSdst, 1);
114786         -      sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
       115614  +      assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
       115615  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
       115616  +                        pDest->zAffSdst, nColumn);
       115617  +      sqlite3ExprCacheAffinityChange(pParse, regRow, nColumn);
114787 115618         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
114788 115619         break;
114789 115620       }
114790 115621       case SRT_Mem: {
114791         -      assert( nColumn==1 );
114792         -      sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
114793 115622         /* The LIMIT clause will terminate the loop for us */
114794 115623         break;
114795 115624       }
114796 115625   #endif
114797 115626       default: {
114798 115627         assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
114799 115628         testcase( eDest==SRT_Output );
................................................................................
114804 115633         }else{
114805 115634           sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
114806 115635         }
114807 115636         break;
114808 115637       }
114809 115638     }
114810 115639     if( regRowid ){
114811         -    sqlite3ReleaseTempReg(pParse, regRow);
       115640  +    if( eDest==SRT_Set ){
       115641  +      sqlite3ReleaseTempRange(pParse, regRow, nColumn);
       115642  +    }else{
       115643  +      sqlite3ReleaseTempReg(pParse, regRow);
       115644  +    }
114812 115645       sqlite3ReleaseTempReg(pParse, regRowid);
114813 115646     }
114814 115647     /* The bottom of the loop
114815 115648     */
114816 115649     sqlite3VdbeResolveLabel(v, addrContinue);
114817 115650     if( pSort->sortFlags & SORTFLAG_UseSorter ){
114818 115651       sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
................................................................................
114951 115784             zOrigCol = pTab->aCol[iCol].zName;
114952 115785             zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
114953 115786             estWidth = pTab->aCol[iCol].szEst;
114954 115787           }
114955 115788           zOrigTab = pTab->zName;
114956 115789           if( pNC->pParse ){
114957 115790             int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
114958         -          zOrigDb = pNC->pParse->db->aDb[iDb].zName;
       115791  +          zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
114959 115792           }
114960 115793   #else
114961 115794           if( iCol<0 ){
114962 115795             zType = "INTEGER";
114963 115796           }else{
114964 115797             zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
114965 115798             estWidth = pTab->aCol[iCol].szEst;
................................................................................
115306 116139   
115307 116140   /*
115308 116141   ** Get a VDBE for the given parser context.  Create a new one if necessary.
115309 116142   ** If an error occurs, return NULL and leave a message in pParse.
115310 116143   */
115311 116144   static SQLITE_NOINLINE Vdbe *allocVdbe(Parse *pParse){
115312 116145     Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
115313         -  if( v ) sqlite3VdbeAddOp0(v, OP_Init);
       116146  +  if( v ) sqlite3VdbeAddOp2(v, OP_Init, 0, 1);
115314 116147     if( pParse->pToplevel==0
115315 116148      && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
115316 116149     ){
115317 116150       pParse->okConstFactor = 1;
115318 116151     }
115319 116152     return v;
115320 116153   }
................................................................................
116145 116978         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
116146 116979         sqlite3ReleaseTempReg(pParse, r2);
116147 116980         sqlite3ReleaseTempReg(pParse, r1);
116148 116981         break;
116149 116982       }
116150 116983   
116151 116984   #ifndef SQLITE_OMIT_SUBQUERY
116152         -    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
116153         -    ** then there should be a single item on the stack.  Write this
116154         -    ** item into the set table with bogus data.
       116985  +    /* If we are creating a set for an "expr IN (SELECT ...)".
116155 116986       */
116156 116987       case SRT_Set: {
116157 116988         int r1;
116158         -      assert( pIn->nSdst==1 || pParse->nErr>0 );
116159         -      pDest->affSdst = 
116160         -         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
       116989  +      testcase( pIn->nSdst>1 );
116161 116990         r1 = sqlite3GetTempReg(pParse);
116162         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
116163         -      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
       116991  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, 
       116992  +          r1, pDest->zAffSdst, pIn->nSdst);
       116993  +      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
116164 116994         sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
116165 116995         sqlite3ReleaseTempReg(pParse, r1);
116166 116996         break;
116167 116997       }
116168 116998   
116169 116999       /* If this is a scalar select that is part of an expression, then
116170 117000       ** store the results in the appropriate memory cell and break out
................................................................................
117212 118042         pSub->pOrderBy = 0;
117213 118043       }
117214 118044       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
117215 118045       if( subqueryIsAgg ){
117216 118046         assert( pParent->pHaving==0 );
117217 118047         pParent->pHaving = pParent->pWhere;
117218 118048         pParent->pWhere = pWhere;
117219         -      pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, 
117220         -                                  sqlite3ExprDup(db, pSub->pHaving, 0));
       118049  +      pParent->pHaving = sqlite3ExprAnd(db, 
       118050  +          sqlite3ExprDup(db, pSub->pHaving, 0), pParent->pHaving
       118051  +      );
117221 118052         assert( pParent->pGroupBy==0 );
117222 118053         pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
117223 118054       }else{
117224         -      pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
       118055  +      pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
117225 118056       }
117226 118057       substSelect(db, pParent, iParent, pSub->pEList, 0);
117227 118058     
117228 118059       /* The flattened query is distinct if either the inner or the
117229 118060       ** outer query is distinct. 
117230 118061       */
117231 118062       pParent->selFlags |= pSub->selFlags & SF_Distinct;
................................................................................
117907 118738             if( db->mallocFailed ) break;
117908 118739             if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
117909 118740               pSub = 0;
117910 118741               if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
117911 118742                 continue;
117912 118743               }
117913 118744               iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
117914         -            zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
       118745  +            zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
117915 118746             }
117916 118747             for(j=0; j<pTab->nCol; j++){
117917 118748               char *zName = pTab->aCol[j].zName;
117918 118749               char *zColname;  /* The computed column name */
117919 118750               char *zToFree;   /* Malloced string that needs to be freed */
117920 118751               Token sColname;  /* Computed column name as a token */
117921 118752   
................................................................................
118390 119221   #if SELECTTRACE_ENABLED
118391 119222     if( sqlite3SelectTrace & 0x100 ){
118392 119223       SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
118393 119224       sqlite3TreeViewSelect(0, p, 0);
118394 119225     }
118395 119226   #endif
118396 119227   
118397         -
118398         -  /* If writing to memory or generating a set
118399         -  ** only a single column may be output.
118400         -  */
118401         -#ifndef SQLITE_OMIT_SUBQUERY
118402         -  if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){
118403         -    goto select_end;
118404         -  }
118405         -#endif
118406         -
118407 119228     /* Try to flatten subqueries in the FROM clause up into the main query
118408 119229     */
118409 119230   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
118410 119231     for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
118411 119232       struct SrcList_item *pItem = &pTabList->a[i];
118412 119233       Select *pSub = pItem->pSelect;
118413 119234       int isAggSub;
................................................................................
118554 119375         pItem->regReturn = ++pParse->nMem;
118555 119376         topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
118556 119377         pItem->addrFillSub = topAddr+1;
118557 119378         if( pItem->fg.isCorrelated==0 ){
118558 119379           /* If the subquery is not correlated and if we are not inside of
118559 119380           ** a trigger, then we only need to compute the value of the subquery
118560 119381           ** once. */
118561         -        onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
       119382  +        onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
118562 119383           VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
118563 119384         }else{
118564 119385           VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
118565 119386         }
118566 119387         sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
118567 119388         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
118568 119389         sqlite3Select(pParse, pSub, &dest);
................................................................................
119501 120322     Trigger *pTrigger = 0;  /* The new trigger */
119502 120323     Table *pTab;            /* Table that the trigger fires off of */
119503 120324     char *zName = 0;        /* Name of the trigger */
119504 120325     sqlite3 *db = pParse->db;  /* The database connection */
119505 120326     int iDb;                /* The database to store the trigger in */
119506 120327     Token *pName;           /* The unqualified db name */
119507 120328     DbFixer sFix;           /* State vector for the DB fixer */
119508         -  int iTabDb;             /* Index of the database holding pTab */
119509 120329   
119510 120330     assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
119511 120331     assert( pName2!=0 );
119512 120332     assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
119513 120333     assert( op>0 && op<0xff );
119514 120334     if( isTemp ){
119515 120335       /* If TEMP was specified, then the trigger name may not be qualified. */
................................................................................
119614 120434       goto trigger_cleanup;
119615 120435     }
119616 120436     if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
119617 120437       sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
119618 120438           " trigger on table: %S", pTableName, 0);
119619 120439       goto trigger_cleanup;
119620 120440     }
119621         -  iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119622 120441   
119623 120442   #ifndef SQLITE_OMIT_AUTHORIZATION
119624 120443     {
       120444  +    int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119625 120445       int code = SQLITE_CREATE_TRIGGER;
119626         -    const char *zDb = db->aDb[iTabDb].zName;
119627         -    const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
       120446  +    const char *zDb = db->aDb[iTabDb].zDbSName;
       120447  +    const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
119628 120448       if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
119629 120449       if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
119630 120450         goto trigger_cleanup;
119631 120451       }
119632 120452       if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
119633 120453         goto trigger_cleanup;
119634 120454       }
................................................................................
119714 120534       /* Make an entry in the sqlite_master table */
119715 120535       v = sqlite3GetVdbe(pParse);
119716 120536       if( v==0 ) goto triggerfinish_cleanup;
119717 120537       sqlite3BeginWriteOperation(pParse, 0, iDb);
119718 120538       z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
119719 120539       sqlite3NestedParse(pParse,
119720 120540          "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
119721         -       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
       120541  +       db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb), zName,
119722 120542          pTrig->table, z);
119723 120543       sqlite3DbFree(db, z);
119724 120544       sqlite3ChangeCookie(pParse, iDb);
119725 120545       sqlite3VdbeAddParseSchemaOp(v, iDb,
119726 120546           sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
119727 120547     }
119728 120548   
................................................................................
119903 120723   
119904 120724     assert( pName->nSrc==1 );
119905 120725     zDb = pName->a[0].zDatabase;
119906 120726     zName = pName->a[0].zName;
119907 120727     assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
119908 120728     for(i=OMIT_TEMPDB; i<db->nDb; i++){
119909 120729       int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
119910         -    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
       120730  +    if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
119911 120731       assert( sqlite3SchemaMutexHeld(db, j, 0) );
119912 120732       pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
119913 120733       if( pTrigger ) break;
119914 120734     }
119915 120735     if( !pTrigger ){
119916 120736       if( !noErr ){
119917 120737         sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
................................................................................
119949 120769     assert( iDb>=0 && iDb<db->nDb );
119950 120770     pTable = tableOfTrigger(pTrigger);
119951 120771     assert( pTable );
119952 120772     assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
119953 120773   #ifndef SQLITE_OMIT_AUTHORIZATION
119954 120774     {
119955 120775       int code = SQLITE_DROP_TRIGGER;
119956         -    const char *zDb = db->aDb[iDb].zName;
       120776  +    const char *zDb = db->aDb[iDb].zDbSName;
119957 120777       const char *zTab = SCHEMA_TABLE(iDb);
119958 120778       if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
119959 120779       if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
119960 120780         sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
119961 120781         return;
119962 120782       }
119963 120783     }
................................................................................
119965 120785   
119966 120786     /* Generate code to destroy the database record of the trigger.
119967 120787     */
119968 120788     assert( pTable!=0 );
119969 120789     if( (v = sqlite3GetVdbe(pParse))!=0 ){
119970 120790       sqlite3NestedParse(pParse,
119971 120791          "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
119972         -       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrigger->zName
       120792  +       db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb), pTrigger->zName
119973 120793       );
119974 120794       sqlite3ChangeCookie(pParse, iDb);
119975 120795       sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
119976 120796     }
119977 120797   }
119978 120798   
119979 120799   /*
................................................................................
120068 120888   
120069 120889     pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
120070 120890     if( pSrc ){
120071 120891       assert( pSrc->nSrc>0 );
120072 120892       pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
120073 120893       iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
120074 120894       if( iDb==0 || iDb>=2 ){
       120895  +      const char *zDb;
120075 120896         assert( iDb<db->nDb );
120076         -      pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
       120897  +      zDb = db->aDb[iDb].zDbSName;
       120898  +      pSrc->a[pSrc->nSrc-1].zDatabase =  sqlite3DbStrDup(db, zDb);
120077 120899       }
120078 120900     }
120079 120901     return pSrc;
120080 120902   }
120081 120903   
120082 120904   /*
120083 120905   ** Generate VDBE code for the statements inside the body of a single 
................................................................................
120283 121105   
120284 121106       transferParseError(pParse, pSubParse);
120285 121107       if( db->mallocFailed==0 ){
120286 121108         pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
120287 121109       }
120288 121110       pProgram->nMem = pSubParse->nMem;
120289 121111       pProgram->nCsr = pSubParse->nTab;
120290         -    pProgram->nOnce = pSubParse->nOnce;
120291 121112       pProgram->token = (void *)pTrigger;
120292 121113       pPrg->aColmask[0] = pSubParse->oldmask;
120293 121114       pPrg->aColmask[1] = pSubParse->newmask;
120294 121115       sqlite3VdbeDelete(v);
120295 121116     }
120296 121117   
120297 121118     assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
................................................................................
120756 121577         }
120757 121578       }
120758 121579   #ifndef SQLITE_OMIT_AUTHORIZATION
120759 121580       {
120760 121581         int rc;
120761 121582         rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
120762 121583                               j<0 ? "ROWID" : pTab->aCol[j].zName,
120763         -                            db->aDb[iDb].zName);
       121584  +                            db->aDb[iDb].zDbSName);
120764 121585         if( rc==SQLITE_DENY ){
120765 121586           goto update_cleanup;
120766 121587         }else if( rc==SQLITE_IGNORE ){
120767 121588           aXRef[j] = -1;
120768 121589         }
120769 121590       }
120770 121591   #endif
................................................................................
121358 122179   ** Most of the code in this file may be omitted by defining the
121359 122180   ** SQLITE_OMIT_VACUUM macro.
121360 122181   */
121361 122182   /* #include "sqliteInt.h" */
121362 122183   /* #include "vdbeInt.h" */
121363 122184   
121364 122185   #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
121365         -/*
121366         -** Finalize a prepared statement.  If there was an error, store the
121367         -** text of the error message in *pzErrMsg.  Return the result code.
121368         -*/
121369         -static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
121370         -  int rc;
121371         -  rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
121372         -  if( rc ){
121373         -    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
121374         -  }
121375         -  return rc;
121376         -}
121377 122186   
121378 122187   /*
121379         -** Execute zSql on database db. Return an error code.
       122188  +** Execute zSql on database db.
       122189  +**
       122190  +** If zSql returns rows, then each row will have exactly one
       122191  +** column.  (This will only happen if zSql begins with "SELECT".)
       122192  +** Take each row of result and call execSql() again recursively.
       122193  +**
       122194  +** The execSqlF() routine does the same thing, except it accepts
       122195  +** a format string as its third argument
121380 122196   */
121381 122197   static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
121382 122198     sqlite3_stmt *pStmt;
121383         -  VVA_ONLY( int rc; )
121384         -  if( !zSql ){
121385         -    return SQLITE_NOMEM_BKPT;
       122199  +  int rc;
       122200  +
       122201  +  /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
       122202  +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
       122203  +  if( rc!=SQLITE_OK ) return rc;
       122204  +  while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
       122205  +    const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
       122206  +    assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
       122207  +    if( zSubSql ){
       122208  +      assert( zSubSql[0]!='S' );
       122209  +      rc = execSql(db, pzErrMsg, zSubSql);
       122210  +      if( rc!=SQLITE_OK ) break;
       122211  +    }
121386 122212     }
121387         -  if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
       122213  +  assert( rc!=SQLITE_ROW );
       122214  +  if( rc==SQLITE_DONE ) rc = SQLITE_OK;
       122215  +  if( rc ){
121388 122216       sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
121389         -    return sqlite3_errcode(db);
121390         -  }
121391         -  VVA_ONLY( rc = ) sqlite3_step(pStmt);
121392         -  assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
121393         -  return vacuumFinalize(db, pStmt, pzErrMsg);
121394         -}
121395         -
121396         -/*
121397         -** Execute zSql on database db. The statement returns exactly
121398         -** one column. Execute this as SQL on the same database.
121399         -*/
121400         -static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
121401         -  sqlite3_stmt *pStmt;
121402         -  int rc;
121403         -
121404         -  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
121405         -  if( rc!=SQLITE_OK ) return rc;
121406         -
121407         -  while( SQLITE_ROW==sqlite3_step(pStmt) ){
121408         -    rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
121409         -    if( rc!=SQLITE_OK ){
121410         -      vacuumFinalize(db, pStmt, pzErrMsg);
121411         -      return rc;
121412         -    }
121413         -  }
121414         -
121415         -  return vacuumFinalize(db, pStmt, pzErrMsg);
       122217  +  }
       122218  +  (void)sqlite3_finalize(pStmt);
       122219  +  return rc;
       122220  +}
       122221  +static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
       122222  +  char *z;
       122223  +  va_list ap;
       122224  +  int rc;
       122225  +  va_start(ap, zSql);
       122226  +  z = sqlite3VMPrintf(db, zSql, ap);
       122227  +  va_end(ap);
       122228  +  if( z==0 ) return SQLITE_NOMEM;
       122229  +  rc = execSql(db, pzErrMsg, z);
       122230  +  sqlite3DbFree(db, z);
       122231  +  return rc;
121416 122232   }
121417 122233   
121418 122234   /*
121419 122235   ** The VACUUM command is used to clean up the database,
121420 122236   ** collapse free space, etc.  It is modelled after the VACUUM command
121421 122237   ** in PostgreSQL.  The VACUUM command works as follows:
121422 122238   **
................................................................................
121441 122257   ** the copy of step (3) were replaced by deleting the original database
121442 122258   ** and renaming the transient database as the original.  But that will
121443 122259   ** not work if other processes are attached to the original database.
121444 122260   ** And a power loss in between deleting the original and renaming the
121445 122261   ** transient would cause the database file to appear to be deleted
121446 122262   ** following reboot.
121447 122263   */
121448         -SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
       122264  +SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm){
121449 122265     Vdbe *v = sqlite3GetVdbe(pParse);
121450         -  if( v ){
121451         -    sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
121452         -    sqlite3VdbeUsesBtree(v, 0);
       122266  +  int iDb = pNm ? sqlite3TwoPartName(pParse, pNm, pNm, &pNm) : 0;
       122267  +  if( v && (iDb>=2 || iDb==0) ){
       122268  +    sqlite3VdbeAddOp1(v, OP_Vacuum, iDb);
       122269  +    sqlite3VdbeUsesBtree(v, iDb);
121453 122270     }
121454 122271     return;
121455 122272   }
121456 122273   
121457 122274   /*
121458 122275   ** This routine implements the OP_Vacuum opcode of the VDBE.
121459 122276   */
121460         -SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
       122277  +SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db, int iDb){
121461 122278     int rc = SQLITE_OK;     /* Return code from service routines */
121462 122279     Btree *pMain;           /* The database being vacuumed */
121463 122280     Btree *pTemp;           /* The temporary database we vacuum into */
121464         -  char *zSql = 0;         /* SQL statements */
121465 122281     int saved_flags;        /* Saved value of the db->flags */
121466 122282     int saved_nChange;      /* Saved value of db->nChange */
121467 122283     int saved_nTotalChange; /* Saved value of db->nTotalChange */
121468 122284     u8 saved_mTrace;        /* Saved trace settings */
121469 122285     Db *pDb = 0;            /* Database to detach at end of vacuum */
121470 122286     int isMemDb;            /* True if vacuuming a :memory: database */
121471 122287     int nRes;               /* Bytes of reserved space at the end of each page */
121472 122288     int nDb;                /* Number of attached databases */
       122289  +  const char *zDbMain;    /* Schema name of database to vacuum */
121473 122290   
121474 122291     if( !db->autoCommit ){
121475 122292       sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
121476 122293       return SQLITE_ERROR;
121477 122294     }
121478 122295     if( db->nVdbeActive>1 ){
121479 122296       sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
................................................................................
121483 122300     /* Save the current value of the database flags so that it can be 
121484 122301     ** restored before returning. Then set the writable-schema flag, and
121485 122302     ** disable CHECK and foreign key constraints.  */
121486 122303     saved_flags = db->flags;
121487 122304     saved_nChange = db->nChange;
121488 122305     saved_nTotalChange = db->nTotalChange;
121489 122306     saved_mTrace = db->mTrace;
121490         -  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
121491         -  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
       122307  +  db->flags |= (SQLITE_WriteSchema | SQLITE_IgnoreChecks
       122308  +                 | SQLITE_PreferBuiltin | SQLITE_Vacuum);
       122309  +  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
121492 122310     db->mTrace = 0;
121493 122311   
121494         -  pMain = db->aDb[0].pBt;
       122312  +  zDbMain = db->aDb[iDb].zDbSName;
       122313  +  pMain = db->aDb[iDb].pBt;
121495 122314     isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
121496 122315   
121497 122316     /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
121498 122317     ** can be set to 'off' for this file, as it is not recovered if a crash
121499 122318     ** occurs anyway. The integrity of the database is maintained by a
121500 122319     ** (possibly synchronous) transaction opened on the main database before
121501 122320     ** sqlite3BtreeCopyFile() is called.
................................................................................
121505 122324     ** that actually made the VACUUM run slower.  Very little journalling
121506 122325     ** actually occurs when doing a vacuum since the vacuum_db is initially
121507 122326     ** empty.  Only the journal header is written.  Apparently it takes more
121508 122327     ** time to parse and run the PRAGMA to turn journalling off than it does
121509 122328     ** to write the journal header file.
121510 122329     */
121511 122330     nDb = db->nDb;
121512         -  if( sqlite3TempInMemory(db) ){
121513         -    zSql = "ATTACH ':memory:' AS vacuum_db;";
121514         -  }else{
121515         -    zSql = "ATTACH '' AS vacuum_db;";
121516         -  }
121517         -  rc = execSql(db, pzErrMsg, zSql);
121518         -  if( db->nDb>nDb ){
121519         -    pDb = &db->aDb[db->nDb-1];
121520         -    assert( strcmp(pDb->zName,"vacuum_db")==0 );
121521         -  }
       122331  +  rc = execSql(db, pzErrMsg, "ATTACH''AS vacuum_db");
121522 122332     if( rc!=SQLITE_OK ) goto end_of_vacuum;
121523         -  pTemp = db->aDb[db->nDb-1].pBt;
       122333  +  assert( (db->nDb-1)==nDb );
       122334  +  pDb = &db->aDb[nDb];
       122335  +  assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
       122336  +  pTemp = pDb->pBt;
121524 122337   
121525 122338     /* The call to execSql() to attach the temp database has left the file
121526 122339     ** locked (as there was more than one active statement when the transaction
121527 122340     ** to read the schema was concluded. Unlock it here so that this doesn't
121528 122341     ** cause problems for the call to BtreeSetPageSize() below.  */
121529 122342     sqlite3BtreeCommit(pTemp);
121530 122343   
................................................................................
121537 122350       int nKey;
121538 122351       char *zKey;
121539 122352       sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
121540 122353       if( nKey ) db->nextPagesize = 0;
121541 122354     }
121542 122355   #endif
121543 122356   
121544         -  sqlite3BtreeSetCacheSize(pTemp, db->aDb[0].pSchema->cache_size);
       122357  +  sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
121545 122358     sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
121546         -  rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
121547         -  if( rc!=SQLITE_OK ) goto end_of_vacuum;
       122359  +  sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF);
121548 122360   
121549 122361     /* Begin a transaction and take an exclusive lock on the main database
121550 122362     ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
121551 122363     ** to ensure that we do not try to change the page-size on a WAL database.
121552 122364     */
121553         -  rc = execSql(db, pzErrMsg, "BEGIN;");
       122365  +  rc = execSql(db, pzErrMsg, "BEGIN");
121554 122366     if( rc!=SQLITE_OK ) goto end_of_vacuum;
121555 122367     rc = sqlite3BtreeBeginTrans(pMain, 2);
121556 122368     if( rc!=SQLITE_OK ) goto end_of_vacuum;
121557 122369   
121558 122370     /* Do not attempt to change the page size for a WAL database */
121559 122371     if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
121560 122372                                                  ==PAGER_JOURNALMODE_WAL ){
................................................................................
121573 122385     sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
121574 122386                                              sqlite3BtreeGetAutoVacuum(pMain));
121575 122387   #endif
121576 122388   
121577 122389     /* Query the schema of the main database. Create a mirror schema
121578 122390     ** in the temporary database.
121579 122391     */
121580         -  rc = execExecSql(db, pzErrMsg,
121581         -      "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
121582         -      "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
121583         -      "   AND coalesce(rootpage,1)>0"
       122392  +  db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
       122393  +  rc = execSqlF(db, pzErrMsg,
       122394  +      "SELECT sql FROM \"%w\".sqlite_master"
       122395  +      " WHERE type='table'AND name<>'sqlite_sequence'"
       122396  +      " AND coalesce(rootpage,1)>0",
       122397  +      zDbMain
       122398  +  );
       122399  +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
       122400  +  rc = execSqlF(db, pzErrMsg,
       122401  +      "SELECT sql FROM \"%w\".sqlite_master"
       122402  +      " WHERE type='index' AND length(sql)>10",
       122403  +      zDbMain
121584 122404     );
121585 122405     if( rc!=SQLITE_OK ) goto end_of_vacuum;
121586         -  rc = execExecSql(db, pzErrMsg,
121587         -      "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
121588         -      "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
121589         -  if( rc!=SQLITE_OK ) goto end_of_vacuum;
121590         -  rc = execExecSql(db, pzErrMsg,
121591         -      "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
121592         -      "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
121593         -  if( rc!=SQLITE_OK ) goto end_of_vacuum;
       122406  +  db->init.iDb = 0;
121594 122407   
121595 122408     /* Loop through the tables in the main database. For each, do
121596 122409     ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
121597 122410     ** the contents to the temporary database.
121598 122411     */
121599         -  assert( (db->flags & SQLITE_Vacuum)==0 );
121600         -  db->flags |= SQLITE_Vacuum;
121601         -  rc = execExecSql(db, pzErrMsg,
121602         -      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
121603         -      "|| ' SELECT * FROM main.' || quote(name) || ';'"
121604         -      "FROM main.sqlite_master "
121605         -      "WHERE type = 'table' AND name!='sqlite_sequence' "
121606         -      "  AND coalesce(rootpage,1)>0"
       122412  +  rc = execSqlF(db, pzErrMsg,
       122413  +      "SELECT'INSERT INTO vacuum_db.'||quote(name)"
       122414  +      "||' SELECT*FROM\"%w\".'||quote(name)"
       122415  +      "FROM vacuum_db.sqlite_master "
       122416  +      "WHERE type='table'AND coalesce(rootpage,1)>0",
       122417  +      zDbMain
121607 122418     );
121608 122419     assert( (db->flags & SQLITE_Vacuum)!=0 );
121609 122420     db->flags &= ~SQLITE_Vacuum;
121610 122421     if( rc!=SQLITE_OK ) goto end_of_vacuum;
121611         -
121612         -  /* Copy over the sequence table
121613         -  */
121614         -  rc = execExecSql(db, pzErrMsg,
121615         -      "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
121616         -      "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
121617         -  );
121618         -  if( rc!=SQLITE_OK ) goto end_of_vacuum;
121619         -  rc = execExecSql(db, pzErrMsg,
121620         -      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
121621         -      "|| ' SELECT * FROM main.' || quote(name) || ';' "
121622         -      "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
121623         -  );
121624         -  if( rc!=SQLITE_OK ) goto end_of_vacuum;
121625         -
121626 122422   
121627 122423     /* Copy the triggers, views, and virtual tables from the main database
121628 122424     ** over to the temporary database.  None of these objects has any
121629 122425     ** associated storage, so all we have to do is copy their entries
121630 122426     ** from the SQLITE_MASTER table.
121631 122427     */
121632         -  rc = execSql(db, pzErrMsg,
121633         -      "INSERT INTO vacuum_db.sqlite_master "
121634         -      "  SELECT type, name, tbl_name, rootpage, sql"
121635         -      "    FROM main.sqlite_master"
121636         -      "   WHERE type='view' OR type='trigger'"
121637         -      "      OR (type='table' AND rootpage=0)"
       122428  +  rc = execSqlF(db, pzErrMsg,
       122429  +      "INSERT INTO vacuum_db.sqlite_master"
       122430  +      " SELECT*FROM \"%w\".sqlite_master"
       122431  +      " WHERE type IN('view','trigger')"
       122432  +      " OR(type='table'AND rootpage=0)",
       122433  +      zDbMain
121638 122434     );
121639 122435     if( rc ) goto end_of_vacuum;
121640 122436   
121641 122437     /* At this point, there is a write transaction open on both the 
121642 122438     ** vacuum database and the main database. Assuming no error occurs,
121643 122439     ** both transactions are closed by this block - the main database
121644 122440     ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
................................................................................
121684 122480     }
121685 122481   
121686 122482     assert( rc==SQLITE_OK );
121687 122483     rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
121688 122484   
121689 122485   end_of_vacuum:
121690 122486     /* Restore the original value of db->flags */
       122487  +  db->init.iDb = 0;
121691 122488     db->flags = saved_flags;
121692 122489     db->nChange = saved_nChange;
121693 122490     db->nTotalChange = saved_nTotalChange;
121694 122491     db->mTrace = saved_mTrace;
121695 122492     sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
121696 122493   
121697 122494     /* Currently there is an SQL level transaction open on the vacuum
................................................................................
122062 122859     /* Creating a virtual table invokes the authorization callback twice.
122063 122860     ** The first invocation, to obtain permission to INSERT a row into the
122064 122861     ** sqlite_master table, has already been made by sqlite3StartTable().
122065 122862     ** The second call, to obtain permission to create the table, is made now.
122066 122863     */
122067 122864     if( pTable->azModuleArg ){
122068 122865       sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
122069         -            pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
       122866  +            pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
122070 122867     }
122071 122868   #endif
122072 122869   }
122073 122870   
122074 122871   /*
122075 122872   ** This routine takes the module argument that has been accumulating
122076 122873   ** in pParse->zArg[] and appends it to the list of arguments on the
................................................................................
122126 122923       ** by sqlite3StartTable().
122127 122924       */
122128 122925       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
122129 122926       sqlite3NestedParse(pParse,
122130 122927         "UPDATE %Q.%s "
122131 122928            "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
122132 122929          "WHERE rowid=#%d",
122133         -      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
       122930  +      db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb),
122134 122931         pTab->zName,
122135 122932         pTab->zName,
122136 122933         zStmt,
122137 122934         pParse->regRowid
122138 122935       );
122139 122936       sqlite3DbFree(db, zStmt);
122140 122937       v = sqlite3GetVdbe(pParse);
................................................................................
122236 123033       sqlite3DbFree(db, zModuleName);
122237 123034       return SQLITE_NOMEM_BKPT;
122238 123035     }
122239 123036     pVTable->db = db;
122240 123037     pVTable->pMod = pMod;
122241 123038   
122242 123039     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
122243         -  pTab->azModuleArg[1] = db->aDb[iDb].zName;
       123040  +  pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
122244 123041   
122245 123042     /* Invoke the virtual table constructor */
122246 123043     assert( &db->pVtabCtx );
122247 123044     assert( xConstruct );
122248 123045     sCtx.pTab = pTab;
122249 123046     sCtx.pVTable = pVTable;
122250 123047     sCtx.pPrior = db->pVtabCtx;
................................................................................
122390 123187     sqlite3VtabLock(pVTab);
122391 123188   }
122392 123189   
122393 123190   /*
122394 123191   ** This function is invoked by the vdbe to call the xCreate method
122395 123192   ** of the virtual table named zTab in database iDb. 
122396 123193   **
122397         -** If an error occurs, *pzErr is set to point an an English language
       123194  +** If an error occurs, *pzErr is set to point to an English language
122398 123195   ** description of the error and an SQLITE_XXX error code is returned.
122399 123196   ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
122400 123197   */
122401 123198   SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
122402 123199     int rc = SQLITE_OK;
122403 123200     Table *pTab;
122404 123201     Module *pMod;
122405 123202     const char *zMod;
122406 123203   
122407         -  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
       123204  +  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
122408 123205     assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
122409 123206   
122410 123207     /* Locate the required virtual table module */
122411 123208     zMod = pTab->azModuleArg[0];
122412 123209     pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
122413 123210   
122414 123211     /* If the module has been registered and includes a Create method, 
................................................................................
122524 123321   **
122525 123322   ** This call is a no-op if zTab is not a virtual table.
122526 123323   */
122527 123324   SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
122528 123325     int rc = SQLITE_OK;
122529 123326     Table *pTab;
122530 123327   
122531         -  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
       123328  +  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
122532 123329     if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
122533 123330       VTable *p;
122534 123331       int (*xDestroy)(sqlite3_vtab *);
122535 123332       for(p=pTab->pVTable; p; p=p->pNext){
122536 123333         assert( p->pVtab );
122537 123334         if( p->pVtab->nRef>0 ){
122538 123335           return SQLITE_LOCKED;
................................................................................
123091 123888     u8 iSortIdx;          /* Sorting index number.  0==None */
123092 123889     LogEst rSetup;        /* One-time setup cost (ex: create transient index) */
123093 123890     LogEst rRun;          /* Cost of running each loop */
123094 123891     LogEst nOut;          /* Estimated number of output rows */
123095 123892     union {
123096 123893       struct {               /* Information for internal btree tables */
123097 123894         u16 nEq;               /* Number of equality constraints */
       123895  +      u16 nBtm;              /* Size of BTM vector */
       123896  +      u16 nTop;              /* Size of TOP vector */
123098 123897         Index *pIndex;         /* Index used, or NULL */
123099 123898       } btree;
123100 123899       struct {               /* Information for virtual tables */
123101 123900         int idxNum;            /* Index number */
123102 123901         u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
123103 123902         i8 isOrdered;          /* True if satisfies ORDER BY */
123104 123903         u16 omitMask;          /* Terms that may be omitted */
................................................................................
123213 124012   **
123214 124013   ** The number of terms in a join is limited by the number of bits
123215 124014   ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
123216 124015   ** is only able to process joins with 64 or fewer tables.
123217 124016   */
123218 124017   struct WhereTerm {
123219 124018     Expr *pExpr;            /* Pointer to the subexpression that is this term */
       124019  +  WhereClause *pWC;       /* The clause this term is part of */
       124020  +  LogEst truthProb;       /* Probability of truth for this expression */
       124021  +  u16 wtFlags;            /* TERM_xxx bit flags.  See below */
       124022  +  u16 eOperator;          /* A WO_xx value describing <op> */
       124023  +  u8 nChild;              /* Number of children that must disable us */
       124024  +  u8 eMatchOp;            /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
123220 124025     int iParent;            /* Disable pWC->a[iParent] when this term disabled */
123221 124026     int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
       124027  +  int iField;             /* Field in (?,?,?) IN (SELECT...) vector */
123222 124028     union {
123223 124029       int leftColumn;         /* Column number of X in "X <op> <expr>" */
123224 124030       WhereOrInfo *pOrInfo;   /* Extra information if (eOperator & WO_OR)!=0 */
123225 124031       WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
123226 124032     } u;
123227         -  LogEst truthProb;       /* Probability of truth for this expression */
123228         -  u16 eOperator;          /* A WO_xx value describing <op> */
123229         -  u16 wtFlags;            /* TERM_xxx bit flags.  See below */
123230         -  u8 nChild;              /* Number of children that must disable us */
123231         -  u8 eMatchOp;            /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
123232         -  WhereClause *pWC;       /* The clause this term is part of */
123233 124033     Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
123234 124034     Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
123235 124035   };
123236 124036   
123237 124037   /*
123238 124038   ** Allowed values of WhereTerm.wtFlags
123239 124039   */
................................................................................
123378 124178   ** planner.
123379 124179   */
123380 124180   struct WhereInfo {
123381 124181     Parse *pParse;            /* Parsing and code generating context */
123382 124182     SrcList *pTabList;        /* List of tables in the join */
123383 124183     ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
123384 124184     ExprList *pDistinctSet;   /* DISTINCT over all these values */
123385         -  WhereLoop *pLoops;        /* List of all WhereLoop objects */
123386         -  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
123387         -  LogEst nRowOut;           /* Estimated number of output rows */
123388 124185     LogEst iLimit;            /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
       124186  +  int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
       124187  +  int iContinue;            /* Jump here to continue with next record */
       124188  +  int iBreak;               /* Jump here to break out of the loop */
       124189  +  int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
123389 124190     u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
       124191  +  u8 nLevel;                /* Number of nested loop */
123390 124192     i8 nOBSat;                /* Number of ORDER BY terms satisfied by indices */
123391 124193     u8 sorted;                /* True if really sorted (not just grouped) */
123392 124194     u8 eOnePass;              /* ONEPASS_OFF, or _SINGLE, or _MULTI */
123393 124195     u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
123394 124196     u8 eDistinct;             /* One of the WHERE_DISTINCT_* values */
123395         -  u8 nLevel;                /* Number of nested loop */
123396 124197     u8 bOrderedInnerLoop;     /* True if only the inner-most loop is ordered */
123397 124198     int iTop;                 /* The very beginning of the WHERE loop */
123398         -  int iContinue;            /* Jump here to continue with next record */
123399         -  int iBreak;               /* Jump here to break out of the loop */
123400         -  int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
123401         -  int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
123402         -  WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
       124199  +  WhereLoop *pLoops;        /* List of all WhereLoop objects */
       124200  +  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
       124201  +  LogEst nRowOut;           /* Estimated number of output rows */
123403 124202     WhereClause sWC;          /* Decomposition of the WHERE clause */
       124203  +  WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
123404 124204     WhereLevel a[1];          /* Information about each nest loop in WHERE */
123405 124205   };
123406 124206   
123407 124207   /*
123408 124208   ** Private interfaces - callable only by other where.c routines.
123409 124209   **
123410 124210   ** where.c:
................................................................................
123520 124320   #define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/
123521 124321   #define WHERE_PARTIALIDX   0x00020000  /* The automatic index is partial */
123522 124322   
123523 124323   /************** End of whereInt.h ********************************************/
123524 124324   /************** Continuing where we left off in wherecode.c ******************/
123525 124325   
123526 124326   #ifndef SQLITE_OMIT_EXPLAIN
       124327  +
       124328  +/*
       124329  +** Return the name of the i-th column of the pIdx index.
       124330  +*/
       124331  +static const char *explainIndexColumnName(Index *pIdx, int i){
       124332  +  i = pIdx->aiColumn[i];
       124333  +  if( i==XN_EXPR ) return "<expr>";
       124334  +  if( i==XN_ROWID ) return "rowid";
       124335  +  return pIdx->pTable->aCol[i].zName;
       124336  +}
       124337  +
123527 124338   /*
123528 124339   ** This routine is a helper for explainIndexRange() below
123529 124340   **
123530 124341   ** pStr holds the text of an expression that we are building up one term
123531 124342   ** at a time.  This routine adds a new term to the end of the expression.
123532 124343   ** Terms are separated by AND so add the "AND" text for second and subsequent
123533 124344   ** terms only.
123534 124345   */
123535 124346   static void explainAppendTerm(
123536 124347     StrAccum *pStr,             /* The text expression being built */
123537         -  int iTerm,                  /* Index of this term.  First is zero */
123538         -  const char *zColumn,        /* Name of the column */
       124348  +  Index *pIdx,                /* Index to read column names from */
       124349  +  int nTerm,                  /* Number of terms */
       124350  +  int iTerm,                  /* Zero-based index of first term. */
       124351  +  int bAnd,                   /* Non-zero to append " AND " */
123539 124352     const char *zOp             /* Name of the operator */
123540 124353   ){
123541         -  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
123542         -  sqlite3StrAccumAppendAll(pStr, zColumn);
       124354  +  int i;
       124355  +
       124356  +  assert( nTerm>=1 );
       124357  +  if( bAnd ) sqlite3StrAccumAppend(pStr, " AND ", 5);
       124358  +
       124359  +  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, "(", 1);
       124360  +  for(i=0; i<nTerm; i++){
       124361  +    if( i ) sqlite3StrAccumAppend(pStr, ",", 1);
       124362  +    sqlite3StrAccumAppendAll(pStr, explainIndexColumnName(pIdx, iTerm+i));
       124363  +  }
       124364  +  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, ")", 1);
       124365  +
123543 124366     sqlite3StrAccumAppend(pStr, zOp, 1);
123544         -  sqlite3StrAccumAppend(pStr, "?", 1);
123545         -}
123546 124367   
123547         -/*
123548         -** Return the name of the i-th column of the pIdx index.
123549         -*/
123550         -static const char *explainIndexColumnName(Index *pIdx, int i){
123551         -  i = pIdx->aiColumn[i];
123552         -  if( i==XN_EXPR ) return "<expr>";
123553         -  if( i==XN_ROWID ) return "rowid";
123554         -  return pIdx->pTable->aCol[i].zName;
       124368  +  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, "(", 1);
       124369  +  for(i=0; i<nTerm; i++){
       124370  +    if( i ) sqlite3StrAccumAppend(pStr, ",", 1);
       124371  +    sqlite3StrAccumAppend(pStr, "?", 1);
       124372  +  }
       124373  +  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, ")", 1);
123555 124374   }
123556 124375   
123557 124376   /*
123558 124377   ** Argument pLevel describes a strategy for scanning table pTab. This 
123559 124378   ** function appends text to pStr that describes the subset of table
123560 124379   ** rows scanned by the strategy in the form of an SQL expression.
123561 124380   **
................................................................................
123580 124399       const char *z = explainIndexColumnName(pIndex, i);
123581 124400       if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
123582 124401       sqlite3XPrintf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
123583 124402     }
123584 124403   
123585 124404     j = i;
123586 124405     if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
123587         -    const char *z = explainIndexColumnName(pIndex, i);
123588         -    explainAppendTerm(pStr, i++, z, ">");
       124406  +    explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
       124407  +    i = 1;
123589 124408     }
123590 124409     if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
123591         -    const char *z = explainIndexColumnName(pIndex, j);
123592         -    explainAppendTerm(pStr, i, z, "<");
       124410  +    explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
123593 124411     }
123594 124412     sqlite3StrAccumAppend(pStr, ")", 1);
123595 124413   }
123596 124414   
123597 124415   /*
123598 124416   ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
123599 124417   ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
................................................................................
123775 124593   ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
123776 124594   ** The TERM_LIKECOND marking indicates that the term should be coded inside
123777 124595   ** a conditional such that is only evaluated on the second pass of a
123778 124596   ** LIKE-optimization loop, when scanning BLOBs instead of strings.
123779 124597   */
123780 124598   static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
123781 124599     int nLoop = 0;
123782         -  while( pTerm
       124600  +  while( ALWAYS(pTerm!=0)
123783 124601         && (pTerm->wtFlags & TERM_CODED)==0
123784 124602         && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
123785 124603         && (pLevel->notReady & pTerm->prereqAll)==0
123786 124604     ){
123787 124605       if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
123788 124606         pTerm->wtFlags |= TERM_LIKECOND;
123789 124607       }else{
................................................................................
123831 124649     /* Code the OP_Affinity opcode if there is anything left to do. */
123832 124650     if( n>0 ){
123833 124651       sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
123834 124652       sqlite3ExprCacheAffinityChange(pParse, base, n);
123835 124653     }
123836 124654   }
123837 124655   
       124656  +/*
       124657  +** Expression pRight, which is the RHS of a comparison operation, is 
       124658  +** either a vector of n elements or, if n==1, a scalar expression.
       124659  +** Before the comparison operation, affinity zAff is to be applied
       124660  +** to the pRight values. This function modifies characters within the
       124661  +** affinity string to SQLITE_AFF_BLOB if either:
       124662  +**
       124663  +**   * the comparison will be performed with no affinity, or
       124664  +**   * the affinity change in zAff is guaranteed not to change the value.
       124665  +*/
       124666  +static void updateRangeAffinityStr(
       124667  +  Expr *pRight,                   /* RHS of comparison */
       124668  +  int n,                          /* Number of vector elements in comparison */
       124669  +  char *zAff                      /* Affinity string to modify */
       124670  +){
       124671  +  int i;
       124672  +  for(i=0; i<n; i++){
       124673  +    Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
       124674  +    if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB
       124675  +     || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
       124676  +    ){
       124677  +      zAff[i] = SQLITE_AFF_BLOB;
       124678  +    }
       124679  +  }
       124680  +}
123838 124681   
123839 124682   /*
123840 124683   ** Generate code for a single equality term of the WHERE clause.  An equality
123841 124684   ** term can be either X=expr or X IN (...).   pTerm is the term to be 
123842 124685   ** coded.
123843 124686   **
123844         -** The current value for the constraint is left in register iReg.
       124687  +** The current value for the constraint is left in a register, the index
       124688  +** of which is returned.  An attempt is made store the result in iTarget but
       124689  +** this is only guaranteed for TK_ISNULL and TK_IN constraints.  If the
       124690  +** constraint is a TK_EQ or TK_IS, then the current value might be left in
       124691  +** some other register and it is the caller's responsibility to compensate.
123845 124692   **
123846         -** For a constraint of the form X=expr, the expression is evaluated and its
123847         -** result is left on the stack.  For constraints of the form X IN (...)
       124693  +** For a constraint of the form X=expr, the expression is evaluated in
       124694  +** straight-line code.  For constraints of the form X IN (...)
123848 124695   ** this routine sets up a loop that will iterate over all values of X.
123849 124696   */
123850 124697   static int codeEqualityTerm(
123851 124698     Parse *pParse,      /* The parsing context */
123852 124699     WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
123853 124700     WhereLevel *pLevel, /* The level of the FROM clause we are working on */
123854 124701     int iEq,            /* Index of the equality term within this level */
................................................................................
123855 124702     int bRev,           /* True for reverse-order IN operations */
123856 124703     int iTarget         /* Attempt to leave results in this register */
123857 124704   ){
123858 124705     Expr *pX = pTerm->pExpr;
123859 124706     Vdbe *v = pParse->pVdbe;
123860 124707     int iReg;                  /* Register holding results */
123861 124708   
       124709  +  assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
123862 124710     assert( iTarget>0 );
123863 124711     if( pX->op==TK_EQ || pX->op==TK_IS ){
123864 124712       iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
123865 124713     }else if( pX->op==TK_ISNULL ){
123866 124714       iReg = iTarget;
123867 124715       sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
123868 124716   #ifndef SQLITE_OMIT_SUBQUERY
123869 124717     }else{
123870         -    int eType;
       124718  +    int eType = IN_INDEX_NOOP;
123871 124719       int iTab;
123872 124720       struct InLoop *pIn;
123873 124721       WhereLoop *pLoop = pLevel->pWLoop;
       124722  +    int i;
       124723  +    int nEq = 0;
       124724  +    int *aiMap = 0;
123874 124725   
123875 124726       if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
123876 124727         && pLoop->u.btree.pIndex!=0
123877 124728         && pLoop->u.btree.pIndex->aSortOrder[iEq]
123878 124729       ){
123879 124730         testcase( iEq==0 );
123880 124731         testcase( bRev );
123881 124732         bRev = !bRev;
123882 124733       }
123883 124734       assert( pX->op==TK_IN );
123884 124735       iReg = iTarget;
123885         -    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
       124736  +
       124737  +    for(i=0; i<iEq; i++){
       124738  +      if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
       124739  +        disableTerm(pLevel, pTerm);
       124740  +        return iTarget;
       124741  +      }
       124742  +    }
       124743  +    for(i=iEq;i<pLoop->nLTerm; i++){
       124744  +      if( ALWAYS(pLoop->aLTerm[i]) && pLoop->aLTerm[i]->pExpr==pX ) nEq++;
       124745  +    }
       124746  +
       124747  +    if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
       124748  +      eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0);
       124749  +    }else{
       124750  +      Select *pSelect = pX->x.pSelect;
       124751  +      sqlite3 *db = pParse->db;
       124752  +      ExprList *pOrigRhs = pSelect->pEList;
       124753  +      ExprList *pOrigLhs = pX->pLeft->x.pList;
       124754  +      ExprList *pRhs = 0;         /* New Select.pEList for RHS */
       124755  +      ExprList *pLhs = 0;         /* New pX->pLeft vector */
       124756  +
       124757  +      for(i=iEq;i<pLoop->nLTerm; i++){
       124758  +        if( pLoop->aLTerm[i]->pExpr==pX ){
       124759  +          int iField = pLoop->aLTerm[i]->iField - 1;
       124760  +          Expr *pNewRhs = sqlite3ExprDup(db, pOrigRhs->a[iField].pExpr, 0);
       124761  +          Expr *pNewLhs = sqlite3ExprDup(db, pOrigLhs->a[iField].pExpr, 0);
       124762  +
       124763  +          pRhs = sqlite3ExprListAppend(pParse, pRhs, pNewRhs);
       124764  +          pLhs = sqlite3ExprListAppend(pParse, pLhs, pNewLhs);
       124765  +        }
       124766  +      }
       124767  +      if( !db->mallocFailed ){
       124768  +        Expr *pLeft = pX->pLeft;
       124769  +
       124770  +        if( pSelect->pOrderBy ){
       124771  +          /* If the SELECT statement has an ORDER BY clause, zero the 
       124772  +          ** iOrderByCol variables. These are set to non-zero when an 
       124773  +          ** ORDER BY term exactly matches one of the terms of the 
       124774  +          ** result-set. Since the result-set of the SELECT statement may
       124775  +          ** have been modified or reordered, these variables are no longer 
       124776  +          ** set correctly.  Since setting them is just an optimization, 
       124777  +          ** it's easiest just to zero them here.  */
       124778  +          ExprList *pOrderBy = pSelect->pOrderBy;
       124779  +          for(i=0; i<pOrderBy->nExpr; i++){
       124780  +            pOrderBy->a[i].u.x.iOrderByCol = 0;
       124781  +          }
       124782  +        }
       124783  +
       124784  +        /* Take care here not to generate a TK_VECTOR containing only a
       124785  +        ** single value. Since the parser never creates such a vector, some
       124786  +        ** of the subroutines do not handle this case.  */
       124787  +        if( pLhs->nExpr==1 ){
       124788  +          pX->pLeft = pLhs->a[0].pExpr;
       124789  +        }else{
       124790  +          pLeft->x.pList = pLhs;
       124791  +          aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int) * nEq);
       124792  +          testcase( aiMap==0 );
       124793  +        }
       124794  +        pSelect->pEList = pRhs;
       124795  +        eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap);
       124796  +        testcase( aiMap!=0 && aiMap[0]!=0 );
       124797  +        pSelect->pEList = pOrigRhs;
       124798  +        pLeft->x.pList = pOrigLhs;
       124799  +        pX->pLeft = pLeft;
       124800  +      }
       124801  +      sqlite3ExprListDelete(pParse->db, pLhs);
       124802  +      sqlite3ExprListDelete(pParse->db, pRhs);
       124803  +    }
       124804  +
123886 124805       if( eType==IN_INDEX_INDEX_DESC ){
123887 124806         testcase( bRev );
123888 124807         bRev = !bRev;
123889 124808       }
123890 124809       iTab = pX->iTable;
123891 124810       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
123892 124811       VdbeCoverageIf(v, bRev);
123893 124812       VdbeCoverageIf(v, !bRev);
123894 124813       assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
       124814  +
123895 124815       pLoop->wsFlags |= WHERE_IN_ABLE;
123896 124816       if( pLevel->u.in.nIn==0 ){
123897 124817         pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
123898 124818       }
123899         -    pLevel->u.in.nIn++;
       124819  +
       124820  +    i = pLevel->u.in.nIn;
       124821  +    pLevel->u.in.nIn += nEq;
123900 124822       pLevel->u.in.aInLoop =
123901 124823          sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
123902 124824                                 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
123903 124825       pIn = pLevel->u.in.aInLoop;
123904 124826       if( pIn ){
123905         -      pIn += pLevel->u.in.nIn - 1;
123906         -      pIn->iCur = iTab;
123907         -      if( eType==IN_INDEX_ROWID ){
123908         -        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
123909         -      }else{
123910         -        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
       124827  +      int iMap = 0;               /* Index in aiMap[] */
       124828  +      pIn += i;
       124829  +      for(i=iEq;i<pLoop->nLTerm; i++){
       124830  +        if( pLoop->aLTerm[i]->pExpr==pX ){
       124831  +          int iOut = iReg + i - iEq;
       124832  +          if( eType==IN_INDEX_ROWID ){
       124833  +            testcase( nEq>1 );  /* Happens with a UNIQUE index on ROWID */
       124834  +            pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
       124835  +          }else{
       124836  +            int iCol = aiMap ? aiMap[iMap++] : 0;
       124837  +            pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
       124838  +          }
       124839  +          sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
       124840  +          if( i==iEq ){
       124841  +            pIn->iCur = iTab;
       124842  +            pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
       124843  +          }else{
       124844  +            pIn->eEndLoopOp = OP_Noop;
       124845  +          }
       124846  +          pIn++;
       124847  +        }
123911 124848         }
123912         -      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
123913         -      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
123914 124849       }else{
123915 124850         pLevel->u.in.nIn = 0;
123916 124851       }
       124852  +    sqlite3DbFree(pParse->db, aiMap);
123917 124853   #endif
123918 124854     }
123919 124855     disableTerm(pLevel, pTerm);
123920 124856     return iReg;
123921 124857   }
123922 124858   
123923 124859   /*
................................................................................
124035 124971         if( nReg==1 ){
124036 124972           sqlite3ReleaseTempReg(pParse, regBase);
124037 124973           regBase = r1;
124038 124974         }else{
124039 124975           sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
124040 124976         }
124041 124977       }
124042         -    if( (pTerm->eOperator & WO_IN)!=0 ){
       124978  +    if( pTerm->eOperator & WO_IN ){
124043 124979         if( pTerm->pExpr->flags & EP_xIsSelect ){
124044 124980           /* No affinity ever needs to be (or should be) applied to a value
124045 124981           ** from the RHS of an "? IN (SELECT ...)" expression. The 
124046 124982           ** sqlite3FindInIndex() routine has already ensured that the 
124047 124983           ** affinity of the comparison has been applied to the value.  */
124048 124984           if( zAff ) zAff[j] = SQLITE_AFF_BLOB;
124049 124985         }
................................................................................
124365 125301           assert( pIdx->aiColumn[i]<pTab->nCol );
124366 125302           if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1;
124367 125303         }
124368 125304         sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
124369 125305       }
124370 125306     }
124371 125307   }
       125308  +
       125309  +/*
       125310  +** If the expression passed as the second argument is a vector, generate
       125311  +** code to write the first nReg elements of the vector into an array
       125312  +** of registers starting with iReg.
       125313  +**
       125314  +** If the expression is not a vector, then nReg must be passed 1. In
       125315  +** this case, generate code to evaluate the expression and leave the
       125316  +** result in register iReg.
       125317  +*/
       125318  +static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
       125319  +  assert( nReg>0 );
       125320  +  if( sqlite3ExprIsVector(p) ){
       125321  +#ifndef SQLITE_OMIT_SUBQUERY
       125322  +    if( (p->flags & EP_xIsSelect) ){
       125323  +      Vdbe *v = pParse->pVdbe;
       125324  +      int iSelect = sqlite3CodeSubselect(pParse, p, 0, 0);
       125325  +      sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
       125326  +    }else
       125327  +#endif
       125328  +    {
       125329  +      int i;
       125330  +      ExprList *pList = p->x.pList;
       125331  +      assert( nReg<=pList->nExpr );
       125332  +      for(i=0; i<nReg; i++){
       125333  +        sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
       125334  +      }
       125335  +    }
       125336  +  }else{
       125337  +    assert( nReg==1 );
       125338  +    sqlite3ExprCode(pParse, p, iReg);
       125339  +  }
       125340  +}
124372 125341   
124373 125342   /*
124374 125343   ** Generate code for the start of the iLevel-th loop in the WHERE clause
124375 125344   ** implementation described by pWInfo.
124376 125345   */
124377 125346   SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
124378 125347     WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
................................................................................
124461 125430         int iTarget = iReg+j+2;
124462 125431         pTerm = pLoop->aLTerm[j];
124463 125432         if( NEVER(pTerm==0) ) continue;
124464 125433         if( pTerm->eOperator & WO_IN ){
124465 125434           codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
124466 125435           addrNotFound = pLevel->addrNxt;
124467 125436         }else{
124468         -        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
       125437  +        Expr *pRight = pTerm->pExpr->pRight;
       125438  +        codeExprOrVector(pParse, pRight, iTarget, 1);
124469 125439         }
124470 125440       }
124471 125441       sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
124472 125442       sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
124473 125443       sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
124474 125444                         pLoop->u.vtab.idxStr,
124475 125445                         pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
................................................................................
124575 125545         pStart = pEnd;
124576 125546         pEnd = pTerm;
124577 125547       }
124578 125548       codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
124579 125549       if( pStart ){
124580 125550         Expr *pX;             /* The expression that defines the start bound */
124581 125551         int r1, rTemp;        /* Registers for holding the start boundary */
       125552  +      int op;               /* Cursor seek operation */
124582 125553   
124583 125554         /* The following constant maps TK_xx codes into corresponding 
124584 125555         ** seek opcodes.  It depends on a particular ordering of TK_xx
124585 125556         */
124586 125557         const u8 aMoveOp[] = {
124587 125558              /* TK_GT */  OP_SeekGT,
124588 125559              /* TK_LE */  OP_SeekLE,
................................................................................
124594 125565         assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
124595 125566   
124596 125567         assert( (pStart->wtFlags & TERM_VNULL)==0 );
124597 125568         testcase( pStart->wtFlags & TERM_VIRTUAL );
124598 125569         pX = pStart->pExpr;
124599 125570         assert( pX!=0 );
124600 125571         testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
124601         -      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
124602         -      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
       125572  +      if( sqlite3ExprIsVector(pX->pRight) ){
       125573  +        r1 = rTemp = sqlite3GetTempReg(pParse);
       125574  +        codeExprOrVector(pParse, pX->pRight, r1, 1);
       125575  +        op = aMoveOp[(pX->op - TK_GT) | 0x0001];
       125576  +      }else{
       125577  +        r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
       125578  +        disableTerm(pLevel, pStart);
       125579  +        op = aMoveOp[(pX->op - TK_GT)];
       125580  +      }
       125581  +      sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
124603 125582         VdbeComment((v, "pk"));
124604 125583         VdbeCoverageIf(v, pX->op==TK_GT);
124605 125584         VdbeCoverageIf(v, pX->op==TK_LE);
124606 125585         VdbeCoverageIf(v, pX->op==TK_LT);
124607 125586         VdbeCoverageIf(v, pX->op==TK_GE);
124608 125587         sqlite3ExprCacheAffinityChange(pParse, r1, 1);
124609 125588         sqlite3ReleaseTempReg(pParse, rTemp);
124610         -      disableTerm(pLevel, pStart);
124611 125589       }else{
124612 125590         sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
124613 125591         VdbeCoverageIf(v, bRev==0);
124614 125592         VdbeCoverageIf(v, bRev!=0);
124615 125593       }
124616 125594       if( pEnd ){
124617 125595         Expr *pX;
124618 125596         pX = pEnd->pExpr;
124619 125597         assert( pX!=0 );
124620 125598         assert( (pEnd->wtFlags & TERM_VNULL)==0 );
124621 125599         testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
124622 125600         testcase( pEnd->wtFlags & TERM_VIRTUAL );
124623 125601         memEndValue = ++pParse->nMem;
124624         -      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
124625         -      if( pX->op==TK_LT || pX->op==TK_GT ){
       125602  +      codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
       125603  +      if( 0==sqlite3ExprIsVector(pX->pRight) 
       125604  +       && (pX->op==TK_LT || pX->op==TK_GT) 
       125605  +      ){
124626 125606           testOp = bRev ? OP_Le : OP_Ge;
124627 125607         }else{
124628 125608           testOp = bRev ? OP_Lt : OP_Gt;
124629 125609         }
124630         -      disableTerm(pLevel, pEnd);
       125610  +      if( 0==sqlite3ExprIsVector(pX->pRight) ){
       125611  +        disableTerm(pLevel, pEnd);
       125612  +      }
124631 125613       }
124632 125614       start = sqlite3VdbeCurrentAddr(v);
124633 125615       pLevel->op = bRev ? OP_Prev : OP_Next;
124634 125616       pLevel->p1 = iCur;
124635 125617       pLevel->p2 = start;
124636 125618       assert( pLevel->p5==0 );
124637 125619       if( testOp!=OP_Noop ){
................................................................................
124690 125672       static const u8 aEndOp[] = {
124691 125673         OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
124692 125674         OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
124693 125675         OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
124694 125676         OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
124695 125677       };
124696 125678       u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
       125679  +    u16 nBtm = pLoop->u.btree.nBtm;   /* Length of BTM vector */
       125680  +    u16 nTop = pLoop->u.btree.nTop;   /* Length of TOP vector */
124697 125681       int regBase;                 /* Base register holding constraint values */
124698 125682       WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
124699 125683       WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
124700 125684       int startEq;                 /* True if range start uses ==, >= or <= */
124701 125685       int endEq;                   /* True if range end uses ==, >= or <= */
124702 125686       int start_constraints;       /* Start of range is constrained */
124703 125687       int nConstraint;             /* Number of constraint terms */
124704 125688       Index *pIdx;                 /* The index we will be using */
124705 125689       int iIdxCur;                 /* The VDBE cursor for the index */
124706 125690       int nExtraReg = 0;           /* Number of extra registers needed */
124707 125691       int op;                      /* Instruction opcode */
124708 125692       char *zStartAff;             /* Affinity for start of range constraint */
124709         -    char cEndAff = 0;            /* Affinity for end of range constraint */
       125693  +    char *zEndAff = 0;           /* Affinity for end of range constraint */
124710 125694       u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
124711 125695       u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
124712 125696   
124713 125697       pIdx = pLoop->u.btree.pIndex;
124714 125698       iIdxCur = pLevel->iIdxCur;
124715 125699       assert( nEq>=pLoop->nSkip );
124716 125700   
................................................................................
124736 125720   
124737 125721       /* Find any inequality constraint terms for the start and end 
124738 125722       ** of the range. 
124739 125723       */
124740 125724       j = nEq;
124741 125725       if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
124742 125726         pRangeStart = pLoop->aLTerm[j++];
124743         -      nExtraReg = 1;
       125727  +      nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
124744 125728         /* Like optimization range constraints always occur in pairs */
124745 125729         assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || 
124746 125730                 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
124747 125731       }
124748 125732       if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
124749 125733         pRangeEnd = pLoop->aLTerm[j++];
124750         -      nExtraReg = 1;
       125734  +      nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
124751 125735   #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
124752 125736         if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
124753 125737           assert( pRangeStart!=0 );                     /* LIKE opt constraints */
124754 125738           assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
124755 125739           pLevel->iLikeRepCntr = (u32)++pParse->nMem;
124756 125740           sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
124757 125741           VdbeComment((v, "LIKE loop counter"));
................................................................................
124761 125745           testcase( bRev );
124762 125746           testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
124763 125747           assert( (bRev & ~1)==0 );
124764 125748           pLevel->iLikeRepCntr <<=1;
124765 125749           pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
124766 125750         }
124767 125751   #endif
124768         -      if( pRangeStart==0
124769         -       && (j = pIdx->aiColumn[nEq])>=0 
124770         -       && pIdx->pTable->aCol[j].notNull==0
124771         -      ){
124772         -        bSeekPastNull = 1;
       125752  +      if( pRangeStart==0 ){
       125753  +        j = pIdx->aiColumn[nEq];
       125754  +        if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
       125755  +          bSeekPastNull = 1;
       125756  +        }
124773 125757         }
124774 125758       }
124775 125759       assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
124776 125760   
124777 125761       /* If we are doing a reverse order scan on an ascending index, or
124778 125762       ** a forward order scan on a descending index, interchange the 
124779 125763       ** start and end terms (pRangeStart and pRangeEnd).
124780 125764       */
124781 125765       if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
124782 125766        || (bRev && pIdx->nKeyCol==nEq)
124783 125767       ){
124784 125768         SWAP(WhereTerm *, pRangeEnd, pRangeStart);
124785 125769         SWAP(u8, bSeekPastNull, bStopAtNull);
       125770  +      SWAP(u8, nBtm, nTop);
124786 125771       }
124787 125772   
124788 125773       /* Generate code to evaluate all constraint terms using == or IN
124789 125774       ** and store the values of those terms in an array of registers
124790 125775       ** starting at regBase.
124791 125776       */
124792 125777       codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
124793 125778       regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
124794 125779       assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
124795         -    if( zStartAff ) cEndAff = zStartAff[nEq];
       125780  +    if( zStartAff && nTop ){
       125781  +      zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
       125782  +    }
124796 125783       addrNxt = pLevel->addrNxt;
124797 125784   
124798 125785       testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
124799 125786       testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
124800 125787       testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
124801 125788       testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
124802 125789       startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
................................................................................
124803 125790       endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
124804 125791       start_constraints = pRangeStart || nEq>0;
124805 125792   
124806 125793       /* Seek the index cursor to the start of the range. */
124807 125794       nConstraint = nEq;
124808 125795       if( pRangeStart ){
124809 125796         Expr *pRight = pRangeStart->pExpr->pRight;
124810         -      sqlite3ExprCode(pParse, pRight, regBase+nEq);
       125797  +      codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
124811 125798         whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
124812 125799         if( (pRangeStart->wtFlags & TERM_VNULL)==0
124813 125800          && sqlite3ExprCanBeNull(pRight)
124814 125801         ){
124815 125802           sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
124816 125803           VdbeCoverage(v);
124817 125804         }
124818 125805         if( zStartAff ){
124819         -        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){
124820         -          /* Since the comparison is to be performed with no conversions
124821         -          ** applied to the operands, set the affinity to apply to pRight to 
124822         -          ** SQLITE_AFF_BLOB.  */
124823         -          zStartAff[nEq] = SQLITE_AFF_BLOB;
124824         -        }
124825         -        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
124826         -          zStartAff[nEq] = SQLITE_AFF_BLOB;
124827         -        }
       125806  +        updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
124828 125807         }  
124829         -      nConstraint++;
       125808  +      nConstraint += nBtm;
124830 125809         testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
       125810  +      if( sqlite3ExprIsVector(pRight)==0 ){
       125811  +        disableTerm(pLevel, pRangeStart);
       125812  +      }else{
       125813  +        startEq = 1;
       125814  +      }
124831 125815         bSeekPastNull = 0;
124832 125816       }else if( bSeekPastNull ){
124833 125817         sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
124834 125818         nConstraint++;
124835 125819         startEq = 0;
124836 125820         start_constraints = 1;
124837 125821       }
................................................................................
124856 125840       /* Load the value for the inequality constraint at the end of the
124857 125841       ** range (if any).
124858 125842       */
124859 125843       nConstraint = nEq;
124860 125844       if( pRangeEnd ){
124861 125845         Expr *pRight = pRangeEnd->pExpr->pRight;
124862 125846         sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
124863         -      sqlite3ExprCode(pParse, pRight, regBase+nEq);
       125847  +      codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
124864 125848         whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
124865 125849         if( (pRangeEnd->wtFlags & TERM_VNULL)==0
124866 125850          && sqlite3ExprCanBeNull(pRight)
124867 125851         ){
124868 125852           sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
124869 125853           VdbeCoverage(v);
124870 125854         }
124871         -      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB
124872         -       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
124873         -      ){
124874         -        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
       125855  +      if( zEndAff ){
       125856  +        updateRangeAffinityStr(pRight, nTop, zEndAff);
       125857  +        codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
       125858  +      }else{
       125859  +        assert( pParse->db->mallocFailed );
124875 125860         }
124876         -      nConstraint++;
       125861  +      nConstraint += nTop;
124877 125862         testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
       125863  +
       125864  +      if( sqlite3ExprIsVector(pRight)==0 ){
       125865  +        disableTerm(pLevel, pRangeEnd);
       125866  +      }else{
       125867  +        endEq = 1;
       125868  +      }
124878 125869       }else if( bStopAtNull ){
124879 125870         sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
124880 125871         endEq = 0;
124881 125872         nConstraint++;
124882 125873       }
124883 125874       sqlite3DbFree(db, zStartAff);
       125875  +    sqlite3DbFree(db, zEndAff);
124884 125876   
124885 125877       /* Top of the loop body */
124886 125878       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
124887 125879   
124888 125880       /* Check if the index cursor is past the end of the range. */
124889 125881       if( nConstraint ){
124890 125882         op = aEndOp[bRev*2 + endEq];
................................................................................
124892 125884         testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
124893 125885         testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
124894 125886         testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
124895 125887         testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
124896 125888       }
124897 125889   
124898 125890       /* Seek the table cursor, if required */
124899         -    disableTerm(pLevel, pRangeStart);
124900         -    disableTerm(pLevel, pRangeEnd);
124901 125891       if( omitTable ){
124902 125892         /* pIdx is a covering index.  No need to access the main table. */
124903 125893       }else if( HasRowid(pIdx->pTable) ){
124904 125894         if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)!=0 ){
124905 125895           iRowidReg = ++pParse->nMem;
124906 125896           sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
124907 125897           sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
................................................................................
124917 125907           k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
124918 125908           sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
124919 125909         }
124920 125910         sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
124921 125911                              iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
124922 125912       }
124923 125913   
124924         -    /* Record the instruction used to terminate the loop. Disable 
124925         -    ** WHERE clause terms made redundant by the index range scan.
124926         -    */
       125914  +    /* Record the instruction used to terminate the loop. */
124927 125915       if( pLoop->wsFlags & WHERE_ONEROW ){
124928 125916         pLevel->op = OP_Noop;
124929 125917       }else if( bRev ){
124930 125918         pLevel->op = OP_Prev;
124931 125919       }else{
124932 125920         pLevel->op = OP_Next;
124933 125921       }
................................................................................
124996 125984       int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
124997 125985       int iRetInit;                             /* Address of regReturn init */
124998 125986       int untestedTerms = 0;             /* Some terms not completely tested */
124999 125987       int ii;                            /* Loop counter */
125000 125988       u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
125001 125989       Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
125002 125990       Table *pTab = pTabItem->pTab;
125003         -   
       125991  +
125004 125992       pTerm = pLoop->aLTerm[0];
125005 125993       assert( pTerm!=0 );
125006 125994       assert( pTerm->eOperator & WO_OR );
125007 125995       assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
125008 125996       pOrWc = &pTerm->u.pOrInfo->wc;
125009 125997       pLevel->op = OP_Return;
125010 125998       pLevel->p1 = regReturn;
................................................................................
125297 126285     **
125298 126286     ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
125299 126287     ** and we are coding the t1 loop and the t2 loop has not yet coded,
125300 126288     ** then we cannot use the "t1.a=t2.b" constraint, but we can code
125301 126289     ** the implied "t1.a=123" constraint.
125302 126290     */
125303 126291     for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
125304         -    Expr *pE, *pEAlt;
       126292  +    Expr *pE, sEAlt;
125305 126293       WhereTerm *pAlt;
125306 126294       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
125307 126295       if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
125308 126296       if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
125309 126297       if( pTerm->leftCursor!=iCur ) continue;
125310 126298       if( pLevel->iLeftJoin ) continue;
125311 126299       pE = pTerm->pExpr;
................................................................................
125315 126303                       WO_EQ|WO_IN|WO_IS, 0);
125316 126304       if( pAlt==0 ) continue;
125317 126305       if( pAlt->wtFlags & (TERM_CODED) ) continue;
125318 126306       testcase( pAlt->eOperator & WO_EQ );
125319 126307       testcase( pAlt->eOperator & WO_IS );
125320 126308       testcase( pAlt->eOperator & WO_IN );
125321 126309       VdbeModuleComment((v, "begin transitive constraint"));
125322         -    pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
125323         -    if( pEAlt ){
125324         -      *pEAlt = *pAlt->pExpr;
125325         -      pEAlt->pLeft = pE->pLeft;
125326         -      sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
125327         -      sqlite3StackFree(db, pEAlt);
125328         -    }
       126310  +    sEAlt = *pAlt->pExpr;
       126311  +    sEAlt.pLeft = pE->pLeft;
       126312  +    sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
125329 126313     }
125330 126314   
125331 126315     /* For a LEFT OUTER JOIN, generate code that will record the fact that
125332 126316     ** at least one row of the right table has matched the left table.  
125333 126317     */
125334 126318     if( pLevel->iLeftJoin ){
125335 126319       pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
................................................................................
125430 126414         return 0;
125431 126415       }
125432 126416       memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
125433 126417       if( pOld!=pWC->aStatic ){
125434 126418         sqlite3DbFree(db, pOld);
125435 126419       }
125436 126420       pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
125437         -    memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
125438 126421     }
125439 126422     pTerm = &pWC->a[idx = pWC->nTerm++];
125440 126423     if( p && ExprHasProperty(p, EP_Unlikely) ){
125441 126424       pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
125442 126425     }else{
125443 126426       pTerm->truthProb = 1;
125444 126427     }
125445 126428     pTerm->pExpr = sqlite3ExprSkipCollate(p);
125446 126429     pTerm->wtFlags = wtFlags;
125447 126430     pTerm->pWC = pWC;
125448 126431     pTerm->iParent = -1;
       126432  +  memset(&pTerm->eOperator, 0,
       126433  +         sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
125449 126434     return idx;
125450 126435   }
125451 126436   
125452 126437   /*
125453 126438   ** Return TRUE if the given operator is one of the operators that is
125454 126439   ** allowed for an indexable WHERE clause term.  The allowed operators are
125455         -** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
       126440  +** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
125456 126441   */
125457 126442   static int allowedOp(int op){
125458 126443     assert( TK_GT>TK_EQ && TK_GT<TK_GE );
125459 126444     assert( TK_LT>TK_EQ && TK_LT<TK_GE );
125460 126445     assert( TK_LE>TK_EQ && TK_LE<TK_GE );
125461 126446     assert( TK_GE==TK_EQ+4 );
125462 126447     return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
................................................................................
125643 126628   **
125644 126629   ** If it is then return TRUE.  If not, return FALSE.
125645 126630   */
125646 126631   static int isMatchOfColumn(
125647 126632     Expr *pExpr,                    /* Test this expression */
125648 126633     unsigned char *peOp2            /* OUT: 0 for MATCH, or else an op2 value */
125649 126634   ){
125650         -  struct Op2 {
       126635  +  static const struct Op2 {
125651 126636       const char *zOp;
125652 126637       unsigned char eOp2;
125653 126638     } aOp[] = {
125654 126639       { "match",  SQLITE_INDEX_CONSTRAINT_MATCH },
125655 126640       { "glob",   SQLITE_INDEX_CONSTRAINT_GLOB },
125656 126641       { "like",   SQLITE_INDEX_CONSTRAINT_LIKE },
125657 126642       { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
................................................................................
126176 127161   
126177 127162   /*
126178 127163   ** Expression pExpr is one operand of a comparison operator that might
126179 127164   ** be useful for indexing.  This routine checks to see if pExpr appears
126180 127165   ** in any index.  Return TRUE (1) if pExpr is an indexed term and return
126181 127166   ** FALSE (0) if not.  If TRUE is returned, also set *piCur to the cursor
126182 127167   ** number of the table that is indexed and *piColumn to the column number
126183         -** of the column that is indexed, or -2 if an expression is being indexed.
       127168  +** of the column that is indexed, or XN_EXPR (-2) if an expression is being
       127169  +** indexed.
126184 127170   **
126185 127171   ** If pExpr is a TK_COLUMN column reference, then this routine always returns
126186 127172   ** true even if that particular column is not indexed, because the column
126187 127173   ** might be added to an automatic index later.
126188 127174   */
126189 127175   static int exprMightBeIndexed(
126190 127176     SrcList *pFrom,        /* The FROM clause */
       127177  +  int op,                /* The specific comparison operator */
126191 127178     Bitmask mPrereq,       /* Bitmask of FROM clause terms referenced by pExpr */
126192 127179     Expr *pExpr,           /* An operand of a comparison operator */
126193 127180     int *piCur,            /* Write the referenced table cursor number here */
126194 127181     int *piColumn          /* Write the referenced table column number here */
126195 127182   ){
126196 127183     Index *pIdx;
126197 127184     int i;
126198 127185     int iCur;
       127186  +
       127187  +  /* If this expression is a vector to the left or right of a 
       127188  +  ** inequality constraint (>, <, >= or <=), perform the processing 
       127189  +  ** on the first element of the vector.  */
       127190  +  assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
       127191  +  assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
       127192  +  assert( op<=TK_GE );
       127193  +  if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
       127194  +    pExpr = pExpr->x.pList->a[0].pExpr;
       127195  +  }
       127196  +
126199 127197     if( pExpr->op==TK_COLUMN ){
126200 127198       *piCur = pExpr->iTable;
126201 127199       *piColumn = pExpr->iColumn;
126202 127200       return 1;
126203 127201     }
126204 127202     if( mPrereq==0 ) return 0;                 /* No table references */
126205 127203     if( (mPrereq&(mPrereq-1))!=0 ) return 0;   /* Refs more than one table */
126206 127204     for(i=0; mPrereq>1; i++, mPrereq>>=1){}
126207 127205     iCur = pFrom->a[i].iCursor;
126208 127206     for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
126209 127207       if( pIdx->aColExpr==0 ) continue;
126210 127208       for(i=0; i<pIdx->nKeyCol; i++){
126211         -      if( pIdx->aiColumn[i]!=(-2) ) continue;
       127209  +      if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
126212 127210         if( sqlite3ExprCompare(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
126213 127211           *piCur = iCur;
126214         -        *piColumn = -2;
       127212  +        *piColumn = XN_EXPR;
126215 127213           return 1;
126216 127214         }
126217 127215       }
126218 127216     }
126219 127217     return 0;
126220 127218   }
126221 127219   
................................................................................
126264 127262     pMaskSet = &pWInfo->sMaskSet;
126265 127263     pExpr = pTerm->pExpr;
126266 127264     assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
126267 127265     prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
126268 127266     op = pExpr->op;
126269 127267     if( op==TK_IN ){
126270 127268       assert( pExpr->pRight==0 );
       127269  +    if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
126271 127270       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
126272 127271         pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
126273 127272       }else{
126274 127273         pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
126275 127274       }
126276 127275     }else if( op==TK_ISNULL ){
126277 127276       pTerm->prereqRight = 0;
................................................................................
126290 127289     pTerm->iParent = -1;
126291 127290     pTerm->eOperator = 0;
126292 127291     if( allowedOp(op) ){
126293 127292       int iCur, iColumn;
126294 127293       Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
126295 127294       Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
126296 127295       u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
126297         -    if( exprMightBeIndexed(pSrc, prereqLeft, pLeft, &iCur, &iColumn) ){
       127296  +
       127297  +    if( pTerm->iField>0 ){
       127298  +      assert( op==TK_IN );
       127299  +      assert( pLeft->op==TK_VECTOR );
       127300  +      pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
       127301  +    }
       127302  +
       127303  +    if( exprMightBeIndexed(pSrc, op, prereqLeft, pLeft, &iCur, &iColumn) ){
126298 127304         pTerm->leftCursor = iCur;
126299 127305         pTerm->u.leftColumn = iColumn;
126300 127306         pTerm->eOperator = operatorMask(op) & opMask;
126301 127307       }
126302 127308       if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
126303 127309       if( pRight 
126304         -     && exprMightBeIndexed(pSrc, pTerm->prereqRight, pRight, &iCur, &iColumn)
       127310  +     && exprMightBeIndexed(pSrc, op, pTerm->prereqRight, pRight, &iCur,&iColumn)
126305 127311       ){
126306 127312         WhereTerm *pNew;
126307 127313         Expr *pDup;
126308 127314         u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
       127315  +      assert( pTerm->iField==0 );
126309 127316         if( pTerm->leftCursor>=0 ){
126310 127317           int idxNew;
126311 127318           pDup = sqlite3ExprDup(db, pExpr, 0);
126312 127319           if( db->mallocFailed ){
126313 127320             sqlite3ExprDelete(db, pDup);
126314 127321             return;
126315 127322           }
................................................................................
126504 127511         markTermAsChild(pWC, idxNew, idxTerm);
126505 127512         pTerm = &pWC->a[idxTerm];
126506 127513         pTerm->wtFlags |= TERM_COPIED;
126507 127514         pNewTerm->prereqAll = pTerm->prereqAll;
126508 127515       }
126509 127516     }
126510 127517   #endif /* SQLITE_OMIT_VIRTUALTABLE */
       127518  +
       127519  +  /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
       127520  +  ** new terms for each component comparison - "a = ?" and "b = ?".  The
       127521  +  ** new terms completely replace the original vector comparison, which is
       127522  +  ** no longer used.
       127523  +  **
       127524  +  ** This is only required if at least one side of the comparison operation
       127525  +  ** is not a sub-select.  */
       127526  +  if( pWC->op==TK_AND 
       127527  +  && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
       127528  +  && sqlite3ExprIsVector(pExpr->pLeft)
       127529  +  && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 
       127530  +    || (pExpr->pRight->flags & EP_xIsSelect)==0
       127531  +  )){
       127532  +    int nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
       127533  +    int i;
       127534  +    assert( nLeft==sqlite3ExprVectorSize(pExpr->pRight) );
       127535  +    for(i=0; i<nLeft; i++){
       127536  +      int idxNew;
       127537  +      Expr *pNew;
       127538  +      Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
       127539  +      Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
       127540  +
       127541  +      pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight, 0);
       127542  +      idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
       127543  +      exprAnalyze(pSrc, pWC, idxNew);
       127544  +    }
       127545  +    pTerm = &pWC->a[idxTerm];
       127546  +    pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
       127547  +    pTerm->eOperator = 0;
       127548  +  }
       127549  +
       127550  +  /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
       127551  +  ** a virtual term for each vector component. The expression object
       127552  +  ** used by each such virtual term is pExpr (the full vector IN(...) 
       127553  +  ** expression). The WhereTerm.iField variable identifies the index within
       127554  +  ** the vector on the LHS that the virtual term represents.
       127555  +  **
       127556  +  ** This only works if the RHS is a simple SELECT, not a compound
       127557  +  */
       127558  +  if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
       127559  +   && pExpr->pLeft->op==TK_VECTOR
       127560  +   && pExpr->x.pSelect->pPrior==0
       127561  +  ){
       127562  +    int i;
       127563  +    for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
       127564  +      int idxNew;
       127565  +      idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL);
       127566  +      pWC->a[idxNew].iField = i+1;
       127567  +      exprAnalyze(pSrc, pWC, idxNew);
       127568  +      markTermAsChild(pWC, idxNew, idxTerm);
       127569  +    }
       127570  +  }
126511 127571   
126512 127572   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
126513 127573     /* When sqlite_stat3 histogram data is available an operator of the
126514 127574     ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
126515 127575     ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
126516 127576     ** virtual term of that form.
126517 127577     **
................................................................................
126525 127585       Expr *pNewExpr;
126526 127586       Expr *pLeft = pExpr->pLeft;
126527 127587       int idxNew;
126528 127588       WhereTerm *pNewTerm;
126529 127589   
126530 127590       pNewExpr = sqlite3PExpr(pParse, TK_GT,
126531 127591                               sqlite3ExprDup(db, pLeft, 0),
126532         -                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
       127592  +                            sqlite3ExprAlloc(db, TK_NULL, 0, 0), 0);
126533 127593   
126534 127594       idxNew = whereClauseInsert(pWC, pNewExpr,
126535 127595                                 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
126536 127596       if( idxNew ){
126537 127597         pNewTerm = &pWC->a[idxNew];
126538 127598         pNewTerm->prereqRight = 0;
126539 127599         pNewTerm->leftCursor = pLeft->iTable;
................................................................................
126628 127688   
126629 127689   /*
126630 127690   ** These routines walk (recursively) an expression tree and generate
126631 127691   ** a bitmask indicating which tables are used in that expression
126632 127692   ** tree.
126633 127693   */
126634 127694   SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
126635         -  Bitmask mask = 0;
       127695  +  Bitmask mask;
126636 127696     if( p==0 ) return 0;
126637 127697     if( p->op==TK_COLUMN ){
126638 127698       mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
126639 127699       return mask;
126640 127700     }
126641         -  mask = sqlite3WhereExprUsage(pMaskSet, p->pRight);
       127701  +  assert( !ExprHasProperty(p, EP_TokenOnly) );
       127702  +  mask = p->pRight ? sqlite3WhereExprUsage(pMaskSet, p->pRight) : 0;
126642 127703     if( p->pLeft ) mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
126643 127704     if( ExprHasProperty(p, EP_xIsSelect) ){
126644 127705       mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
126645 127706     }else if( p->x.pList ){
126646 127707       mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
126647 127708     }
126648 127709     return mask;
................................................................................
126702 127763     for(j=k=0; j<pArgs->nExpr; j++){
126703 127764       while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
126704 127765       if( k>=pTab->nCol ){
126705 127766         sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
126706 127767                         pTab->zName, j);
126707 127768         return;