System.Data.SQLite
Check-in [4de915832b]
Not logged in

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

Overview
Comment:Update SQLite core library to the latest trunk code.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4de915832bd733dd6cdaf2be96d7e6e534ea461b
User & Date: mistachkin 2014-06-03 19:02:41
Context
2014-06-04
14:32
Update SQLite core library to the 3.8.5 release. check-in: 3f1192ea1d user: mistachkin tags: trunk
2014-06-03
19:02
Update SQLite core library to the latest trunk code. check-in: 4de915832b user: mistachkin tags: trunk
18:57
Update Windows CE 200x testing tool to improve SmartDevice connectivity assembly name handling. check-in: ad0a7d4f73 user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

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

    41     41         </table>
    42     42       </div>
    43     43       <div id="mainSection">
    44     44       <div id="mainBody">
    45     45       <h1 class="heading">Version History</h1>
    46     46       <p><b>1.0.93.0 - June XX, 2014 <font color="red">(release scheduled)</font></b></p>
    47     47       <ul>
    48         -      <li>Updated to <a href="http://www.sqlite.org/releaselog/3_8_4_3.html">SQLite 3.8.4.3</a>.</li>
           48  +      <li>Updated to <a href="http://www.sqlite.org/src/info/trunk">SQLite 3.8.5</a>.</li>
    49     49         <li>Add support for mapping transaction isolation levels to their legacy default values. Pursuant to <a href="http://system.data.sqlite.org/index.html/info/56b42d99c1">[56b42d99c1]</a>.</li>
    50     50         <li>Add support for setting the default DbType and type name used for mappings on a per-connection basis. Pursuant to <a href="http://system.data.sqlite.org/index.html/info/3c00ec5b52">[3c00ec5b52]</a>.</li>
    51     51         <li>Add DetectTextAffinity and DetectStringType connection flags to enable automatic detection of column types, when necessary. Pursuant to <a href="http://system.data.sqlite.org/index.html/info/3c00ec5b52">[3c00ec5b52]</a>.</li>
    52     52         <li>Add SetChunkSize method to the SQLiteConnection class. Pursuant to [d1c008fa0a].</li>
    53     53         <li>Add SharedFlags static property to the SQLiteConnection class.</li>
    54     54         <li>Make the ISQLiteSchemaExtensions interface public.&nbsp;<b>** Potentially Incompatible Change **</b></li>
    55     55         <li>Have the SQLiteProviderFactory class (in the System.Data.SQLite.Linq assembly) implement the IServiceProvider interface.</li>

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.8.4.3</SQLITE_MANIFEST_VERSION>
    13         -    <SQLITE_RC_VERSION>3,8,4,3</SQLITE_RC_VERSION>
           12  +    <SQLITE_MANIFEST_VERSION>3.8.5</SQLITE_MANIFEST_VERSION>
           13  +    <SQLITE_RC_VERSION>3,8,5</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_COMMON_DEFINES>
    15     15       <SQLITE_EXTRA_DEFINES>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.8.4.3"
           17  +		Value="3.8.5"
    18     18   		PerformEnvironmentSet="true"
    19     19   	/>
    20     20   	<UserMacro
    21     21   		Name="SQLITE_RC_VERSION"
    22         -		Value="3,8,4,3"
           22  +		Value="3,8,5"
    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"
    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.8.4.3.  By combining all the individual C code files into this 
            3  +** version 3.8.5.  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
................................................................................
   218    218   ** string contains the date and time of the check-in (UTC) and an SHA1
   219    219   ** hash of the entire source tree.
   220    220   **
   221    221   ** See also: [sqlite3_libversion()],
   222    222   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   223    223   ** [sqlite_version()] and [sqlite_source_id()].
   224    224   */
   225         -#define SQLITE_VERSION        "3.8.4.3"
   226         -#define SQLITE_VERSION_NUMBER 3008004
   227         -#define SQLITE_SOURCE_ID      "2014-04-03 16:53:12 a611fa96c4a848614efe899130359c9f6fb889c3"
          225  +#define SQLITE_VERSION        "3.8.5"
          226  +#define SQLITE_VERSION_NUMBER 3008005
          227  +#define SQLITE_SOURCE_ID      "2014-06-03 14:42:34 6d81805298ea882f5fee99504d5a95f1a762a90b"
   228    228   
   229    229   /*
   230    230   ** CAPI3REF: Run-Time Library Version Numbers
   231    231   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   232    232   **
   233    233   ** These interfaces provide the same information as the [SQLITE_VERSION],
   234    234   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   671    671   ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   672    672   ** information is written to disk in the same order as calls
   673    673   ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
   674    674   ** after reboot following a crash or power loss, the only bytes in a
   675    675   ** file that were written at the application level might have changed
   676    676   ** and that adjacent bytes, even bytes within the same sector are
   677    677   ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
   678         -** flag indicate that a file cannot be deleted when open.
          678  +** flag indicate that a file cannot be deleted when open.  The
          679  +** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
          680  +** read-only media and cannot be changed even by processes with
          681  +** elevated privileges.
   679    682   */
   680    683   #define SQLITE_IOCAP_ATOMIC                 0x00000001
   681    684   #define SQLITE_IOCAP_ATOMIC512              0x00000002
   682    685   #define SQLITE_IOCAP_ATOMIC1K               0x00000004
   683    686   #define SQLITE_IOCAP_ATOMIC2K               0x00000008
   684    687   #define SQLITE_IOCAP_ATOMIC4K               0x00000010
   685    688   #define SQLITE_IOCAP_ATOMIC8K               0x00000020
................................................................................
   686    689   #define SQLITE_IOCAP_ATOMIC16K              0x00000040
   687    690   #define SQLITE_IOCAP_ATOMIC32K              0x00000080
   688    691   #define SQLITE_IOCAP_ATOMIC64K              0x00000100
   689    692   #define SQLITE_IOCAP_SAFE_APPEND            0x00000200
   690    693   #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
   691    694   #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
   692    695   #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
          696  +#define SQLITE_IOCAP_IMMUTABLE              0x00002000
   693    697   
   694    698   /*
   695    699   ** CAPI3REF: File Locking Levels
   696    700   **
   697    701   ** SQLite uses one of these integer values as the second
   698    702   ** argument to calls it makes to the xLock() and xUnlock() methods
   699    703   ** of an [sqlite3_io_methods] object.
................................................................................
  1054   1058   **
  1055   1059   ** <li>[[SQLITE_FCNTL_HAS_MOVED]]
  1056   1060   ** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
  1057   1061   ** pointer to an integer and it writes a boolean into that integer depending
  1058   1062   ** on whether or not the file has been renamed, moved, or deleted since it
  1059   1063   ** was first opened.
  1060   1064   **
         1065  +** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
         1066  +** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging.  This
         1067  +** opcode causes the xFileControl method to swap the file handle with the one
         1068  +** pointed to by the pArg argument.  This capability is used during testing
         1069  +** and only needs to be supported when SQLITE_TEST is defined.
         1070  +**
  1061   1071   ** </ul>
  1062   1072   */
  1063   1073   #define SQLITE_FCNTL_LOCKSTATE               1
  1064   1074   #define SQLITE_GET_LOCKPROXYFILE             2
  1065   1075   #define SQLITE_SET_LOCKPROXYFILE             3
  1066   1076   #define SQLITE_LAST_ERRNO                    4
  1067   1077   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  1077   1087   #define SQLITE_FCNTL_BUSYHANDLER            15
  1078   1088   #define SQLITE_FCNTL_TEMPFILENAME           16
  1079   1089   #define SQLITE_FCNTL_MMAP_SIZE              18
  1080   1090   #define SQLITE_FCNTL_TRACE                  19
  1081   1091   #define SQLITE_FCNTL_HAS_MOVED              20
  1082   1092   #define SQLITE_FCNTL_SYNC                   21
  1083   1093   #define SQLITE_FCNTL_COMMIT_PHASETWO        22
         1094  +#define SQLITE_FCNTL_WIN32_SET_HANDLE       23
  1084   1095   
  1085   1096   /*
  1086   1097   ** CAPI3REF: Mutex Handle
  1087   1098   **
  1088   1099   ** The mutex module within SQLite defines [sqlite3_mutex] to be an
  1089   1100   ** abstract type for a mutex object.  The SQLite core never looks
  1090   1101   ** at the internal representation of an [sqlite3_mutex].  It only
................................................................................
  2890   2901   **     "private". ^Setting it to "shared" is equivalent to setting the
  2891   2902   **     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
  2892   2903   **     sqlite3_open_v2(). ^Setting the cache parameter to "private" is 
  2893   2904   **     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
  2894   2905   **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
  2895   2906   **     a URI filename, its value overrides any behavior requested by setting
  2896   2907   **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
         2908  +**
         2909  +**  <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
         2910  +**     "1") or "false" (or "off" or "no" or "0") to indicate that the
         2911  +**     [powersafe overwrite] property does or does not apply to the
         2912  +**     storage media on which the database file resides.  ^The psow query
         2913  +**     parameter only works for the built-in unix and Windows VFSes.
         2914  +**
         2915  +**  <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
         2916  +**     which if set disables file locking in rollback journal modes.  This
         2917  +**     is useful for accessing a database on a filesystem that does not
         2918  +**     support locking.  Caution:  Database corruption might result if two
         2919  +**     or more processes write to the same database and any one of those
         2920  +**     processes uses nolock=1.
         2921  +**
         2922  +**  <li> <b>immutable</b>: ^The immutable parameter is a boolean query
         2923  +**     parameter that indicates that the database file is stored on
         2924  +**     read-only media.  ^When immutable is set, SQLite assumes that the
         2925  +**     database file cannot be changed, even by a process with higher
         2926  +**     privilege, and so the database is opened read-only and all locking
         2927  +**     and change detection is disabled.  Caution: Setting the immutable
         2928  +**     property on a database file that does in fact change can result
         2929  +**     in incorrect query results and/or [SQLITE_CORRUPT] errors.
         2930  +**     See also: [SQLITE_IOCAP_IMMUTABLE].
         2931  +**       
  2897   2932   ** </ul>
  2898   2933   **
  2899   2934   ** ^Specifying an unknown parameter in the query component of a URI is not an
  2900   2935   ** error.  Future versions of SQLite might understand additional query
  2901   2936   ** parameters.  See "[query parameters with special meaning to SQLite]" for
  2902   2937   ** additional information.
  2903   2938   **
................................................................................
  2919   2954   **          C:. Note that the %20 escaping in this example is not strictly 
  2920   2955   **          necessary - space characters can be used literally
  2921   2956   **          in URI filenames.
  2922   2957   ** <tr><td> file:data.db?mode=ro&cache=private <td> 
  2923   2958   **          Open file "data.db" in the current directory for read-only access.
  2924   2959   **          Regardless of whether or not shared-cache mode is enabled by
  2925   2960   **          default, use a private cache.
  2926         -** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
  2927         -**          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
         2961  +** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
         2962  +**          Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
         2963  +**          that uses dot-files in place of posix advisory locking.
  2928   2964   ** <tr><td> file:data.db?mode=readonly <td> 
  2929   2965   **          An error. "readonly" is not a valid option for the "mode" parameter.
  2930   2966   ** </table>
  2931   2967   **
  2932   2968   ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
  2933   2969   ** query components of a URI. A hexadecimal escape sequence consists of a
  2934   2970   ** percent sign - "%" - followed by exactly two hexadecimal digits 
................................................................................
  6234   6270   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  6235   6271   #define SQLITE_TESTCTRL_ISKEYWORD               16
  6236   6272   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  6237   6273   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  6238   6274   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
  6239   6275   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  6240   6276   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  6241         -#define SQLITE_TESTCTRL_LAST                    21
         6277  +#define SQLITE_TESTCTRL_BYTEORDER               22
         6278  +#define SQLITE_TESTCTRL_LAST                    22
  6242   6279   
  6243   6280   /*
  6244   6281   ** CAPI3REF: SQLite Runtime Status
  6245   6282   **
  6246   6283   ** ^This interface is used to retrieve runtime status information
  6247   6284   ** about the performance of SQLite, and optionally to reset various
  6248   6285   ** highwater marks.  ^The first argument is an integer code for
................................................................................
  7457   7494   
  7458   7495   
  7459   7496   #if 0
  7460   7497   extern "C" {
  7461   7498   #endif
  7462   7499   
  7463   7500   typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
         7501  +typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
         7502  +
         7503  +/* The double-precision datatype used by RTree depends on the
         7504  +** SQLITE_RTREE_INT_ONLY compile-time option.
         7505  +*/
         7506  +#ifdef SQLITE_RTREE_INT_ONLY
         7507  +  typedef sqlite3_int64 sqlite3_rtree_dbl;
         7508  +#else
         7509  +  typedef double sqlite3_rtree_dbl;
         7510  +#endif
  7464   7511   
  7465   7512   /*
  7466   7513   ** Register a geometry callback named zGeom that can be used as part of an
  7467   7514   ** R-Tree geometry query as follows:
  7468   7515   **
  7469   7516   **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
  7470   7517   */
  7471   7518   SQLITE_API int sqlite3_rtree_geometry_callback(
  7472   7519     sqlite3 *db,
  7473   7520     const char *zGeom,
  7474         -#ifdef SQLITE_RTREE_INT_ONLY
  7475         -  int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
  7476         -#else
  7477         -  int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
  7478         -#endif
         7521  +  int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
  7479   7522     void *pContext
  7480   7523   );
  7481   7524   
  7482   7525   
  7483   7526   /*
  7484   7527   ** A pointer to a structure of the following type is passed as the first
  7485   7528   ** argument to callbacks registered using rtree_geometry_callback().
  7486   7529   */
  7487   7530   struct sqlite3_rtree_geometry {
  7488   7531     void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
  7489   7532     int nParam;                     /* Size of array aParam[] */
  7490         -  double *aParam;                 /* Parameters passed to SQL geom function */
         7533  +  sqlite3_rtree_dbl *aParam;      /* Parameters passed to SQL geom function */
  7491   7534     void *pUser;                    /* Callback implementation user data */
  7492   7535     void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
  7493   7536   };
  7494   7537   
         7538  +/*
         7539  +** Register a 2nd-generation geometry callback named zScore that can be 
         7540  +** used as part of an R-Tree geometry query as follows:
         7541  +**
         7542  +**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
         7543  +*/
         7544  +SQLITE_API int sqlite3_rtree_query_callback(
         7545  +  sqlite3 *db,
         7546  +  const char *zQueryFunc,
         7547  +  int (*xQueryFunc)(sqlite3_rtree_query_info*),
         7548  +  void *pContext,
         7549  +  void (*xDestructor)(void*)
         7550  +);
         7551  +
         7552  +
         7553  +/*
         7554  +** A pointer to a structure of the following type is passed as the 
         7555  +** argument to scored geometry callback registered using
         7556  +** sqlite3_rtree_query_callback().
         7557  +**
         7558  +** Note that the first 5 fields of this structure are identical to
         7559  +** sqlite3_rtree_geometry.  This structure is a subclass of
         7560  +** sqlite3_rtree_geometry.
         7561  +*/
         7562  +struct sqlite3_rtree_query_info {
         7563  +  void *pContext;                   /* pContext from when function registered */
         7564  +  int nParam;                       /* Number of function parameters */
         7565  +  sqlite3_rtree_dbl *aParam;        /* value of function parameters */
         7566  +  void *pUser;                      /* callback can use this, if desired */
         7567  +  void (*xDelUser)(void*);          /* function to free pUser */
         7568  +  sqlite3_rtree_dbl *aCoord;        /* Coordinates of node or entry to check */
         7569  +  unsigned int *anQueue;            /* Number of pending entries in the queue */
         7570  +  int nCoord;                       /* Number of coordinates */
         7571  +  int iLevel;                       /* Level of current node or entry */
         7572  +  int mxLevel;                      /* The largest iLevel value in the tree */
         7573  +  sqlite3_int64 iRowid;             /* Rowid for current entry */
         7574  +  sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
         7575  +  int eParentWithin;                /* Visibility of parent node */
         7576  +  int eWithin;                      /* OUT: Visiblity */
         7577  +  sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
         7578  +};
         7579  +
         7580  +/*
         7581  +** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
         7582  +*/
         7583  +#define NOT_WITHIN       0   /* Object completely outside of query region */
         7584  +#define PARTLY_WITHIN    1   /* Object partially overlaps query region */
         7585  +#define FULLY_WITHIN     2   /* Object fully contained within query region */
         7586  +
  7495   7587   
  7496   7588   #if 0
  7497   7589   }  /* end of the 'extern "C"' block */
  7498   7590   #endif
  7499   7591   
  7500   7592   #endif  /* ifndef _SQLITE3RTREE_H_ */
  7501   7593   
................................................................................
  8414   8506   
  8415   8507   /*
  8416   8508   ** Estimated quantities used for query planning are stored as 16-bit
  8417   8509   ** logarithms.  For quantity X, the value stored is 10*log2(X).  This
  8418   8510   ** gives a possible range of values of approximately 1.0e986 to 1e-986.
  8419   8511   ** But the allowed values are "grainy".  Not every value is representable.
  8420   8512   ** For example, quantities 16 and 17 are both represented by a LogEst
  8421         -** of 40.  However, since LogEst quantatites are suppose to be estimates,
         8513  +** of 40.  However, since LogEst quantaties are suppose to be estimates,
  8422   8514   ** not exact values, this imprecision is not a problem.
  8423   8515   **
  8424         -** "LogEst" is short for "Logarithimic Estimate".
         8516  +** "LogEst" is short for "Logarithmic Estimate".
  8425   8517   **
  8426   8518   ** Examples:
  8427   8519   **      1 -> 0              20 -> 43          10000 -> 132
  8428   8520   **      2 -> 10             25 -> 46          25000 -> 146
  8429   8521   **      3 -> 16            100 -> 66        1000000 -> 199
  8430   8522   **      4 -> 20           1000 -> 99        1048576 -> 200
  8431   8523   **     10 -> 33           1024 -> 100    4294967296 -> 320
................................................................................
  8435   8527   **
  8436   8528   **    0.5 -> -10           0.1 -> -33        0.0625 -> -40
  8437   8529   */
  8438   8530   typedef INT16_TYPE LogEst;
  8439   8531   
  8440   8532   /*
  8441   8533   ** Macros to determine whether the machine is big or little endian,
  8442         -** evaluated at runtime.
         8534  +** and whether or not that determination is run-time or compile-time.
         8535  +**
         8536  +** For best performance, an attempt is made to guess at the byte-order
         8537  +** using C-preprocessor macros.  If that is unsuccessful, or if
         8538  +** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
         8539  +** at run-time.
  8443   8540   */
  8444   8541   #ifdef SQLITE_AMALGAMATION
  8445   8542   SQLITE_PRIVATE const int sqlite3one = 1;
  8446   8543   #else
  8447   8544   SQLITE_PRIVATE const int sqlite3one;
  8448   8545   #endif
  8449         -#if defined(i386) || defined(__i386__) || defined(_M_IX86)\
  8450         -                             || defined(__x86_64) || defined(__x86_64__)
         8546  +#if (defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
         8547  +     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
         8548  +     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
         8549  +     defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
         8550  +# define SQLITE_BYTEORDER    1234
  8451   8551   # define SQLITE_BIGENDIAN    0
  8452   8552   # define SQLITE_LITTLEENDIAN 1
  8453   8553   # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
  8454         -#else
         8554  +#endif
         8555  +#if (defined(sparc)    || defined(__ppc__))  \
         8556  +    && !defined(SQLITE_RUNTIME_BYTEORDER)
         8557  +# define SQLITE_BYTEORDER    4321
         8558  +# define SQLITE_BIGENDIAN    1
         8559  +# define SQLITE_LITTLEENDIAN 0
         8560  +# define SQLITE_UTF16NATIVE  SQLITE_UTF16BE
         8561  +#endif
         8562  +#if !defined(SQLITE_BYTEORDER)
         8563  +# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
  8455   8564   # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
  8456   8565   # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
  8457   8566   # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  8458   8567   #endif
  8459   8568   
  8460   8569   /*
  8461   8570   ** Constants for the largest and smallest possible 64-bit signed integers.
................................................................................
  8765   8874   #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
  8766   8875   #define BTREE_MEMORY        2  /* This is an in-memory DB */
  8767   8876   #define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
  8768   8877   #define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */
  8769   8878   
  8770   8879   SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
  8771   8880   SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
         8881  +#if SQLITE_MAX_MMAP_SIZE>0
  8772   8882   SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
         8883  +#endif
  8773   8884   SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
  8774   8885   SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  8775   8886   SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  8776   8887   SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
  8777   8888   SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
  8778   8889   SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
  8779   8890   SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
................................................................................
  8815   8926   ** indices.)
  8816   8927   */
  8817   8928   #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
  8818   8929   #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
  8819   8930   
  8820   8931   SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
  8821   8932   SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
         8933  +SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
  8822   8934   SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
  8823   8935   
  8824   8936   SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
  8825   8937   SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
  8826   8938   
  8827   8939   SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
  8828   8940   
................................................................................
  8889   9001   SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  8890   9002   SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  8891   9003   
  8892   9004   SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  8893   9005   SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  8894   9006   
  8895   9007   SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  8896         -SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
         9008  +SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
  8897   9009   SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
  8898   9010   SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
  8899   9011   SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
         9012  +SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
  8900   9013   
  8901   9014   #ifndef NDEBUG
  8902   9015   SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
  8903   9016   #endif
  8904   9017   
  8905   9018   #ifndef SQLITE_OMIT_BTREECOUNT
  8906   9019   SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
................................................................................
  9132   9245   #define OP_NextIfOpen      7
  9133   9246   #define OP_Prev            8
  9134   9247   #define OP_Next            9
  9135   9248   #define OP_AggStep        10 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  9136   9249   #define OP_Checkpoint     11
  9137   9250   #define OP_JournalMode    12
  9138   9251   #define OP_Vacuum         13
  9139         -#define OP_VFilter        14 /* synopsis: iPlan=r[P3] zPlan='P4'           */
         9252  +#define OP_VFilter        14 /* synopsis: iplan=r[P3] zplan='P4'           */
  9140   9253   #define OP_VUpdate        15 /* synopsis: data=r[P3@P2]                    */
  9141   9254   #define OP_Goto           16
  9142   9255   #define OP_Gosub          17
  9143   9256   #define OP_Return         18
  9144   9257   #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
  9145   9258   #define OP_InitCoroutine  20
  9146   9259   #define OP_EndCoroutine   21
................................................................................
  9159   9272   #define OP_SCopy          34 /* synopsis: r[P2]=r[P1]                      */
  9160   9273   #define OP_ResultRow      35 /* synopsis: output=r[P1@P2]                  */
  9161   9274   #define OP_CollSeq        36
  9162   9275   #define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
  9163   9276   #define OP_MustBeInt      38
  9164   9277   #define OP_RealAffinity   39
  9165   9278   #define OP_Permutation    40
  9166         -#define OP_Compare        41
         9279  +#define OP_Compare        41 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
  9167   9280   #define OP_Jump           42
  9168   9281   #define OP_Once           43
  9169   9282   #define OP_If             44
  9170   9283   #define OP_IfNot          45
  9171   9284   #define OP_Column         46 /* synopsis: r[P3]=PX                         */
  9172   9285   #define OP_Affinity       47 /* synopsis: affinity(r[P1@P2])               */
  9173   9286   #define OP_MakeRecord     48 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
................................................................................
  9186   9299   #define OP_SeekGE         61
  9187   9300   #define OP_SeekGT         62
  9188   9301   #define OP_Seek           63 /* synopsis: intkey=r[P2]                     */
  9189   9302   #define OP_NoConflict     64 /* synopsis: key=r[P3@P4]                     */
  9190   9303   #define OP_NotFound       65 /* synopsis: key=r[P3@P4]                     */
  9191   9304   #define OP_Found          66 /* synopsis: key=r[P3@P4]                     */
  9192   9305   #define OP_NotExists      67 /* synopsis: intkey=r[P3]                     */
  9193         -#define OP_Sequence       68 /* synopsis: r[P2]=rowid                      */
         9306  +#define OP_Sequence       68 /* synopsis: r[P2]=cursor[P1].ctr++           */
  9194   9307   #define OP_NewRowid       69 /* synopsis: r[P2]=rowid                      */
  9195   9308   #define OP_Insert         70 /* synopsis: intkey=r[P3] data=r[P2]          */
  9196   9309   #define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  9197   9310   #define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  9198   9311   #define OP_InsertInt      73 /* synopsis: intkey=P3 data=r[P2]             */
  9199   9312   #define OP_Delete         74
  9200   9313   #define OP_ResetCount     75
................................................................................
  9234   9347   #define OP_IdxRowid      109 /* synopsis: r[P2]=rowid                      */
  9235   9348   #define OP_IdxLE         110 /* synopsis: key=r[P3@P4]                     */
  9236   9349   #define OP_IdxGT         111 /* synopsis: key=r[P3@P4]                     */
  9237   9350   #define OP_IdxLT         112 /* synopsis: key=r[P3@P4]                     */
  9238   9351   #define OP_IdxGE         113 /* synopsis: key=r[P3@P4]                     */
  9239   9352   #define OP_Destroy       114
  9240   9353   #define OP_Clear         115
  9241         -#define OP_CreateIndex   116 /* synopsis: r[P2]=root iDb=P1                */
  9242         -#define OP_CreateTable   117 /* synopsis: r[P2]=root iDb=P1                */
  9243         -#define OP_ParseSchema   118
  9244         -#define OP_LoadAnalysis  119
  9245         -#define OP_DropTable     120
  9246         -#define OP_DropIndex     121
  9247         -#define OP_DropTrigger   122
  9248         -#define OP_IntegrityCk   123
  9249         -#define OP_RowSetAdd     124 /* synopsis: rowset(P1)=r[P2]                 */
  9250         -#define OP_RowSetRead    125 /* synopsis: r[P3]=rowset(P1)                 */
  9251         -#define OP_RowSetTest    126 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  9252         -#define OP_Program       127
  9253         -#define OP_Param         128
  9254         -#define OP_FkCounter     129 /* synopsis: fkctr[P1]+=P2                    */
  9255         -#define OP_FkIfZero      130 /* synopsis: if fkctr[P1]==0 goto P2          */
  9256         -#define OP_MemMax        131 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  9257         -#define OP_IfPos         132 /* synopsis: if r[P1]>0 goto P2               */
         9354  +#define OP_ResetSorter   116
         9355  +#define OP_CreateIndex   117 /* synopsis: r[P2]=root iDb=P1                */
         9356  +#define OP_CreateTable   118 /* synopsis: r[P2]=root iDb=P1                */
         9357  +#define OP_ParseSchema   119
         9358  +#define OP_LoadAnalysis  120
         9359  +#define OP_DropTable     121
         9360  +#define OP_DropIndex     122
         9361  +#define OP_DropTrigger   123
         9362  +#define OP_IntegrityCk   124
         9363  +#define OP_RowSetAdd     125 /* synopsis: rowset(P1)=r[P2]                 */
         9364  +#define OP_RowSetRead    126 /* synopsis: r[P3]=rowset(P1)                 */
         9365  +#define OP_RowSetTest    127 /* synopsis: if r[P3] in rowset(P1) goto P2   */
         9366  +#define OP_Program       128
         9367  +#define OP_Param         129
         9368  +#define OP_FkCounter     130 /* synopsis: fkctr[P1]+=P2                    */
         9369  +#define OP_FkIfZero      131 /* synopsis: if fkctr[P1]==0 goto P2          */
         9370  +#define OP_MemMax        132 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  9258   9371   #define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  9259         -#define OP_IfNeg         134 /* synopsis: if r[P1]<0 goto P2               */
  9260         -#define OP_IfZero        135 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
  9261         -#define OP_AggFinal      136 /* synopsis: accum=r[P1] N=P2                 */
  9262         -#define OP_IncrVacuum    137
  9263         -#define OP_Expire        138
  9264         -#define OP_TableLock     139 /* synopsis: iDb=P1 root=P2 write=P3          */
  9265         -#define OP_VBegin        140
  9266         -#define OP_VCreate       141
  9267         -#define OP_VDestroy      142
         9372  +#define OP_IfPos         134 /* synopsis: if r[P1]>0 goto P2               */
         9373  +#define OP_IfNeg         135 /* synopsis: if r[P1]<0 goto P2               */
         9374  +#define OP_IfZero        136 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
         9375  +#define OP_AggFinal      137 /* synopsis: accum=r[P1] N=P2                 */
         9376  +#define OP_IncrVacuum    138
         9377  +#define OP_Expire        139
         9378  +#define OP_TableLock     140 /* synopsis: iDb=P1 root=P2 write=P3          */
         9379  +#define OP_VBegin        141
         9380  +#define OP_VCreate       142
  9268   9381   #define OP_ToText        143 /* same as TK_TO_TEXT                         */
  9269   9382   #define OP_ToBlob        144 /* same as TK_TO_BLOB                         */
  9270   9383   #define OP_ToNumeric     145 /* same as TK_TO_NUMERIC                      */
  9271   9384   #define OP_ToInt         146 /* same as TK_TO_INT                          */
  9272   9385   #define OP_ToReal        147 /* same as TK_TO_REAL                         */
  9273         -#define OP_VOpen         148
  9274         -#define OP_VColumn       149 /* synopsis: r[P3]=vcolumn(P2)                */
  9275         -#define OP_VNext         150
  9276         -#define OP_VRename       151
  9277         -#define OP_Pagecount     152
  9278         -#define OP_MaxPgcnt      153
  9279         -#define OP_Init          154 /* synopsis: Start at P2                      */
  9280         -#define OP_Noop          155
  9281         -#define OP_Explain       156
         9386  +#define OP_VDestroy      148
         9387  +#define OP_VOpen         149
         9388  +#define OP_VColumn       150 /* synopsis: r[P3]=vcolumn(P2)                */
         9389  +#define OP_VNext         151
         9390  +#define OP_VRename       152
         9391  +#define OP_Pagecount     153
         9392  +#define OP_MaxPgcnt      154
         9393  +#define OP_Init          155 /* synopsis: Start at P2                      */
         9394  +#define OP_Noop          156
         9395  +#define OP_Explain       157
  9282   9396   
  9283   9397   
  9284   9398   /* Properties such as "out2" or "jump" that are specified in
  9285   9399   ** comments following the "case" for each opcode in the vdbe.c
  9286   9400   ** are encoded into bitvectors as follows:
  9287   9401   */
  9288   9402   #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
................................................................................
  9303   9417   /*  56 */ 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x08,\
  9304   9418   /*  64 */ 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00, 0x4c,\
  9305   9419   /*  72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
  9306   9420   /*  80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
  9307   9421   /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
  9308   9422   /*  96 */ 0x24, 0x02, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01,\
  9309   9423   /* 104 */ 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01, 0x01,\
  9310         -/* 112 */ 0x01, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00,\
  9311         -/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45, 0x15, 0x01,\
  9312         -/* 128 */ 0x02, 0x00, 0x01, 0x08, 0x05, 0x02, 0x05, 0x05,\
  9313         -/* 136 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,\
  9314         -/* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x01, 0x00,\
  9315         -/* 152 */ 0x02, 0x02, 0x01, 0x00, 0x00,}
         9424  +/* 112 */ 0x01, 0x01, 0x02, 0x00, 0x00, 0x02, 0x02, 0x00,\
         9425  +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45, 0x15,\
         9426  +/* 128 */ 0x01, 0x02, 0x00, 0x01, 0x08, 0x02, 0x05, 0x05,\
         9427  +/* 136 */ 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04,\
         9428  +/* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x01,\
         9429  +/* 152 */ 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,}
  9316   9430   
  9317   9431   /************** End of opcodes.h *********************************************/
  9318   9432   /************** Continuing where we left off in vdbe.h ***********************/
  9319   9433   
  9320   9434   /*
  9321   9435   ** Prototypes for the VDBE interface.  See comments on the implementation
  9322   9436   ** for a description of what each of these routines does.
................................................................................
  9365   9479   SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
  9366   9480   SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
  9367   9481   #ifndef SQLITE_OMIT_TRACE
  9368   9482   SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  9369   9483   #endif
  9370   9484   
  9371   9485   SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  9372         -SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,const UnpackedRecord*,int);
         9486  +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*,int);
  9373   9487   SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  9374   9488   
  9375         -typedef int (*RecordCompare)(int,const void*,const UnpackedRecord*,int);
         9489  +typedef int (*RecordCompare)(int,const void*,UnpackedRecord*,int);
  9376   9490   SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
  9377   9491   
  9378   9492   #ifndef SQLITE_OMIT_TRIGGER
  9379   9493   SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  9380   9494   #endif
  9381   9495   
  9382   9496   /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
................................................................................
  9846   9960   ** This header file is #include-ed by sqliteInt.h and thus ends up
  9847   9961   ** being included by every source file.
  9848   9962   */
  9849   9963   #ifndef _SQLITE_OS_H_
  9850   9964   #define _SQLITE_OS_H_
  9851   9965   
  9852   9966   /*
  9853         -** Figure out if we are dealing with Unix, Windows, or some other
  9854         -** operating system.  After the following block of preprocess macros,
  9855         -** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
  9856         -** will defined to either 1 or 0.  One of the four will be 1.  The other 
  9857         -** three will be 0.
         9967  +** Attempt to automatically detect the operating system and setup the
         9968  +** necessary pre-processor macros for it.
         9969  +*/
         9970  +/************** Include os_setup.h in the middle of os.h *********************/
         9971  +/************** Begin file os_setup.h ****************************************/
         9972  +/*
         9973  +** 2013 November 25
         9974  +**
         9975  +** The author disclaims copyright to this source code.  In place of
         9976  +** a legal notice, here is a blessing:
         9977  +**
         9978  +**    May you do good and not evil.
         9979  +**    May you find forgiveness for yourself and forgive others.
         9980  +**    May you share freely, never taking more than you give.
         9981  +**
         9982  +******************************************************************************
         9983  +**
         9984  +** This file contains pre-processor directives related to operating system
         9985  +** detection and/or setup.
         9986  +*/
         9987  +#ifndef _OS_SETUP_H_
         9988  +#define _OS_SETUP_H_
         9989  +
         9990  +/*
         9991  +** Figure out if we are dealing with Unix, Windows, or some other operating
         9992  +** system.
         9993  +**
         9994  +** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
         9995  +** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0.  One of
         9996  +** the three will be 1.  The other two will be 0.
  9858   9997   */
  9859   9998   #if defined(SQLITE_OS_OTHER)
  9860   9999   # if SQLITE_OS_OTHER==1
  9861  10000   #   undef SQLITE_OS_UNIX
  9862  10001   #   define SQLITE_OS_UNIX 0
  9863  10002   #   undef SQLITE_OS_WIN
  9864  10003   #   define SQLITE_OS_WIN 0
................................................................................
  9865  10004   # else
  9866  10005   #   undef SQLITE_OS_OTHER
  9867  10006   # endif
  9868  10007   #endif
  9869  10008   #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  9870  10009   # define SQLITE_OS_OTHER 0
  9871  10010   # ifndef SQLITE_OS_WIN
  9872         -#   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
        10011  +#    if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
        10012  +        defined(__MINGW32__) || defined(__BORLANDC__)
  9873  10013   #     define SQLITE_OS_WIN 1
  9874  10014   #     define SQLITE_OS_UNIX 0
  9875  10015   #   else
  9876  10016   #     define SQLITE_OS_WIN 0
  9877  10017   #     define SQLITE_OS_UNIX 1
  9878  10018   #  endif
  9879  10019   # else
................................................................................
  9881  10021   # endif
  9882  10022   #else
  9883  10023   # ifndef SQLITE_OS_WIN
  9884  10024   #  define SQLITE_OS_WIN 0
  9885  10025   # endif
  9886  10026   #endif
  9887  10027   
  9888         -#if SQLITE_OS_WIN
  9889         -# include <windows.h>
  9890         -#endif
        10028  +#endif /* _OS_SETUP_H_ */
  9891  10029   
  9892         -/*
  9893         -** Determine if we are dealing with Windows NT.
  9894         -**
  9895         -** We ought to be able to determine if we are compiling for win98 or winNT
  9896         -** using the _WIN32_WINNT macro as follows:
  9897         -**
  9898         -** #if defined(_WIN32_WINNT)
  9899         -** # define SQLITE_OS_WINNT 1
  9900         -** #else
  9901         -** # define SQLITE_OS_WINNT 0
  9902         -** #endif
  9903         -**
  9904         -** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
  9905         -** so the above test does not work.  We'll just assume that everything is
  9906         -** winNT unless the programmer explicitly says otherwise by setting
  9907         -** SQLITE_OS_WINNT to 0.
  9908         -*/
  9909         -#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
  9910         -# define SQLITE_OS_WINNT 1
  9911         -#endif
  9912         -
  9913         -/*
  9914         -** Determine if we are dealing with WindowsCE - which has a much
  9915         -** reduced API.
  9916         -*/
  9917         -#if defined(_WIN32_WCE)
  9918         -# define SQLITE_OS_WINCE 1
  9919         -#else
  9920         -# define SQLITE_OS_WINCE 0
  9921         -#endif
  9922         -
  9923         -/*
  9924         -** Determine if we are dealing with WinRT, which provides only a subset of
  9925         -** the full Win32 API.
  9926         -*/
  9927         -#if !defined(SQLITE_OS_WINRT)
  9928         -# define SQLITE_OS_WINRT 0
  9929         -#endif
        10030  +/************** End of os_setup.h ********************************************/
        10031  +/************** Continuing where we left off in os.h *************************/
  9930  10032   
  9931  10033   /* If the SET_FULLSYNC macro is not defined above, then make it
  9932  10034   ** a no-op
  9933  10035   */
  9934  10036   #ifndef SET_FULLSYNC
  9935  10037   # define SET_FULLSYNC(x,y)
  9936  10038   #endif
................................................................................
 10821  10923     Index *pIndex;       /* List of SQL indexes on this table. */
 10822  10924     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 10823  10925     FKey *pFKey;         /* Linked list of all foreign keys in this table */
 10824  10926     char *zColAff;       /* String defining the affinity of each column */
 10825  10927   #ifndef SQLITE_OMIT_CHECK
 10826  10928     ExprList *pCheck;    /* All CHECK constraints */
 10827  10929   #endif
 10828         -  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
        10930  +  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
 10829  10931     int tnum;            /* Root BTree node for this table (see note above) */
 10830  10932     i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
 10831  10933     i16 nCol;            /* Number of columns in this table */
 10832  10934     u16 nRef;            /* Number of pointers to this Table */
 10833  10935     LogEst szTabRow;     /* Estimated size of each table row in bytes */
 10834  10936     u8 tabFlags;         /* Mask of TF_* values */
 10835  10937     u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
................................................................................
 10994  11096   ** The r1 and r2 member variables are only used by the optimized comparison
 10995  11097   ** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
 10996  11098   */
 10997  11099   struct UnpackedRecord {
 10998  11100     KeyInfo *pKeyInfo;  /* Collation and sort-order information */
 10999  11101     u16 nField;         /* Number of entries in apMem[] */
 11000  11102     i8 default_rc;      /* Comparison result if keys are equal */
        11103  +  u8 isCorrupt;       /* Corruption detected by xRecordCompare() */
 11001  11104     Mem *aMem;          /* Values */
 11002  11105     int r1;             /* Value to return if (lhs > rhs) */
 11003  11106     int r2;             /* Value to return if (rhs < lhs) */
 11004  11107   };
 11005  11108   
 11006  11109   
 11007  11110   /*
................................................................................
 11029  11132   ** and the value of Index.onError indicate the which conflict resolution 
 11030  11133   ** algorithm to employ whenever an attempt is made to insert a non-unique
 11031  11134   ** element.
 11032  11135   */
 11033  11136   struct Index {
 11034  11137     char *zName;             /* Name of this index */
 11035  11138     i16 *aiColumn;           /* Which columns are used by this index.  1st is 0 */
 11036         -  tRowcnt *aiRowEst;       /* From ANALYZE: Est. rows selected by each column */
        11139  +  LogEst *aiRowLogEst;     /* From ANALYZE: Est. rows selected by each column */
 11037  11140     Table *pTable;           /* The SQL table being indexed */
 11038  11141     char *zColAff;           /* String defining the affinity of each column */
 11039  11142     Index *pNext;            /* The next index associated with the same table */
 11040  11143     Schema *pSchema;         /* Schema containing this index */
 11041  11144     u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
 11042  11145     char **azColl;           /* Array of collation sequence names for index */
 11043  11146     Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
 11044  11147     KeyInfo *pKeyInfo;       /* A KeyInfo object suitable for this index */
 11045  11148     int tnum;                /* DB Page containing root of this index */
 11046  11149     LogEst szIdxRow;         /* Estimated average row size in bytes */
 11047  11150     u16 nKeyCol;             /* Number of columns forming the key */
 11048  11151     u16 nColumn;             /* Number of columns stored in the index */
 11049  11152     u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 11050         -  unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
        11153  +  unsigned idxType:2;      /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
 11051  11154     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
 11052  11155     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
 11053  11156     unsigned isResized:1;    /* True if resizeIndexObject() has been called */
 11054  11157     unsigned isCovering:1;   /* True if this is a covering index */
 11055  11158   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 11056  11159     int nSample;             /* Number of elements in aSample[] */
 11057  11160     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
 11058  11161     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
 11059  11162     IndexSample *aSample;    /* Samples of the left-most key */
 11060  11163   #endif
 11061  11164   };
 11062  11165   
        11166  +/*
        11167  +** Allowed values for Index.idxType
        11168  +*/
        11169  +#define SQLITE_IDXTYPE_APPDEF      0   /* Created using CREATE INDEX */
        11170  +#define SQLITE_IDXTYPE_UNIQUE      1   /* Implements a UNIQUE constraint */
        11171  +#define SQLITE_IDXTYPE_PRIMARYKEY  2   /* Is the PRIMARY KEY for the table */
        11172  +
        11173  +/* Return true if index X is a PRIMARY KEY index */
        11174  +#define IsPrimaryKeyIndex(X)  ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
        11175  +
 11063  11176   /*
 11064  11177   ** Each sample stored in the sqlite_stat3 table is represented in memory 
 11065  11178   ** using a structure of this type.  See documentation at the top of the
 11066  11179   ** analyze.c source file for additional information.
 11067  11180   */
 11068  11181   struct IndexSample {
 11069  11182     void *p;          /* Pointer to sampled record */
................................................................................
 11260  11373   #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
 11261  11374   #define EP_Resolved  0x000004 /* IDs have been resolved to COLUMNs */
 11262  11375   #define EP_Error     0x000008 /* Expression contains one or more errors */
 11263  11376   #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
 11264  11377   #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
 11265  11378   #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
 11266  11379   #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
 11267         -#define EP_Collate   0x000100 /* Tree contains a TK_COLLATE opeartor */
 11268         -      /* unused      0x000200 */
        11380  +#define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
        11381  +#define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
 11269  11382   #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
 11270  11383   #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
 11271  11384   #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
 11272  11385   #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
 11273  11386   #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
 11274  11387   #define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
 11275  11388   #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
................................................................................
 11325  11438   ** column expression as it exists in a SELECT statement.  However, if
 11326  11439   ** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
 11327  11440   ** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
 11328  11441   ** form is used for name resolution with nested FROM clauses.
 11329  11442   */
 11330  11443   struct ExprList {
 11331  11444     int nExpr;             /* Number of expressions on the list */
 11332         -  int iECursor;          /* VDBE Cursor associated with this ExprList */
 11333  11445     struct ExprList_item { /* For each expression in the list */
 11334  11446       Expr *pExpr;            /* The list of expressions */
 11335  11447       char *zName;            /* Token associated with this expression */
 11336  11448       char *zSpan;            /* Original text of the expression */
 11337  11449       u8 sortOrder;           /* 1 for DESC or 0 for ASC */
 11338  11450       unsigned done :1;       /* A flag to indicate when processing is finished */
 11339  11451       unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
................................................................................
 11475  11587   #define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
 11476  11588   #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
 11477  11589   #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
 11478  11590   #define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
 11479  11591   #define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
 11480  11592   #define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
 11481  11593   #define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
        11594  +#define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
 11482  11595   
 11483  11596   /* Allowed return values from sqlite3WhereIsDistinct()
 11484  11597   */
 11485  11598   #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
 11486  11599   #define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
 11487  11600   #define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
 11488  11601   #define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
................................................................................
 11549  11662   ** sequences for the ORDER BY clause.
 11550  11663   */
 11551  11664   struct Select {
 11552  11665     ExprList *pEList;      /* The fields of the result */
 11553  11666     u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
 11554  11667     u16 selFlags;          /* Various SF_* values */
 11555  11668     int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
 11556         -  int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
        11669  +  int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
 11557  11670     u64 nSelectRow;        /* Estimated number of result rows */
 11558  11671     SrcList *pSrc;         /* The FROM clause */
 11559  11672     Expr *pWhere;          /* The WHERE clause */
 11560  11673     ExprList *pGroupBy;    /* The GROUP BY clause */
 11561  11674     Expr *pHaving;         /* The HAVING clause */
 11562  11675     ExprList *pOrderBy;    /* The ORDER BY clause */
 11563  11676     Select *pPrior;        /* Prior select in a compound select statement */
................................................................................
 11573  11686   */
 11574  11687   #define SF_Distinct        0x0001  /* Output should be DISTINCT */
 11575  11688   #define SF_Resolved        0x0002  /* Identifiers have been resolved */
 11576  11689   #define SF_Aggregate       0x0004  /* Contains aggregate functions */
 11577  11690   #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
 11578  11691   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
 11579  11692   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
 11580         -#define SF_UseSorter       0x0040  /* Sort using a sorter */
        11693  +                    /*     0x0040  NOT USED */
 11581  11694   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
 11582         -#define SF_Materialize     0x0100  /* NOT USED */
        11695  +                    /*     0x0100  NOT USED */
 11583  11696   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
 11584  11697   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
 11585  11698   #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
 11586  11699   #define SF_Compound        0x1000  /* Part of a compound query */
 11587  11700   
 11588  11701   
 11589  11702   /*
................................................................................
 11628  11741   **     SRT_Coroutine   Generate a co-routine that returns a new row of
 11629  11742   **                     results each time it is invoked.  The entry point
 11630  11743   **                     of the co-routine is stored in register pDest->iSDParm
 11631  11744   **                     and the result row is stored in pDest->nDest registers
 11632  11745   **                     starting with pDest->iSdst.
 11633  11746   **
 11634  11747   **     SRT_Table       Store results in temporary table pDest->iSDParm.
 11635         -**                     This is like SRT_EphemTab except that the table
 11636         -**                     is assumed to already be open.
        11748  +**     SRT_Fifo        This is like SRT_EphemTab except that the table
        11749  +**                     is assumed to already be open.  SRT_Fifo has
        11750  +**                     the additional property of being able to ignore
        11751  +**                     the ORDER BY clause.
 11637  11752   **
 11638         -**     SRT_DistTable   Store results in a temporary table pDest->iSDParm.
        11753  +**     SRT_DistFifo    Store results in a temporary table pDest->iSDParm.
 11639  11754   **                     But also use temporary table pDest->iSDParm+1 as
 11640  11755   **                     a record of all prior results and ignore any duplicate
 11641         -**                     rows.  Name means:  "Distinct Table".
        11756  +**                     rows.  Name means:  "Distinct Fifo".
 11642  11757   **
 11643  11758   **     SRT_Queue       Store results in priority queue pDest->iSDParm (really
 11644  11759   **                     an index).  Append a sequence number so that all entries
 11645  11760   **                     are distinct.
 11646  11761   **
 11647  11762   **     SRT_DistQueue   Store results in priority queue pDest->iSDParm only if
 11648  11763   **                     the same record has never been stored before.  The
 11649  11764   **                     index at pDest->iSDParm+1 hold all prior stores.
 11650  11765   */
 11651  11766   #define SRT_Union        1  /* Store result as keys in an index */
 11652  11767   #define SRT_Except       2  /* Remove result from a UNION index */
 11653  11768   #define SRT_Exists       3  /* Store 1 if the result is not empty */
 11654  11769   #define SRT_Discard      4  /* Do not save the results anywhere */
        11770  +#define SRT_Fifo         5  /* Store result as data with an automatic rowid */
        11771  +#define SRT_DistFifo     6  /* Like SRT_Fifo, but unique results only */
        11772  +#define SRT_Queue        7  /* Store result in an queue */
        11773  +#define SRT_DistQueue    8  /* Like SRT_Queue, but unique results only */
 11655  11774   
 11656  11775   /* The ORDER BY clause is ignored for all of the above */
 11657         -#define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
        11776  +#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
 11658  11777   
 11659         -#define SRT_Output       5  /* Output each row of result */
 11660         -#define SRT_Mem          6  /* Store result in a memory cell */
 11661         -#define SRT_Set          7  /* Store results as keys in an index */
 11662         -#define SRT_EphemTab     8  /* Create transient tab and store like SRT_Table */
 11663         -#define SRT_Coroutine    9  /* Generate a single row of result */
 11664         -#define SRT_Table       10  /* Store result as data with an automatic rowid */
 11665         -#define SRT_DistTable   11  /* Like SRT_Table, but unique results only */
 11666         -#define SRT_Queue       12  /* Store result in an queue */
 11667         -#define SRT_DistQueue   13  /* Like SRT_Queue, but unique results only */
        11778  +#define SRT_Output       9  /* Output each row of result */
        11779  +#define SRT_Mem         10  /* Store result in a memory cell */
        11780  +#define SRT_Set         11  /* Store results as keys in an index */
        11781  +#define SRT_EphemTab    12  /* Create transient tab and store like SRT_Table */
        11782  +#define SRT_Coroutine   13  /* Generate a single row of result */
        11783  +#define SRT_Table       14  /* Store result as data with an automatic rowid */
 11668  11784   
 11669  11785   /*
 11670  11786   ** An instance of this object describes where to put of the results of
 11671  11787   ** a SELECT statement.
 11672  11788   */
 11673  11789   struct SelectDest {
 11674  11790     u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
................................................................................
 11758  11874     char *zErrMsg;       /* An error message */
 11759  11875     Vdbe *pVdbe;         /* An engine for executing database bytecode */
 11760  11876     int rc;              /* Return code from execution */
 11761  11877     u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
 11762  11878     u8 checkSchema;      /* Causes schema cookie check after an error */
 11763  11879     u8 nested;           /* Number of nested calls to the parser/code generator */
 11764  11880     u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
 11765         -  u8 nColCache;        /* Number of entries in aColCache[] */
 11766         -  u8 iColCache;        /* Next entry in aColCache[] to replace */
 11767  11881     u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
 11768  11882     u8 mayAbort;         /* True if statement may throw an ABORT exception */
 11769  11883     u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
 11770  11884     u8 okConstFactor;    /* OK to factor out constants */
 11771  11885     int aTempReg[8];     /* Holding area for temporary registers */
 11772  11886     int nRangeReg;       /* Size of the temporary register block */
 11773  11887     int iRangeReg;       /* First register in temporary register block */
................................................................................
 12057  12171     /* The above might be initialized to non-zero.  The following need to always
 12058  12172     ** initially be zero, however. */
 12059  12173     int isInit;                       /* True after initialization has finished */
 12060  12174     int inProgress;                   /* True while initialization in progress */
 12061  12175     int isMutexInit;                  /* True after mutexes are initialized */
 12062  12176     int isMallocInit;                 /* True after malloc is initialized */
 12063  12177     int isPCacheInit;                 /* True after malloc is initialized */
 12064         -  sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
 12065  12178     int nRefInitMutex;                /* Number of users of pInitMutex */
        12179  +  sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
 12066  12180     void (*xLog)(void*,int,const char*); /* Function for logging */
 12067  12181     void *pLogArg;                       /* First argument to xLog() */
 12068         -  int bLocaltimeFault;              /* True to fail localtime() calls */
 12069  12182   #ifdef SQLITE_ENABLE_SQLLOG
 12070  12183     void(*xSqllog)(void*,sqlite3*,const char*, int);
 12071  12184     void *pSqllogArg;
 12072  12185   #endif
 12073  12186   #ifdef SQLITE_VDBE_COVERAGE
 12074  12187     /* The following callback (if not NULL) is invoked on every VDBE branch
 12075  12188     ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
 12076  12189     */
 12077  12190     void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
 12078  12191     void *pVdbeBranchArg;                                     /* 1st argument */
 12079  12192   #endif
        12193  +#ifndef SQLITE_OMIT_BUILTIN_TEST
        12194  +  int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
        12195  +#endif
        12196  +  int bLocaltimeFault;              /* True to fail localtime() calls */
 12080  12197   };
 12081  12198   
 12082  12199   /*
 12083  12200   ** This macro is used inside of assert() statements to indicate that
 12084  12201   ** the assert is only valid on a well-formed database.  Instead of:
 12085  12202   **
 12086  12203   **     assert( X );
................................................................................
 12373  12490   SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
 12374  12491   SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
 12375  12492   SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
 12376  12493   SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
 12377  12494                       sqlite3_vfs**,char**,char **);
 12378  12495   SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
 12379  12496   SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
        12497  +
        12498  +#ifdef SQLITE_OMIT_BUILTIN_TEST
        12499  +# define sqlite3FaultSim(X) SQLITE_OK
        12500  +#else
        12501  +SQLITE_PRIVATE   int sqlite3FaultSim(int);
        12502  +#endif
 12380  12503   
 12381  12504   SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
 12382  12505   SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
 12383  12506   SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
 12384  12507   SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
 12385  12508   SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
 12386  12509   SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
 12387  12510   SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
 12388  12511   
 12389  12512   SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
 12390  12513   SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
 12391  12514   SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
 12392         -SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
        12515  +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
 12393  12516   SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
 12394  12517   
 12395  12518   SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
 12396  12519   
 12397  12520   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 12398  12521   SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
 12399  12522   #else
................................................................................
 12441  12564   SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
 12442  12565   SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
 12443  12566   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 12444  12567   SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 12445  12568   SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
 12446  12569   SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
 12447  12570   SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
        12571  +SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
 12448  12572   SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
 12449  12573   SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
 12450  12574   SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
 12451  12575   SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
 12452  12576   SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
 12453  12577   SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
 12454  12578   SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
 12455  12579   SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
 12456         -SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
        12580  +SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*);
 12457  12581   SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
 12458  12582   SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
 12459  12583   SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
 12460  12584   SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
 12461  12585   SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
 12462  12586   SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
 12463  12587   SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
................................................................................
 12501  12625   SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
 12502  12626   SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
 12503  12627   SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
 12504  12628   SQLITE_PRIVATE int sqlite3IsRowid(const char*);
 12505  12629   SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
 12506  12630   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
 12507  12631   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
        12632  +SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
 12508  12633   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 12509  12634                                        u8,u8,int,int*);
 12510  12635   SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
 12511  12636   SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
 12512  12637   SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
 12513  12638   SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
 12514  12639   SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
................................................................................
 12658  12783   #endif
 12659  12784   
 12660  12785   SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 12661  12786   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 12662  12787   SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 12663  12788   SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 12664  12789   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 12665         -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
        12790  +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*);
 12666  12791   SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
 12667  12792   SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
 12668  12793   SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
 12669  12794   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 12670  12795   SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
 12671  12796   SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
 12672  12797   SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
................................................................................
 13177  13302      0,                         /* sharedCacheEnabled */
 13178  13303      /* All the rest should always be initialized to zero */
 13179  13304      0,                         /* isInit */
 13180  13305      0,                         /* inProgress */
 13181  13306      0,                         /* isMutexInit */
 13182  13307      0,                         /* isMallocInit */
 13183  13308      0,                         /* isPCacheInit */
 13184         -   0,                         /* pInitMutex */
 13185  13309      0,                         /* nRefInitMutex */
        13310  +   0,                         /* pInitMutex */
 13186  13311      0,                         /* xLog */
 13187  13312      0,                         /* pLogArg */
 13188         -   0,                         /* bLocaltimeFault */
 13189  13313   #ifdef SQLITE_ENABLE_SQLLOG
 13190  13314      0,                         /* xSqllog */
 13191         -   0                          /* pSqllogArg */
        13315  +   0,                         /* pSqllogArg */
 13192  13316   #endif
        13317  +#ifdef SQLITE_VDBE_COVERAGE
        13318  +   0,                         /* xVdbeBranch */
        13319  +   0,                         /* pVbeBranchArg */
        13320  +#endif
        13321  +#ifndef SQLITE_OMIT_BUILTIN_TEST
        13322  +   0,                         /* xTestCallback */
        13323  +#endif
        13324  +   0                          /* bLocaltimeFault */
 13193  13325   };
 13194  13326   
 13195  13327   /*
 13196  13328   ** Hash table for global functions - functions common to all
 13197  13329   ** database connections.  After initialization, this table is
 13198  13330   ** read-only.
 13199  13331   */
................................................................................
 13743  13875     int pseudoTableReg;   /* Register holding pseudotable content. */
 13744  13876     i16 nField;           /* Number of fields in the header */
 13745  13877     u16 nHdrParsed;       /* Number of header fields parsed so far */
 13746  13878     i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
 13747  13879     u8 nullRow;           /* True if pointing to a row with no data */
 13748  13880     u8 rowidIsValid;      /* True if lastRowid is valid */
 13749  13881     u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
        13882  +  Bool isEphemeral:1;   /* True for an ephemeral table */
 13750  13883     Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
 13751  13884     Bool isTable:1;       /* True if a table requiring integer keys */
 13752  13885     Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
 13753  13886     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
 13754  13887     i64 seqCount;         /* Sequence counter */
 13755  13888     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
 13756  13889     i64 lastRowid;        /* Rowid being deleted by OP_Delete */
................................................................................
 14062  14195   SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
 14063  14196   SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
 14064  14197   SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
 14065  14198   SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
 14066  14199   SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
 14067  14200   
 14068  14201   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
 14069         -SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,const UnpackedRecord*,int*);
        14202  +SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
 14070  14203   SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
 14071  14204   SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
 14072  14205   SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
 14073  14206   SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
 14074  14207   SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
 14075  14208   SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
 14076  14209   SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
................................................................................
 14108  14241   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 14109  14242   SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 14110  14243   SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
 14111  14244   SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
 14112  14245   SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
 14113  14246   
 14114  14247   SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
        14248  +SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
 14115  14249   SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
 14116  14250   SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
 14117  14251   SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
 14118  14252   SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
 14119  14253   SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
 14120  14254   SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
 14121  14255   
................................................................................
 17847  17981     /* Round nByte up to the next valid power of two */
 17848  17982     for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
 17849  17983   
 17850  17984     /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
 17851  17985     ** block.  If not, then split a block of the next larger power of
 17852  17986     ** two in order to create a new free block of size iLogsize.
 17853  17987     */
 17854         -  for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
        17988  +  for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
 17855  17989     if( iBin>LOGMAX ){
 17856  17990       testcase( sqlite3GlobalConfig.xLog!=0 );
 17857  17991       sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
 17858  17992       return 0;
 17859  17993     }
 17860  17994     i = mem5.aiFreelist[iBin];
 17861  17995     memsys5Unlink(i, iBin);
................................................................................
 18905  19039   **    May you do good and not evil.
 18906  19040   **    May you find forgiveness for yourself and forgive others.
 18907  19041   **    May you share freely, never taking more than you give.
 18908  19042   **
 18909  19043   *************************************************************************
 18910  19044   ** This file contains the C functions that implement mutexes for win32
 18911  19045   */
        19046  +
        19047  +#if SQLITE_OS_WIN
        19048  +/*
        19049  +** Include the header file for the Windows VFS.
        19050  +*/
        19051  +/************** Include os_win.h in the middle of mutex_w32.c ****************/
        19052  +/************** Begin file os_win.h ******************************************/
        19053  +/*
        19054  +** 2013 November 25
        19055  +**
        19056  +** The author disclaims copyright to this source code.  In place of
        19057  +** a legal notice, here is a blessing:
        19058  +**
        19059  +**    May you do good and not evil.
        19060  +**    May you find forgiveness for yourself and forgive others.
        19061  +**    May you share freely, never taking more than you give.
        19062  +**
        19063  +******************************************************************************
        19064  +**
        19065  +** This file contains code that is specific to Windows.
        19066  +*/
        19067  +#ifndef _OS_WIN_H_
        19068  +#define _OS_WIN_H_
        19069  +
        19070  +/*
        19071  +** Include the primary Windows SDK header file.
        19072  +*/
        19073  +#include "windows.h"
        19074  +
        19075  +#ifdef __CYGWIN__
        19076  +# include <sys/cygwin.h>
        19077  +# include <errno.h> /* amalgamator: dontcache */
        19078  +#endif
        19079  +
        19080  +/*
        19081  +** Determine if we are dealing with Windows NT.
        19082  +**
        19083  +** We ought to be able to determine if we are compiling for Windows 9x or
        19084  +** Windows NT using the _WIN32_WINNT macro as follows:
        19085  +**
        19086  +** #if defined(_WIN32_WINNT)
        19087  +** # define SQLITE_OS_WINNT 1
        19088  +** #else
        19089  +** # define SQLITE_OS_WINNT 0
        19090  +** #endif
        19091  +**
        19092  +** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
        19093  +** it ought to, so the above test does not work.  We'll just assume that
        19094  +** everything is Windows NT unless the programmer explicitly says otherwise
        19095  +** by setting SQLITE_OS_WINNT to 0.
        19096  +*/
        19097  +#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
        19098  +# define SQLITE_OS_WINNT 1
        19099  +#endif
        19100  +
        19101  +/*
        19102  +** Determine if we are dealing with Windows CE - which has a much reduced
        19103  +** API.
        19104  +*/
        19105  +#if defined(_WIN32_WCE)
        19106  +# define SQLITE_OS_WINCE 1
        19107  +#else
        19108  +# define SQLITE_OS_WINCE 0
        19109  +#endif
        19110  +
        19111  +/*
        19112  +** Determine if we are dealing with WinRT, which provides only a subset of
        19113  +** the full Win32 API.
        19114  +*/
        19115  +#if !defined(SQLITE_OS_WINRT)
        19116  +# define SQLITE_OS_WINRT 0
        19117  +#endif
        19118  +
        19119  +#endif /* _OS_WIN_H_ */
        19120  +
        19121  +/************** End of os_win.h **********************************************/
        19122  +/************** Continuing where we left off in mutex_w32.c ******************/
        19123  +#endif
 18912  19124   
 18913  19125   /*
 18914  19126   ** The code in this file is only used if we are compiling multithreaded
 18915  19127   ** on a win32 system.
 18916  19128   */
 18917  19129   #ifdef SQLITE_MUTEX_W32
 18918  19130   
................................................................................
 20157  20369     d = digit;
 20158  20370     digit += '0';
 20159  20371     *val = (*val - d)*10.0;
 20160  20372     return (char)digit;
 20161  20373   }
 20162  20374   #endif /* SQLITE_OMIT_FLOATING_POINT */
 20163  20375   
 20164         -/*
 20165         -** Append N space characters to the given string buffer.
 20166         -*/
 20167         -SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
 20168         -  static const char zSpaces[] = "                             ";
 20169         -  while( N>=(int)sizeof(zSpaces)-1 ){
 20170         -    sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
 20171         -    N -= sizeof(zSpaces)-1;
 20172         -  }
 20173         -  if( N>0 ){
 20174         -    sqlite3StrAccumAppend(pAccum, zSpaces, N);
 20175         -  }
 20176         -}
 20177         -
 20178  20376   /*
 20179  20377   ** Set the StrAccum object to an error mode.
 20180  20378   */
 20181  20379   static void setStrAccumError(StrAccum *p, u8 eError){
 20182  20380     p->accError = eError;
 20183  20381     p->nAlloc = 0;
 20184  20382   }
................................................................................
 20260  20458       }
 20261  20459       useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
 20262  20460     }else{
 20263  20461       bArgList = useIntern = 0;
 20264  20462     }
 20265  20463     for(; (c=(*fmt))!=0; ++fmt){
 20266  20464       if( c!='%' ){
 20267         -      int amt;
 20268  20465         bufpt = (char *)fmt;
 20269         -      amt = 1;
 20270         -      while( (c=(*++fmt))!='%' && c!=0 ) amt++;
 20271         -      sqlite3StrAccumAppend(pAccum, bufpt, amt);
        20466  +      while( (c=(*++fmt))!='%' && c!=0 ){};
        20467  +      sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
 20272  20468         if( c==0 ) break;
 20273  20469       }
 20274  20470       if( (c=(*++fmt))==0 ){
 20275  20471         sqlite3StrAccumAppend(pAccum, "%", 1);
 20276  20472         break;
 20277  20473       }
 20278  20474       /* Find out what flags are present */
................................................................................
 20445  20641             if( x>=4 || (longvalue/10)%10==1 ){
 20446  20642               x = 0;
 20447  20643             }
 20448  20644             *(--bufpt) = zOrd[x*2+1];
 20449  20645             *(--bufpt) = zOrd[x*2];
 20450  20646           }
 20451  20647           {
 20452         -          register const char *cset;      /* Use registers for speed */
 20453         -          register int base;
 20454         -          cset = &aDigits[infop->charset];
 20455         -          base = infop->base;
        20648  +          const char *cset = &aDigits[infop->charset];
        20649  +          u8 base = infop->base;
 20456  20650             do{                                           /* Convert to ascii */
 20457  20651               *(--bufpt) = cset[longvalue%base];
 20458  20652               longvalue = longvalue/base;
 20459  20653             }while( longvalue>0 );
 20460  20654           }
 20461  20655           length = (int)(&zOut[nOut-1]-bufpt);
 20462  20656           for(idx=precision-length; idx>0; idx--){
................................................................................
 20752  20946         }
 20753  20947       }/* End switch over the format type */
 20754  20948       /*
 20755  20949       ** The text of the conversion is pointed to by "bufpt" and is
 20756  20950       ** "length" characters long.  The field width is "width".  Do
 20757  20951       ** the output.
 20758  20952       */
 20759         -    if( !flag_leftjustify ){
 20760         -      register int nspace;
 20761         -      nspace = width-length;
 20762         -      if( nspace>0 ){
 20763         -        sqlite3AppendSpace(pAccum, nspace);
 20764         -      }
 20765         -    }
 20766         -    if( length>0 ){
        20953  +    width -= length;
        20954  +    if( width>0 && !flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
 20767  20955         sqlite3StrAccumAppend(pAccum, bufpt, length);
 20768         -    }
 20769         -    if( flag_leftjustify ){
 20770         -      register int nspace;
 20771         -      nspace = width-length;
 20772         -      if( nspace>0 ){
 20773         -        sqlite3AppendSpace(pAccum, nspace);
 20774         -      }
 20775         -    }
        20956  +    if( width>0 && flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
        20957  +
 20776  20958       if( zExtra ) sqlite3_free(zExtra);
 20777  20959     }/* End for loop over the format string */
 20778  20960   } /* End of function */
 20779  20961   
 20780  20962   /*
 20781         -** Append N bytes of text from z to the StrAccum object.
        20963  +** Enlarge the memory allocation on a StrAccum object so that it is
        20964  +** able to accept at least N more bytes of text.
        20965  +**
        20966  +** Return the number of bytes of text that StrAccum is able to accept
        20967  +** after the attempted enlargement.  The value returned might be zero.
 20782  20968   */
 20783         -SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
 20784         -  assert( z!=0 );
 20785         -  assert( p->zText!=0 || p->nChar==0 || p->accError );
 20786         -  assert( N>=0 );
 20787         -  assert( p->accError==0 || p->nAlloc==0 );
 20788         -  if( p->nChar+N >= p->nAlloc ){
        20969  +static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
 20789  20970       char *zNew;
        20971  +  assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
 20790  20972       if( p->accError ){
 20791  20973         testcase(p->accError==STRACCUM_TOOBIG);
 20792  20974         testcase(p->accError==STRACCUM_NOMEM);
 20793         -      return;
        20975  +    return 0;
 20794  20976       }
 20795  20977       if( !p->useMalloc ){
 20796  20978         N = p->nAlloc - p->nChar - 1;
 20797  20979         setStrAccumError(p, STRACCUM_TOOBIG);
 20798         -      if( N<=0 ){
 20799         -        return;
 20800         -      }
        20980  +    return N;
 20801  20981       }else{
 20802  20982         char *zOld = (p->zText==p->zBase ? 0 : p->zText);
 20803  20983         i64 szNew = p->nChar;
 20804  20984         szNew += N + 1;
 20805  20985         if( szNew > p->mxAlloc ){
 20806  20986           sqlite3StrAccumReset(p);
 20807  20987           setStrAccumError(p, STRACCUM_TOOBIG);
 20808         -        return;
        20988  +      return 0;
 20809  20989         }else{
 20810  20990           p->nAlloc = (int)szNew;
 20811  20991         }
 20812  20992         if( p->useMalloc==1 ){
 20813  20993           zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
 20814  20994         }else{
 20815  20995           zNew = sqlite3_realloc(zOld, p->nAlloc);
 20816  20996         }
 20817  20997         if( zNew ){
        20998  +      assert( p->zText!=0 || p->nChar==0 );
 20818  20999           if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
 20819  21000           p->zText = zNew;
 20820  21001         }else{
 20821  21002           sqlite3StrAccumReset(p);
 20822  21003           setStrAccumError(p, STRACCUM_NOMEM);
 20823         -        return;
        21004  +      return 0;
 20824  21005         }
 20825  21006       }
        21007  +  return N;
        21008  +}
        21009  +
        21010  +/*
        21011  +** Append N space characters to the given string buffer.
        21012  +*/
        21013  +SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *p, int N){
        21014  +  if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
        21015  +  while( (N--)>0 ) p->zText[p->nChar++] = ' ';
        21016  +}
        21017  +
        21018  +/*
        21019  +** The StrAccum "p" is not large enough to accept N new bytes of z[].
        21020  +** So enlarge if first, then do the append.
        21021  +**
        21022  +** This is a helper routine to sqlite3StrAccumAppend() that does special-case
        21023  +** work (enlarging the buffer) using tail recursion, so that the
        21024  +** sqlite3StrAccumAppend() routine can use fast calling semantics.
        21025  +*/
        21026  +static void enlargeAndAppend(StrAccum *p, const char *z, int N){
        21027  +  N = sqlite3StrAccumEnlarge(p, N);
        21028  +  if( N>0 ){
        21029  +    memcpy(&p->zText[p->nChar], z, N);
        21030  +    p->nChar += N;
        21031  +  }
        21032  +}
        21033  +
        21034  +/*
        21035  +** Append N bytes of text from z to the StrAccum object.  Increase the
        21036  +** size of the memory allocation for StrAccum if necessary.
        21037  +*/
        21038  +SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
        21039  +  assert( z!=0 );
        21040  +  assert( p->zText!=0 || p->nChar==0 || p->accError );
        21041  +  assert( N>=0 );
        21042  +  assert( p->accError==0 || p->nAlloc==0 );
        21043  +  if( p->nChar+N >= p->nAlloc ){
        21044  +    enlargeAndAppend(p,z,N);
        21045  +    return;
 20826  21046     }
 20827  21047     assert( p->zText );
 20828  21048     memcpy(&p->zText[p->nChar], z, N);
 20829  21049     p->nChar += N;
 20830  21050   }
 20831  21051   
 20832  21052   /*
................................................................................
 21754  21974   #ifdef SQLITE_COVERAGE_TEST
 21755  21975   SQLITE_PRIVATE void sqlite3Coverage(int x){
 21756  21976     static unsigned dummy = 0;
 21757  21977     dummy += (unsigned)x;
 21758  21978   }
 21759  21979   #endif
 21760  21980   
        21981  +/*
        21982  +** Give a callback to the test harness that can be used to simulate faults
        21983  +** in places where it is difficult or expensive to do so purely by means
        21984  +** of inputs.
        21985  +**
        21986  +** The intent of the integer argument is to let the fault simulator know
        21987  +** which of multiple sqlite3FaultSim() calls has been hit.
        21988  +**
        21989  +** Return whatever integer value the test callback returns, or return
        21990  +** SQLITE_OK if no test callback is installed.
        21991  +*/
        21992  +#ifndef SQLITE_OMIT_BUILTIN_TEST
        21993  +SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
        21994  +  int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
        21995  +  return xCallback ? xCallback(iTest) : SQLITE_OK;
        21996  +}
        21997  +#endif
        21998  +
 21761  21999   #ifndef SQLITE_OMIT_FLOATING_POINT
 21762  22000   /*
 21763  22001   ** Return true if the floating point value is Not a Number (NaN).
 21764  22002   **
 21765  22003   ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
 21766  22004   ** Otherwise, we have our own implementation that works on most systems.
 21767  22005   */
................................................................................
 22969  23207       if( b>a+49 ) return b;
 22970  23208       if( b>a+31 ) return b+1;
 22971  23209       return b+x[b-a];
 22972  23210     }
 22973  23211   }
 22974  23212   
 22975  23213   /*
 22976         -** Convert an integer into a LogEst.  In other words, compute a
 22977         -** good approximatation for 10*log2(x).
        23214  +** Convert an integer into a LogEst.  In other words, compute an
        23215  +** approximation for 10*log2(x).
 22978  23216   */
 22979  23217   SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
 22980  23218     static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
 22981  23219     LogEst y = 40;
 22982  23220     if( x<8 ){
 22983  23221       if( x<2 ) return 0;
 22984  23222       while( x<8 ){  y -= 10; x <<= 1; }
................................................................................
 23326  23564        /*   7 */ "NextIfOpen"       OpHelp(""),
 23327  23565        /*   8 */ "Prev"             OpHelp(""),
 23328  23566        /*   9 */ "Next"             OpHelp(""),
 23329  23567        /*  10 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 23330  23568        /*  11 */ "Checkpoint"       OpHelp(""),
 23331  23569        /*  12 */ "JournalMode"      OpHelp(""),
 23332  23570        /*  13 */ "Vacuum"           OpHelp(""),
 23333         -     /*  14 */ "VFilter"          OpHelp("iPlan=r[P3] zPlan='P4'"),
        23571  +     /*  14 */ "VFilter"          OpHelp("iplan=r[P3] zplan='P4'"),
 23334  23572        /*  15 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
 23335  23573        /*  16 */ "Goto"             OpHelp(""),
 23336  23574        /*  17 */ "Gosub"            OpHelp(""),
 23337  23575        /*  18 */ "Return"           OpHelp(""),
 23338  23576        /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
 23339  23577        /*  20 */ "InitCoroutine"    OpHelp(""),
 23340  23578        /*  21 */ "EndCoroutine"     OpHelp(""),
................................................................................
 23353  23591        /*  34 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
 23354  23592        /*  35 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
 23355  23593        /*  36 */ "CollSeq"          OpHelp(""),
 23356  23594        /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
 23357  23595        /*  38 */ "MustBeInt"        OpHelp(""),
 23358  23596        /*  39 */ "RealAffinity"     OpHelp(""),
 23359  23597        /*  40 */ "Permutation"      OpHelp(""),
 23360         -     /*  41 */ "Compare"          OpHelp(""),
        23598  +     /*  41 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 23361  23599        /*  42 */ "Jump"             OpHelp(""),
 23362  23600        /*  43 */ "Once"             OpHelp(""),
 23363  23601        /*  44 */ "If"               OpHelp(""),
 23364  23602        /*  45 */ "IfNot"            OpHelp(""),
 23365  23603        /*  46 */ "Column"           OpHelp("r[P3]=PX"),
 23366  23604        /*  47 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 23367  23605        /*  48 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
................................................................................
 23380  23618        /*  61 */ "SeekGE"           OpHelp(""),
 23381  23619        /*  62 */ "SeekGT"           OpHelp(""),
 23382  23620        /*  63 */ "Seek"             OpHelp("intkey=r[P2]"),
 23383  23621        /*  64 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
 23384  23622        /*  65 */ "NotFound"         OpHelp("key=r[P3@P4]"),
 23385  23623        /*  66 */ "Found"            OpHelp("key=r[P3@P4]"),
 23386  23624        /*  67 */ "NotExists"        OpHelp("intkey=r[P3]"),
 23387         -     /*  68 */ "Sequence"         OpHelp("r[P2]=rowid"),
        23625  +     /*  68 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 23388  23626        /*  69 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 23389  23627        /*  70 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 23390  23628        /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
 23391  23629        /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
 23392  23630        /*  73 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 23393  23631        /*  74 */ "Delete"           OpHelp(""),
 23394  23632        /*  75 */ "ResetCount"       OpHelp(""),
................................................................................
 23428  23666        /* 109 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 23429  23667        /* 110 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
 23430  23668        /* 111 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
 23431  23669        /* 112 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
 23432  23670        /* 113 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
 23433  23671        /* 114 */ "Destroy"          OpHelp(""),
 23434  23672        /* 115 */ "Clear"            OpHelp(""),
 23435         -     /* 116 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
 23436         -     /* 117 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
 23437         -     /* 118 */ "ParseSchema"      OpHelp(""),
 23438         -     /* 119 */ "LoadAnalysis"     OpHelp(""),
 23439         -     /* 120 */ "DropTable"        OpHelp(""),
 23440         -     /* 121 */ "DropIndex"        OpHelp(""),
 23441         -     /* 122 */ "DropTrigger"      OpHelp(""),
 23442         -     /* 123 */ "IntegrityCk"      OpHelp(""),
 23443         -     /* 124 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 23444         -     /* 125 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
 23445         -     /* 126 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
 23446         -     /* 127 */ "Program"          OpHelp(""),
 23447         -     /* 128 */ "Param"            OpHelp(""),
 23448         -     /* 129 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 23449         -     /* 130 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
 23450         -     /* 131 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 23451         -     /* 132 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
        23673  +     /* 116 */ "ResetSorter"      OpHelp(""),
        23674  +     /* 117 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
        23675  +     /* 118 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
        23676  +     /* 119 */ "ParseSchema"      OpHelp(""),
        23677  +     /* 120 */ "LoadAnalysis"     OpHelp(""),
        23678  +     /* 121 */ "DropTable"        OpHelp(""),
        23679  +     /* 122 */ "DropIndex"        OpHelp(""),
        23680  +     /* 123 */ "DropTrigger"      OpHelp(""),
        23681  +     /* 124 */ "IntegrityCk"      OpHelp(""),
        23682  +     /* 125 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        23683  +     /* 126 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
        23684  +     /* 127 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
        23685  +     /* 128 */ "Program"          OpHelp(""),
        23686  +     /* 129 */ "Param"            OpHelp(""),
        23687  +     /* 130 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        23688  +     /* 131 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
        23689  +     /* 132 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 23452  23690        /* 133 */ "Real"             OpHelp("r[P2]=P4"),
 23453         -     /* 134 */ "IfNeg"            OpHelp("if r[P1]<0 goto P2"),
 23454         -     /* 135 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
 23455         -     /* 136 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 23456         -     /* 137 */ "IncrVacuum"       OpHelp(""),
 23457         -     /* 138 */ "Expire"           OpHelp(""),
 23458         -     /* 139 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 23459         -     /* 140 */ "VBegin"           OpHelp(""),
 23460         -     /* 141 */ "VCreate"          OpHelp(""),
 23461         -     /* 142 */ "VDestroy"         OpHelp(""),
        23691  +     /* 134 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
        23692  +     /* 135 */ "IfNeg"            OpHelp("if r[P1]<0 goto P2"),
        23693  +     /* 136 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
        23694  +     /* 137 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        23695  +     /* 138 */ "IncrVacuum"       OpHelp(""),
        23696  +     /* 139 */ "Expire"           OpHelp(""),
        23697  +     /* 140 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        23698  +     /* 141 */ "VBegin"           OpHelp(""),
        23699  +     /* 142 */ "VCreate"          OpHelp(""),
 23462  23700        /* 143 */ "ToText"           OpHelp(""),
 23463  23701        /* 144 */ "ToBlob"           OpHelp(""),
 23464  23702        /* 145 */ "ToNumeric"        OpHelp(""),
 23465  23703        /* 146 */ "ToInt"            OpHelp(""),
 23466  23704        /* 147 */ "ToReal"           OpHelp(""),
 23467         -     /* 148 */ "VOpen"            OpHelp(""),
 23468         -     /* 149 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 23469         -     /* 150 */ "VNext"            OpHelp(""),
 23470         -     /* 151 */ "VRename"          OpHelp(""),
 23471         -     /* 152 */ "Pagecount"        OpHelp(""),
 23472         -     /* 153 */ "MaxPgcnt"         OpHelp(""),
 23473         -     /* 154 */ "Init"             OpHelp("Start at P2"),
 23474         -     /* 155 */ "Noop"             OpHelp(""),
 23475         -     /* 156 */ "Explain"          OpHelp(""),
        23705  +     /* 148 */ "VDestroy"         OpHelp(""),
        23706  +     /* 149 */ "VOpen"            OpHelp(""),
        23707  +     /* 150 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        23708  +     /* 151 */ "VNext"            OpHelp(""),
        23709  +     /* 152 */ "VRename"          OpHelp(""),
        23710  +     /* 153 */ "Pagecount"        OpHelp(""),
        23711  +     /* 154 */ "MaxPgcnt"         OpHelp(""),
        23712  +     /* 155 */ "Init"             OpHelp("Start at P2"),
        23713  +     /* 156 */ "Noop"             OpHelp(""),
        23714  +     /* 157 */ "Explain"          OpHelp(""),
 23476  23715     };
 23477  23716     return azName[i];
 23478  23717   }
 23479  23718   #endif
 23480  23719   
 23481  23720   /************** End of opcodes.c *********************************************/
 23482  23721   /************** Begin file os_unix.c *****************************************/
................................................................................
 24008  24247   */
 24009  24248   static int posixFchown(int fd, uid_t uid, gid_t gid){
 24010  24249     return geteuid() ? 0 : fchown(fd,uid,gid);
 24011  24250   }
 24012  24251   
 24013  24252   /* Forward reference */
 24014  24253   static int openDirectory(const char*, int*);
        24254  +static int unixGetpagesize(void);
 24015  24255   
 24016  24256   /*
 24017  24257   ** Many system calls are accessed through pointer-to-functions so that
 24018  24258   ** they may be overridden at runtime to facilitate fault injection during
 24019  24259   ** testing and sandboxing.  The following array holds the names and pointers
 24020  24260   ** to all overrideable system calls.
 24021  24261   */
................................................................................
 24130  24370   #if HAVE_MREMAP
 24131  24371     { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
 24132  24372   #else
 24133  24373     { "mremap",       (sqlite3_syscall_ptr)0,               0 },
 24134  24374   #endif
 24135  24375   #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
 24136  24376   #endif
        24377  +
        24378  +  { "getpagesize",  (sqlite3_syscall_ptr)unixGetpagesize, 0 },
        24379  +#define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent)
 24137  24380   
 24138  24381   }; /* End of the overrideable system calls */
 24139  24382   
 24140  24383   /*
 24141  24384   ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
 24142  24385   ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
 24143  24386   ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
................................................................................
 27790  28033              pShmNode->sharedMask, pShmNode->exclMask));
 27791  28034     }
 27792  28035   #endif
 27793  28036   
 27794  28037     return rc;        
 27795  28038   }
 27796  28039   
        28040  +/*
        28041  +** Return the system page size.
        28042  +**
        28043  +** This function should not be called directly by other code in this file. 
        28044  +** Instead, it should be called via macro osGetpagesize().
        28045  +*/
        28046  +static int unixGetpagesize(void){
        28047  +#if defined(_BSD_SOURCE)
        28048  +  return getpagesize();
        28049  +#else
        28050  +  return (int)sysconf(_SC_PAGESIZE);
        28051  +#endif
        28052  +}
        28053  +
        28054  +/*
        28055  +** Return the minimum number of 32KB shm regions that should be mapped at
        28056  +** a time, assuming that each mapping must be an integer multiple of the
        28057  +** current system page-size.
        28058  +**
        28059  +** Usually, this is 1. The exception seems to be systems that are configured
        28060  +** to use 64KB pages - in this case each mapping must cover at least two
        28061  +** shm regions.
        28062  +*/
        28063  +static int unixShmRegionPerMap(void){
        28064  +  int shmsz = 32*1024;            /* SHM region size */
        28065  +  int pgsz = osGetpagesize();   /* System page size */
        28066  +  assert( ((pgsz-1)&pgsz)==0 );   /* Page size must be a power of 2 */
        28067  +  if( pgsz<shmsz ) return 1;
        28068  +  return pgsz/shmsz;
        28069  +}
 27797  28070   
 27798  28071   /*
 27799  28072   ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
 27800  28073   **
 27801  28074   ** This is not a VFS shared-memory method; it is a utility function called
 27802  28075   ** by VFS shared-memory methods.
 27803  28076   */
 27804  28077   static void unixShmPurge(unixFile *pFd){
 27805  28078     unixShmNode *p = pFd->pInode->pShmNode;
 27806  28079     assert( unixMutexHeld() );
 27807  28080     if( p && p->nRef==0 ){
        28081  +    int nShmPerMap = unixShmRegionPerMap();
 27808  28082       int i;
 27809  28083       assert( p->pInode==pFd->pInode );
 27810  28084       sqlite3_mutex_free(p->mutex);
 27811         -    for(i=0; i<p->nRegion; i++){
        28085  +    for(i=0; i<p->nRegion; i+=nShmPerMap){
 27812  28086         if( p->h>=0 ){
 27813  28087           osMunmap(p->apRegion[i], p->szRegion);
 27814  28088         }else{
 27815  28089           sqlite3_free(p->apRegion[i]);
 27816  28090         }
 27817  28091       }
 27818  28092       sqlite3_free(p->apRegion);
................................................................................
 28011  28285     int bExtend,                    /* True to extend file if necessary */
 28012  28286     void volatile **pp              /* OUT: Mapped memory */
 28013  28287   ){
 28014  28288     unixFile *pDbFd = (unixFile*)fd;
 28015  28289     unixShm *p;
 28016  28290     unixShmNode *pShmNode;
 28017  28291     int rc = SQLITE_OK;
        28292  +  int nShmPerMap = unixShmRegionPerMap();
        28293  +  int nReqRegion;
 28018  28294   
 28019  28295     /* If the shared-memory file has not yet been opened, open it now. */
 28020  28296     if( pDbFd->pShm==0 ){
 28021  28297       rc = unixOpenSharedMemory(pDbFd);
 28022  28298       if( rc!=SQLITE_OK ) return rc;
 28023  28299     }
 28024  28300   
................................................................................
 28026  28302     pShmNode = p->pShmNode;
 28027  28303     sqlite3_mutex_enter(pShmNode->mutex);
 28028  28304     assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 28029  28305     assert( pShmNode->pInode==pDbFd->pInode );
 28030  28306     assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 28031  28307     assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 28032  28308   
 28033         -  if( pShmNode->nRegion<=iRegion ){
        28309  +  /* Minimum number of regions required to be mapped. */
        28310  +  nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
        28311  +
        28312  +  if( pShmNode->nRegion<nReqRegion ){
 28034  28313       char **apNew;                      /* New apRegion[] array */
 28035         -    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
        28314  +    int nByte = nReqRegion*szRegion;   /* Minimum required file size */
 28036  28315       struct stat sStat;                 /* Used by fstat() */
 28037  28316   
 28038  28317       pShmNode->szRegion = szRegion;
 28039  28318   
 28040  28319       if( pShmNode->h>=0 ){
 28041  28320         /* The requested region is not mapped into this processes address space.
 28042  28321         ** Check to see if it has been allocated (i.e. if the wal-index file is
................................................................................
 28077  28356             }
 28078  28357           }
 28079  28358         }
 28080  28359       }
 28081  28360   
 28082  28361       /* Map the requested memory region into this processes address space. */
 28083  28362       apNew = (char **)sqlite3_realloc(
 28084         -        pShmNode->apRegion, (iRegion+1)*sizeof(char *)
        28363  +        pShmNode->apRegion, nReqRegion*sizeof(char *)
 28085  28364       );
 28086  28365       if( !apNew ){
 28087  28366         rc = SQLITE_IOERR_NOMEM;
 28088  28367         goto shmpage_out;
 28089  28368       }
 28090  28369       pShmNode->apRegion = apNew;
 28091         -    while(pShmNode->nRegion<=iRegion){
        28370  +    while( pShmNode->nRegion<nReqRegion ){
        28371  +      int nMap = szRegion*nShmPerMap;
        28372  +      int i;
 28092  28373         void *pMem;
 28093  28374         if( pShmNode->h>=0 ){
 28094         -        pMem = osMmap(0, szRegion,
        28375  +        pMem = osMmap(0, nMap,
 28095  28376               pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
 28096  28377               MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
 28097  28378           );
 28098  28379           if( pMem==MAP_FAILED ){
 28099  28380             rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
 28100  28381             goto shmpage_out;
 28101  28382           }
................................................................................
 28103  28384           pMem = sqlite3_malloc(szRegion);
 28104  28385           if( pMem==0 ){
 28105  28386             rc = SQLITE_NOMEM;
 28106  28387             goto shmpage_out;
 28107  28388           }
 28108  28389           memset(pMem, 0, szRegion);
 28109  28390         }
 28110         -      pShmNode->apRegion[pShmNode->nRegion] = pMem;
 28111         -      pShmNode->nRegion++;
        28391  +
        28392  +      for(i=0; i<nShmPerMap; i++){
        28393  +        pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
        28394  +      }
        28395  +      pShmNode->nRegion += nShmPerMap;
 28112  28396       }
 28113  28397     }
 28114  28398   
 28115  28399   shmpage_out:
 28116  28400     if( pShmNode->nRegion>iRegion ){
 28117  28401       *pp = pShmNode->apRegion[iRegion];
 28118  28402     }else{
................................................................................
 28318  28602       osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
 28319  28603       pFd->pMapRegion = 0;
 28320  28604       pFd->mmapSize = 0;
 28321  28605       pFd->mmapSizeActual = 0;
 28322  28606     }
 28323  28607   }
 28324  28608   
 28325         -/*
 28326         -** Return the system page size.
 28327         -*/
 28328         -static int unixGetPagesize(void){
 28329         -#if HAVE_MREMAP
 28330         -  return 512;
 28331         -#elif defined(_BSD_SOURCE)
 28332         -  return getpagesize();
 28333         -#else
 28334         -  return (int)sysconf(_SC_PAGESIZE);
 28335         -#endif
 28336         -}
 28337         -
 28338  28609   /*
 28339  28610   ** Attempt to set the size of the memory mapping maintained by file 
 28340  28611   ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
 28341  28612   **
 28342  28613   ** If successful, this function sets the following variables:
 28343  28614   **
 28344  28615   **       unixFile.pMapRegion
................................................................................
 28367  28638     assert( nNew>0 );
 28368  28639     assert( pFd->mmapSizeActual>=pFd->mmapSize );
 28369  28640     assert( MAP_FAILED!=0 );
 28370  28641   
 28371  28642     if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
 28372  28643   
 28373  28644     if( pOrig ){
 28374         -    const int szSyspage = unixGetPagesize();
        28645  +#if HAVE_MREMAP
        28646  +    i64 nReuse = pFd->mmapSize;
        28647  +#else
        28648  +    const int szSyspage = osGetpagesize();
 28375  28649       i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
        28650  +#endif
 28376  28651       u8 *pReq = &pOrig[nReuse];
 28377  28652   
 28378  28653       /* Unmap any pages of the existing mapping that cannot be reused. */
 28379  28654       if( nReuse!=nOrig ){
 28380  28655         osMunmap(pReq, nOrig-nReuse);
 28381  28656       }
 28382  28657   
................................................................................
 31114  31389       UNIXVFS("unix-proxy",    proxyIoFinder ),
 31115  31390   #endif
 31116  31391     };
 31117  31392     unsigned int i;          /* Loop counter */
 31118  31393   
 31119  31394     /* Double-check that the aSyscall[] array has been constructed
 31120  31395     ** correctly.  See ticket [bb3a86e890c8e96ab] */
 31121         -  assert( ArraySize(aSyscall)==24 );
        31396  +  assert( ArraySize(aSyscall)==25 );
 31122  31397   
 31123  31398     /* Register all VFSes defined in the aVfs[] array */
 31124  31399     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 31125  31400       sqlite3_vfs_register(&aVfs[i], i==0);
 31126  31401     }
 31127  31402     return SQLITE_OK; 
 31128  31403   }
................................................................................
 31154  31429   **
 31155  31430   ******************************************************************************
 31156  31431   **
 31157  31432   ** This file contains code that is specific to Windows.
 31158  31433   */
 31159  31434   #if SQLITE_OS_WIN               /* This file is used for Windows only */
 31160  31435   
 31161         -#ifdef __CYGWIN__
 31162         -# include <sys/cygwin.h>
 31163         -# include <errno.h> /* amalgamator: keep */
 31164         -#endif
 31165         -
 31166  31436   /*
 31167  31437   ** Include code that is common to all os_*.c files
 31168  31438   */
 31169  31439   /************** Include os_common.h in the middle of os_win.c ****************/
 31170  31440   /************** Begin file os_common.h ***************************************/
 31171  31441   /*
 31172  31442   ** 2004 May 22
................................................................................
 31371  31641   #define OpenCounter(X)
 31372  31642   #endif
 31373  31643   
 31374  31644   #endif /* !defined(_OS_COMMON_H_) */
 31375  31645   
 31376  31646   /************** End of os_common.h *******************************************/
 31377  31647   /************** Continuing where we left off in os_win.c *********************/
        31648  +
        31649  +/*
        31650  +** Include the header file for the Windows VFS.
        31651  +*/
 31378  31652   
 31379  31653   /*
 31380  31654   ** Compiling and using WAL mode requires several APIs that are only
 31381  31655   ** available in Windows platforms based on the NT kernel.
 31382  31656   */
 31383  31657   #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
 31384  31658   #  error "WAL mode requires support from the Windows NT kernel, compile\
................................................................................
 33183  33457   #endif
 33184  33458   #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
 33185  33459   # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
 33186  33460   #endif
 33187  33461   static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
 33188  33462   static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
 33189  33463   
        33464  +/*
        33465  +** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
        33466  +** error code obtained via GetLastError() is eligible to be retried.  It
        33467  +** must accept the error code DWORD as its only argument and should return
        33468  +** non-zero if the error code is transient in nature and the operation
        33469  +** responsible for generating the original error might succeed upon being
        33470  +** retried.  The argument to this macro should be a variable.
        33471  +**
        33472  +** Additionally, a macro named "winIoerrCanRetry2" may be defined.  If it
        33473  +** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
        33474  +** returns zero.  The "winIoerrCanRetry2" macro is completely optional and
        33475  +** may be used to include additional error codes in the set that should
        33476  +** result in the failing I/O operation being retried by the caller.  If
        33477  +** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
        33478  +** identical to those of the "winIoerrCanRetry1" macro.
        33479  +*/
        33480  +#if !defined(winIoerrCanRetry1)
        33481  +#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED)        || \
        33482  +                              ((a)==ERROR_SHARING_VIOLATION)    || \
        33483  +                              ((a)==ERROR_LOCK_VIOLATION)       || \
        33484  +                              ((a)==ERROR_DEV_NOT_EXIST)        || \
        33485  +                              ((a)==ERROR_NETNAME_DELETED)      || \
        33486  +                              ((a)==ERROR_SEM_TIMEOUT)          || \
        33487  +                              ((a)==ERROR_NETWORK_UNREACHABLE))
        33488  +#endif
        33489  +
 33190  33490   /*
 33191  33491   ** If a ReadFile() or WriteFile() error occurs, invoke this routine
 33192  33492   ** to see if it should be retried.  Return TRUE to retry.  Return FALSE
 33193  33493   ** to give up with an error.
 33194  33494   */
 33195  33495   static int winRetryIoerr(int *pnRetry, DWORD *pError){
 33196  33496     DWORD e = osGetLastError();
 33197  33497     if( *pnRetry>=winIoerrRetry ){
 33198  33498       if( pError ){
 33199  33499         *pError = e;
 33200  33500       }
 33201  33501       return 0;
 33202  33502     }
 33203         -  if( e==ERROR_ACCESS_DENIED ||
 33204         -      e==ERROR_LOCK_VIOLATION ||
 33205         -      e==ERROR_SHARING_VIOLATION ){
        33503  +  if( winIoerrCanRetry1(e) ){
        33504  +    sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
        33505  +    ++*pnRetry;
        33506  +    return 1;
        33507  +  }
        33508  +#if defined(winIoerrCanRetry2)
        33509  +  else if( winIoerrCanRetry2(e) ){
 33206  33510       sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
 33207  33511       ++*pnRetry;
 33208  33512       return 1;
 33209  33513     }
        33514  +#endif
 33210  33515     if( pError ){
 33211  33516       *pError = e;
 33212  33517     }
 33213  33518     return 0;
 33214  33519   }
 33215  33520   
 33216  33521   /*
................................................................................
 34141  34446                         SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
 34142  34447     }
 34143  34448   #endif
 34144  34449     if( res == 0 ){
 34145  34450       pFile->lastErrno = osGetLastError();
 34146  34451       /* No need to log a failure to lock */
 34147  34452     }
 34148         -  OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
        34453  +  OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
 34149  34454     return res;
 34150  34455   }
 34151  34456   
 34152  34457   /*
 34153  34458   ** Undo a readlock
 34154  34459   */
 34155  34460   static int winUnlockReadLock(winFile *pFile){
................................................................................
 34165  34470     }
 34166  34471   #endif
 34167  34472     if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
 34168  34473       pFile->lastErrno = lastErrno;
 34169  34474       winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
 34170  34475                   "winUnlockReadLock", pFile->zPath);
 34171  34476     }
 34172         -  OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
        34477  +  OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
 34173  34478     return res;
 34174  34479   }
 34175  34480   
 34176  34481   /*
 34177  34482   ** Lock the file with the lock specified by parameter locktype - one
 34178  34483   ** of the following:
 34179  34484   **
................................................................................
 34240  34545                                            PENDING_BYTE, 0, 1, 0))==0 ){
 34241  34546         /* Try 3 times to get the pending lock.  This is needed to work
 34242  34547         ** around problems caused by indexing and/or anti-virus software on
 34243  34548         ** Windows systems.
 34244  34549         ** If you are using this code as a model for alternative VFSes, do not
 34245  34550         ** copy this retry logic.  It is a hack intended for Windows only.
 34246  34551         */
 34247         -      OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, rc=%s\n",
 34248         -               pFile->h, cnt, sqlite3ErrName(res)));
        34552  +      lastErrno = osGetLastError();
        34553  +      OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
        34554  +               pFile->h, cnt, res));
        34555  +      if( lastErrno==ERROR_INVALID_HANDLE ){
        34556  +        pFile->lastErrno = lastErrno;
        34557  +        rc = SQLITE_IOERR_LOCK;
        34558  +        OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
        34559  +                 pFile->h, cnt, sqlite3ErrName(rc)));
        34560  +        return rc;
        34561  +      }
 34249  34562         if( cnt ) sqlite3_win32_sleep(1);
 34250  34563       }
 34251  34564       gotPendingLock = res;
 34252  34565       if( !res ){
 34253  34566         lastErrno = osGetLastError();
 34254  34567       }
 34255  34568     }
................................................................................
 34326  34639   
 34327  34640   /*
 34328  34641   ** This routine checks if there is a RESERVED lock held on the specified
 34329  34642   ** file by this or any other process. If such a lock is held, return
 34330  34643   ** non-zero, otherwise zero.
 34331  34644   */
 34332  34645   static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
 34333         -  int rc;
        34646  +  int res;
 34334  34647     winFile *pFile = (winFile*)id;
 34335  34648   
 34336  34649     SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 34337  34650     OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
 34338  34651   
 34339  34652     assert( id!=0 );
 34340  34653     if( pFile->locktype>=RESERVED_LOCK ){
 34341         -    rc = 1;
 34342         -    OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (local)\n", pFile->h, rc));
        34654  +    res = 1;
        34655  +    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
 34343  34656     }else{
 34344         -    rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
 34345         -    if( rc ){
        34657  +    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
        34658  +    if( res ){
 34346  34659         winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
 34347  34660       }
 34348         -    rc = !rc;
 34349         -    OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (remote)\n", pFile->h, rc));
        34661  +    res = !res;
        34662  +    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
 34350  34663     }
 34351         -  *pResOut = rc;
        34664  +  *pResOut = res;
 34352  34665     OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
 34353  34666              pFile->h, pResOut, *pResOut));
 34354  34667     return SQLITE_OK;
 34355  34668   }
 34356  34669   
 34357  34670   /*
 34358  34671   ** Lower the locking level on file descriptor id to locktype.  locktype
................................................................................
 34485  34798           winIoerrRetryDelay = a[1];
 34486  34799         }else{
 34487  34800           a[1] = winIoerrRetryDelay;
 34488  34801         }
 34489  34802         OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 34490  34803         return SQLITE_OK;
 34491  34804       }
        34805  +#ifdef SQLITE_TEST
        34806  +    case SQLITE_FCNTL_WIN32_SET_HANDLE: {
        34807  +      LPHANDLE phFile = (LPHANDLE)pArg;
        34808  +      HANDLE hOldFile = pFile->h;
        34809  +      pFile->h = *phFile;
        34810  +      *phFile = hOldFile;
        34811  +      OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
        34812  +               hOldFile, pFile->h));
        34813  +      return SQLITE_OK;
        34814  +    }
        34815  +#endif
 34492  34816       case SQLITE_FCNTL_TEMPFILENAME: {
 34493  34817         char *zTFile = 0;
 34494  34818         int rc = winGetTempname(pFile->pVfs, &zTFile);
 34495  34819         if( rc==SQLITE_OK ){
 34496  34820           *(char**)pArg = zTFile;
 34497  34821         }
 34498  34822         OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
................................................................................
 38995  39319     struct RowSetChunk *pChunk;    /* List of all chunk allocations */
 38996  39320     sqlite3 *db;                   /* The database connection */
 38997  39321     struct RowSetEntry *pEntry;    /* List of entries using pRight */
 38998  39322     struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
 38999  39323     struct RowSetEntry *pFresh;    /* Source of new entry objects */
 39000  39324     struct RowSetEntry *pForest;   /* List of binary trees of entries */
 39001  39325     u16 nFresh;                    /* Number of objects on pFresh */
 39002         -  u8 rsFlags;                    /* Various flags */
 39003         -  u8 iBatch;                     /* Current insert batch */
        39326  +  u16 rsFlags;                   /* Various flags */
        39327  +  int iBatch;                    /* Current insert batch */
 39004  39328   };
 39005  39329   
 39006  39330   /*
 39007  39331   ** Allowed values for RowSet.rsFlags
 39008  39332   */
 39009  39333   #define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
 39010  39334   #define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
................................................................................
 39330  39654   ** Check to see if element iRowid was inserted into the rowset as
 39331  39655   ** part of any insert batch prior to iBatch.  Return 1 or 0.
 39332  39656   **
 39333  39657   ** If this is the first test of a new batch and if there exist entires
 39334  39658   ** on pRowSet->pEntry, then sort those entires into the forest at
 39335  39659   ** pRowSet->pForest so that they can be tested.
 39336  39660   */
 39337         -SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
        39661  +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
 39338  39662     struct RowSetEntry *p, *pTree;
 39339  39663   
 39340  39664     /* This routine is never called after sqlite3RowSetNext() */
 39341  39665     assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
 39342  39666   
 39343  39667     /* Sort entries into the forest on the first test of a new batch 
 39344  39668     */
................................................................................
 40159  40483     u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
 40160  40484     u8 useJournal;              /* Use a rollback journal on this file */
 40161  40485     u8 noSync;                  /* Do not sync the journal if true */
 40162  40486     u8 fullSync;                /* Do extra syncs of the journal for robustness */
 40163  40487     u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
 40164  40488     u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
 40165  40489     u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
 40166         -  u8 tempFile;                /* zFilename is a temporary file */
        40490  +  u8 tempFile;                /* zFilename is a temporary or immutable file */
        40491  +  u8 noLock;                  /* Do not lock (except in WAL mode) */
 40167  40492     u8 readOnly;                /* True for a read-only database */
 40168  40493     u8 memDb;                   /* True to inhibit all file I/O */
 40169  40494   
 40170  40495     /**************************************************************************
 40171  40496     ** The following block contains those class members that change during
 40172  40497     ** routine opertion.  Class members not in this block are either fixed
 40173  40498     ** when the pager is first created or else only change when there is a
................................................................................
 40624  40949     int rc = SQLITE_OK;
 40625  40950   
 40626  40951     assert( !pPager->exclusiveMode || pPager->eLock==eLock );
 40627  40952     assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
 40628  40953     assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
 40629  40954     if( isOpen(pPager->fd) ){
 40630  40955       assert( pPager->eLock>=eLock );
 40631         -    rc = sqlite3OsUnlock(pPager->fd, eLock);
        40956  +    rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
 40632  40957       if( pPager->eLock!=UNKNOWN_LOCK ){
 40633  40958         pPager->eLock = (u8)eLock;
 40634  40959       }
 40635  40960       IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
 40636  40961     }
 40637  40962     return rc;
 40638  40963   }
................................................................................
 40648  40973   ** of this.
 40649  40974   */
 40650  40975   static int pagerLockDb(Pager *pPager, int eLock){
 40651  40976     int rc = SQLITE_OK;
 40652  40977   
 40653  40978     assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
 40654  40979     if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
 40655         -    rc = sqlite3OsLock(pPager->fd, eLock);
        40980  +    rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
 40656  40981       if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
 40657  40982         pPager->eLock = (u8)eLock;
 40658  40983         IOTRACE(("LOCK %p %d\n", pPager, eLock))
 40659  40984       }
 40660  40985     }
 40661  40986     return rc;
 40662  40987   }
................................................................................
 41157  41482     u32 cksum = 0;                   /* Checksum of string zMaster */
 41158  41483   
 41159  41484     assert( pPager->setMaster==0 );
 41160  41485     assert( !pagerUseWal(pPager) );
 41161  41486   
 41162  41487     if( !zMaster 
 41163  41488      || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
 41164         -   || pPager->journalMode==PAGER_JOURNALMODE_OFF 
        41489  +   || !isOpen(pPager->jfd)
 41165  41490     ){
 41166  41491       return SQLITE_OK;
 41167  41492     }
 41168  41493     pPager->setMaster = 1;
 41169         -  assert( isOpen(pPager->jfd) );
 41170  41494     assert( pPager->journalHdr <= pPager->journalOff );
 41171  41495   
 41172  41496     /* Calculate the length in bytes and the checksum of zMaster */
 41173  41497     for(nMaster=0; zMaster[nMaster]; nMaster++){
 41174  41498       cksum += zMaster[nMaster];
 41175  41499     }
 41176  41500   
................................................................................
 44208  44532       ** choose a default page size in case we have to create the
 44209  44533       ** database file. The default page size is the maximum of:
 44210  44534       **
 44211  44535       **    + SQLITE_DEFAULT_PAGE_SIZE,
 44212  44536       **    + The value returned by sqlite3OsSectorSize()
 44213  44537       **    + The largest page size that can be written atomically.
 44214  44538       */
 44215         -    if( rc==SQLITE_OK && !readOnly ){
        44539  +    if( rc==SQLITE_OK ){
        44540  +      int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
        44541  +      if( !readOnly ){
 44216  44542         setSectorSize(pPager);
 44217  44543         assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
 44218  44544         if( szPageDflt<pPager->sectorSize ){
 44219  44545           if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
 44220  44546             szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
 44221  44547           }else{
 44222  44548             szPageDflt = (u32)pPager->sectorSize;
 44223  44549           }
 44224  44550         }
 44225  44551   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 44226  44552         {
 44227         -        int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
 44228  44553           int ii;
 44229  44554           assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
 44230  44555           assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
 44231  44556           assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
 44232  44557           for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
 44233  44558             if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
 44234  44559               szPageDflt = ii;
 44235  44560             }
 44236  44561           }
 44237  44562         }
 44238  44563   #endif
        44564  +      }
        44565  +      pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
        44566  +      if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
        44567  +       || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
        44568  +          vfsFlags |= SQLITE_OPEN_READONLY;
        44569  +          goto act_like_temp_file;
        44570  +      }
 44239  44571       }
 44240  44572     }else{
 44241  44573       /* If a temporary file is requested, it is not opened immediately.
 44242  44574       ** In this case we accept the default page size and delay actually
 44243  44575       ** opening the file until the first call to OsWrite().
 44244  44576       **
 44245  44577       ** This branch is also run for an in-memory database. An in-memory
 44246  44578       ** database is the same as a temp-file that is never written out to
 44247  44579       ** disk and uses an in-memory rollback journal.
        44580  +    **
        44581  +    ** This branch also runs for files marked as immutable.
 44248  44582       */ 
        44583  +act_like_temp_file:
 44249  44584       tempFile = 1;
 44250         -    pPager->eState = PAGER_READER;
 44251         -    pPager->eLock = EXCLUSIVE_LOCK;
        44585  +    pPager->eState = PAGER_READER;     /* Pretend we already have a lock */
        44586  +    pPager->eLock = EXCLUSIVE_LOCK;    /* Pretend we are in EXCLUSIVE locking mode */
        44587  +    pPager->noLock = 1;                /* Do no locking */
 44252  44588       readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
 44253  44589     }
 44254  44590   
 44255  44591     /* The following call to PagerSetPagesize() serves to set the value of 
 44256  44592     ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
 44257  44593     */
 44258  44594     if( rc==SQLITE_OK ){
................................................................................
 44285  44621     /* pPager->stmtInUse = 0; */
 44286  44622     /* pPager->nRef = 0; */
 44287  44623     /* pPager->stmtSize = 0; */
 44288  44624     /* pPager->stmtJSize = 0; */
 44289  44625     /* pPager->nPage = 0; */
 44290  44626     pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
 44291  44627     /* pPager->state = PAGER_UNLOCK; */
 44292         -#if 0
 44293         -  assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
 44294         -#endif
 44295  44628     /* pPager->errMask = 0; */
 44296  44629     pPager->tempFile = (u8)tempFile;
 44297  44630     assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
 44298  44631             || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
 44299  44632     assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
 44300  44633     pPager->exclusiveMode = (u8)tempFile; 
 44301  44634     pPager->changeCountDone = pPager->tempFile;
................................................................................
 50369  50702   ** found at self->pBt->mutex. 
 50370  50703   */
 50371  50704   struct BtCursor {
 50372  50705     Btree *pBtree;            /* The Btree to which this cursor belongs */
 50373  50706     BtShared *pBt;            /* The BtShared this cursor points to */
 50374  50707     BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
 50375  50708     struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
 50376         -#ifndef SQLITE_OMIT_INCRBLOB
 50377  50709     Pgno *aOverflow;          /* Cache of overflow page locations */
 50378         -#endif
 50379         -  Pgno pgnoRoot;            /* The root page of this tree */
 50380  50710     CellInfo info;            /* A parse of the cell we are pointing at */
 50381  50711     i64 nKey;        /* Size of pKey, or last integer key */
 50382         -  void *pKey;      /* Saved key that was cursor's last known position */
        50712  +  void *pKey;               /* Saved key that was cursor last known position */
        50713  +  Pgno pgnoRoot;            /* The root page of this tree */
        50714  +  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
 50383  50715     int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
 50384         -  u8 wrFlag;                /* True if writable */
 50385         -  u8 atLast;                /* Cursor pointing to the last entry */
 50386         -  u8 validNKey;             /* True if info.nKey is valid */
        50716  +  u8 curFlags;              /* zero or more BTCF_* flags defined below */
 50387  50717     u8 eState;                /* One of the CURSOR_XXX constants (see below) */
 50388         -#ifndef SQLITE_OMIT_INCRBLOB
 50389         -  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
 50390         -#endif
 50391  50718     u8 hints;                             /* As configured by CursorSetHints() */
 50392  50719     i16 iPage;                            /* Index of current page in apPage */
 50393  50720     u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
 50394  50721     MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
 50395  50722   };
 50396  50723   
        50724  +/*
        50725  +** Legal values for BtCursor.curFlags
        50726  +*/
        50727  +#define BTCF_WriteFlag    0x01   /* True if a write cursor */
        50728  +#define BTCF_ValidNKey    0x02   /* True if info.nKey is valid */
        50729  +#define BTCF_ValidOvfl    0x04   /* True if aOverflow is valid */
        50730  +#define BTCF_AtLast       0x08   /* Cursor is pointing ot the last entry */
        50731  +#define BTCF_Incrblob     0x10   /* True if an incremental I/O handle */
        50732  +
 50397  50733   /*
 50398  50734   ** Potential values for BtCursor.eState.
 50399  50735   **
 50400  50736   ** CURSOR_INVALID:
 50401  50737   **   Cursor does not point to a valid entry. This can happen (for example) 
 50402  50738   **   because the table is empty or because BtreeCursorFirst() has not been
 50403  50739   **   called.
................................................................................
 51260  51596   */
 51261  51597   #ifdef SQLITE_DEBUG
 51262  51598   static int cursorHoldsMutex(BtCursor *p){
 51263  51599     return sqlite3_mutex_held(p->pBt->mutex);
 51264  51600   }
 51265  51601   #endif
 51266  51602   
 51267         -
 51268         -#ifndef SQLITE_OMIT_INCRBLOB
 51269  51603   /*
 51270         -** Invalidate the overflow page-list cache for cursor pCur, if any.
        51604  +** Invalidate the overflow cache of the cursor passed as the first argument.
        51605  +** on the shared btree structure pBt.
 51271  51606   */
 51272         -static void invalidateOverflowCache(BtCursor *pCur){
 51273         -  assert( cursorHoldsMutex(pCur) );
 51274         -  sqlite3_free(pCur->aOverflow);
 51275         -  pCur->aOverflow = 0;
 51276         -}
        51607  +#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
 51277  51608   
 51278  51609   /*
 51279  51610   ** Invalidate the overflow page-list cache for all cursors opened
 51280  51611   ** on the shared btree structure pBt.
 51281  51612   */
 51282  51613   static void invalidateAllOverflowCache(BtShared *pBt){
 51283  51614     BtCursor *p;
 51284  51615     assert( sqlite3_mutex_held(pBt->mutex) );
 51285  51616     for(p=pBt->pCursor; p; p=p->pNext){
 51286  51617       invalidateOverflowCache(p);
 51287  51618     }
 51288  51619   }
 51289  51620   
        51621  +#ifndef SQLITE_OMIT_INCRBLOB
 51290  51622   /*
 51291  51623   ** This function is called before modifying the contents of a table
 51292  51624   ** to invalidate any incrblob cursors that are open on the
 51293  51625   ** row or one of the rows being modified.
 51294  51626   **
 51295  51627   ** If argument isClearTable is true, then the entire contents of the
 51296  51628   ** table is about to be deleted. In this case invalidate all incrblob
................................................................................
 51305  51637     i64 iRow,               /* The rowid that might be changing */
 51306  51638     int isClearTable        /* True if all rows are being deleted */
 51307  51639   ){
 51308  51640     BtCursor *p;
 51309  51641     BtShared *pBt = pBtree->pBt;
 51310  51642     assert( sqlite3BtreeHoldsMutex(pBtree) );
 51311  51643     for(p=pBt->pCursor; p; p=p->pNext){
 51312         -    if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
        51644  +    if( (p->curFlags & BTCF_Incrblob)!=0 && (isClearTable || p->info.nKey==iRow) ){
 51313  51645         p->eState = CURSOR_INVALID;
 51314  51646       }
 51315  51647     }
 51316  51648   }
 51317  51649   
 51318  51650   #else
 51319         -  /* Stub functions when INCRBLOB is omitted */
 51320         -  #define invalidateOverflowCache(x)
 51321         -  #define invalidateAllOverflowCache(x)
        51651  +  /* Stub function when INCRBLOB is omitted */
 51322  51652     #define invalidateIncrblobCursors(x,y,z)
 51323  51653   #endif /* SQLITE_OMIT_INCRBLOB */
 51324  51654   
 51325  51655   /*
 51326  51656   ** Set bit pgno of the BtShared.pHasContent bitvec. This is called 
 51327  51657   ** when a page that previously contained data becomes a free-list leaf 
 51328  51658   ** page.
................................................................................
 51560  51890   
 51561  51891   /*
 51562  51892   ** Determine whether or not a cursor has moved from the position it
 51563  51893   ** was last placed at.  Cursors can move when the row they are pointing
 51564  51894   ** at is deleted out from under them.
 51565  51895   **
 51566  51896   ** This routine returns an error code if something goes wrong.  The
 51567         -** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
        51897  +** integer *pHasMoved is set as follows:
        51898  +**
        51899  +**    0:   The cursor is unchanged
        51900  +**    1:   The cursor is still pointing at the same row, but the pointers
        51901  +**         returned by sqlite3BtreeKeyFetch() or sqlite3BtreeDataFetch()
        51902  +**         might now be invalid because of a balance() or other change to the
        51903  +**         b-tree.
        51904  +**    2:   The cursor is no longer pointing to the row.  The row might have
        51905  +**         been deleted out from under the cursor.
 51568  51906   */
 51569  51907   SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
 51570  51908     int rc;
 51571  51909   
        51910  +  if( pCur->eState==CURSOR_VALID ){
        51911  +    *pHasMoved = 0;
        51912  +    return SQLITE_OK;
        51913  +  }
 51572  51914     rc = restoreCursorPosition(pCur);
 51573  51915     if( rc ){
 51574         -    *pHasMoved = 1;
        51916  +    *pHasMoved = 2;
 51575  51917       return rc;
 51576  51918     }
 51577  51919     if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
        51920  +    *pHasMoved = 2;
        51921  +  }else{
 51578  51922       *pHasMoved = 1;
 51579         -  }else{
 51580         -    *pHasMoved = 0;
 51581  51923     }
 51582  51924     return SQLITE_OK;
 51583  51925   }
 51584  51926   
 51585  51927   #ifndef SQLITE_OMIT_AUTOVACUUM
 51586  51928   /*
 51587  51929   ** Given a page number of a regular database page, return the page
................................................................................
 52975  53317     assert( sqlite3_mutex_held(p->db->mutex) );
 52976  53318     sqlite3BtreeEnter(p);
 52977  53319     sqlite3PagerSetCachesize(pBt->pPager, mxPage);
 52978  53320     sqlite3BtreeLeave(p);
 52979  53321     return SQLITE_OK;
 52980  53322   }
 52981  53323   
        53324  +#if SQLITE_MAX_MMAP_SIZE>0
 52982  53325   /*
 52983  53326   ** Change the limit on the amount of the database file that may be
 52984  53327   ** memory mapped.
 52985  53328   */
 52986  53329   SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
 52987  53330     BtShared *pBt = p->pBt;
 52988  53331     assert( sqlite3_mutex_held(p->db->mutex) );
 52989  53332     sqlite3BtreeEnter(p);
 52990  53333     sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
 52991  53334     sqlite3BtreeLeave(p);
 52992  53335     return SQLITE_OK;
 52993  53336   }
        53337  +#endif /* SQLITE_MAX_MMAP_SIZE>0 */
 52994  53338   
 52995  53339   /*
 52996  53340   ** Change the way data is synced to disk in order to increase or decrease
 52997  53341   ** how well the database resists damage due to OS crashes and power
 52998  53342   ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
 52999  53343   ** there is a high probability of damage)  Level 2 is the default.  There
 53000  53344   ** is a very low but non-zero probability of damage.  Level 3 reduces the
................................................................................
 53363  53707   ** is capable of reading or writing to the databse.  Cursors that
 53364  53708   ** have been tripped into the CURSOR_FAULT state are not counted.
 53365  53709   */
 53366  53710   static int countValidCursors(BtShared *pBt, int wrOnly){
 53367  53711     BtCursor *pCur;
 53368  53712     int r = 0;
 53369  53713     for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
 53370         -    if( (wrOnly==0 || pCur->wrFlag) && pCur->eState!=CURSOR_FAULT ) r++; 
        53714  +    if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
        53715  +     && pCur->eState!=CURSOR_FAULT ) r++; 
 53371  53716     }
 53372  53717     return r;
 53373  53718   }
 53374  53719   #endif
 53375  53720   
 53376  53721   /*
 53377  53722   ** If there are no outstanding cursors and we are not in the middle
................................................................................
 54438  54783     /* Now that no other errors can occur, finish filling in the BtCursor
 54439  54784     ** variables and link the cursor into the BtShared list.  */
 54440  54785     pCur->pgnoRoot = (Pgno)iTable;
 54441  54786     pCur->iPage = -1;
 54442  54787     pCur->pKeyInfo = pKeyInfo;
 54443  54788     pCur->pBtree = p;
 54444  54789     pCur->pBt = pBt;
 54445         -  pCur->wrFlag = (u8)wrFlag;
        54790  +  assert( wrFlag==0 || wrFlag==BTCF_WriteFlag );
        54791  +  pCur->curFlags = wrFlag;
 54446  54792     pCur->pNext = pBt->pCursor;
 54447  54793     if( pCur->pNext ){
 54448  54794       pCur->pNext->pPrev = pCur;
 54449  54795     }
 54450  54796     pBt->pCursor = pCur;
 54451  54797     pCur->eState = CURSOR_INVALID;
 54452  54798     return SQLITE_OK;
................................................................................
 54508  54854       if( pCur->pNext ){
 54509  54855         pCur->pNext->pPrev = pCur->pPrev;
 54510  54856       }
 54511  54857       for(i=0; i<=pCur->iPage; i++){
 54512  54858         releasePage(pCur->apPage[i]);
 54513  54859       }
 54514  54860       unlockBtreeIfUnused(pBt);
 54515         -    invalidateOverflowCache(pCur);
        54861  +    sqlite3DbFree(pBtree->db, pCur->aOverflow);
 54516  54862       /* sqlite3_free(pCur); */
 54517  54863       sqlite3BtreeLeave(pBtree);
 54518  54864     }
 54519  54865     return SQLITE_OK;
 54520  54866   }
 54521  54867   
 54522  54868   /*
................................................................................
 54547  54893   #endif
 54548  54894   #ifdef _MSC_VER
 54549  54895     /* Use a real function in MSVC to work around bugs in that compiler. */
 54550  54896     static void getCellInfo(BtCursor *pCur){
 54551  54897       if( pCur->info.nSize==0 ){
 54552  54898         int iPage = pCur->iPage;
 54553  54899         btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
 54554         -      pCur->validNKey = 1;
        54900  +      pCur->curFlags |= BTCF_ValidNKey;
 54555  54901       }else{
 54556  54902         assertCellInfo(pCur);
 54557  54903       }
 54558  54904     }
 54559  54905   #else /* if not _MSC_VER */
 54560  54906     /* Use a macro in all other compilers so that the function is inlined */
 54561  54907   #define getCellInfo(pCur)                                                      \
 54562  54908     if( pCur->info.nSize==0 ){                                                   \
 54563  54909       int iPage = pCur->iPage;                                                   \
 54564  54910       btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
 54565         -    pCur->validNKey = 1;                                                       \
        54911  +    pCur->curFlags |= BTCF_ValidNKey;                                          \
 54566  54912     }else{                                                                       \
 54567  54913       assertCellInfo(pCur);                                                      \
 54568  54914     }
 54569  54915   #endif /* _MSC_VER */
 54570  54916   
 54571  54917   #ifndef NDEBUG  /* The next routine used only within assert() statements */
 54572  54918   /*
................................................................................
 54729  55075       memcpy(pBuf, pPayload, nByte);
 54730  55076     }
 54731  55077     return SQLITE_OK;
 54732  55078   }
 54733  55079   
 54734  55080   /*
 54735  55081   ** This function is used to read or overwrite payload information
 54736         -** for the entry that the pCur cursor is pointing to. If the eOp
 54737         -** parameter is 0, this is a read operation (data copied into
 54738         -** buffer pBuf). If it is non-zero, a write (data copied from
 54739         -** buffer pBuf).
        55082  +** for the entry that the pCur cursor is pointing to. The eOp
        55083  +** argument is interpreted as follows:
        55084  +**
        55085  +**   0: The operation is a read. Populate the overflow cache.
        55086  +**   1: The operation is a write. Populate the overflow cache.
        55087  +**   2: The operation is a read. Do not populate the overflow cache.
 54740  55088   **
 54741  55089   ** A total of "amt" bytes are read or written beginning at "offset".
 54742  55090   ** Data is read to or from the buffer pBuf.
 54743  55091   **
 54744  55092   ** The content being read or written might appear on the main page
 54745  55093   ** or be scattered out on multiple overflow pages.
 54746  55094   **
 54747         -** If the BtCursor.isIncrblobHandle flag is set, and the current
 54748         -** cursor entry uses one or more overflow pages, this function
 54749         -** allocates space for and lazily popluates the overflow page-list 
 54750         -** cache array (BtCursor.aOverflow). Subsequent calls use this
 54751         -** cache to make seeking to the supplied offset more efficient.
        55095  +** If the current cursor entry uses one or more overflow pages and the
        55096  +** eOp argument is not 2, this function may allocate space for and lazily 
        55097  +** popluates the overflow page-list cache array (BtCursor.aOverflow). 
        55098  +** Subsequent calls use this cache to make seeking to the supplied offset 
        55099  +** more efficient.
 54752  55100   **
 54753  55101   ** Once an overflow page-list cache has been allocated, it may be
 54754  55102   ** invalidated if some other cursor writes to the same table, or if
 54755  55103   ** the cursor is moved to a different row. Additionally, in auto-vacuum
 54756  55104   ** mode, the following events may invalidate an overflow page-list cache.
 54757  55105   **
 54758  55106   **   * An incremental vacuum,
................................................................................
 54768  55116   ){
 54769  55117     unsigned char *aPayload;
 54770  55118     int rc = SQLITE_OK;
 54771  55119     u32 nKey;
 54772  55120     int iIdx = 0;
 54773  55121     MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
 54774  55122     BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
        55123  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        55124  +  int bEnd;                                   /* True if reading to end of data */
        55125  +#endif
 54775  55126   
 54776  55127     assert( pPage );
 54777  55128     assert( pCur->eState==CURSOR_VALID );
 54778  55129     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 54779  55130     assert( cursorHoldsMutex(pCur) );
        55131  +  assert( eOp!=2 || offset==0 );      /* Always start from beginning for eOp==2 */
 54780  55132   
 54781  55133     getCellInfo(pCur);
 54782  55134     aPayload = pCur->info.pCell + pCur->info.nHeader;
 54783  55135     nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
        55136  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        55137  +  bEnd = (offset+amt==nKey+pCur->info.nData);
        55138  +#endif
 54784  55139   
 54785  55140     if( NEVER(offset+amt > nKey+pCur->info.nData) 
 54786  55141      || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
 54787  55142     ){
 54788  55143       /* Trying to read or write past the end of the data is an error */
 54789  55144       return SQLITE_CORRUPT_BKPT;
 54790  55145     }
................................................................................
 54791  55146   
 54792  55147     /* Check if data must be read/written to/from the btree page itself. */
 54793  55148     if( offset<pCur->info.nLocal ){
 54794  55149       int a = amt;
 54795  55150       if( a+offset>pCur->info.nLocal ){
 54796  55151         a = pCur->info.nLocal - offset;
 54797  55152       }
 54798         -    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
        55153  +    rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
 54799  55154       offset = 0;
 54800  55155       pBuf += a;
 54801  55156       amt -= a;
 54802  55157     }else{
 54803  55158       offset -= pCur->info.nLocal;
 54804  55159     }
 54805  55160   
 54806  55161     if( rc==SQLITE_OK && amt>0 ){
 54807  55162       const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
 54808  55163       Pgno nextPage;
 54809  55164   
 54810  55165       nextPage = get4byte(&aPayload[pCur->info.nLocal]);
 54811  55166   
 54812         -#ifndef SQLITE_OMIT_INCRBLOB
 54813         -    /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
 54814         -    ** has not been allocated, allocate it now. The array is sized at
 54815         -    ** one entry for each overflow page in the overflow chain. The
 54816         -    ** page number of the first overflow page is stored in aOverflow[0],
 54817         -    ** etc. A value of 0 in the aOverflow[] array means "not yet known"
 54818         -    ** (the cache is lazily populated).
        55167  +    /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
        55168  +    ** Except, do not allocate aOverflow[] for eOp==2.
        55169  +    **
        55170  +    ** The aOverflow[] array is sized at one entry for each overflow page
        55171  +    ** in the overflow chain. The page number of the first overflow page is
        55172  +    ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
        55173  +    ** means "not yet known" (the cache is lazily populated).
 54819  55174       */
 54820         -    if( pCur->isIncrblobHandle && !pCur->aOverflow ){
        55175  +    if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
 54821  55176         int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
 54822         -      pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
 54823         -      /* nOvfl is always positive.  If it were zero, fetchPayload would have
 54824         -      ** been used instead of this routine. */
 54825         -      if( ALWAYS(nOvfl) && !pCur->aOverflow ){
        55177  +      if( nOvfl>pCur->nOvflAlloc ){
        55178  +        Pgno *aNew = (Pgno*)sqlite3DbRealloc(
        55179  +            pCur->pBtree->db, pCur->aOverflow, nOvfl*2*sizeof(Pgno)
        55180  +        );
        55181  +        if( aNew==0 ){
 54826  55182           rc = SQLITE_NOMEM;
        55183  +        }else{
        55184  +          pCur->nOvflAlloc = nOvfl*2;
        55185  +          pCur->aOverflow = aNew;
        55186  +        }
        55187  +      }
        55188  +      if( rc==SQLITE_OK ){
        55189  +        memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
        55190  +        pCur->curFlags |= BTCF_ValidOvfl;
 54827  55191         }
 54828  55192       }
 54829  55193   
 54830  55194       /* If the overflow page-list cache has been allocated and the
 54831  55195       ** entry for the first required overflow page is valid, skip
 54832  55196       ** directly to it.
 54833  55197       */
 54834         -    if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
        55198  +    if( (pCur->curFlags & BTCF_ValidOvfl)!=0 && pCur->aOverflow[offset/ovflSize] ){
 54835  55199         iIdx = (offset/ovflSize);
 54836  55200         nextPage = pCur->aOverflow[iIdx];
 54837  55201         offset = (offset%ovflSize);
 54838  55202       }
 54839         -#endif
 54840  55203   
 54841  55204       for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
 54842  55205   
 54843         -#ifndef SQLITE_OMIT_INCRBLOB
 54844  55206         /* If required, populate the overflow page-list cache. */
 54845         -      if( pCur->aOverflow ){
        55207  +      if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
 54846  55208           assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
 54847  55209           pCur->aOverflow[iIdx] = nextPage;
 54848  55210         }
 54849         -#endif
 54850  55211   
 54851  55212         if( offset>=ovflSize ){
 54852  55213           /* The only reason to read this page is to obtain the page
 54853  55214           ** number for the next page in the overflow chain. The page
 54854  55215           ** data is not required. So first try to lookup the overflow
 54855  55216           ** page-list cache, if any, then fall back to the getOverflowPage()
 54856  55217           ** function.
        55218  +        **
        55219  +        ** Note that the aOverflow[] array must be allocated because eOp!=2
        55220  +        ** here.  If eOp==2, then offset==0 and this branch is never taken.
 54857  55221           */
 54858         -#ifndef SQLITE_OMIT_INCRBLOB
 54859         -        if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
        55222  +        assert( eOp!=2 );
        55223  +        assert( pCur->curFlags & BTCF_ValidOvfl );
        55224  +        if( pCur->aOverflow[iIdx+1] ){
 54860  55225             nextPage = pCur->aOverflow[iIdx+1];
 54861         -        } else 
 54862         -#endif
        55226  +        }else{
 54863  55227             rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
        55228  +        }
 54864  55229           offset -= ovflSize;
 54865  55230         }else{
 54866  55231           /* Need to read this page properly. It contains some of the
 54867  55232           ** range of data that is being read (eOp==0) or written (eOp!=0).
 54868  55233           */
 54869  55234   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 54870  55235           sqlite3_file *fd;
................................................................................
 54878  55243           /* If all the following are true:
 54879  55244           **
 54880  55245           **   1) this is a read operation, and 
 54881  55246           **   2) data is required from the start of this overflow page, and
 54882  55247           **   3) the database is file-backed, and
 54883  55248           **   4) there is no open write-transaction, and
 54884  55249           **   5) the database is not a WAL database,
        55250  +        **   6) all data from the page is being read.
 54885  55251           **
 54886  55252           ** then data can be read directly from the database file into the
 54887  55253           ** output buffer, bypassing the page-cache altogether. This speeds
 54888  55254           ** up loading large records that span many overflow pages.
 54889  55255           */
 54890         -        if( eOp==0                                             /* (1) */
        55256  +        if( (eOp&0x01)==0                                      /* (1) */
 54891  55257            && offset==0                                          /* (2) */
        55258  +         && (bEnd || a==ovflSize)                              /* (6) */
 54892  55259            && pBt->inTransaction==TRANS_READ                     /* (4) */
 54893  55260            && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
 54894  55261            && pBt->pPage1->aData[19]==0x01                       /* (5) */
 54895  55262           ){
 54896  55263             u8 aSave[4];
 54897  55264             u8 *aWrite = &pBuf[-4];
 54898  55265             memcpy(aSave, aWrite, 4);
................................................................................
 54901  55268             memcpy(aWrite, aSave, 4);
 54902  55269           }else
 54903  55270   #endif
 54904  55271   
 54905  55272           {
 54906  55273             DbPage *pDbPage;
 54907  55274             rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
 54908         -              (eOp==0 ? PAGER_GET_READONLY : 0)
        55275  +              ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
 54909  55276             );
 54910  55277             if( rc==SQLITE_OK ){
 54911  55278               aPayload = sqlite3PagerGetData(pDbPage);
 54912  55279               nextPage = get4byte(aPayload);
 54913         -            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
        55280  +            rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
 54914  55281               sqlite3PagerUnref(pDbPage);
 54915  55282               offset = 0;
 54916  55283             }
 54917  55284           }
 54918  55285           amt -= a;
 54919  55286           pBuf += a;
 54920  55287         }
................................................................................
 55000  55367     u32 *pAmt            /* Write the number of available bytes here */
 55001  55368   ){
 55002  55369     assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
 55003  55370     assert( pCur->eState==CURSOR_VALID );
 55004  55371     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 55005  55372     assert( cursorHoldsMutex(pCur) );
 55006  55373     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 55007         -  if( pCur->info.nSize==0 ){
 55008         -    btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
 55009         -                   &pCur->info);
 55010         -  }
        55374  +  assert( pCur->info.nSize>0 );
 55011  55375     *pAmt = pCur->info.nLocal;
 55012  55376     return (void*)(pCur->info.pCell + pCur->info.nHeader);
 55013  55377   }
 55014  55378   
 55015  55379   
 55016  55380   /*
 55017  55381   ** For the entry that cursor pCur is point to, return as
................................................................................
 55054  55418     assert( pCur->eState==CURSOR_VALID );
 55055  55419     assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
 55056  55420     assert( pCur->iPage>=0 );
 55057  55421     if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
 55058  55422       return SQLITE_CORRUPT_BKPT;
 55059  55423     }
 55060  55424     rc = getAndInitPage(pBt, newPgno, &pNewPage,
 55061         -               pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
        55425  +               (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
 55062  55426     if( rc ) return rc;
 55063  55427     pCur->apPage[i+1] = pNewPage;
 55064  55428     pCur->aiIdx[i+1] = 0;
 55065  55429     pCur->iPage++;
 55066  55430   
 55067  55431     pCur->info.nSize = 0;
 55068         -  pCur->validNKey = 0;
        55432  +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 55069  55433     if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
 55070  55434       return SQLITE_CORRUPT_BKPT;
 55071  55435     }
 55072  55436     return SQLITE_OK;
 55073  55437   }
 55074  55438   
 55075  55439   #if 0
................................................................................
 55119  55483     );
 55120  55484   #endif
 55121  55485     testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
 55122  55486   
 55123  55487     releasePage(pCur->apPage[pCur->iPage]);
 55124  55488     pCur->iPage--;
 55125  55489     pCur->info.nSize = 0;
 55126         -  pCur->validNKey = 0;
        55490  +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 55127  55491   }
 55128  55492   
 55129  55493   /*
 55130  55494   ** Move the cursor to point to the root page of its b-tree structure.
 55131  55495   **
 55132  55496   ** If the table has a virtual root page, then the cursor is moved to point
 55133  55497   ** to the virtual root page instead of the actual root page. A table has a
................................................................................
 55166  55530     if( pCur->iPage>=0 ){
 55167  55531       while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
 55168  55532     }else if( pCur->pgnoRoot==0 ){
 55169  55533       pCur->eState = CURSOR_INVALID;
 55170  55534       return SQLITE_OK;
 55171  55535     }else{
 55172  55536       rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
 55173         -                        pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
        55537  +                 (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
 55174  55538       if( rc!=SQLITE_OK ){
 55175  55539         pCur->eState = CURSOR_INVALID;
 55176  55540         return rc;
 55177  55541       }
 55178  55542       pCur->iPage = 0;
 55179  55543     }
 55180  55544     pRoot = pCur->apPage[0];
................................................................................
 55193  55557     assert( pRoot->intKey==1 || pRoot->intKey==0 );
 55194  55558     if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
 55195  55559       return SQLITE_CORRUPT_BKPT;
 55196  55560     }
 55197  55561   
 55198  55562     pCur->aiIdx[0] = 0;
 55199  55563     pCur->info.nSize = 0;
 55200         -  pCur->atLast = 0;
 55201         -  pCur->validNKey = 0;
        55564  +  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
 55202  55565   
 55203  55566     if( pRoot->nCell>0 ){
 55204  55567       pCur->eState = CURSOR_VALID;
 55205  55568     }else if( !pRoot->leaf ){
 55206  55569       Pgno subpage;
 55207  55570       if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
 55208  55571       subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
................................................................................
 55257  55620       pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 55258  55621       pCur->aiIdx[pCur->iPage] = pPage->nCell;
 55259  55622       rc = moveToChild(pCur, pgno);
 55260  55623     }
 55261  55624     if( rc==SQLITE_OK ){
 55262  55625       pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
 55263  55626       pCur->info.nSize = 0;
 55264         -    pCur->validNKey = 0;
        55627  +    pCur->curFlags &= ~BTCF_ValidNKey;
 55265  55628     }
 55266  55629     return rc;
 55267  55630   }
 55268  55631   
 55269  55632   /* Move the cursor to the first entry in the table.  Return SQLITE_OK
 55270  55633   ** on success.  Set *pRes to 0 if the cursor actually points to something
 55271  55634   ** or set *pRes to 1 if the table is empty.
................................................................................
 55296  55659   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
 55297  55660     int rc;
 55298  55661    
 55299  55662     assert( cursorHoldsMutex(pCur) );
 55300  55663     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 55301  55664   
 55302  55665     /* If the cursor already points to the last entry, this is a no-op. */
 55303         -  if( CURSOR_VALID==pCur->eState && pCur->atLast ){
        55666  +  if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
 55304  55667   #ifdef SQLITE_DEBUG
 55305  55668       /* This block serves to assert() that the cursor really does point 
 55306  55669       ** to the last entry in the b-tree. */
 55307  55670       int ii;
 55308  55671       for(ii=0; ii<pCur->iPage; ii++){
 55309  55672         assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
 55310  55673       }
................................................................................
 55319  55682       if( CURSOR_INVALID==pCur->eState ){
 55320  55683         assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 55321  55684         *pRes = 1;
 55322  55685       }else{
 55323  55686         assert( pCur->eState==CURSOR_VALID );
 55324  55687         *pRes = 0;
 55325  55688         rc = moveToRightmost(pCur);
 55326         -      pCur->atLast = rc==SQLITE_OK ?1:0;
        55689  +      if( rc==SQLITE_OK ){
        55690  +        pCur->curFlags |= BTCF_AtLast;
        55691  +      }else{
        55692  +        pCur->curFlags &= ~BTCF_AtLast;
        55693  +      }
        55694  +   
 55327  55695       }
 55328  55696     }
 55329  55697     return rc;
 55330  55698   }
 55331  55699   
 55332  55700   /* Move the cursor so that it points to an entry near the key 
 55333  55701   ** specified by pIdxKey or intKey.   Return a success code.
................................................................................
 55370  55738     assert( cursorHoldsMutex(pCur) );
 55371  55739     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 55372  55740     assert( pRes );
 55373  55741     assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
 55374  55742   
 55375  55743     /* If the cursor is already positioned at the point we are trying
 55376  55744     ** to move to, then just return without doing any work */
 55377         -  if( pCur->eState==CURSOR_VALID && pCur->validNKey 
        55745  +  if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
 55378  55746      && pCur->apPage[0]->intKey 
 55379  55747     ){
 55380  55748       if( pCur->info.nKey==intKey ){
 55381  55749         *pRes = 0;
 55382  55750         return SQLITE_OK;
 55383  55751       }
 55384         -    if( pCur->atLast && pCur->info.nKey<intKey ){
        55752  +    if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
 55385  55753         *pRes = -1;
 55386  55754         return SQLITE_OK;
 55387  55755       }
 55388  55756     }
 55389  55757   
 55390  55758     if( pIdxKey ){
 55391  55759       xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
        55760  +    pIdxKey->isCorrupt = 0;
 55392  55761       assert( pIdxKey->default_rc==1 
 55393  55762            || pIdxKey->default_rc==0 
 55394  55763            || pIdxKey->default_rc==-1
 55395  55764       );
 55396  55765     }else{
 55397  55766       xRecordCompare = 0; /* All keys are integers */
 55398  55767     }
................................................................................
 55443  55812             lwr = idx+1;
 55444  55813             if( lwr>upr ){ c = -1; break; }
 55445  55814           }else if( nCellKey>intKey ){
 55446  55815             upr = idx-1;
 55447  55816             if( lwr>upr ){ c = +1; break; }
 55448  55817           }else{
 55449  55818             assert( nCellKey==intKey );
 55450         -          pCur->validNKey = 1;
        55819  +          pCur->curFlags |= BTCF_ValidNKey;
 55451  55820             pCur->info.nKey = nCellKey;
 55452  55821             pCur->aiIdx[pCur->iPage] = (u16)idx;
 55453  55822             if( !pPage->leaf ){
 55454  55823               lwr = idx;
 55455  55824               goto moveto_next_layer;
 55456  55825             }else{
 55457  55826               *pRes = 0;
................................................................................
 55500  55869             nCell = (int)pCur->info.nKey;
 55501  55870             pCellKey = sqlite3Malloc( nCell );
 55502  55871             if( pCellKey==0 ){
 55503  55872               rc = SQLITE_NOMEM;
 55504  55873               goto moveto_finish;
 55505  55874             }
 55506  55875             pCur->aiIdx[pCur->iPage] = (u16)idx;
 55507         -          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
        55876  +          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
 55508  55877             if( rc ){
 55509  55878               sqlite3_free(pCellKey);
 55510  55879               goto moveto_finish;
 55511  55880             }
 55512  55881             c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
 55513  55882             sqlite3_free(pCellKey);
 55514  55883           }
        55884  +        assert( pIdxKey->isCorrupt==0 || c==0 );
 55515  55885           if( c<0 ){
 55516  55886             lwr = idx+1;
 55517  55887           }else if( c>0 ){
 55518  55888             upr = idx-1;
 55519  55889           }else{
 55520  55890             assert( c==0 );
 55521  55891             *pRes = 0;
 55522  55892             rc = SQLITE_OK;
 55523  55893             pCur->aiIdx[pCur->iPage] = (u16)idx;
        55894  +          if( pIdxKey->isCorrupt ) rc = SQLITE_CORRUPT;
 55524  55895             goto moveto_finish;
 55525  55896           }
 55526  55897           if( lwr>upr ) break;
 55527  55898           assert( lwr+upr>=0 );
 55528  55899           idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
 55529  55900         }
 55530  55901       }
................................................................................
 55545  55916       }
 55546  55917       pCur->aiIdx[pCur->iPage] = (u16)lwr;
 55547  55918       rc = moveToChild(pCur, chldPg);
 55548  55919       if( rc ) break;
 55549  55920     }
 55550  55921   moveto_finish:
 55551  55922     pCur->info.nSize = 0;
 55552         -  pCur->validNKey = 0;
        55923  +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 55553  55924     return rc;
 55554  55925   }
 55555  55926   
 55556  55927   
 55557  55928   /*
 55558  55929   ** Return TRUE if the cursor is not pointing at an entry of the table.
 55559  55930   **
................................................................................
 55590  55961     MemPage *pPage;
 55591  55962   
 55592  55963     assert( cursorHoldsMutex(pCur) );
 55593  55964     assert( pRes!=0 );
 55594  55965     assert( *pRes==0 || *pRes==1 );
 55595  55966     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
 55596  55967     if( pCur->eState!=CURSOR_VALID ){
        55968  +    invalidateOverflowCache(pCur);
 55597  55969       rc = restoreCursorPosition(pCur);
 55598  55970       if( rc!=SQLITE_OK ){
 55599  55971         *pRes = 0;
 55600  55972         return rc;
 55601  55973       }
 55602  55974       if( CURSOR_INVALID==pCur->eState ){
 55603  55975         *pRes = 1;
................................................................................
 55623  55995     ** to be invalid here. This can only occur if a second cursor modifies
 55624  55996     ** the page while cursor pCur is holding a reference to it. Which can
 55625  55997     ** only happen if the database is corrupt in such a way as to link the
 55626  55998     ** page into more than one b-tree structure. */
 55627  55999     testcase( idx>pPage->nCell );
 55628  56000   
 55629  56001     pCur->info.nSize = 0;
 55630         -  pCur->validNKey = 0;
        56002  +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 55631  56003     if( idx>=pPage->nCell ){
 55632  56004       if( !pPage->leaf ){
 55633  56005         rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
 55634  56006         if( rc ){
 55635  56007           *pRes = 0;
 55636  56008           return rc;
 55637  56009         }
................................................................................
 55684  56056     int rc;
 55685  56057     MemPage *pPage;
 55686  56058   
 55687  56059     assert( cursorHoldsMutex(pCur) );
 55688  56060     assert( pRes!=0 );
 55689  56061     assert( *pRes==0 || *pRes==1 );
 55690  56062     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
 55691         -  pCur->atLast = 0;
        56063  +  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl);
 55692  56064     if( pCur->eState!=CURSOR_VALID ){
 55693  56065       if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
 55694  56066         rc = btreeRestoreCursorPosition(pCur);
 55695  56067         if( rc!=SQLITE_OK ){
 55696  56068           *pRes = 0;
 55697  56069           return rc;
 55698  56070         }
................................................................................
 55729  56101           pCur->eState = CURSOR_INVALID;
 55730  56102           *pRes = 1;
 55731  56103           return SQLITE_OK;
 55732  56104         }
 55733  56105         moveToParent(pCur);
 55734  56106       }
 55735  56107       pCur->info.nSize = 0;
 55736         -    pCur->validNKey = 0;
        56108  +    pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 55737  56109   
 55738  56110       pCur->aiIdx[pCur->iPage]--;
 55739  56111       pPage = pCur->apPage[pCur->iPage];
 55740  56112       if( pPage->intKey && !pPage->leaf ){
 55741  56113         rc = sqlite3BtreePrevious(pCur, pRes);
 55742  56114       }else{
 55743  56115         rc = SQLITE_OK;
................................................................................
 57754  58126   
 57755  58127     if( pCur->eState==CURSOR_FAULT ){
 57756  58128       assert( pCur->skipNext!=SQLITE_OK );
 57757  58129       return pCur->skipNext;
 57758  58130     }
 57759  58131   
 57760  58132     assert( cursorHoldsMutex(pCur) );
 57761         -  assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
        58133  +  assert( (pCur->curFlags & BTCF_WriteFlag)!=0 && pBt->inTransaction==TRANS_WRITE
 57762  58134                 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
 57763  58135     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 57764  58136   
 57765  58137     /* Assert that the caller has been consistent. If this cursor was opened
 57766  58138     ** expecting an index b-tree, then the caller should be inserting blob
 57767  58139     ** keys with no associated data. If the cursor was opened expecting an
 57768  58140     ** intkey table, the caller should be inserting integer keys with a
................................................................................
 57787  58159       /* If this is an insert into a table b-tree, invalidate any incrblob 
 57788  58160       ** cursors open on the row being replaced */
 57789  58161       invalidateIncrblobCursors(p, nKey, 0);
 57790  58162   
 57791  58163       /* If the cursor is currently on the last row and we are appending a
 57792  58164       ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
 57793  58165       ** call */
 57794         -    if( pCur->validNKey && nKey>0 && pCur->info.nKey==nKey-1 ){
        58166  +    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 && pCur->info.nKey==nKey-1 ){
 57795  58167         loc = -1;
 57796  58168       }
 57797  58169     }
 57798  58170   
 57799  58171     if( !loc ){
 57800  58172       rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
 57801  58173       if( rc ) return rc;
................................................................................
 57840  58212       assert( pPage->leaf );
 57841  58213     }
 57842  58214     insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
 57843  58215     assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
 57844  58216   
 57845  58217     /* If no error has occurred and pPage has an overflow cell, call balance() 
 57846  58218     ** to redistribute the cells within the tree. Since balance() may move
 57847         -  ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
        58219  +  ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
 57848  58220     ** variables.
 57849  58221     **
 57850  58222     ** Previous versions of SQLite called moveToRoot() to move the cursor
 57851  58223     ** back to the root page as balance() used to invalidate the contents
 57852  58224     ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
 57853  58225     ** set the cursor state to "invalid". This makes common insert operations
 57854  58226     ** slightly faster.
................................................................................
 57860  58232     ** the b-tree if possible. If the cursor is left pointing to the last
 57861  58233     ** entry in the table, and the next row inserted has an integer key
 57862  58234     ** larger than the largest existing key, it is possible to insert the
 57863  58235     ** row without seeking the cursor. This can be a big performance boost.
 57864  58236     */
 57865  58237     pCur->info.nSize = 0;
 57866  58238     if( rc==SQLITE_OK && pPage->nOverflow ){
 57867         -    pCur->validNKey = 0;
        58239  +    pCur->curFlags &= ~(BTCF_ValidNKey);
 57868  58240       rc = balance(pCur);
 57869  58241   
 57870  58242       /* Must make sure nOverflow is reset to zero even if the balance()
 57871  58243       ** fails. Internal data structure corruption will result otherwise. 
 57872  58244       ** Also, set the cursor state to invalid. This stops saveCursorPosition()
 57873  58245       ** from trying to save the current position of the cursor.  */
 57874  58246       pCur->apPage[pCur->iPage]->nOverflow = 0;
................................................................................
 57892  58264     unsigned char *pCell;                /* Pointer to cell to delete */
 57893  58265     int iCellIdx;                        /* Index of cell to delete */
 57894  58266     int iCellDepth;                      /* Depth of node containing pCell */ 
 57895  58267   
 57896  58268     assert( cursorHoldsMutex(pCur) );
 57897  58269     assert( pBt->inTransaction==TRANS_WRITE );
 57898  58270     assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 57899         -  assert( pCur->wrFlag );
        58271  +  assert( pCur->curFlags & BTCF_WriteFlag );
 57900  58272     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 57901  58273     assert( !hasReadConflicts(p, pCur->pgnoRoot) );
 57902  58274   
 57903  58275     if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
 57904  58276      || NEVER(pCur->eState!=CURSOR_VALID)
 57905  58277     ){
 57906  58278       return SQLITE_ERROR;  /* Something has gone awry. */
................................................................................
 58236  58608       ** a no-op).  */
 58237  58609       invalidateIncrblobCursors(p, 0, 1);
 58238  58610       rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
 58239  58611     }
 58240  58612     sqlite3BtreeLeave(p);
 58241  58613     return rc;
 58242  58614   }
        58615  +
        58616  +/*
        58617  +** Delete all information from the single table that pCur is open on.
        58618  +**
        58619  +** This routine only work for pCur on an ephemeral table.
        58620  +*/
        58621  +SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
        58622  +  return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
        58623  +}
 58243  58624   
 58244  58625   /*
 58245  58626   ** Erase all information in a table and add the root of the table to
 58246  58627   ** the freelist.  Except, the root of the principle table (the one on
 58247  58628   ** page 1) is never added to the freelist.
 58248  58629   **
 58249  58630   ** This routine will fail with SQLITE_LOCKED if there are any open
................................................................................
 59196  59577   ** parameters that attempt to write past the end of the existing data,
 59197  59578   ** no modifications are made and SQLITE_CORRUPT is returned.
 59198  59579   */
 59199  59580   SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
 59200  59581     int rc;
 59201  59582     assert( cursorHoldsMutex(pCsr) );
 59202  59583     assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
 59203         -  assert( pCsr->isIncrblobHandle );
        59584  +  assert( pCsr->curFlags & BTCF_Incrblob );
 59204  59585   
 59205  59586     rc = restoreCursorPosition(pCsr);
 59206  59587     if( rc!=SQLITE_OK ){
 59207  59588       return rc;
 59208  59589     }
 59209  59590     assert( pCsr->eState!=CURSOR_REQUIRESEEK );
 59210  59591     if( pCsr->eState!=CURSOR_VALID ){
................................................................................
 59225  59606     /* Check some assumptions: 
 59226  59607     **   (a) the cursor is open for writing,
 59227  59608     **   (b) there is a read/write transaction open,
 59228  59609     **   (c) the connection holds a write-lock on the table (if required),
 59229  59610     **   (d) there are no conflicting read-locks, and
 59230  59611     **   (e) the cursor points at a valid row of an intKey table.
 59231  59612     */
 59232         -  if( !pCsr->wrFlag ){
        59613  +  if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
 59233  59614       return SQLITE_READONLY;
 59234  59615     }
 59235  59616     assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
 59236  59617                 && pCsr->pBt->inTransaction==TRANS_WRITE );
 59237  59618     assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
 59238  59619     assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
 59239  59620     assert( pCsr->apPage[pCsr->iPage]->intKey );
 59240  59621   
 59241  59622     return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
 59242  59623   }
 59243  59624   
 59244  59625   /* 
 59245         -** Set a flag on this cursor to cache the locations of pages from the 
 59246         -** overflow list for the current row. This is used by cursors opened
 59247         -** for incremental blob IO only.
 59248         -**
 59249         -** This function sets a flag only. The actual page location cache
 59250         -** (stored in BtCursor.aOverflow[]) is allocated and used by function
 59251         -** accessPayload() (the worker function for sqlite3BtreeData() and
 59252         -** sqlite3BtreePutData()).
        59626  +** Mark this cursor as an incremental blob cursor.
 59253  59627   */
 59254         -SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
 59255         -  assert( cursorHoldsMutex(pCur) );
 59256         -  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 59257         -  invalidateOverflowCache(pCur);
 59258         -  pCur->isIncrblobHandle = 1;
        59628  +SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
        59629  +  pCur->curFlags |= BTCF_Incrblob;
 59259  59630   }
 59260  59631   #endif
 59261  59632   
 59262  59633   /*
 59263  59634   ** Set both the "read version" (single byte at byte offset 18) and 
 59264  59635   ** "write version" (single byte at byte offset 19) fields in the database
 59265  59636   ** header to iVersion.
................................................................................
 59299  59670   ** set the mask of hint flags for cursor pCsr. Currently the only valid
 59300  59671   ** values are 0 and BTREE_BULKLOAD.
 59301  59672   */
 59302  59673   SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
 59303  59674     assert( mask==BTREE_BULKLOAD || mask==0 );
 59304  59675     pCsr->hints = mask;
 59305  59676   }
        59677  +
        59678  +/*
        59679  +** Return true if the given Btree is read-only.
        59680  +*/
        59681  +SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
        59682  +  return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
        59683  +}
 59306  59684   
 59307  59685   /************** End of btree.c ***********************************************/
 59308  59686   /************** Begin file backup.c ******************************************/
 59309  59687   /*
 59310  59688   ** 2009 January 28
 59311  59689   **
 59312  59690   ** The author disclaims copyright to this source code.  In place of
................................................................................
 61622  62000   ** a prior call to sqlite3VdbeMakeLabel().
 61623  62001   */
 61624  62002   SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
 61625  62003     Parse *p = v->pParse;
 61626  62004     int j = -1-x;
 61627  62005     assert( v->magic==VDBE_MAGIC_INIT );
 61628  62006     assert( j<p->nLabel );
 61629         -  if( j>=0 && p->aLabel ){
        62007  +  if( ALWAYS(j>=0) && p->aLabel ){
 61630  62008       p->aLabel[j] = v->nOp;
 61631  62009     }
 61632  62010     p->iFixedOp = v->nOp - 1;
 61633  62011   }
 61634  62012   
 61635  62013   /*
 61636  62014   ** Mark the VDBE as one that can only be run one time.
................................................................................
 62129  62507     }
 62130  62508     assert( p->nOp>0 );
 62131  62509     assert( addr<p->nOp );
 62132  62510     if( addr<0 ){
 62133  62511       addr = p->nOp - 1;
 62134  62512     }
 62135  62513     pOp = &p->aOp[addr];
 62136         -  assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
        62514  +  assert( pOp->p4type==P4_NOTUSED
        62515  +       || pOp->p4type==P4_INT32
        62516  +       || pOp->p4type==P4_KEYINFO );
 62137  62517     freeP4(db, pOp->p4type, pOp->p4.p);
 62138  62518     pOp->p4.p = 0;
 62139  62519     if( n==P4_INT32 ){
 62140  62520       /* Note: this cast is safe, because the origin data point was an int
 62141  62521       ** that was cast to a (const char *). */
 62142  62522       pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
 62143  62523       pOp->p4type = P4_INT32;
................................................................................
 64079  64459       p->cacheStatus = CACHE_STALE;
 64080  64460     }else if( p->pCursor ){
 64081  64461       int hasMoved;
 64082  64462       int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
 64083  64463       if( rc ) return rc;
 64084  64464       if( hasMoved ){
 64085  64465         p->cacheStatus = CACHE_STALE;
 64086         -      p->nullRow = 1;
        64466  +      if( hasMoved==2 ) p->nullRow = 1;
 64087  64467       }
 64088  64468     }
 64089  64469     return SQLITE_OK;
 64090  64470   }
 64091  64471   
 64092  64472   /*
 64093  64473   ** The following functions:
................................................................................
 64749  65129   **
 64750  65130   ** If argument bSkip is non-zero, it is assumed that the caller has already
 64751  65131   ** determined that the first fields of the keys are equal.
 64752  65132   **
 64753  65133   ** Key1 and Key2 do not have to contain the same number of fields. If all 
 64754  65134   ** fields that appear in both keys are equal, then pPKey2->default_rc is 
 64755  65135   ** returned.
        65136  +**
        65137  +** If database corruption is discovered, set pPKey2->isCorrupt to non-zero
        65138  +** and return 0.
 64756  65139   */
 64757  65140   SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
 64758  65141     int nKey1, const void *pKey1,   /* Left key */
 64759         -  const UnpackedRecord *pPKey2,   /* Right key */
        65142  +  UnpackedRecord *pPKey2,         /* Right key */
 64760  65143     int bSkip                       /* If true, skip the first field */
 64761  65144   ){
 64762  65145     u32 d1;                         /* Offset into aKey[] of next data element */
 64763  65146     int i;                          /* Index of next field to compare */
 64764  65147     u32 szHdr1;                     /* Size of record header in bytes */
 64765  65148     u32 idx1;                       /* Offset of first type in header */
 64766  65149     int rc = 0;                     /* Return value */
................................................................................
 64778  65161       szHdr1 = aKey1[0];
 64779  65162       d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
 64780  65163       i = 1;
 64781  65164       pRhs++;
 64782  65165     }else{
 64783  65166       idx1 = getVarint32(aKey1, szHdr1);
 64784  65167       d1 = szHdr1;
 64785         -    if( d1>(unsigned)nKey1 ) return 1;  /* Corruption */
        65168  +    if( d1>(unsigned)nKey1 ){ 
        65169  +      pPKey2->isCorrupt = (u8)SQLITE_CORRUPT_BKPT;
        65170  +      return 0;  /* Corruption */
        65171  +    }
 64786  65172       i = 0;
 64787  65173     }
 64788  65174   
 64789  65175     VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
 64790  65176     assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField 
 64791  65177          || CORRUPT_DB );
 64792  65178     assert( pPKey2->pKeyInfo->aSortOrder!=0 );
................................................................................
 64855  65241         }else if( !(serial_type & 0x01) ){
 64856  65242           rc = +1;
 64857  65243         }else{
 64858  65244           mem1.n = (serial_type - 12) / 2;
 64859  65245           testcase( (d1+mem1.n)==(unsigned)nKey1 );
 64860  65246           testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
 64861  65247           if( (d1+mem1.n) > (unsigned)nKey1 ){
 64862         -          rc = 1;                /* Corruption */
        65248  +          pPKey2->isCorrupt = (u8)SQLITE_CORRUPT_BKPT;
        65249  +          return 0;                /* Corruption */
 64863  65250           }else if( pKeyInfo->aColl[i] ){
 64864  65251             mem1.enc = pKeyInfo->enc;
 64865  65252             mem1.db = pKeyInfo->db;
 64866  65253             mem1.flags = MEM_Str;
 64867  65254             mem1.z = (char*)&aKey1[d1];
 64868  65255             rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
 64869  65256           }else{
................................................................................
 64881  65268         if( serial_type<12 || (serial_type & 0x01) ){
 64882  65269           rc = -1;
 64883  65270         }else{
 64884  65271           int nStr = (serial_type - 12) / 2;
 64885  65272           testcase( (d1+nStr)==(unsigned)nKey1 );
 64886  65273           testcase( (d1+nStr+1)==(unsigned)nKey1 );
 64887  65274           if( (d1+nStr) > (unsigned)nKey1 ){
 64888         -          rc = 1;                /* Corruption */
        65275  +          pPKey2->isCorrupt = (u8)SQLITE_CORRUPT_BKPT;
        65276  +          return 0;                /* Corruption */
 64889  65277           }else{
 64890  65278             int nCmp = MIN(nStr, pRhs->n);
 64891  65279             rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
 64892  65280             if( rc==0 ) rc = nStr - pRhs->n;
 64893  65281           }
 64894  65282         }
 64895  65283       }
................................................................................
 64934  65322   }
 64935  65323   
 64936  65324   /*
 64937  65325   ** This function is an optimized version of sqlite3VdbeRecordCompare() 
 64938  65326   ** that (a) the first field of pPKey2 is an integer, and (b) the 
 64939  65327   ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
 64940  65328   ** byte (i.e. is less than 128).
        65329  +**
        65330  +** To avoid concerns about buffer overreads, this routine is only used
        65331  +** on schemas where the maximum valid header size is 63 bytes or less.
 64941  65332   */
 64942  65333   static int vdbeRecordCompareInt(
 64943  65334     int nKey1, const void *pKey1, /* Left key */
 64944         -  const UnpackedRecord *pPKey2, /* Right key */
        65335  +  UnpackedRecord *pPKey2,       /* Right key */
 64945  65336     int bSkip                     /* Ignored */
 64946  65337   ){
 64947  65338     const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
 64948  65339     int serial_type = ((const u8*)pKey1)[1];
 64949  65340     int res;
 64950  65341     u32 y;
 64951  65342     u64 x;
 64952  65343     i64 v = pPKey2->aMem[0].u.i;
 64953  65344     i64 lhs;
 64954  65345     UNUSED_PARAMETER(bSkip);
 64955  65346   
 64956  65347     assert( bSkip==0 );
        65348  +  assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
 64957  65349     switch( serial_type ){
 64958  65350       case 1: { /* 1-byte signed integer */
 64959  65351         lhs = ONE_BYTE_INT(aKey);
 64960  65352         testcase( lhs<0 );
 64961  65353         break;
 64962  65354       }
 64963  65355       case 2: { /* 2-byte signed integer */
................................................................................
 65034  65426   ** This function is an optimized version of sqlite3VdbeRecordCompare() 
 65035  65427   ** that (a) the first field of pPKey2 is a string, that (b) the first field
 65036  65428   ** uses the collation sequence BINARY and (c) that the size-of-header varint 
 65037  65429   ** at the start of (pKey1/nKey1) fits in a single byte.
 65038  65430   */
 65039  65431   static int vdbeRecordCompareString(
 65040  65432     int nKey1, const void *pKey1, /* Left key */
 65041         -  const UnpackedRecord *pPKey2, /* Right key */
        65433  +  UnpackedRecord *pPKey2,       /* Right key */
 65042  65434     int bSkip
 65043  65435   ){
 65044  65436     const u8 *aKey1 = (const u8*)pKey1;
 65045  65437     int serial_type;
 65046  65438     int res;
 65047  65439     UNUSED_PARAMETER(bSkip);
 65048  65440   
................................................................................
 65055  65447       res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
 65056  65448     }else{
 65057  65449       int nCmp;
 65058  65450       int nStr;
 65059  65451       int szHdr = aKey1[0];
 65060  65452   
 65061  65453       nStr = (serial_type-12) / 2;
 65062         -    if( (szHdr + nStr) > nKey1 ) return 0;    /* Corruption */
        65454  +    if( (szHdr + nStr) > nKey1 ){
        65455  +      pPKey2->isCorrupt = (u8)SQLITE_CORRUPT_BKPT;
        65456  +      return 0;    /* Corruption */
        65457  +    }
 65063  65458       nCmp = MIN( pPKey2->aMem[0].n, nStr );
 65064  65459       res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
 65065  65460   
 65066  65461       if( res==0 ){
 65067  65462         res = nStr - pPKey2->aMem[0].n;
 65068  65463         if( res==0 ){
 65069  65464           if( pPKey2->nField>1 ){
................................................................................
 65220  65615   ** pUnpacked is either created without a rowid or is truncated so that it
 65221  65616   ** omits the rowid at the end.  The rowid at the end of the index entry
 65222  65617   ** is ignored as well.  Hence, this routine only compares the prefixes 
 65223  65618   ** of the keys prior to the final rowid, not the entire key.
 65224  65619   */
 65225  65620   SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
 65226  65621     VdbeCursor *pC,                  /* The cursor to compare against */
 65227         -  const UnpackedRecord *pUnpacked, /* Unpacked version of key */
        65622  +  UnpackedRecord *pUnpacked,       /* Unpacked version of key */
 65228  65623     int *res                         /* Write the comparison result here */
 65229  65624   ){
 65230  65625     i64 nCellKey = 0;
 65231  65626     int rc;
 65232  65627     BtCursor *pCur = pC->pCursor;
 65233  65628     Mem m;
 65234  65629   
................................................................................
 67310  67705   SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
 67311  67706     sqlite3_value *pVal, 
 67312  67707     u8 affinity, 
 67313  67708     u8 enc
 67314  67709   ){
 67315  67710     applyAffinity((Mem *)pVal, affinity, enc);
 67316  67711   }
        67712  +
        67713  +/*
        67714  +** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
        67715  +** none.  
        67716  +**
        67717  +** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
        67718  +** But it does set pMem->r and pMem->u.i appropriately.
        67719  +*/
        67720  +static u16 numericType(Mem *pMem){
        67721  +  if( pMem->flags & (MEM_Int|MEM_Real) ){
        67722  +    return pMem->flags & (MEM_Int|MEM_Real);
        67723  +  }
        67724  +  if( pMem->flags & (MEM_Str|MEM_Blob) ){
        67725  +    if( sqlite3AtoF(pMem->z, &pMem->r, pMem->n, pMem->enc)==0 ){
        67726  +      return 0;
        67727  +    }
        67728  +    if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
        67729  +      return MEM_Int;
        67730  +    }
        67731  +    return MEM_Real;
        67732  +  }
        67733  +  return 0;
        67734  +}
 67317  67735   
 67318  67736   #ifdef SQLITE_DEBUG
 67319  67737   /*
 67320  67738   ** Write a nice string representation of the contents of cell pMem
 67321  67739   ** into buffer zBuf, length nBuf.
 67322  67740   */
 67323  67741   SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
................................................................................
 68170  68588     UPDATE_MAX_BLOBSIZE(pOut);
 68171  68589     break;
 68172  68590   }
 68173  68591   
 68174  68592   /* Opcode: Move P1 P2 P3 * *
 68175  68593   ** Synopsis:  r[P2@P3]=r[P1@P3]
 68176  68594   **
 68177         -** Move the values in register P1..P1+P3 over into
 68178         -** registers P2..P2+P3.  Registers P1..P1+P3 are
        68595  +** Move the P3 values in register P1..P1+P3-1 over into
        68596  +** registers P2..P2+P3-1.  Registers P1..P1+P3-1 are
 68179  68597   ** left holding a NULL.  It is an error for register ranges
 68180         -** P1..P1+P3 and P2..P2+P3 to overlap.
        68598  +** P1..P1+P3-1 and P2..P2+P3-1 to overlap.  It is an error
        68599  +** for P3 to be less than 1.
 68181  68600   */
 68182  68601   case OP_Move: {
 68183  68602     char *zMalloc;   /* Holding variable for allocated memory */
 68184  68603     int n;           /* Number of registers left to copy */
 68185  68604     int p1;          /* Register to copy from */
 68186  68605     int p2;          /* Register to copy to */
 68187  68606   
 68188  68607     n = pOp->p3;
 68189  68608     p1 = pOp->p1;
 68190  68609     p2 = pOp->p2;
 68191         -  assert( n>=0 && p1>0 && p2>0 );
        68610  +  assert( n>0 && p1>0 && p2>0 );
 68192  68611     assert( p1+n<=p2 || p2+n<=p1 );
 68193  68612   
 68194  68613     pIn1 = &aMem[p1];
 68195  68614     pOut = &aMem[p2];
 68196  68615     do{
 68197  68616       assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
 68198  68617       assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
................................................................................
 68208  68627   #endif
 68209  68628       pIn1->flags = MEM_Undefined;
 68210  68629       pIn1->xDel = 0;
 68211  68630       pIn1->zMalloc = zMalloc;
 68212  68631       REGISTER_TRACE(p2++, pOut);
 68213  68632       pIn1++;
 68214  68633       pOut++;
 68215         -  }while( n-- );
        68634  +  }while( --n );
 68216  68635     break;
 68217  68636   }
 68218  68637   
 68219  68638   /* Opcode: Copy P1 P2 P3 * *
 68220  68639   ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
 68221  68640   **
 68222  68641   ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
................................................................................
 68440  68859   */
 68441  68860   case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
 68442  68861   case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
 68443  68862   case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
 68444  68863   case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
 68445  68864   case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
 68446  68865     char bIntint;   /* Started out as two integer operands */
 68447         -  int flags;      /* Combined MEM_* flags from both inputs */
        68866  +  u16 flags;      /* Combined MEM_* flags from both inputs */
        68867  +  u16 type1;      /* Numeric type of left operand */
        68868  +  u16 type2;      /* Numeric type of right operand */
 68448  68869     i64 iA;         /* Integer value of left operand */
 68449  68870     i64 iB;         /* Integer value of right operand */
 68450  68871     double rA;      /* Real value of left operand */
 68451  68872     double rB;      /* Real value of right operand */
 68452  68873   
 68453  68874     pIn1 = &aMem[pOp->p1];
 68454         -  applyNumericAffinity(pIn1);
        68875  +  type1 = numericType(pIn1);
 68455  68876     pIn2 = &aMem[pOp->p2];
 68456         -  applyNumericAffinity(pIn2);
        68877  +  type2 = numericType(pIn2);
 68457  68878     pOut = &aMem[pOp->p3];
 68458  68879     flags = pIn1->flags | pIn2->flags;
 68459  68880     if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
 68460         -  if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
        68881  +  if( (type1 & type2 & MEM_Int)!=0 ){
 68461  68882       iA = pIn1->u.i;
 68462  68883       iB = pIn2->u.i;
 68463  68884       bIntint = 1;
 68464  68885       switch( pOp->opcode ){
 68465  68886         case OP_Add:       if( sqlite3AddInt64(&iB,iA) ) goto fp_math;  break;
 68466  68887         case OP_Subtract:  if( sqlite3SubInt64(&iB,iA) ) goto fp_math;  break;
 68467  68888         case OP_Multiply:  if( sqlite3MulInt64(&iB,iA) ) goto fp_math;  break;
................................................................................
 68509  68930       MemSetTypeFlag(pOut, MEM_Int);
 68510  68931   #else
 68511  68932       if( sqlite3IsNaN(rB) ){
 68512  68933         goto arithmetic_result_is_null;
 68513  68934       }
 68514  68935       pOut->r = rB;
 68515  68936       MemSetTypeFlag(pOut, MEM_Real);
 68516         -    if( (flags & MEM_Real)==0 && !bIntint ){
        68937  +    if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
 68517  68938         sqlite3VdbeIntegerAffinity(pOut);
 68518  68939       }
 68519  68940   #endif
 68520  68941     }
 68521  68942     break;
 68522  68943   
 68523  68944   arithmetic_result_is_null:
................................................................................
 69085  69506     assert( pOp->p4type==P4_INTARRAY );
 69086  69507     assert( pOp->p4.ai );
 69087  69508     aPermute = pOp->p4.ai;
 69088  69509     break;
 69089  69510   }
 69090  69511   
 69091  69512   /* Opcode: Compare P1 P2 P3 P4 P5
        69513  +** Synopsis: r[P1@P3] <-> r[P2@P3]
 69092  69514   **
 69093  69515   ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
 69094  69516   ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
 69095  69517   ** the comparison for use by the next OP_Jump instruct.
 69096  69518   **
 69097  69519   ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
 69098  69520   ** determined by the most recent OP_Permutation operator.  If the
................................................................................
 70420  70842         SQLITE_OPEN_DELETEONCLOSE |
 70421  70843         SQLITE_OPEN_TRANSIENT_DB;
 70422  70844     assert( pOp->p1>=0 );
 70423  70845     assert( pOp->p2>=0 );
 70424  70846     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
 70425  70847     if( pCx==0 ) goto no_mem;
 70426  70848     pCx->nullRow = 1;
        70849  +  pCx->isEphemeral = 1;
 70427  70850     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 
 70428  70851                           BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
 70429  70852     if( rc==SQLITE_OK ){
 70430  70853       rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
 70431  70854     }
 70432  70855     if( rc==SQLITE_OK ){
 70433  70856       /* If a transient index is required, create it by calling
................................................................................
 70526  70949   ** to an SQL index, then P3 is the first in an array of P4 registers 
 70527  70950   ** that are used as an unpacked index key. 
 70528  70951   **
 70529  70952   ** Reposition cursor P1 so that  it points to the smallest entry that 
 70530  70953   ** is greater than or equal to the key value. If there are no records 
 70531  70954   ** greater than or equal to the key and P2 is not zero, then jump to P2.
 70532  70955   **
 70533         -** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
        70956  +** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
 70534  70957   */
 70535  70958   /* Opcode: SeekGt P1 P2 P3 P4 *
 70536  70959   ** Synopsis: key=r[P3@P4]
 70537  70960   **
 70538  70961   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 70539  70962   ** use the value in register P3 as a key. If cursor P1 refers 
 70540  70963   ** to an SQL index, then P3 is the first in an array of P4 registers 
 70541  70964   ** that are used as an unpacked index key. 
 70542  70965   **
 70543  70966   ** Reposition cursor P1 so that  it points to the smallest entry that 
 70544  70967   ** is greater than the key value. If there are no records greater than 
 70545  70968   ** the key and P2 is not zero, then jump to P2.
 70546  70969   **
 70547         -** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
        70970  +** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
 70548  70971   */
 70549  70972   /* Opcode: SeekLt P1 P2 P3 P4 * 
 70550  70973   ** Synopsis: key=r[P3@P4]
 70551  70974   **
 70552  70975   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 70553  70976   ** use the value in register P3 as a key. If cursor P1 refers 
 70554  70977   ** to an SQL index, then P3 is the first in an array of P4 registers 
 70555  70978   ** that are used as an unpacked index key. 
 70556  70979   **
 70557  70980   ** Reposition cursor P1 so that  it points to the largest entry that 
 70558  70981   ** is less than the key value. If there are no records less than 
 70559  70982   ** the key and P2 is not zero, then jump to P2.
 70560  70983   **
 70561         -** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
        70984  +** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
 70562  70985   */
 70563  70986   /* Opcode: SeekLe P1 P2 P3 P4 *
 70564  70987   ** Synopsis: key=r[P3@P4]
 70565  70988   **
 70566  70989   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 70567  70990   ** use the value in register P3 as a key. If cursor P1 refers 
 70568  70991   ** to an SQL index, then P3 is the first in an array of P4 registers 
 70569  70992   ** that are used as an unpacked index key. 
 70570  70993   **
 70571  70994   ** Reposition cursor P1 so that it points to the largest entry that 
 70572  70995   ** is less than or equal to the key value. If there are no records 
 70573  70996   ** less than or equal to the key and P2 is not zero, then jump to P2.
 70574  70997   **
 70575         -** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
        70998  +** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
 70576  70999   */
 70577  71000   case OP_SeekLT:         /* jump, in3 */
 70578  71001   case OP_SeekLE:         /* jump, in3 */
 70579  71002   case OP_SeekGE:         /* jump, in3 */
 70580  71003   case OP_SeekGT: {       /* jump, in3 */
 70581  71004     int res;
 70582  71005     int oc;
................................................................................
 70910  71333       assert( pC->rowidIsValid==0 );
 70911  71334     }
 70912  71335     pC->seekResult = res;
 70913  71336     break;
 70914  71337   }
 70915  71338   
 70916  71339   /* Opcode: Sequence P1 P2 * * *
 70917         -** Synopsis: r[P2]=rowid
        71340  +** Synopsis: r[P2]=cursor[P1].ctr++
 70918  71341   **
 70919  71342   ** Find the next available sequence number for cursor P1.
 70920  71343   ** Write the sequence number into register P2.
 70921  71344   ** The sequence number on the cursor is incremented after this
 70922  71345   ** instruction.  
 70923  71346   */
 70924  71347   case OP_Sequence: {           /* out2-prerelease */
................................................................................
 71294  71717   case OP_SorterData: {
 71295  71718     VdbeCursor *pC;
 71296  71719   
 71297  71720     pOut = &aMem[pOp->p2];
 71298  71721     pC = p->apCsr[pOp->p1];
 71299  71722     assert( isSorter(pC) );
 71300  71723     rc = sqlite3VdbeSorterRowkey(pC, pOut);
        71724  +  assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
 71301  71725     break;
 71302  71726   }
 71303  71727   
 71304  71728   /* Opcode: RowData P1 P2 * * *
 71305  71729   ** Synopsis: r[P2]=data
 71306  71730   **
 71307  71731   ** Write into register P2 the complete row data for cursor P1.
................................................................................
 71601  72025   */
 71602  72026   case OP_SorterNext: {  /* jump */
 71603  72027     VdbeCursor *pC;
 71604  72028     int res;
 71605  72029   
 71606  72030     pC = p->apCsr[pOp->p1];
 71607  72031     assert( isSorter(pC) );
        72032  +  res = 0;
 71608  72033     rc = sqlite3VdbeSorterNext(db, pC, &res);
 71609  72034     goto next_tail;
 71610  72035   case OP_PrevIfOpen:    /* jump */
 71611  72036   case OP_NextIfOpen:    /* jump */
 71612  72037     if( p->apCsr[pOp->p1]==0 ) break;
 71613  72038     /* Fall through */
 71614  72039   case OP_Prev:          /* jump */
................................................................................
 71958  72383         assert( memIsValid(&aMem[pOp->p3]) );
 71959  72384         memAboutToChange(p, &aMem[pOp->p3]);
 71960  72385         aMem[pOp->p3].u.i += nChange;
 71961  72386       }
 71962  72387     }
 71963  72388     break;
 71964  72389   }
        72390  +
        72391  +/* Opcode: ResetSorter P1 * * * *
        72392  +**
        72393  +** Delete all contents from the ephemeral table or sorter
        72394  +** that is open on cursor P1.
        72395  +**
        72396  +** This opcode only works for cursors used for sorting and
        72397  +** opened with OP_OpenEphemeral or OP_SorterOpen.
        72398  +*/
        72399  +case OP_ResetSorter: {
        72400  +  VdbeCursor *pC;
        72401  + 
        72402  +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
        72403  +  pC = p->apCsr[pOp->p1];
        72404  +  assert( pC!=0 );
        72405  +  if( pC->pSorter ){
        72406  +    sqlite3VdbeSorterReset(db, pC->pSorter);
        72407  +  }else{
        72408  +    assert( pC->isEphemeral );
        72409  +    rc = sqlite3BtreeClearTableOfCursor(pC->pCursor);
        72410  +  }
        72411  +  break;
        72412  +}
 71965  72413   
 71966  72414   /* Opcode: CreateTable P1 P2 * * *
 71967  72415   ** Synopsis: r[P2]=root iDb=P1
 71968  72416   **
 71969  72417   ** Allocate a new table in the main database file if P1==0 or in the
 71970  72418   ** auxiliary database file if P1==1 or in an attached database if
 71971  72419   ** P1>1.  Write the root page number of the new table into
................................................................................
 72265  72713       sqlite3VdbeMemSetRowSet(pIn1);
 72266  72714       if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
 72267  72715     }
 72268  72716   
 72269  72717     assert( pOp->p4type==P4_INT32 );
 72270  72718     assert( iSet==-1 || iSet>=0 );
 72271  72719     if( iSet ){
 72272         -    exists = sqlite3RowSetTest(pIn1->u.pRowSet, 
 72273         -                               (u8)(iSet>=0 ? iSet & 0xf : 0xff),
 72274         -                               pIn3->u.i);
        72720  +    exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
 72275  72721       VdbeBranchTaken(exists!=0,2);
 72276  72722       if( exists ){
 72277  72723         pc = pOp->p2 - 1;
 72278  72724         break;
 72279  72725       }
 72280  72726     }
 72281  72727     if( iSet>=0 ){
................................................................................
 72967  73413     }
 72968  73414     break;
 72969  73415   }
 72970  73416   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 72971  73417   
 72972  73418   #ifndef SQLITE_OMIT_VIRTUALTABLE
 72973  73419   /* Opcode: VFilter P1 P2 P3 P4 *
 72974         -** Synopsis: iPlan=r[P3] zPlan='P4'
        73420  +** Synopsis: iplan=r[P3] zplan='P4'
 72975  73421   **
 72976  73422   ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
 72977  73423   ** the filtered result set is empty.
 72978  73424   **
 72979  73425   ** P4 is either NULL or a string that was generated by the xBestIndex
 72980  73426   ** method of the module.  The interpretation of the P4 string is left
 72981  73427   ** to the module implementation.
................................................................................
 73320  73766       sqlite3DbFree(db, z);
 73321  73767     }
 73322  73768   #ifdef SQLITE_USE_FCNTL_TRACE
 73323  73769     zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
 73324  73770     if( zTrace ){
 73325  73771       int i;
 73326  73772       for(i=0; i<db->nDb; i++){
 73327         -      if( MASKBIT(i) & p->btreeMask)==0 ) continue;
        73773  +      if( (MASKBIT(i) & p->btreeMask)==0 ) continue;
 73328  73774         sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
 73329  73775       }
 73330  73776     }
 73331  73777   #endif /* SQLITE_USE_FCNTL_TRACE */
 73332  73778   #ifdef SQLITE_DEBUG
 73333  73779     if( (db->flags & SQLITE_SqlTrace)!=0
 73334  73780      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
................................................................................
 73363  73809   ** readability.  From this point on down, the normal indentation rules are
 73364  73810   ** restored.
 73365  73811   *****************************************************************************/
 73366  73812       }
 73367  73813   
 73368  73814   #ifdef VDBE_PROFILE
 73369  73815       {
 73370         -      u64 elapsed = sqlite3Hwtime() - start;
 73371         -      pOp->cycles += elapsed;
        73816  +      u64 endTime = sqlite3Hwtime();
        73817  +      if( endTime>start ) pOp->cycles += endTime - start;
 73372  73818         pOp->cnt++;
 73373  73819       }
 73374  73820   #endif
 73375  73821   
 73376  73822       /* The following code adds nothing to the actual functionality
 73377  73823       ** of the program.  It is only here for testing and debugging.
 73378  73824       ** On the other hand, it does burn CPU cycles every time through
................................................................................
 73535  73981         rc = SQLITE_ERROR;
 73536  73982         sqlite3_finalize(p->pStmt);
 73537  73983         p->pStmt = 0;
 73538  73984       }else{
 73539  73985         p->iOffset = pC->aType[p->iCol + pC->nField];
 73540  73986         p->nByte = sqlite3VdbeSerialTypeLen(type);
 73541  73987         p->pCsr =  pC->pCursor;
 73542         -      sqlite3BtreeEnterCursor(p->pCsr);
 73543         -      sqlite3BtreeCacheOverflow(p->pCsr);
 73544         -      sqlite3BtreeLeaveCursor(p->pCsr);
        73988  +      sqlite3BtreeIncrblobCursor(p->pCsr);
 73545  73989       }
 73546  73990     }
 73547  73991   
 73548  73992     if( rc==SQLITE_ROW ){
 73549  73993       rc = SQLITE_OK;
 73550  73994     }else if( p->pStmt ){
 73551  73995       rc = sqlite3_finalize(p->pStmt);
................................................................................
 74277  74721         int nRead = nBuf - iBuf;
 74278  74722         if( (iStart + nRead) > pSorter->iWriteOff ){
 74279  74723           nRead = (int)(pSorter->iWriteOff - iStart);
 74280  74724         }
 74281  74725         rc = sqlite3OsRead(
 74282  74726             pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
 74283  74727         );
 74284         -      assert( rc!=SQLITE_IOERR_SHORT_READ );
 74285  74728       }
 74286  74729   
 74287  74730       if( rc==SQLITE_OK ){
 74288  74731         u64 nByte;                       /* Size of PMA in bytes */
 74289  74732         pIter->iEof = pSorter->iWriteOff;
 74290  74733         rc = vdbeSorterIterVarint(db, pIter, &nByte);
 74291  74734         pIter->iEof = pIter->iReadOff + nByte;
................................................................................
 74432  74875     for(p=pRecord; p; p=pNext){
 74433  74876       pNext = p->pNext;
 74434  74877       sqlite3DbFree(db, p);
 74435  74878     }
 74436  74879   }
 74437  74880   
 74438  74881   /*
 74439         -** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
        74882  +** Reset a sorting cursor back to its original empty state.
 74440  74883   */
 74441         -SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
 74442         -  VdbeSorter *pSorter = pCsr->pSorter;
 74443         -  if( pSorter ){
        74884  +SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
 74444  74885       if( pSorter->aIter ){
 74445  74886         int i;
 74446  74887         for(i=0; i<pSorter->nTree; i++){
 74447  74888           vdbeSorterIterZero(db, &pSorter->aIter[i]);
 74448  74889         }
 74449  74890         sqlite3DbFree(db, pSorter->aIter);
        74891  +    pSorter->aIter = 0;
 74450  74892       }
 74451  74893       if( pSorter->pTemp1 ){
 74452  74894         sqlite3OsCloseFree(pSorter->pTemp1);
        74895  +    pSorter->pTemp1 = 0;
 74453  74896       }
 74454  74897       vdbeSorterRecordFree(db, pSorter->pRecord);
        74898  +  pSorter->pRecord = 0;
        74899  +  pSorter->iWriteOff = 0;
        74900  +  pSorter->iReadOff = 0;
        74901  +  pSorter->nInMemory = 0;
        74902  +  pSorter->nTree = 0;
        74903  +  pSorter->nPMA = 0;
        74904  +  pSorter->aTree = 0;
        74905  +}
        74906  +
        74907  +
        74908  +/*
        74909  +** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
        74910  +*/
        74911  +SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
        74912  +  VdbeSorter *pSorter = pCsr->pSorter;
        74913  +  if( pSorter ){
        74914  +    sqlite3VdbeSorterReset(db, pSorter);
 74455  74915       sqlite3DbFree(db, pSorter->pUnpacked);
 74456  74916       sqlite3DbFree(db, pSorter);
 74457  74917       pCsr->pSorter = 0;
 74458  74918     }
 74459  74919   }
 74460  74920   
 74461  74921   /*
................................................................................
 74883  75343   */
 74884  75344   SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 74885  75345     VdbeSorter *pSorter = pCsr->pSorter;
 74886  75346     int rc;                         /* Return code */
 74887  75347   
 74888  75348     if( pSorter->aTree ){
 74889  75349       int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
 74890         -    int i;                        /* Index of aTree[] to recalculate */
 74891         -
 74892  75350       rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
 74893         -    for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
 74894         -      rc = vdbeSorterDoCompare(pCsr, i);
        75351  +    if( rc==SQLITE_OK ){
        75352  +      int i;                      /* Index of aTree[] to recalculate */
        75353  +      VdbeSorterIter *pIter1;     /* First iterator to compare */
        75354  +      VdbeSorterIter *pIter2;     /* Second iterator to compare */
        75355  +      u8 *pKey2;                  /* To pIter2->aKey, or 0 if record cached */
        75356  +
        75357  +      /* Find the first two iterators to compare. The one that was just
        75358  +      ** advanced (iPrev) and the one next to it in the array.  */
        75359  +      pIter1 = &pSorter->aIter[(iPrev & 0xFFFE)];
        75360  +      pIter2 = &pSorter->aIter[(iPrev | 0x0001)];
        75361  +      pKey2 = pIter2->aKey;
        75362  +
        75363  +      for(i=(pSorter->nTree+iPrev)/2; i>0; i=i/2){
        75364  +        /* Compare pIter1 and pIter2. Store the result in variable iRes. */
        75365  +        int iRes;
        75366  +        if( pIter1->pFile==0 ){
        75367  +          iRes = +1;
        75368  +        }else if( pIter2->pFile==0 ){
        75369  +          iRes = -1;
        75370  +        }else{
        75371  +          vdbeSorterCompare(pCsr, 0, 
        75372  +              pIter1->aKey, pIter1->nKey, pKey2, pIter2->nKey, &iRes
        75373  +          );
 74895  75374       }
 74896  75375   
        75376  +        /* If pIter1 contained the smaller value, set aTree[i] to its index.
        75377  +        ** Then set pIter2 to the next iterator to compare to pIter1. In this
        75378  +        ** case there is no cache of pIter2 in pSorter->pUnpacked, so set
        75379  +        ** pKey2 to point to the record belonging to pIter2.
        75380  +        **
        75381  +        ** Alternatively, if pIter2 contains the smaller of the two values,
        75382  +        ** set aTree[i] to its index and update pIter1. If vdbeSorterCompare()
        75383  +        ** was actually called above, then pSorter->pUnpacked now contains
        75384  +        ** a value equivalent to pIter2. So set pKey2 to NULL to prevent
        75385  +        ** vdbeSorterCompare() from decoding pIter2 again.  */
        75386  +        if( iRes<=0 ){
        75387  +          pSorter->aTree[i] = (int)(pIter1 - pSorter->aIter);
        75388  +          pIter2 = &pSorter->aIter[ pSorter->aTree[i ^ 0x0001] ];
        75389  +          pKey2 = pIter2->aKey;
        75390  +        }else{
        75391  +          if( pIter1->pFile ) pKey2 = 0;
        75392  +          pSorter->aTree[i] = (int)(pIter2 - pSorter->aIter);
        75393  +          pIter1 = &pSorter->aIter[ pSorter->aTree[i ^ 0x0001] ];
        75394  +        }
        75395  +
        75396  +      }
 74897  75397       *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
        75398  +    }
 74898  75399     }else{
 74899  75400       SorterRecord *pFree = pSorter->pRecord;
 74900  75401       pSorter->pRecord = pFree->pNext;
 74901  75402       pFree->pNext = 0;
 74902  75403       vdbeSorterRecordFree(db, pFree);
 74903  75404       *pbEof = !pSorter->pRecord;
 74904  75405       rc = SQLITE_OK;
................................................................................
 77126  77627   ** SELECT * FROM t1 WHERE a;
 77127  77628   ** SELECT a AS b FROM t1 WHERE b;
 77128  77629   ** SELECT * FROM t1 WHERE (select a from t1);
 77129  77630   */
 77130  77631   SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
 77131  77632     int op;
 77132  77633     pExpr = sqlite3ExprSkipCollate(pExpr);
        77634  +  if( pExpr->flags & EP_Generic ) return SQLITE_AFF_NONE;
 77133  77635     op = pExpr->op;
 77134  77636     if( op==TK_SELECT ){
 77135  77637       assert( pExpr->flags&EP_xIsSelect );
 77136  77638       return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
 77137  77639     }
 77138  77640   #ifndef SQLITE_OMIT_CAST
 77139  77641     if( op==TK_CAST ){
................................................................................
 77158  77660   ** Set the collating sequence for expression pExpr to be the collating
 77159  77661   ** sequence named by pToken.   Return a pointer to a new Expr node that
 77160  77662   ** implements the COLLATE operator.
 77161  77663   **
 77162  77664   ** If a memory allocation error occurs, that fact is recorded in pParse->db
 77163  77665   ** and the pExpr parameter is returned unchanged.
 77164  77666   */
 77165         -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
        77667  +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
        77668  +  Parse *pParse,           /* Parsing context */
        77669  +  Expr *pExpr,             /* Add the "COLLATE" clause to this expression */
        77670  +  const Token *pCollName   /* Name of collating sequence */
        77671  +){
 77166  77672     if( pCollName->n>0 ){
 77167  77673       Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
 77168  77674       if( pNew ){
 77169  77675         pNew->pLeft = pExpr;
 77170  77676         pNew->flags |= EP_Collate|EP_Skip;
 77171  77677         pExpr = pNew;
 77172  77678       }
................................................................................
 77211  77717   */
 77212  77718   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
 77213  77719     sqlite3 *db = pParse->db;
 77214  77720     CollSeq *pColl = 0;
 77215  77721     Expr *p = pExpr;
 77216  77722     while( p ){
 77217  77723       int op = p->op;
        77724  +    if( p->flags & EP_Generic ) break;
 77218  77725       if( op==TK_CAST || op==TK_UPLUS ){
 77219  77726         p = p->pLeft;
 77220  77727         continue;
 77221  77728       }
 77222  77729       if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
 77223  77730         pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
 77224  77731         break;
................................................................................
 78042  78549   SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
 78043  78550     ExprList *pNew;
 78044  78551     struct ExprList_item *pItem, *pOldItem;
 78045  78552     int i;
 78046  78553     if( p==0 ) return 0;
 78047  78554     pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
 78048  78555     if( pNew==0 ) return 0;
 78049         -  pNew->iECursor = 0;
 78050  78556     pNew->nExpr = i = p->nExpr;
 78051  78557     if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
 78052  78558     pNew->a = pItem = sqlite3DbMallocRaw(db,  i*sizeof(p->a[0]) );
 78053  78559     if( pItem==0 ){
 78054  78560       sqlite3DbFree(db, pNew);
 78055  78561       return 0;
 78056  78562     } 
................................................................................
 78155  78661     pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
 78156  78662     pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
 78157  78663     pNew->iLimit = 0;
 78158  78664     pNew->iOffset = 0;
 78159  78665     pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
 78160  78666     pNew->addrOpenEphm[0] = -1;
 78161  78667     pNew->addrOpenEphm[1] = -1;
 78162         -  pNew->addrOpenEphm[2] = -1;
 78163  78668     pNew->nSelectRow = p->nSelectRow;
 78164  78669     pNew->pWith = withDup(db, p->pWith);
 78165  78670     return pNew;
 78166  78671   }
 78167  78672   #else
 78168  78673   SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 78169  78674     assert( p==0 );
................................................................................
 78723  79228       u32 savedNQueryLoop = pParse->nQueryLoop;
 78724  79229       int rMayHaveNull = 0;
 78725  79230       eType = IN_INDEX_EPH;
 78726  79231       if( prNotFound ){
 78727  79232         *prNotFound = rMayHaveNull = ++pParse->nMem;
 78728  79233         sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
 78729  79234       }else{
 78730         -      testcase( pParse->nQueryLoop>0 );
 78731  79235         pParse->nQueryLoop = 0;
 78732  79236         if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
 78733  79237           eType = IN_INDEX_ROWID;
 78734  79238         }
 78735  79239       }
 78736  79240       sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
 78737  79241       pParse->nQueryLoop = savedNQueryLoop;
................................................................................
 78973  79477         break;
 78974  79478       }
 78975  79479     }
 78976  79480   
 78977  79481     if( testAddr>=0 ){
 78978  79482       sqlite3VdbeJumpHere(v, testAddr);
 78979  79483     }
 78980         -  sqlite3ExprCachePop(pParse, 1);
        79484  +  sqlite3ExprCachePop(pParse);
 78981  79485   
 78982  79486     return rReg;
 78983  79487   }
 78984  79488   #endif /* SQLITE_OMIT_SUBQUERY */
 78985  79489   
 78986  79490   #ifndef SQLITE_OMIT_SUBQUERY
 78987  79491   /*
................................................................................
 79108  79612         /* The OP_Found at the top of this branch jumps here when true, 
 79109  79613         ** causing the overall IN expression evaluation to fall through.
 79110  79614         */
 79111  79615         sqlite3VdbeJumpHere(v, j1);
 79112  79616       }
 79113  79617     }
 79114  79618     sqlite3ReleaseTempReg(pParse, r1);
 79115         -  sqlite3ExprCachePop(pParse, 1);
        79619  +  sqlite3ExprCachePop(pParse);
 79116  79620     VdbeComment((v, "end IN expr"));
 79117  79621   }
 79118  79622   #endif /* SQLITE_OMIT_SUBQUERY */
 79119  79623   
 79120  79624   /*
 79121  79625   ** Duplicate an 8-byte value
 79122  79626   */
................................................................................
 79291  79795       printf("PUSH to %d\n", pParse->iCacheLevel);
 79292  79796     }
 79293  79797   #endif
 79294  79798   }
 79295  79799   
 79296  79800   /*
 79297  79801   ** Remove from the column cache any entries that were added since the
 79298         -** the previous N Push operations.  In other words, restore the cache
 79299         -** to the state it was in N Pushes ago.
        79802  +** the previous sqlite3ExprCachePush operation.  In other words, restore
        79803  +** the cache to the state it was in prior the most recent Push.
 79300  79804   */
 79301         -SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
        79805  +SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse){
 79302  79806     int i;
 79303  79807     struct yColCache *p;
 79304         -  assert( N>0 );
 79305         -  assert( pParse->iCacheLevel>=N );
 79306         -  pParse->iCacheLevel -= N;
        79808  +  assert( pParse->iCacheLevel>=1 );
        79809  +  pParse->iCacheLevel--;
 79307  79810   #ifdef SQLITE_DEBUG
 79308  79811     if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
 79309  79812       printf("POP  to %d\n", pParse->iCacheLevel);
 79310  79813     }
 79311  79814   #endif
 79312  79815     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79313  79816       if( p->iReg && p->iLevel>pParse->iCacheLevel ){
................................................................................
 79428  79931   ** Generate code to move content from registers iFrom...iFrom+nReg-1
 79429  79932   ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
 79430  79933   */
 79431  79934   SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
 79432  79935     int i;
 79433  79936     struct yColCache *p;
 79434  79937     assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
 79435         -  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
        79938  +  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
 79436  79939     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79437  79940       int x = p->iReg;
 79438  79941       if( x>=iFrom && x<iFrom+nReg ){
 79439  79942         p->iReg += iTo-iFrom;
 79440  79943       }
 79441  79944     }
 79442  79945   }
................................................................................
 79777  80280           sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
 79778  80281           for(i=1; i<nFarg; i++){
 79779  80282             sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
 79780  80283             VdbeCoverage(v);
 79781  80284             sqlite3ExprCacheRemove(pParse, target, 1);
 79782  80285             sqlite3ExprCachePush(pParse);
 79783  80286             sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
 79784         -          sqlite3ExprCachePop(pParse, 1);
        80287  +          sqlite3ExprCachePop(pParse);
 79785  80288           }
 79786  80289           sqlite3VdbeResolveLabel(v, endCoalesce);
 79787  80290           break;
 79788  80291         }
 79789  80292   
 79790  80293         /* The UNLIKELY() function is a no-op.  The result is the value
 79791  80294         ** of the first argument.
................................................................................
 79831  80334                     pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
 79832  80335             }
 79833  80336           }
 79834  80337   
 79835  80338           sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
 79836  80339           sqlite3ExprCodeExprList(pParse, pFarg, r1, 
 79837  80340                                   SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
 79838         -        sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
        80341  +        sqlite3ExprCachePop(pParse);      /* Ticket 2ea2425d34be */
 79839  80342         }else{
 79840  80343           r1 = 0;
 79841  80344         }
 79842  80345   #ifndef SQLITE_OMIT_VIRTUALTABLE
 79843  80346         /* Possibly overload the function if the first argument is
 79844  80347         ** a virtual table column.
 79845  80348         **
................................................................................
 80051  80554           }
 80052  80555           nextCase = sqlite3VdbeMakeLabel(v);
 80053  80556           testcase( pTest->op==TK_COLUMN );
 80054  80557           sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
 80055  80558           testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
 80056  80559           sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
 80057  80560           sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
 80058         -        sqlite3ExprCachePop(pParse, 1);
        80561  +        sqlite3ExprCachePop(pParse);
 80059  80562           sqlite3VdbeResolveLabel(v, nextCase);
 80060  80563         }
 80061  80564         if( (nExpr&1)!=0 ){
 80062  80565           sqlite3ExprCachePush(pParse);
 80063  80566           sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
 80064         -        sqlite3ExprCachePop(pParse, 1);
        80567  +        sqlite3ExprCachePop(pParse);
 80065  80568         }else{
 80066  80569           sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 80067  80570         }
 80068  80571         assert( db->mallocFailed || pParse->nErr>0 
 80069  80572              || pParse->iCacheLevel==iCacheLevel );
 80070  80573         sqlite3VdbeResolveLabel(v, endLabel);
 80071  80574         break;
................................................................................
 80636  81139       case TK_AND: {
 80637  81140         int d2 = sqlite3VdbeMakeLabel(v);
 80638  81141         testcase( jumpIfNull==0 );
 80639  81142         sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
 80640  81143         sqlite3ExprCachePush(pParse);
 80641  81144         sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
 80642  81145         sqlite3VdbeResolveLabel(v, d2);
 80643         -      sqlite3ExprCachePop(pParse, 1);
        81146  +      sqlite3ExprCachePop(pParse);
 80644  81147         break;
 80645  81148       }
 80646  81149       case TK_OR: {
 80647  81150         testcase( jumpIfNull==0 );
 80648  81151         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 80649  81152         sqlite3ExprCachePush(pParse);
 80650  81153         sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
 80651         -      sqlite3ExprCachePop(pParse, 1);
        81154  +      sqlite3ExprCachePop(pParse);
 80652  81155         break;
 80653  81156       }
 80654  81157       case TK_NOT: {
 80655  81158         testcase( jumpIfNull==0 );
 80656  81159         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 80657  81160         break;
 80658  81161       }
................................................................................
 80790  81293   
 80791  81294     switch( pExpr->op ){
 80792  81295       case TK_AND: {
 80793  81296         testcase( jumpIfNull==0 );
 80794  81297         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 80795  81298         sqlite3ExprCachePush(pParse);
 80796  81299         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
 80797         -      sqlite3ExprCachePop(pParse, 1);
        81300  +      sqlite3ExprCachePop(pParse);
 80798  81301         break;
 80799  81302       }
 80800  81303       case TK_OR: {
 80801  81304         int d2 = sqlite3VdbeMakeLabel(v);
 80802  81305         testcase( jumpIfNull==0 );
 80803  81306         sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
 80804  81307         sqlite3ExprCachePush(pParse);
 80805  81308         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
 80806  81309         sqlite3VdbeResolveLabel(v, d2);
 80807         -      sqlite3ExprCachePop(pParse, 1);
        81310  +      sqlite3ExprCachePop(pParse);
 80808  81311         break;
 80809  81312       }
 80810  81313       case TK_NOT: {
 80811  81314         testcase( jumpIfNull==0 );
 80812  81315         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 80813  81316         break;
 80814  81317       }
................................................................................
 81464  81967     unsigned char const *zNew = sqlite3_value_text(argv[2]);
 81465  81968   
 81466  81969     unsigned const char *z;         /* Pointer to token */
 81467  81970     int n;                          /* Length of token z */
 81468  81971     int token;                      /* Type of token */
 81469  81972   
 81470  81973     UNUSED_PARAMETER(NotUsed);
        81974  +  if( zInput==0 || zOld==0 ) return;
 81471  81975     for(z=zInput; *z; z=z+n){
 81472  81976       n = sqlite3GetToken(z, &token);
 81473  81977       if( token==TK_REFERENCES ){
 81474  81978         char *zParent;
 81475  81979         do {
 81476  81980           z += n;
 81477  81981           n = sqlite3GetToken(z, &token);
................................................................................
 83179  83683       if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
 83180  83684       VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
 83181  83685       nCol = pIdx->nKeyCol;
 83182  83686       aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
 83183  83687       if( aGotoChng==0 ) continue;
 83184  83688   
 83185  83689       /* Populate the register containing the index name. */
 83186         -    if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
        83690  +    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
 83187  83691         zIdxName = pTab->zName;
 83188  83692       }else{
 83189  83693         zIdxName = pIdx->zName;
 83190  83694       }
 83191  83695       sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
 83192  83696   
 83193  83697       /*
................................................................................
 83548  84052   ** list of space separated integers. Read the first nOut of these into
 83549  84053   ** the array aOut[].
 83550  84054   */
 83551  84055   static void decodeIntArray(
 83552  84056     char *zIntArray,       /* String containing int array to decode */
 83553  84057     int nOut,              /* Number of slots in aOut[] */
 83554  84058     tRowcnt *aOut,         /* Store integers here */
        84059  +  LogEst *aLog,          /* Or, if aOut==0, here */
 83555  84060     Index *pIndex          /* Handle extra flags for this index, if not NULL */
 83556  84061   ){
 83557  84062     char *z = zIntArray;
 83558  84063     int c;
 83559  84064     int i;
 83560  84065     tRowcnt v;
 83561  84066   
................................................................................
 83566  84071   #endif
 83567  84072     for(i=0; *z && i<nOut; i++){
 83568  84073       v = 0;
 83569  84074       while( (c=z[0])>='0' && c<='9' ){
 83570  84075         v = v*10 + c - '0';
 83571  84076         z++;
 83572  84077       }
        84078  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
        84079  +    if( aOut ){
 83573  84080       aOut[i] = v;
        84081  +    }else
        84082  +#else
        84083  +    assert( aOut==0 );
        84084  +    UNUSED_PARAMETER(aOut);
        84085  +#endif
        84086  +    {
        84087  +      aLog[i] = sqlite3LogEst(v);
        84088  +    }
 83574  84089       if( *z==' ' ) z++;
 83575  84090     }
 83576  84091   #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
 83577  84092     assert( pIndex!=0 );
 83578  84093   #else
 83579  84094     if( pIndex )
 83580  84095   #endif
................................................................................
 83622  84137       pIndex = sqlite3PrimaryKeyIndex(pTable);
 83623  84138     }else{
 83624  84139       pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
 83625  84140     }
 83626  84141     z = argv[2];
 83627  84142   
 83628  84143     if( pIndex ){
 83629         -    decodeIntArray((char*)z, pIndex->nKeyCol+1, pIndex->aiRowEst, pIndex);
 83630         -    if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
        84144  +    decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex);
        84145  +    if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
 83631  84146     }else{
 83632  84147       Index fakeIdx;
 83633  84148       fakeIdx.szIdxRow = pTable->szTabRow;
 83634         -    decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
        84149  +    decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
 83635  84150       pTable->szTabRow = fakeIdx.szIdxRow;
 83636  84151     }
 83637  84152   
 83638  84153     return 0;
 83639  84154   }
 83640  84155   
 83641  84156   /*
................................................................................
 83819  84334       nCol = pIdx->nSampleCol;
 83820  84335       if( bStat3 && nCol>1 ) continue;
 83821  84336       if( pIdx!=pPrevIdx ){
 83822  84337         initAvgEq(pPrevIdx);
 83823  84338         pPrevIdx = pIdx;
 83824  84339       }
 83825  84340       pSample = &pIdx->aSample[pIdx->nSample];
 83826         -    decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
 83827         -    decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
 83828         -    decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
        84341  +    decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
        84342  +    decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
        84343  +    decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
 83829  84344   
 83830  84345       /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
 83831  84346       ** This is in case the sample record is corrupted. In that case, the
 83832  84347       ** sqlite3VdbeRecordCompare() may read up to two varints past the
 83833  84348       ** end of the allocated buffer before it realizes it is dealing with
 83834  84349       ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
 83835  84350       ** a buffer overread.  */
................................................................................
 85535  86050   }
 85536  86051   
 85537  86052   /*
 85538  86053   ** Return the PRIMARY KEY index of a table
 85539  86054   */
 85540  86055   SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
 85541  86056     Index *p;
 85542         -  for(p=pTab->pIndex; p && p->autoIndex!=2; p=p->pNext){}
        86057  +  for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
 85543  86058     return p;
 85544  86059   }
 85545  86060   
 85546  86061   /*
 85547  86062   ** Return the column of index pIdx that corresponds to table
 85548  86063   ** column iCol.  Return -1 if not found.
 85549  86064   */
................................................................................
 85683  86198       pParse->nErr++;
 85684  86199       goto begin_table_error;
 85685  86200     }
 85686  86201     pTable->zName = zName;
 85687  86202     pTable->iPKey = -1;
 85688  86203     pTable->pSchema = db->aDb[iDb].pSchema;
 85689  86204     pTable->nRef = 1;
 85690         -  pTable->nRowEst = 1048576;
        86205  +  pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
 85691  86206     assert( pParse->pNewTable==0 );
 85692  86207     pParse->pNewTable = pTable;
 85693  86208   
 85694  86209     /* If this is the magic sqlite_sequence table used by autoincrement,
 85695  86210     ** then record a pointer to this table in the main database structure
 85696  86211     ** so that INSERT can find the table easily.
 85697  86212     */
................................................................................
 86064  86579     }else{
 86065  86580       Vdbe *v = pParse->pVdbe;
 86066  86581       Index *p;
 86067  86582       if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
 86068  86583       p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
 86069  86584                              0, sortOrder, 0);
 86070  86585       if( p ){
 86071         -      p->autoIndex = 2;
        86586  +      p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
 86072  86587         if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
 86073  86588       }
 86074  86589       pList = 0;
 86075  86590     }
 86076  86591   
 86077  86592   primary_key_exit:
 86078  86593     sqlite3ExprListDelete(pParse->db, pList);
................................................................................
 86084  86599   */
 86085  86600   SQLITE_PRIVATE void sqlite3AddCheckConstraint(
 86086  86601     Parse *pParse,    /* Parsing context */
 86087  86602     Expr *pCheckExpr  /* The check expression */
 86088  86603   ){
 86089  86604   #ifndef SQLITE_OMIT_CHECK
 86090  86605     Table *pTab = pParse->pNewTable;
 86091         -  if( pTab && !IN_DECLARE_VTAB ){
        86606  +  sqlite3 *db = pParse->db;
        86607  +  if( pTab && !IN_DECLARE_VTAB
        86608  +   && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
        86609  +  ){
 86092  86610       pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
 86093  86611       if( pParse->constraintName.n ){
 86094  86612         sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
 86095  86613       }
 86096  86614     }else
 86097  86615   #endif
 86098  86616     {
................................................................................
 86436  86954       if( pList==0 ) return;
 86437  86955       pList->a[0].zName = sqlite3DbStrDup(pParse->db,
 86438  86956                                           pTab->aCol[pTab->iPKey].zName);
 86439  86957       pList->a[0].sortOrder = pParse->iPkSortOrder;
 86440  86958       assert( pParse->pNewTable==pTab );
 86441  86959       pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
 86442  86960       if( pPk==0 ) return;
 86443         -    pPk->autoIndex = 2;
        86961  +    pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
 86444  86962       pTab->iPKey = -1;
 86445  86963     }else{
 86446  86964       pPk = sqlite3PrimaryKeyIndex(pTab);
 86447  86965     }
 86448  86966     pPk->isCovering = 1;
 86449  86967     assert( pPk!=0 );
 86450  86968     nPk = pPk->nKeyCol;
................................................................................
 86459  86977     pPk->tnum = pTab->tnum;
 86460  86978   
 86461  86979     /* Update the in-memory representation of all UNIQUE indices by converting
 86462  86980     ** the final rowid column into one or more columns of the PRIMARY KEY.
 86463  86981     */
 86464  86982     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 86465  86983       int n;
 86466         -    if( pIdx->autoIndex==2 ) continue;
        86984  +    if( IsPrimaryKeyIndex(pIdx) ) continue;
 86467  86985       for(i=n=0; i<nPk; i++){
 86468  86986         if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
 86469  86987       }
 86470  86988       if( n==0 ){
 86471  86989         /* This index is a superset of the primary key */
 86472  86990         pIdx->nColumn = pIdx->nKeyCol;
 86473  86991         continue;
................................................................................
 87458  87976     ** records into the sorter. */
 87459  87977     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 87460  87978     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
 87461  87979     regRecord = sqlite3GetTempReg(pParse);
 87462  87980   
 87463  87981     sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
 87464  87982     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
 87465         -  sqlite3VdbeResolveLabel(v, iPartIdxLabel);
        87983  +  sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
 87466  87984     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
 87467  87985     sqlite3VdbeJumpHere(v, addr1);
 87468  87986     if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
 87469  87987     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
 87470  87988                       (char *)pKey, P4_KEYINFO);
 87471  87989     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
 87472  87990   
................................................................................
 87508  88026     char **ppExtra       /* Pointer to the "extra" space */
 87509  88027   ){
 87510  88028     Index *p;            /* Allocated index object */
 87511  88029     int nByte;           /* Bytes of space for Index object + arrays */
 87512  88030   
 87513  88031     nByte = ROUND8(sizeof(Index)) +              /* Index structure  */
 87514  88032             ROUND8(sizeof(char*)*nCol) +         /* Index.azColl     */
 87515         -          ROUND8(sizeof(tRowcnt)*(nCol+1) +    /* Index.aiRowEst   */
        88033  +          ROUND8(sizeof(LogEst)*(nCol+1) +     /* Index.aiRowLogEst   */
 87516  88034                    sizeof(i16)*nCol +            /* Index.aiColumn   */
 87517  88035                    sizeof(u8)*nCol);             /* Index.aSortOrder */
 87518  88036     p = sqlite3DbMallocZero(db, nByte + nExtra);
 87519  88037     if( p ){
 87520  88038       char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
 87521  88039       p->azColl = (char**)pExtra;      pExtra += ROUND8(sizeof(char*)*nCol);
 87522         -    p->aiRowEst = (tRowcnt*)pExtra;  pExtra += sizeof(tRowcnt)*(nCol+1);
        88040  +    p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
 87523  88041       p->aiColumn = (i16*)pExtra;      pExtra += sizeof(i16)*nCol;
 87524  88042       p->aSortOrder = (u8*)pExtra;
 87525  88043       p->nColumn = nCol;
 87526  88044       p->nKeyCol = nCol - 1;
 87527  88045       *ppExtra = ((char*)p) + nByte;
 87528  88046     }
 87529  88047     return p;
................................................................................
 87539  88057   **
 87540  88058   ** pList is a list of columns to be indexed.  pList will be NULL if this
 87541  88059   ** is a primary key or unique-constraint on the most recent column added
 87542  88060   ** to the table currently under construction.  
 87543  88061   **
 87544  88062   ** If the index is created successfully, return a pointer to the new Index
 87545  88063   ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
 87546         -** as the tables primary key (Index.autoIndex==2).
        88064  +** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY)
 87547  88065   */
 87548  88066   SQLITE_PRIVATE Index *sqlite3CreateIndex(
 87549  88067     Parse *pParse,     /* All information about this parse */
 87550  88068     Token *pName1,     /* First part of index name. May be NULL */
 87551  88069     Token *pName2,     /* Second part of index name. May be NULL */
 87552  88070     SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
 87553  88071     ExprList *pList,   /* A list of columns to be indexed */
................................................................................
 87746  88264     nName = sqlite3Strlen30(zName);
 87747  88265     nExtraCol = pPk ? pPk->nKeyCol : 1;
 87748  88266     pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
 87749  88267                                         nName + nExtra + 1, &zExtra);
 87750  88268     if( db->mallocFailed ){
 87751  88269       goto exit_create_index;
 87752  88270     }
 87753         -  assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
        88271  +  assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
 87754  88272     assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
 87755  88273     pIndex->zName = zExtra;
 87756  88274     zExtra += nName + 1;
 87757  88275     memcpy(pIndex->zName, zName, nName+1);
 87758  88276     pIndex->pTable = pTab;
 87759  88277     pIndex->onError = (u8)onError;
 87760  88278     pIndex->uniqNotNull = onError!=OE_None;
 87761         -  pIndex->autoIndex = (u8)(pName==0);
        88279  +  pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE;
 87762  88280     pIndex->pSchema = db->aDb[iDb].pSchema;
 87763  88281     pIndex->nKeyCol = pList->nExpr;
 87764  88282     if( pPIWhere ){
 87765  88283       sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
 87766  88284       pIndex->pPartIdxWhere = pPIWhere;
 87767  88285       pPIWhere = 0;
 87768  88286     }
................................................................................
 87866  88384       ** the constraint occur in different orders, then the constraints are
 87867  88385       ** considered distinct and both result in separate indices.
 87868  88386       */
 87869  88387       Index *pIdx;
 87870  88388       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 87871  88389         int k;
 87872  88390         assert( pIdx->onError!=OE_None );
 87873         -      assert( pIdx->autoIndex );
        88391  +      assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
 87874  88392         assert( pIndex->onError!=OE_None );
 87875  88393   
 87876  88394         if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
 87877  88395         for(k=0; k<pIdx->nKeyCol; k++){
 87878  88396           const char *z1;
 87879  88397           const char *z2;
 87880  88398           if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
................................................................................
 88027  88545   ** Fill the Index.aiRowEst[] array with default information - information
 88028  88546   ** to be used when we have not run the ANALYZE command.
 88029  88547   **
 88030  88548   ** aiRowEst[0] is suppose to contain the number of elements in the index.
 88031  88549   ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
 88032  88550   ** number of rows in the table that match any particular value of the
 88033  88551   ** first column of the index.  aiRowEst[2] is an estimate of the number
 88034         -** of rows that match any particular combiniation of the first 2 columns
        88552  +** of rows that match any particular combination of the first 2 columns
 88035  88553   ** of the index.  And so forth.  It must always be the case that
 88036  88554   *
 88037  88555   **           aiRowEst[N]<=aiRowEst[N-1]
 88038  88556   **           aiRowEst[N]>=1
 88039  88557   **
 88040  88558   ** Apart from that, we have little to go on besides intuition as to
 88041  88559   ** how aiRowEst[] should be initialized.  The numbers generated here
 88042  88560   ** are based on typical values found in actual indices.
 88043  88561   */
 88044  88562   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
 88045         -  tRowcnt *a = pIdx->aiRowEst;
        88563  +  /*                10,  9,  8,  7,  6 */
        88564  +  LogEst aVal[] = { 33, 32, 30, 28, 26 };
        88565  +  LogEst *a = pIdx->aiRowLogEst;
        88566  +  int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
 88046  88567     int i;
 88047         -  tRowcnt n;
 88048         -  assert( a!=0 );
 88049         -  a[0] = pIdx->pTable->nRowEst;
 88050         -  if( a[0]<10 ) a[0] = 10;
 88051         -  n = 10;
 88052         -  for(i=1; i<=pIdx->nKeyCol; i++){
 88053         -    a[i] = n;
 88054         -    if( n>5 ) n--;
        88568  +
        88569  +  /* Set the first entry (number of rows in the index) to the estimated 
        88570  +  ** number of rows in the table. Or 10, if the estimated number of rows 
        88571  +  ** in the table is less than that.  */
        88572  +  a[0] = pIdx->pTable->nRowLogEst;
        88573  +  if( a[0]<33 ) a[0] = 33;        assert( 33==sqlite3LogEst(10) );
        88574  +
        88575  +  /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
        88576  +  ** 6 and each subsequent value (if any) is 5.  */
        88577  +  memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
        88578  +  for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
        88579  +    a[i] = 23;                    assert( 23==sqlite3LogEst(5) );
 88055  88580     }
 88056         -  if( pIdx->onError!=OE_None ){
 88057         -    a[pIdx->nKeyCol] = 1;
 88058         -  }
        88581  +
        88582  +  assert( 0==sqlite3LogEst(1) );
        88583  +  if( pIdx->onError!=OE_None ) a[pIdx->nKeyCol] = 0;
 88059  88584   }
 88060  88585   
 88061  88586   /*
 88062  88587   ** This routine will drop an existing named index.  This routine
 88063  88588   ** implements the DROP INDEX statement.
 88064  88589   */
 88065  88590   SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
................................................................................
 88082  88607         sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
 88083  88608       }else{
 88084  88609         sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 88085  88610       }
 88086  88611       pParse->checkSchema = 1;
 88087  88612       goto exit_drop_index;
 88088  88613     }
 88089         -  if( pIndex->autoIndex ){
        88614  +  if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
 88090  88615       sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
 88091  88616         "or PRIMARY KEY constraint cannot be dropped", 0);
 88092  88617       goto exit_drop_index;
 88093  88618     }
 88094  88619     iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 88095  88620   #ifndef SQLITE_OMIT_AUTHORIZATION
 88096  88621     {
................................................................................
 88741  89266       if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
 88742  89267       sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
 88743  89268       sqlite3StrAccumAppend(&errMsg, ".", 1);
 88744  89269       sqlite3StrAccumAppendAll(&errMsg, zCol);
 88745  89270     }
 88746  89271     zErr = sqlite3StrAccumFinish(&errMsg);
 88747  89272     sqlite3HaltConstraint(pParse, 
 88748         -    (pIdx->autoIndex==2)?SQLITE_CONSTRAINT_PRIMARYKEY:SQLITE_CONSTRAINT_UNIQUE,
        89273  +    IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY 
        89274  +                            : SQLITE_CONSTRAINT_UNIQUE,
 88749  89275       onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
 88750  89276   }
 88751  89277   
 88752  89278   
 88753  89279   /*
 88754  89280   ** Code an OP_Halt due to non-unique rowid.
 88755  89281   */
................................................................................
 90235  90761       if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
 90236  90762       if( pIdx==pPk ) continue;
 90237  90763       VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
 90238  90764       r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
 90239  90765                                    &iPartIdxLabel, pPrior, r1);
 90240  90766       sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
 90241  90767                         pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
 90242         -    sqlite3VdbeResolveLabel(v, iPartIdxLabel);
        90768  +    sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
 90243  90769       pPrior = pIdx;
 90244  90770     }
 90245  90771   }
 90246  90772   
 90247  90773   /*
 90248  90774   ** Generate code that will assemble an index key and stores it in register
 90249  90775   ** regOut.  The key with be for index pIdx which is an index on pTab.
................................................................................
 90254  90780   ** Return a register number which is the first in a block of
 90255  90781   ** registers that holds the elements of the index key.  The
 90256  90782   ** block of registers has already been deallocated by the time
 90257  90783   ** this routine returns.
 90258  90784   **
 90259  90785   ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
 90260  90786   ** to that label if pIdx is a partial index that should be skipped.
        90787  +** The label should be resolved using sqlite3ResolvePartIdxLabel().
 90261  90788   ** A partial index should be skipped if its WHERE clause evaluates
 90262  90789   ** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
 90263  90790   ** will be set to zero which is an empty label that is ignored by
 90264         -** sqlite3VdbeResolveLabel().
        90791  +** sqlite3ResolvePartIdxLabel().
 90265  90792   **
 90266  90793   ** The pPrior and regPrior parameters are used to implement a cache to
 90267  90794   ** avoid unnecessary register loads.  If pPrior is not NULL, then it is
 90268  90795   ** a pointer to a different index for which an index key has just been
 90269  90796   ** computed into register regPrior.  If the current pIdx index is generating
 90270  90797   ** its key into the same sequence of registers and if pPrior and pIdx share
 90271  90798   ** a column in common, then the register corresponding to that column already
................................................................................
 90290  90817     int regBase;
 90291  90818     int nCol;
 90292  90819   
 90293  90820     if( piPartIdxLabel ){
 90294  90821       if( pIdx->pPartIdxWhere ){
 90295  90822         *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
 90296  90823         pParse->iPartIdxTab = iDataCur;
        90824  +      sqlite3ExprCachePush(pParse);
 90297  90825         sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
 90298  90826                            SQLITE_JUMPIFNULL);
 90299  90827       }else{
 90300  90828         *piPartIdxLabel = 0;
 90301  90829       }
 90302  90830     }
 90303  90831     nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
................................................................................
 90317  90845     }
 90318  90846     if( regOut ){
 90319  90847       sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
 90320  90848     }
 90321  90849     sqlite3ReleaseTempRange(pParse, regBase, nCol);
 90322  90850     return regBase;
 90323  90851   }
        90852  +
        90853  +/*
        90854  +** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
        90855  +** because it was a partial index, then this routine should be called to
        90856  +** resolve that label.
        90857  +*/
        90858  +SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
        90859  +  if( iLabel ){
        90860  +    sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
        90861  +    sqlite3ExprCachePop(pParse);
        90862  +  }
        90863  +}
 90324  90864   
 90325  90865   /************** End of delete.c **********************************************/
 90326  90866   /************** Begin file func.c ********************************************/
 90327  90867   /*
 90328  90868   ** 2002 February 23
 90329  90869   **
 90330  90870   ** The author disclaims copyright to this source code.  In place of
................................................................................
 91861  92401           zSep = ",";
 91862  92402           nSep = 1;
 91863  92403         }
 91864  92404         if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
 91865  92405       }
 91866  92406       zVal = (char*)sqlite3_value_text(argv[0]);
 91867  92407       nVal = sqlite3_value_bytes(argv[0]);
 91868         -    if( nVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
        92408  +    if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
 91869  92409     }
 91870  92410   }
 91871  92411   static void groupConcatFinalize(sqlite3_context *context){
 91872  92412     StrAccum *pAccum;
 91873  92413     pAccum = sqlite3_aggregate_context(context, 0);
 91874  92414     if( pAccum ){
 91875  92415       if( pAccum->accError==STRACCUM_TOOBIG ){
................................................................................
 92305  92845         /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
 92306  92846         ** of columns. If each indexed column corresponds to a foreign key
 92307  92847         ** column of pFKey, then this index is a winner.  */
 92308  92848   
 92309  92849         if( zKey==0 ){
 92310  92850           /* If zKey is NULL, then this foreign key is implicitly mapped to 
 92311  92851           ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
 92312         -        ** identified by the test (Index.autoIndex==2).  */
 92313         -        if( pIdx->autoIndex==2 ){
        92852  +        ** identified by the test.  */
        92853  +        if( IsPrimaryKeyIndex(pIdx) ){
 92314  92854             if( aiCol ){
 92315  92855               int i;
 92316  92856               for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
 92317  92857             }
 92318  92858             break;
 92319  92859           }
 92320  92860         }else{
................................................................................
 94051  94591             }
 94052  94592             break;
 94053  94593           }
 94054  94594         }
 94055  94595         if( j>=pTab->nCol ){
 94056  94596           if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
 94057  94597             ipkColumn = i;
        94598  +          bIdListInOrder = 0;
 94058  94599           }else{
 94059  94600             sqlite3ErrorMsg(pParse, "table %S has no column named %s",
 94060  94601                 pTabList, 0, pColumn->a[i].zName);
 94061  94602             pParse->checkSchema = 1;
 94062  94603             goto insert_cleanup;
 94063  94604           }
 94064  94605         }
................................................................................
 94899  95440             ** different from the old.
 94900  95441             **
 94901  95442             ** For a UNIQUE index, only conflict if the PRIMARY KEY values
 94902  95443             ** of the matched index row are different from the original PRIMARY
 94903  95444             ** KEY values of this row before the update.  */
 94904  95445             int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
 94905  95446             int op = OP_Ne;
 94906         -          int regCmp = (pIdx->autoIndex==2 ? regIdx : regR);
        95447  +          int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
 94907  95448     
 94908  95449             for(i=0; i<pPk->nKeyCol; i++){
 94909  95450               char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
 94910  95451               x = pPk->aiColumn[i];
 94911  95452               if( i==(pPk->nKeyCol-1) ){
 94912  95453                 addrJump = addrUniqueOk;
 94913  95454                 op = OP_Eq;
................................................................................
 95000  95541       if( pIdx->pPartIdxWhere ){
 95001  95542         sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
 95002  95543         VdbeCoverage(v);
 95003  95544       }
 95004  95545       sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
 95005  95546       pik_flags = 0;
 95006  95547       if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
 95007         -    if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
        95548  +    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
 95008  95549         assert( pParse->nested==0 );
 95009  95550         pik_flags |= OPFLAG_NCHANGE;
 95010  95551       }
 95011  95552       if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
 95012  95553     }
 95013  95554     if( !HasRowid(pTab) ) return;
 95014  95555     regData = regNewData + 1;
................................................................................
 95086  95627     }else{
 95087  95628       sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
 95088  95629     }
 95089  95630     if( piIdxCur ) *piIdxCur = iBase;
 95090  95631     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 95091  95632       int iIdxCur = iBase++;
 95092  95633       assert( pIdx->pSchema==pTab->pSchema );
 95093         -    if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
        95634  +    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){
 95094  95635         *piDataCur = iIdxCur;
 95095  95636       }
 95096  95637       if( aToOpen==0 || aToOpen[i+1] ){
 95097  95638         sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
 95098  95639         sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 95099  95640         VdbeComment((v, "%s", pIdx->zName));
 95100  95641       }
................................................................................
 95302  95843     if( pDest->nCol!=pSrc->nCol ){
 95303  95844       return 0;   /* Number of columns must be the same in tab1 and tab2 */
 95304  95845     }
 95305  95846     if( pDest->iPKey!=pSrc->iPKey ){
 95306  95847       return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
 95307  95848     }
 95308  95849     for(i=0; i<pDest->nCol; i++){
 95309         -    if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
        95850  +    Column *pDestCol = &pDest->aCol[i];
        95851  +    Column *pSrcCol = &pSrc->aCol[i];
        95852  +    if( pDestCol->affinity!=pSrcCol->affinity ){
 95310  95853         return 0;    /* Affinity must be the same on all columns */
 95311  95854       }
 95312         -    if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
        95855  +    if( !xferCompatibleCollation(pDestCol->zColl, pSrcCol->zColl) ){
 95313  95856         return 0;    /* Collating sequence must be the same on all columns */
 95314  95857       }
 95315         -    if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
        95858  +    if( pDestCol->notNull && !pSrcCol->notNull ){
 95316  95859         return 0;    /* tab2 must be NOT NULL if tab1 is */
        95860  +    }
        95861  +    /* Default values for second and subsequent columns need to match. */
        95862  +    if( i>0
        95863  +     && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0) 
        95864  +         || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0))
        95865  +    ){
        95866  +      return 0;    /* Default values must be the same for all columns */
 95317  95867       }
 95318  95868     }
 95319  95869     for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
 95320  95870       if( pDestIdx->onError!=OE_None ){
 95321  95871         destHasUniqueIdx = 1;
 95322  95872       }
 95323  95873       for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
................................................................................
 98328  98878       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
 98329  98879       for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
 98330  98880         Table *pTab = sqliteHashData(i);
 98331  98881         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
 98332  98882         sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
 98333  98883         sqlite3VdbeAddOp2(v, OP_Integer,
 98334  98884                              (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
 98335         -      sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
        98885  +      sqlite3VdbeAddOp2(v, OP_Integer, 
        98886  +          (int)sqlite3LogEstToInt(pTab->nRowLogEst), 4);
 98336  98887         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
 98337  98888         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 98338  98889           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
 98339  98890           sqlite3VdbeAddOp2(v, OP_Integer,
 98340  98891                                (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
 98341         -        sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
        98892  +        sqlite3VdbeAddOp2(v, OP_Integer, 
        98893  +            (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]), 4);
 98342  98894           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
 98343  98895         }
 98344  98896       }
 98345  98897     }
 98346  98898     break;
 98347  98899   
 98348  98900     case PragTyp_INDEX_INFO: if( zRight ){
................................................................................
 98715  99267         /* Do the b-tree integrity checks */
 98716  99268         sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
 98717  99269         sqlite3VdbeChangeP5(v, (u8)i);
 98718  99270         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
 98719  99271         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 98720  99272            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
 98721  99273            P4_DYNAMIC);
 98722         -      sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
        99274  +      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
 98723  99275         sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
 98724  99276         sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
 98725  99277         sqlite3VdbeJumpHere(v, addr);
 98726  99278   
 98727  99279         /* Make sure all the indices are constructed correctly.
 98728  99280         */
 98729  99281         for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
................................................................................
 98768  99320             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
 98769  99321             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
 98770  99322             sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
 98771  99323             jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
 98772  99324             sqlite3VdbeAddOp0(v, OP_Halt);
 98773  99325             sqlite3VdbeJumpHere(v, jmp4);
 98774  99326             sqlite3VdbeJumpHere(v, jmp2);
 98775         -          sqlite3VdbeResolveLabel(v, jmp3);
        99327  +          sqlite3ResolvePartIdxLabel(pParse, jmp3);
 98776  99328           }
 98777  99329           sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
 98778  99330           sqlite3VdbeJumpHere(v, loopTop-1);
 98779  99331   #ifndef SQLITE_OMIT_BTREECOUNT
 98780  99332           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, 
 98781  99333                        "wrong # of entries in index ", P4_STATIC);
 98782  99334           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
................................................................................
100053 100605   **    May you share freely, never taking more than you give.
100054 100606   **
100055 100607   *************************************************************************
100056 100608   ** This file contains C code routines that are called by the parser
100057 100609   ** to handle SELECT statements in SQLite.
100058 100610   */
100059 100611   
       100612  +/*
       100613  +** An instance of the following object is used to record information about
       100614  +** how to process the DISTINCT keyword, to simplify passing that information
       100615  +** into the selectInnerLoop() routine.
       100616  +*/
       100617  +typedef struct DistinctCtx DistinctCtx;
       100618  +struct DistinctCtx {
       100619  +  u8 isTnct;      /* True if the DISTINCT keyword is present */
       100620  +  u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
       100621  +  int tabTnct;    /* Ephemeral table used for DISTINCT processing */
       100622  +  int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
       100623  +};
       100624  +
       100625  +/*
       100626  +** An instance of the following object is used to record information about
       100627  +** the ORDER BY (or GROUP BY) clause of query is being coded.
       100628  +*/
       100629  +typedef struct SortCtx SortCtx;
       100630  +struct SortCtx {
       100631  +  ExprList *pOrderBy;   /* The ORDER BY (or GROUP BY clause) */
       100632  +  int nOBSat;           /* Number of ORDER BY terms satisfied by indices */
       100633  +  int iECursor;         /* Cursor number for the sorter */
       100634  +  int regReturn;        /* Register holding block-output return address */
       100635  +  int labelBkOut;       /* Start label for the block-output subroutine */
       100636  +  int addrSortIndex;    /* Address of the OP_SorterOpen or OP_OpenEphemeral */
       100637  +  u8 sortFlags;         /* Zero or more SORTFLAG_* bits */
       100638  +};
       100639  +#define SORTFLAG_UseSorter  0x01   /* Use SorterOpen instead of OpenEphemeral */
100060 100640   
100061 100641   /*
100062 100642   ** Delete all the content of a Select structure but do not deallocate
100063 100643   ** the select structure itself.
100064 100644   */
100065 100645   static void clearSelect(sqlite3 *db, Select *p){
100066 100646     sqlite3ExprListDelete(db, p->pEList);
................................................................................
100126 100706     pNew->selFlags = selFlags;
100127 100707     pNew->op = TK_SELECT;
100128 100708     pNew->pLimit = pLimit;
100129 100709     pNew->pOffset = pOffset;
100130 100710     assert( pOffset==0 || pLimit!=0 );
100131 100711     pNew->addrOpenEphm[0] = -1;
100132 100712     pNew->addrOpenEphm[1] = -1;
100133         -  pNew->addrOpenEphm[2] = -1;
100134 100713     if( db->mallocFailed ) {
100135 100714       clearSelect(db, pNew);
100136 100715       if( pNew!=&standin ) sqlite3DbFree(db, pNew);
100137 100716       pNew = 0;
100138 100717     }else{
100139 100718       assert( pNew->pSrc!=0 || pParse->nErr>0 );
100140 100719     }
................................................................................
100458 101037                        isOuter, &p->pWhere);
100459 101038         }
100460 101039       }
100461 101040     }
100462 101041     return 0;
100463 101042   }
100464 101043   
       101044  +/* Forward reference */
       101045  +static KeyInfo *keyInfoFromExprList(
       101046  +  Parse *pParse,       /* Parsing context */
       101047  +  ExprList *pList,     /* Form the KeyInfo object from this ExprList */
       101048  +  int iStart,          /* Begin with this column of pList */
       101049  +  int nExtra           /* Add this many extra columns to the end */
       101050  +);
       101051  +
100465 101052   /*
100466         -** Insert code into "v" that will push the record on the top of the
100467         -** stack into the sorter.
       101053  +** Insert code into "v" that will push the record in register regData
       101054  +** into the sorter.
100468 101055   */
100469 101056   static void pushOntoSorter(
100470 101057     Parse *pParse,         /* Parser context */
100471         -  ExprList *pOrderBy,    /* The ORDER BY clause */
       101058  +  SortCtx *pSort,        /* Information about the ORDER BY clause */
100472 101059     Select *pSelect,       /* The whole SELECT statement */
100473 101060     int regData            /* Register holding data to be sorted */
100474 101061   ){
100475 101062     Vdbe *v = pParse->pVdbe;
100476         -  int nExpr = pOrderBy->nExpr;
100477         -  int regBase = sqlite3GetTempRange(pParse, nExpr+2);
100478         -  int regRecord = sqlite3GetTempReg(pParse);
       101063  +  int nExpr = pSort->pOrderBy->nExpr;
       101064  +  int regRecord = ++pParse->nMem;
       101065  +  int regBase = pParse->nMem+1;
       101066  +  int nOBSat = pSort->nOBSat;
100479 101067     int op;
       101068  +
       101069  +  pParse->nMem += nExpr+2;        /* nExpr+2 registers allocated at regBase */
100480 101070     sqlite3ExprCacheClear(pParse);
100481         -  sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
100482         -  sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
       101071  +  sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, 0);
       101072  +  sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
100483 101073     sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
100484         -  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
100485         -  if( pSelect->selFlags & SF_UseSorter ){
       101074  +  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nExpr+2-nOBSat,regRecord);
       101075  +  if( nOBSat>0 ){
       101076  +    int regPrevKey;   /* The first nOBSat columns of the previous row */
       101077  +    int addrFirst;    /* Address of the OP_IfNot opcode */
       101078  +    int addrJmp;      /* Address of the OP_Jump opcode */
       101079  +    VdbeOp *pOp;      /* Opcode that opens the sorter */
       101080  +    int nKey;         /* Number of sorting key columns, including OP_Sequence */
       101081  +    KeyInfo *pKI;     /* Original KeyInfo on the sorter table */
       101082  +
       101083  +    regPrevKey = pParse->nMem+1;
       101084  +    pParse->nMem += pSort->nOBSat;
       101085  +    nKey = nExpr - pSort->nOBSat + 1;
       101086  +    addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr); VdbeCoverage(v);
       101087  +    sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
       101088  +    pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
       101089  +    if( pParse->db->mallocFailed ) return;
       101090  +    pOp->p2 = nKey + 1;
       101091  +    pKI = pOp->p4.pKeyInfo;
       101092  +    memset(pKI->aSortOrder, 0, pKI->nField); /* Makes OP_Jump below testable */
       101093  +    sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
       101094  +    pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat, 1);
       101095  +    addrJmp = sqlite3VdbeCurrentAddr(v);
       101096  +    sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
       101097  +    pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
       101098  +    pSort->regReturn = ++pParse->nMem;
       101099  +    sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
       101100  +    sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
       101101  +    sqlite3VdbeJumpHere(v, addrFirst);
       101102  +    sqlite3VdbeAddOp3(v, OP_Move, regBase, regPrevKey, pSort->nOBSat);
       101103  +    sqlite3VdbeJumpHere(v, addrJmp);
       101104  +  }
       101105  +  if( pSort->sortFlags & SORTFLAG_UseSorter ){
100486 101106       op = OP_SorterInsert;
100487 101107     }else{
100488 101108       op = OP_IdxInsert;
100489 101109     }
100490         -  sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
100491         -  sqlite3ReleaseTempReg(pParse, regRecord);
100492         -  sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
       101110  +  sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord);
100493 101111     if( pSelect->iLimit ){
100494 101112       int addr1, addr2;
100495 101113       int iLimit;
100496 101114       if( pSelect->iOffset ){
100497 101115         iLimit = pSelect->iOffset+1;
100498 101116       }else{
100499 101117         iLimit = pSelect->iLimit;
100500 101118       }
100501 101119       addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit); VdbeCoverage(v);
100502 101120       sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
100503 101121       addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
100504 101122       sqlite3VdbeJumpHere(v, addr1);
100505         -    sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
100506         -    sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
       101123  +    sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
       101124  +    sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor);
100507 101125       sqlite3VdbeJumpHere(v, addr2);
100508 101126     }
100509 101127   }
100510 101128   
100511 101129   /*
100512 101130   ** Add code to implement the OFFSET
100513 101131   */
100514 101132   static void codeOffset(
100515 101133     Vdbe *v,          /* Generate code into this VM */
100516 101134     int iOffset,      /* Register holding the offset counter */
100517 101135     int iContinue     /* Jump here to skip the current record */
100518 101136   ){
100519         -  if( iOffset>0 && iContinue!=0 ){
       101137  +  if( iOffset>0 ){
100520 101138       int addr;
100521 101139       sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
100522 101140       addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset); VdbeCoverage(v);
100523 101141       sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
100524 101142       VdbeComment((v, "skip OFFSET records"));
100525 101143       sqlite3VdbeJumpHere(v, addr);
100526 101144     }
................................................................................
100573 101191       return 1;
100574 101192     }else{
100575 101193       return 0;
100576 101194     }
100577 101195   }
100578 101196   #endif
100579 101197   
100580         -/*
100581         -** An instance of the following object is used to record information about
100582         -** how to process the DISTINCT keyword, to simplify passing that information
100583         -** into the selectInnerLoop() routine.
100584         -*/
100585         -typedef struct DistinctCtx DistinctCtx;
100586         -struct DistinctCtx {
100587         -  u8 isTnct;      /* True if the DISTINCT keyword is present */
100588         -  u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
100589         -  int tabTnct;    /* Ephemeral table used for DISTINCT processing */
100590         -  int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
100591         -};
100592         -
100593 101198   /*
100594 101199   ** This routine generates the code for the inside of the inner loop
100595 101200   ** of a SELECT.
100596 101201   **
100597 101202   ** If srcTab is negative, then the pEList expressions
100598 101203   ** are evaluated in order to get the data for this row.  If srcTab is
100599 101204   ** zero or more, then data is pulled from srcTab and pEList is used only 
................................................................................
100600 101205   ** to get number columns and the datatype for each column.
100601 101206   */
100602 101207   static void selectInnerLoop(
100603 101208     Parse *pParse,          /* The parser context */
100604 101209     Select *p,              /* The complete select statement being coded */
100605 101210     ExprList *pEList,       /* List of values being extracted */
100606 101211     int srcTab,             /* Pull data from this table */
100607         -  ExprList *pOrderBy,     /* If not NULL, sort results using this key */
       101212  +  SortCtx *pSort,         /* If not NULL, info on how to process ORDER BY */
100608 101213     DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
100609 101214     SelectDest *pDest,      /* How to dispose of the results */
100610 101215     int iContinue,          /* Jump here to continue with next row */
100611 101216     int iBreak              /* Jump here to break out of the inner loop */
100612 101217   ){
100613 101218     Vdbe *v = pParse->pVdbe;
100614 101219     int i;
................................................................................
100617 101222     int eDest = pDest->eDest;   /* How to dispose of results */
100618 101223     int iParm = pDest->iSDParm; /* First argument to disposal method */
100619 101224     int nResultCol;             /* Number of result columns */
100620 101225   
100621 101226     assert( v );
100622 101227     assert( pEList!=0 );
100623 101228     hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
100624         -  if( pOrderBy==0 && !hasDistinct ){
       101229  +  if( pSort && pSort->pOrderBy==0 ) pSort = 0;
       101230  +  if( pSort==0 && !hasDistinct ){
       101231  +    assert( iContinue!=0 );
100625 101232       codeOffset(v, p->iOffset, iContinue);
100626 101233     }
100627 101234   
100628 101235     /* Pull the requested columns.
100629 101236     */
100630 101237     nResultCol = pEList->nExpr;
100631 101238   
................................................................................
100707 101314   
100708 101315         default: {
100709 101316           assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
100710 101317           codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
100711 101318           break;
100712 101319         }
100713 101320       }
100714         -    if( pOrderBy==0 ){
       101321  +    if( pSort==0 ){
100715 101322         codeOffset(v, p->iOffset, iContinue);
100716 101323       }
100717 101324     }
100718 101325   
100719 101326     switch( eDest ){
100720 101327       /* In this mode, write each query result to the key of the temporary
100721 101328       ** table iParm.
................................................................................
100738 101345         sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
100739 101346         break;
100740 101347       }
100741 101348   #endif /* SQLITE_OMIT_COMPOUND_SELECT */
100742 101349   
100743 101350       /* Store the result as data using a unique key.
100744 101351       */
100745         -    case SRT_DistTable:
       101352  +    case SRT_Fifo:
       101353  +    case SRT_DistFifo:
100746 101354       case SRT_Table:
100747 101355       case SRT_EphemTab: {
100748 101356         int r1 = sqlite3GetTempReg(pParse);
100749 101357         testcase( eDest==SRT_Table );
100750 101358         testcase( eDest==SRT_EphemTab );
100751 101359         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
100752 101360   #ifndef SQLITE_OMIT_CTE
100753         -      if( eDest==SRT_DistTable ){
100754         -        /* If the destination is DistTable, then cursor (iParm+1) is open
       101361  +      if( eDest==SRT_DistFifo ){
       101362  +        /* If the destination is DistFifo, then cursor (iParm+1) is open
100755 101363           ** on an ephemeral index. If the current row is already present
100756 101364           ** in the index, do not write it to the output. If not, add the
100757 101365           ** current row to the index and proceed with writing it to the
100758 101366           ** output table as well.  */
100759 101367           int addr = sqlite3VdbeCurrentAddr(v) + 4;
100760 101368           sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
100761 101369           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
100762         -        assert( pOrderBy==0 );
       101370  +        assert( pSort==0 );
100763 101371         }
100764 101372   #endif
100765         -      if( pOrderBy ){
100766         -        pushOntoSorter(pParse, pOrderBy, p, r1);
       101373  +      if( pSort ){
       101374  +        pushOntoSorter(pParse, pSort, p, r1);
100767 101375         }else{
100768 101376           int r2 = sqlite3GetTempReg(pParse);
100769 101377           sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
100770 101378           sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
100771 101379           sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
100772 101380           sqlite3ReleaseTempReg(pParse, r2);
100773 101381         }
................................................................................
100780 101388       ** then there should be a single item on the stack.  Write this
100781 101389       ** item into the set table with bogus data.
100782 101390       */
100783 101391       case SRT_Set: {
100784 101392         assert( nResultCol==1 );
100785 101393         pDest->affSdst =
100786 101394                     sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
100787         -      if( pOrderBy ){
       101395  +      if( pSort ){
100788 101396           /* At first glance you would think we could optimize out the
100789 101397           ** ORDER BY in this case since the order of entries in the set
100790 101398           ** does not matter.  But there might be a LIMIT clause, in which
100791 101399           ** case the order does matter */
100792         -        pushOntoSorter(pParse, pOrderBy, p, regResult);
       101400  +        pushOntoSorter(pParse, pSort, p, regResult);
100793 101401         }else{
100794 101402           int r1 = sqlite3GetTempReg(pParse);
100795 101403           sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
100796 101404           sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
100797 101405           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
100798 101406           sqlite3ReleaseTempReg(pParse, r1);
100799 101407         }
................................................................................
100810 101418   
100811 101419       /* If this is a scalar select that is part of an expression, then
100812 101420       ** store the results in the appropriate memory cell and break out
100813 101421       ** of the scan loop.
100814 101422       */
100815 101423       case SRT_Mem: {
100816 101424         assert( nResultCol==1 );
100817         -      if( pOrderBy ){
100818         -        pushOntoSorter(pParse, pOrderBy, p, regResult);
       101425  +      if( pSort ){
       101426  +        pushOntoSorter(pParse, pSort, p, regResult);
100819 101427         }else{
100820 101428           sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
100821 101429           /* The LIMIT clause will jump out of the loop for us */
100822 101430         }
100823 101431         break;
100824 101432       }
100825 101433   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
100826 101434   
100827 101435       case SRT_Coroutine:       /* Send data to a co-routine */
100828 101436       case SRT_Output: {        /* Return the results */
100829 101437         testcase( eDest==SRT_Coroutine );
100830 101438         testcase( eDest==SRT_Output );
100831         -      if( pOrderBy ){
       101439  +      if( pSort ){
100832 101440           int r1 = sqlite3GetTempReg(pParse);
100833 101441           sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
100834         -        pushOntoSorter(pParse, pOrderBy, p, r1);
       101442  +        pushOntoSorter(pParse, pSort, p, r1);
100835 101443           sqlite3ReleaseTempReg(pParse, r1);
100836 101444         }else if( eDest==SRT_Coroutine ){
100837 101445           sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
100838 101446         }else{
100839 101447           sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
100840 101448           sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
100841 101449         }
................................................................................
100904 101512   #endif
100905 101513     }
100906 101514   
100907 101515     /* Jump to the end of the loop if the LIMIT is reached.  Except, if
100908 101516     ** there is a sorter, in which case the sorter has already limited
100909 101517     ** the output for us.
100910 101518     */
100911         -  if( pOrderBy==0 && p->iLimit ){
       101519  +  if( pSort==0 && p->iLimit ){
100912 101520       sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v);
100913 101521     }
100914 101522   }
100915 101523   
100916 101524   /*
100917 101525   ** Allocate a KeyInfo object sufficient for an index of N key columns and
100918 101526   ** X extra columns.
................................................................................
100975 101583   ** then the KeyInfo structure is appropriate for initializing a virtual
100976 101584   ** index to implement a DISTINCT test.
100977 101585   **
100978 101586   ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
100979 101587   ** function is responsible for seeing that this structure is eventually
100980 101588   ** freed.
100981 101589   */
100982         -static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList, int nExtra){
       101590  +static KeyInfo *keyInfoFromExprList(
       101591  +  Parse *pParse,       /* Parsing context */
       101592  +  ExprList *pList,     /* Form the KeyInfo object from this ExprList */
       101593  +  int iStart,          /* Begin with this column of pList */
       101594  +  int nExtra           /* Add this many extra columns to the end */
       101595  +){
100983 101596     int nExpr;
100984 101597     KeyInfo *pInfo;
100985 101598     struct ExprList_item *pItem;
100986 101599     sqlite3 *db = pParse->db;
100987 101600     int i;
100988 101601   
100989 101602     nExpr = pList->nExpr;
100990         -  pInfo = sqlite3KeyInfoAlloc(db, nExpr+nExtra, 1);
       101603  +  pInfo = sqlite3KeyInfoAlloc(db, nExpr+nExtra-iStart, 1);
100991 101604     if( pInfo ){
100992 101605       assert( sqlite3KeyInfoIsWriteable(pInfo) );
100993         -    for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
       101606  +    for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
100994 101607         CollSeq *pColl;
100995 101608         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
100996 101609         if( !pColl ) pColl = db->pDfltColl;
100997         -      pInfo->aColl[i] = pColl;
100998         -      pInfo->aSortOrder[i] = pItem->sortOrder;
       101610  +      pInfo->aColl[i-iStart] = pColl;
       101611  +      pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
100999 101612       }
101000 101613     }
101001 101614     return pInfo;
101002 101615   }
101003 101616   
101004 101617   #ifndef SQLITE_OMIT_COMPOUND_SELECT
101005 101618   /*
................................................................................
101093 101706   ** then the results were placed in a sorter.  After the loop is terminated
101094 101707   ** we need to run the sorter and output the results.  The following
101095 101708   ** routine generates the code needed to do that.
101096 101709   */
101097 101710   static void generateSortTail(
101098 101711     Parse *pParse,    /* Parsing context */
101099 101712     Select *p,        /* The SELECT statement */
101100         -  Vdbe *v,          /* Generate code into this VDBE */
       101713  +  SortCtx *pSort,   /* Information on the ORDER BY clause */
101101 101714     int nColumn,      /* Number of columns of data */
101102 101715     SelectDest *pDest /* Write the sorted results here */
101103 101716   ){
       101717  +  Vdbe *v = pParse->pVdbe;                     /* The prepared statement */
101104 101718     int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
101105 101719     int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
101106 101720     int addr;
       101721  +  int addrOnce = 0;
101107 101722     int iTab;
101108 101723     int pseudoTab = 0;
101109         -  ExprList *pOrderBy = p->pOrderBy;
101110         -
       101724  +  ExprList *pOrderBy = pSort->pOrderBy;
101111 101725     int eDest = pDest->eDest;
101112 101726     int iParm = pDest->iSDParm;
101113         -
101114 101727     int regRow;
101115 101728     int regRowid;
       101729  +  int nKey;
101116 101730   
101117         -  iTab = pOrderBy->iECursor;
       101731  +  if( pSort->labelBkOut ){
       101732  +    sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
       101733  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBreak);
       101734  +    sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
       101735  +    addrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
       101736  +  }
       101737  +  iTab = pSort->iECursor;
101118 101738     regRow = sqlite3GetTempReg(pParse);
101119 101739     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
101120 101740       pseudoTab = pParse->nTab++;
101121 101741       sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
101122 101742       regRowid = 0;
101123 101743     }else{
101124 101744       regRowid = sqlite3GetTempReg(pParse);
101125 101745     }
101126         -  if( p->selFlags & SF_UseSorter ){
       101746  +  nKey = pOrderBy->nExpr - pSort->nOBSat;
       101747  +  if( pSort->sortFlags & SORTFLAG_UseSorter ){
101127 101748       int regSortOut = ++pParse->nMem;
101128 101749       int ptab2 = pParse->nTab++;
101129         -    sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
       101750  +    sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, nKey+2);
       101751  +    if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
101130 101752       addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
101131 101753       VdbeCoverage(v);
101132 101754       codeOffset(v, p->iOffset, addrContinue);
101133 101755       sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
101134         -    sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
       101756  +    sqlite3VdbeAddOp3(v, OP_Column, ptab2, nKey+1, regRow);
101135 101757       sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
101136 101758     }else{
       101759  +    if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
101137 101760       addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
101138 101761       codeOffset(v, p->iOffset, addrContinue);
101139         -    sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
       101762  +    sqlite3VdbeAddOp3(v, OP_Column, iTab, nKey+1, regRow);
101140 101763     }
101141 101764     switch( eDest ){
101142 101765       case SRT_Table:
101143 101766       case SRT_EphemTab: {
101144 101767         testcase( eDest==SRT_Table );
101145 101768         testcase( eDest==SRT_EphemTab );
101146 101769         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
................................................................................
101187 101810     }
101188 101811     sqlite3ReleaseTempReg(pParse, regRow);
101189 101812     sqlite3ReleaseTempReg(pParse, regRowid);
101190 101813   
101191 101814     /* The bottom of the loop
101192 101815     */
101193 101816     sqlite3VdbeResolveLabel(v, addrContinue);
101194         -  if( p->selFlags & SF_UseSorter ){
       101817  +  if( pSort->sortFlags & SORTFLAG_UseSorter ){
101195 101818       sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
101196 101819     }else{
101197 101820       sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
101198 101821     }
       101822  +  if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
101199 101823     sqlite3VdbeResolveLabel(v, addrBreak);
101200         -  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
101201         -    sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
101202         -  }
101203 101824   }
101204 101825   
101205 101826   /*
101206 101827   ** Return a pointer to a string containing the 'declaration type' of the
101207 101828   ** expression pExpr. The string may be treated as static by the caller.
101208 101829   **
101209 101830   ** Also try to estimate the size of the returned value and return that
................................................................................
101655 102276       return 0;
101656 102277     }
101657 102278     /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
101658 102279     ** is disabled */
101659 102280     assert( db->lookaside.bEnabled==0 );
101660 102281     pTab->nRef = 1;
101661 102282     pTab->zName = 0;
101662         -  pTab->nRowEst = 1048576;
       102283  +  pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
101663 102284     selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
101664 102285     selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
101665 102286     pTab->iPKey = -1;
101666 102287     if( db->mallocFailed ){
101667 102288       sqlite3DeleteTable(db, pTab);
101668 102289       return 0;
101669 102290     }
................................................................................
101873 102494     Select *pSetup = p->pPrior;   /* The setup query */
101874 102495     int addrTop;                  /* Top of the loop */
101875 102496     int addrCont, addrBreak;      /* CONTINUE and BREAK addresses */
101876 102497     int iCurrent = 0;             /* The Current table */
101877 102498     int regCurrent;               /* Register holding Current table */
101878 102499     int iQueue;                   /* The Queue table */
101879 102500     int iDistinct = 0;            /* To ensure unique results if UNION */
101880         -  int eDest = SRT_Table;        /* How to write to Queue */
       102501  +  int eDest = SRT_Fifo;         /* How to write to Queue */
101881 102502     SelectDest destQueue;         /* SelectDest targetting the Queue table */
101882 102503     int i;                        /* Loop counter */
101883 102504     int rc;                       /* Result code */
101884 102505     ExprList *pOrderBy;           /* The ORDER BY clause */
101885 102506     Expr *pLimit, *pOffset;       /* Saved LIMIT and OFFSET */
101886 102507     int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
101887 102508   
................................................................................
101905 102526         iCurrent = pSrc->a[i].iCursor;
101906 102527         break;
101907 102528       }
101908 102529     }
101909 102530   
101910 102531     /* Allocate cursors numbers for Queue and Distinct.  The cursor number for
101911 102532     ** the Distinct table must be exactly one greater than Queue in order
101912         -  ** for the SRT_DistTable and SRT_DistQueue destinations to work. */
       102533  +  ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
101913 102534     iQueue = pParse->nTab++;
101914 102535     if( p->op==TK_UNION ){
101915         -    eDest = pOrderBy ? SRT_DistQueue : SRT_DistTable;
       102536  +    eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
101916 102537       iDistinct = pParse->nTab++;
101917 102538     }else{
101918         -    eDest = pOrderBy ? SRT_Queue : SRT_Table;
       102539  +    eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
101919 102540     }
101920 102541     sqlite3SelectDestInit(&destQueue, eDest, iQueue);
101921 102542   
101922 102543     /* Allocate cursors for Current, Queue, and Distinct. */
101923 102544     regCurrent = ++pParse->nMem;
101924 102545     sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
101925 102546     if( pOrderBy ){
................................................................................
101977 102598     p->pPrior = pSetup;
101978 102599   
101979 102600     /* Keep running the loop until the Queue is empty */
101980 102601     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
101981 102602     sqlite3VdbeResolveLabel(v, addrBreak);
101982 102603   
101983 102604   end_of_recursive_query:
       102605  +  sqlite3ExprListDelete(pParse->db, p->pOrderBy);
101984 102606     p->pOrderBy = pOrderBy;
101985 102607     p->pLimit = pLimit;
101986 102608     p->pOffset = pOffset;
101987 102609     return;
101988 102610   }
101989 102611   #endif /* SQLITE_OMIT_CTE */
101990 102612   
................................................................................
103793 104415   
103794 104416       assert( pFrom->pTab==0 );
103795 104417       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
103796 104418       if( pTab==0 ) return WRC_Abort;
103797 104419       pTab->nRef = 1;
103798 104420       pTab->zName = sqlite3DbStrDup(db, pCte->zName);
103799 104421       pTab->iPKey = -1;
103800         -    pTab->nRowEst = 1048576;
       104422  +    pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
103801 104423       pTab->tabFlags |= TF_Ephemeral;
103802 104424       pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
103803 104425       if( db->mallocFailed ) return SQLITE_NOMEM;
103804 104426       assert( pFrom->pSelect );
103805 104427   
103806 104428       /* Check if this is a recursive CTE. */
103807 104429       pSel = pFrom->pSelect;
................................................................................
103969 104591         pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
103970 104592         if( pTab==0 ) return WRC_Abort;
103971 104593         pTab->nRef = 1;
103972 104594         pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
103973 104595         while( pSel->pPrior ){ pSel = pSel->pPrior; }
103974 104596         selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
103975 104597         pTab->iPKey = -1;
103976         -      pTab->nRowEst = 1048576;
       104598  +      pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
103977 104599         pTab->tabFlags |= TF_Ephemeral;
103978 104600   #endif
103979 104601       }else{
103980 104602         /* An ordinary table or view name in the FROM clause */
103981 104603         assert( pFrom->pTab==0 );
103982 104604         pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
103983 104605         if( pTab==0 ) return WRC_Abort;
................................................................................
104348 104970         Expr *pE = pFunc->pExpr;
104349 104971         assert( !ExprHasProperty(pE, EP_xIsSelect) );
104350 104972         if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
104351 104973           sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
104352 104974              "argument");
104353 104975           pFunc->iDistinct = -1;
104354 104976         }else{
104355         -        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0);
       104977  +        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0, 0);
104356 104978           sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
104357 104979                             (char*)pKeyInfo, P4_KEYINFO);
104358 104980         }
104359 104981       }
104360 104982     }
104361 104983   }
104362 104984   
................................................................................
104464 105086   #ifndef SQLITE_OMIT_EXPLAIN
104465 105087   static void explainSimpleCount(
104466 105088     Parse *pParse,                  /* Parse context */
104467 105089     Table *pTab,                    /* Table being queried */
104468 105090     Index *pIdx                     /* Index used to optimize scan, or NULL */
104469 105091   ){
104470 105092     if( pParse->explain==2 ){
       105093  +    int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
104471 105094       char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
104472 105095           pTab->zName, 
104473         -        pIdx ? " USING COVERING INDEX " : "",
104474         -        pIdx ? pIdx->zName : ""
       105096  +        bCover ? " USING COVERING INDEX " : "",
       105097  +        bCover ? pIdx->zName : ""
104475 105098       );
104476 105099       sqlite3VdbeAddOp4(
104477 105100           pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
104478 105101       );
104479 105102     }
104480 105103   }
104481 105104   #else
................................................................................
104503 105126     int i, j;              /* Loop counters */
104504 105127     WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
104505 105128     Vdbe *v;               /* The virtual machine under construction */
104506 105129     int isAgg;             /* True for select lists like "count(*)" */
104507 105130     ExprList *pEList;      /* List of columns to extract. */
104508 105131     SrcList *pTabList;     /* List of tables to select from */
104509 105132     Expr *pWhere;          /* The WHERE clause.  May be NULL */
104510         -  ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
104511 105133     ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
104512 105134     Expr *pHaving;         /* The HAVING clause.  May be NULL */
104513 105135     int rc = 1;            /* Value to return from this function */
104514         -  int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
104515 105136     DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
       105137  +  SortCtx sSort;         /* Info on how to code the ORDER BY clause */
104516 105138     AggInfo sAggInfo;      /* Information used by aggregate queries */
104517 105139     int iEnd;              /* Address of the end of the query */
104518 105140     sqlite3 *db;           /* The database connection */
104519 105141   
104520 105142   #ifndef SQLITE_OMIT_EXPLAIN
104521 105143     int iRestoreSelectId = pParse->iSelectId;
104522 105144     pParse->iSelectId = pParse->iNextSelectId++;
................................................................................
104525 105147     db = pParse->db;
104526 105148     if( p==0 || db->mallocFailed || pParse->nErr ){
104527 105149       return 1;
104528 105150     }
104529 105151     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
104530 105152     memset(&sAggInfo, 0, sizeof(sAggInfo));
104531 105153   
       105154  +  assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
       105155  +  assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
       105156  +  assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
       105157  +  assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
104532 105158     if( IgnorableOrderby(pDest) ){
104533 105159       assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || 
104534         -           pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
       105160  +           pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
       105161  +           pDest->eDest==SRT_Queue  || pDest->eDest==SRT_DistFifo ||
       105162  +           pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
104535 105163       /* If ORDER BY makes no difference in the output then neither does
104536 105164       ** DISTINCT so it can be removed too. */
104537 105165       sqlite3ExprListDelete(db, p->pOrderBy);
104538 105166       p->pOrderBy = 0;
104539 105167       p->selFlags &= ~SF_Distinct;
104540 105168     }
104541 105169     sqlite3SelectPrep(pParse, p, 0);
104542         -  pOrderBy = p->pOrderBy;
       105170  +  memset(&sSort, 0, sizeof(sSort));
       105171  +  sSort.pOrderBy = p->pOrderBy;
104543 105172     pTabList = p->pSrc;
104544 105173     pEList = p->pEList;
104545 105174     if( pParse->nErr || db->mallocFailed ){
104546 105175       goto select_end;
104547 105176     }
104548 105177     isAgg = (p->selFlags & SF_Aggregate)!=0;
104549 105178     assert( pEList!=0 );
................................................................................
104613 105242         pItem->regReturn = ++pParse->nMem;
104614 105243         sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
104615 105244         VdbeComment((v, "%s", pItem->pTab->zName));
104616 105245         pItem->addrFillSub = addrTop;
104617 105246         sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
104618 105247         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104619 105248         sqlite3Select(pParse, pSub, &dest);
104620         -      pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
       105249  +      pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
104621 105250         pItem->viaCoroutine = 1;
104622 105251         pItem->regResult = dest.iSdst;
104623 105252         sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
104624 105253         sqlite3VdbeJumpHere(v, addrTop-1);
104625 105254         sqlite3ClearTempRegCache(pParse);
104626 105255       }else{
104627 105256         /* Generate a subroutine that will fill an ephemeral table with
................................................................................
104644 105273           VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
104645 105274         }else{
104646 105275           VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
104647 105276         }
104648 105277         sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
104649 105278         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104650 105279         sqlite3Select(pParse, pSub, &dest);
104651         -      pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
       105280  +      pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
104652 105281         if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
104653 105282         retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
104654 105283         VdbeComment((v, "end %s", pItem->pTab->zName));
104655 105284         sqlite3VdbeChangeP1(v, topAddr, retAddr);
104656 105285         sqlite3ClearTempRegCache(pParse);
104657 105286       }
104658 105287       if( /*pParse->nErr ||*/ db->mallocFailed ){
104659 105288         goto select_end;
104660 105289       }
104661 105290       pParse->nHeight -= sqlite3SelectExprHeight(p);
104662 105291       pTabList = p->pSrc;
104663 105292       if( !IgnorableOrderby(pDest) ){
104664         -      pOrderBy = p->pOrderBy;
       105293  +      sSort.pOrderBy = p->pOrderBy;
104665 105294       }
104666 105295     }
104667 105296     pEList = p->pEList;
104668 105297   #endif
104669 105298     pWhere = p->pWhere;
104670 105299     pGroupBy = p->pGroupBy;
104671 105300     pHaving = p->pHaving;
................................................................................
104677 105306     if( p->pPrior ){
104678 105307       rc = multiSelect(pParse, p, pDest);
104679 105308       explainSetInteger(pParse->iSelectId, iRestoreSelectId);
104680 105309       return rc;
104681 105310     }
104682 105311   #endif
104683 105312   
104684         -  /* If there is both a GROUP BY and an ORDER BY clause and they are
104685         -  ** identical, then disable the ORDER BY clause since the GROUP BY
104686         -  ** will cause elements to come out in the correct order.  This is
104687         -  ** an optimization - the correct answer should result regardless.
104688         -  ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
104689         -  ** to disable this optimization for testing purposes.
104690         -  */
104691         -  if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy, -1)==0
104692         -         && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
104693         -    pOrderBy = 0;
104694         -  }
104695         -
104696 105313     /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
104697 105314     ** if the select-list is the same as the ORDER BY list, then this query
104698 105315     ** can be rewritten as a GROUP BY. In other words, this:
104699 105316     **
104700 105317     **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
104701 105318     **
104702 105319     ** is transformed to:
................................................................................
104705 105322     **
104706 105323     ** The second form is preferred as a single index (or temp-table) may be 
104707 105324     ** used for both the ORDER BY and DISTINCT processing. As originally 
104708 105325     ** written the query must use a temp-table for at least one of the ORDER 
104709 105326     ** BY and DISTINCT, and an index or separate temp-table for the other.
104710 105327     */
104711 105328     if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
104712         -   && sqlite3ExprListCompare(pOrderBy, p->pEList, -1)==0
       105329  +   && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0
104713 105330     ){
104714 105331       p->selFlags &= ~SF_Distinct;
104715 105332       p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
104716 105333       pGroupBy = p->pGroupBy;
104717         -    pOrderBy = 0;
       105334  +    sSort.pOrderBy = 0;
104718 105335       /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
104719 105336       ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
104720 105337       ** original setting of the SF_Distinct flag, not the current setting */
104721 105338       assert( sDistinct.isTnct );
104722 105339     }
104723 105340   
104724 105341     /* If there is an ORDER BY clause, then this sorting
104725 105342     ** index might end up being unused if the data can be 
104726 105343     ** extracted in pre-sorted order.  If that is the case, then the
104727 105344     ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
104728 105345     ** we figure out that the sorting index is not needed.  The addrSortIndex
104729 105346     ** variable is used to facilitate that change.
104730 105347     */
104731         -  if( pOrderBy ){
       105348  +  if( sSort.pOrderBy ){
104732 105349       KeyInfo *pKeyInfo;
104733         -    pKeyInfo = keyInfoFromExprList(pParse, pOrderBy, 0);
104734         -    pOrderBy->iECursor = pParse->nTab++;
104735         -    p->addrOpenEphm[2] = addrSortIndex =
       105350  +    pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, 0);
       105351  +    sSort.iECursor = pParse->nTab++;
       105352  +    sSort.addrSortIndex =
104736 105353         sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
104737         -                           pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
       105354  +                           sSort.iECursor, sSort.pOrderBy->nExpr+2, 0,
104738 105355                              (char*)pKeyInfo, P4_KEYINFO);
104739 105356     }else{
104740         -    addrSortIndex = -1;
       105357  +    sSort.addrSortIndex = -1;
104741 105358     }
104742 105359   
104743 105360     /* If the output is destined for a temporary table, open that table.
104744 105361     */
104745 105362     if( pDest->eDest==SRT_EphemTab ){
104746 105363       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
104747 105364     }
104748 105365   
104749 105366     /* Set the limiter.
104750 105367     */
104751 105368     iEnd = sqlite3VdbeMakeLabel(v);
104752 105369     p->nSelectRow = LARGEST_INT64;
104753 105370     computeLimitRegisters(pParse, p, iEnd);
104754         -  if( p->iLimit==0 && addrSortIndex>=0 ){
104755         -    sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
104756         -    p->selFlags |= SF_UseSorter;
       105371  +  if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
       105372  +    sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen;
       105373  +    sSort.sortFlags |= SORTFLAG_UseSorter;
104757 105374     }
104758 105375   
104759 105376     /* Open a virtual index to use for the distinct set.
104760 105377     */
104761 105378     if( p->selFlags & SF_Distinct ){
104762 105379       sDistinct.tabTnct = pParse->nTab++;
104763 105380       sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
104764 105381                                   sDistinct.tabTnct, 0, 0,
104765         -                                (char*)keyInfoFromExprList(pParse, p->pEList, 0),
       105382  +                                (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
104766 105383                                   P4_KEYINFO);
104767 105384       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
104768 105385       sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
104769 105386     }else{
104770 105387       sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
104771 105388     }
104772 105389   
104773 105390     if( !isAgg && pGroupBy==0 ){
104774 105391       /* No aggregate functions and no GROUP BY clause */
104775 105392       u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
104776 105393   
104777 105394       /* Begin the database scan. */
104778         -    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, p->pEList,
104779         -                               wctrlFlags, 0);
       105395  +    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
       105396  +                               p->pEList, wctrlFlags, 0);
104780 105397       if( pWInfo==0 ) goto select_end;
104781 105398       if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
104782 105399         p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
104783 105400       }
104784 105401       if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
104785 105402         sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
104786 105403       }
104787         -    if( pOrderBy && sqlite3WhereIsOrdered(pWInfo) ) pOrderBy = 0;
       105404  +    if( sSort.pOrderBy ){
       105405  +      sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
       105406  +      if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
       105407  +        sSort.pOrderBy = 0;
       105408  +      }
       105409  +    }
104788 105410   
104789 105411       /* If sorting index that was created by a prior OP_OpenEphemeral 
104790 105412       ** instruction ended up not being needed, then change the OP_OpenEphemeral
104791 105413       ** into an OP_Noop.
104792 105414       */
104793         -    if( addrSortIndex>=0 && pOrderBy==0 ){
104794         -      sqlite3VdbeChangeToNoop(v, addrSortIndex);
104795         -      p->addrOpenEphm[2] = -1;
       105415  +    if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
       105416  +      sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
104796 105417       }
104797 105418   
104798 105419       /* Use the standard inner loop. */
104799         -    selectInnerLoop(pParse, p, pEList, -1, pOrderBy, &sDistinct, pDest,
       105420  +    selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest,
104800 105421                       sqlite3WhereContinueLabel(pWInfo),
104801 105422                       sqlite3WhereBreakLabel(pWInfo));
104802 105423   
104803 105424       /* End the database scan loop.
104804 105425       */
104805 105426       sqlite3WhereEnd(pWInfo);
104806 105427     }else{
................................................................................
104813 105434                           ** one row of the input to the aggregator has been
104814 105435                           ** processed */
104815 105436       int iAbortFlag;     /* Mem address which causes query abort if positive */
104816 105437       int groupBySort;    /* Rows come from source in GROUP BY order */
104817 105438       int addrEnd;        /* End of processing for this SELECT */
104818 105439       int sortPTab = 0;   /* Pseudotable used to decode sorting results */
104819 105440       int sortOut = 0;    /* Output register from the sorter */
       105441  +    int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
104820 105442   
104821 105443       /* Remove any and all aliases between the result set and the
104822 105444       ** GROUP BY clause.
104823 105445       */
104824 105446       if( pGroupBy ){
104825 105447         int k;                        /* Loop counter */
104826 105448         struct ExprList_item *pItem;  /* For looping over expression in a list */
................................................................................
104832 105454           pItem->u.x.iAlias = 0;
104833 105455         }
104834 105456         if( p->nSelectRow>100 ) p->nSelectRow = 100;
104835 105457       }else{
104836 105458         p->nSelectRow = 1;
104837 105459       }
104838 105460   
       105461  +
       105462  +    /* If there is both a GROUP BY and an ORDER BY clause and they are
       105463  +    ** identical, then it may be possible to disable the ORDER BY clause 
       105464  +    ** on the grounds that the GROUP BY will cause elements to come out 
       105465  +    ** in the correct order. It also may not - the GROUP BY may use a
       105466  +    ** database index that causes rows to be grouped together as required
       105467  +    ** but not actually sorted. Either way, record the fact that the
       105468  +    ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
       105469  +    ** variable.  */
       105470  +    if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
       105471  +      orderByGrp = 1;
       105472  +    }
104839 105473    
104840 105474       /* Create a label to jump to when we want to abort the query */
104841 105475       addrEnd = sqlite3VdbeMakeLabel(v);
104842 105476   
104843 105477       /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
104844 105478       ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
104845 105479       ** SELECT statement.
................................................................................
104848 105482       sNC.pParse = pParse;
104849 105483       sNC.pSrcList = pTabList;
104850 105484       sNC.pAggInfo = &sAggInfo;
104851 105485       sAggInfo.mnReg = pParse->nMem+1;
104852 105486       sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
104853 105487       sAggInfo.pGroupBy = pGroupBy;
104854 105488       sqlite3ExprAnalyzeAggList(&sNC, pEList);
104855         -    sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
       105489  +    sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
104856 105490       if( pHaving ){
104857 105491         sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
104858 105492       }
104859 105493       sAggInfo.nAccumulator = sAggInfo.nColumn;
104860 105494       for(i=0; i<sAggInfo.nFunc; i++){
104861 105495         assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
104862 105496         sNC.ncFlags |= NC_InAggFunc;
................................................................................
104882 105516   
104883 105517         /* If there is a GROUP BY clause we might need a sorting index to
104884 105518         ** implement it.  Allocate that sorting index now.  If it turns out
104885 105519         ** that we do not need it after all, the OP_SorterOpen instruction
104886 105520         ** will be converted into a Noop.  
104887 105521         */
104888 105522         sAggInfo.sortingIdx = pParse->nTab++;
104889         -      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0);
       105523  +      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0, 0);
104890 105524         addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
104891 105525             sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
104892 105526             0, (char*)pKeyInfo, P4_KEYINFO);
104893 105527   
104894 105528         /* Initialize memory locations used by GROUP BY aggregate processing
104895 105529         */
104896 105530         iUseFlag = ++pParse->nMem;
................................................................................
104912 105546         /* Begin a loop that will extract all source rows in GROUP BY order.
104913 105547         ** This might involve two separate loops with an OP_Sort in between, or
104914 105548         ** it might be a single loop that uses an index to extract information
104915 105549         ** in the right order to begin with.
104916 105550         */
104917 105551         sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
104918 105552         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 
104919         -                                 WHERE_GROUPBY, 0);
       105553  +          WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
       105554  +      );
104920 105555         if( pWInfo==0 ) goto select_end;
104921         -      if( sqlite3WhereIsOrdered(pWInfo) ){
       105556  +      if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
104922 105557           /* The optimizer is able to deliver rows in group by order so
104923 105558           ** we do not have to sort.  The OP_OpenEphemeral table will be
104924 105559           ** cancelled later because we still need to use the pKeyInfo
104925 105560           */
104926 105561           groupBySort = 0;
104927 105562         }else{
104928 105563           /* Rows are coming out in undetermined order.  We have to push
................................................................................
104977 105612           sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
104978 105613           sortOut = sqlite3GetTempReg(pParse);
104979 105614           sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
104980 105615           sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
104981 105616           VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
104982 105617           sAggInfo.useSortingIdx = 1;
104983 105618           sqlite3ExprCacheClear(pParse);
       105619  +
       105620  +      }
       105621  +
       105622  +      /* If the index or temporary table used by the GROUP BY sort
       105623  +      ** will naturally deliver rows in the order required by the ORDER BY
       105624  +      ** clause, cancel the ephemeral table open coded earlier.
       105625  +      **
       105626  +      ** This is an optimization - the correct answer should result regardless.
       105627  +      ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to 
       105628  +      ** disable this optimization for testing purposes.  */
       105629  +      if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder) 
       105630  +       && (groupBySort || sqlite3WhereIsSorted(pWInfo))
       105631  +      ){
       105632  +        sSort.pOrderBy = 0;
       105633  +        sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
104984 105634         }
104985 105635   
104986 105636         /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
104987 105637         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
104988 105638         ** Then compare the current GROUP BY terms against the GROUP BY terms
104989 105639         ** from the previous row currently stored in a0, a1, a2...
104990 105640         */
................................................................................
105065 105715         sqlite3VdbeResolveLabel(v, addrOutputRow);
105066 105716         addrOutputRow = sqlite3VdbeCurrentAddr(v);
105067 105717         sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
105068 105718         VdbeComment((v, "Groupby result generator entry point"));
105069 105719         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
105070 105720         finalizeAggFunctions(pParse, &sAggInfo);
105071 105721         sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
105072         -      selectInnerLoop(pParse, p, p->pEList, -1, pOrderBy,
       105722  +      selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
105073 105723                         &sDistinct, pDest,
105074 105724                         addrOutputRow+1, addrSetAbort);
105075 105725         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
105076 105726         VdbeComment((v, "end groupby result generator"));
105077 105727   
105078 105728         /* Generate a subroutine that will reset the group-by accumulator
105079 105729         */
................................................................................
105197 105847           pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
105198 105848           if( pWInfo==0 ){
105199 105849             sqlite3ExprListDelete(db, pDel);
105200 105850             goto select_end;
105201 105851           }
105202 105852           updateAccumulator(pParse, &sAggInfo);
105203 105853           assert( pMinMax==0 || pMinMax->nExpr==1 );
105204         -        if( sqlite3WhereIsOrdered(pWInfo) ){
       105854  +        if( sqlite3WhereIsOrdered(pWInfo)>0 ){
105205 105855             sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo));
105206 105856             VdbeComment((v, "%s() by index",
105207 105857                   (flag==WHERE_ORDERBY_MIN?"min":"max")));
105208 105858           }
105209 105859           sqlite3WhereEnd(pWInfo);
105210 105860           finalizeAggFunctions(pParse, &sAggInfo);
105211 105861         }
105212 105862   
105213         -      pOrderBy = 0;
       105863  +      sSort.pOrderBy = 0;
105214 105864         sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
105215 105865         selectInnerLoop(pParse, p, p->pEList, -1, 0, 0, 
105216 105866                         pDest, addrEnd, addrEnd);
105217 105867         sqlite3ExprListDelete(db, pDel);
105218 105868       }
105219 105869       sqlite3VdbeResolveLabel(v, addrEnd);
105220 105870       
................................................................................
105223 105873     if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
105224 105874       explainTempTable(pParse, "DISTINCT");
105225 105875     }
105226 105876   
105227 105877     /* If there is an ORDER BY clause, then we need to sort the results
105228 105878     ** and send them to the callback one by one.
105229 105879     */
105230         -  if( pOrderBy ){
105231         -    explainTempTable(pParse, "ORDER BY");
105232         -    generateSortTail(pParse, p, v, pEList->nExpr, pDest);
       105880  +  if( sSort.pOrderBy ){
       105881  +    explainTempTable(pParse, sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
       105882  +    generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
105233 105883     }
105234 105884   
105235 105885     /* Jump here to skip this query
105236 105886     */
105237 105887     sqlite3VdbeResolveLabel(v, iEnd);
105238 105888   
105239 105889     /* The SELECT was successfully coded.   Set the return code to 0
................................................................................
106864 107514     ** need to occur right after the database cursor.  So go ahead and
106865 107515     ** allocate enough space, just in case.
106866 107516     */
106867 107517     pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
106868 107518     iIdxCur = iDataCur+1;
106869 107519     pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
106870 107520     for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
106871         -    if( pIdx->autoIndex==2 && pPk!=0 ){
       107521  +    if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){
106872 107522         iDataCur = pParse->nTab;
106873 107523         pTabList->a[0].iCursor = iDataCur;
106874 107524       }
106875 107525       pParse->nTab++;
106876 107526     }
106877 107527   
106878 107528     /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].  
................................................................................
109063 109713         u16 nEq;               /* Number of equality constraints */
109064 109714         u16 nSkip;             /* Number of initial index columns to skip */
109065 109715         Index *pIndex;         /* Index used, or NULL */
109066 109716       } btree;
109067 109717       struct {               /* Information for virtual tables */
109068 109718         int idxNum;            /* Index number */
109069 109719         u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
109070         -      u8 isOrdered;          /* True if satisfies ORDER BY */
       109720  +      i8 isOrdered;          /* True if satisfies ORDER BY */
109071 109721         u16 omitMask;          /* Terms that may be omitted */
109072 109722         char *idxStr;          /* Index identifier string */
109073 109723       } vtab;
109074 109724     } u;
109075 109725     u32 wsFlags;          /* WHERE_* flags describing the plan */
109076 109726     u16 nLTerm;           /* Number of entries in aLTerm[] */
109077 109727     /**** whereLoopXfer() copies fields above ***********************/
................................................................................
109125 109775   ** at the end is the choosen query plan.
109126 109776   */
109127 109777   struct WherePath {
109128 109778     Bitmask maskLoop;     /* Bitmask of all WhereLoop objects in this path */
109129 109779     Bitmask revLoop;      /* aLoop[]s that should be reversed for ORDER BY */
109130 109780     LogEst nRow;          /* Estimated number of rows generated by this path */
109131 109781     LogEst rCost;         /* Total cost of this path */
109132         -  u8 isOrdered;         /* True if this path satisfies ORDER BY */
109133         -  u8 isOrderedValid;    /* True if the isOrdered field is valid */
       109782  +  i8 isOrdered;         /* No. of ORDER BY terms satisfied. -1 for unknown */
109134 109783     WhereLoop **aLoop;    /* Array of WhereLoop objects implementing this path */
109135 109784   };
109136 109785   
109137 109786   /*
109138 109787   ** The query generator uses an array of instances of this structure to
109139 109788   ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
109140 109789   ** clause subexpression is separated from the others by AND operators,
................................................................................
109340 109989     SrcList *pTabList;        /* List of tables in the join */
109341 109990     ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
109342 109991     ExprList *pResultSet;     /* Result set. DISTINCT operates on these */
109343 109992     WhereLoop *pLoops;        /* List of all WhereLoop objects */
109344 109993     Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
109345 109994     LogEst nRowOut;           /* Estimated number of output rows */
109346 109995     u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
109347         -  u8 bOBSat;                /* ORDER BY satisfied by indices */
       109996  +  i8 nOBSat;                /* Number of ORDER BY terms satisfied by indices */
       109997  +  u8 sorted;                /* True if really sorted (not just grouped) */
109348 109998     u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
109349 109999     u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
109350 110000     u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
109351 110001     u8 nLevel;                /* Number of nested loop */
109352 110002     int iTop;                 /* The very beginning of the WHERE loop */
109353 110003     int iContinue;            /* Jump here to continue with next record */
109354 110004     int iBreak;               /* Jump here to break out of the loop */
................................................................................
109424 110074   }
109425 110075   
109426 110076   /*
109427 110077   ** Return TRUE if the WHERE clause returns rows in ORDER BY order.
109428 110078   ** Return FALSE if the output needs to be sorted.
109429 110079   */
109430 110080   SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
109431         -  return pWInfo->bOBSat!=0;
       110081  +  return pWInfo->nOBSat;
109432 110082   }
109433 110083   
109434 110084   /*
109435 110085   ** Return the VDBE address or label to jump to in order to continue
109436 110086   ** immediately with the next row of a WHERE clause.
109437 110087   */
109438 110088   SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
       110089  +  assert( pWInfo->iContinue!=0 );
109439 110090     return pWInfo->iContinue;
109440 110091   }
109441 110092   
109442 110093   /*
109443 110094   ** Return the VDBE address or label to jump to in order to break
109444 110095   ** out of a WHERE loop.
109445 110096   */
................................................................................
109611 110262       }
109612 110263       pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
109613 110264     }
109614 110265     pTerm = &pWC->a[idx = pWC->nTerm++];
109615 110266     if( p && ExprHasProperty(p, EP_Unlikely) ){
109616 110267       pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
109617 110268     }else{
109618         -    pTerm->truthProb = -1;
       110269  +    pTerm->truthProb = 1;
109619 110270     }
109620 110271     pTerm->pExpr = sqlite3ExprSkipCollate(p);
109621 110272     pTerm->wtFlags = wtFlags;
109622 110273     pTerm->pWC = pWC;
109623 110274     pTerm->iParent = -1;
109624 110275     return idx;
109625 110276   }
................................................................................
111340 111991       aStat[1] = aSample[i].anEq[iCol];
111341 111992     }else{
111342 111993       tRowcnt iLower, iUpper, iGap;
111343 111994       if( i==0 ){
111344 111995         iLower = 0;
111345 111996         iUpper = aSample[0].anLt[iCol];
111346 111997       }else{
111347         -      iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
       111998  +      i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
       111999  +      iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
111348 112000         iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
111349 112001       }
111350 112002       aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
111351 112003       if( iLower>=iUpper ){
111352 112004         iGap = 0;
111353 112005       }else{
111354 112006         iGap = iUpper - iLower;
................................................................................
111358 112010       }else{
111359 112011         iGap = iGap/3;
111360 112012       }
111361 112013       aStat[0] = iLower + iGap;
111362 112014     }
111363 112015   }
111364 112016   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
       112017  +
       112018  +/*
       112019  +** If it is not NULL, pTerm is a term that provides an upper or lower
       112020  +** bound on a range scan. Without considering pTerm, it is estimated 
       112021  +** that the scan will visit nNew rows. This function returns the number
       112022  +** estimated to be visited after taking pTerm into account.
       112023  +**
       112024  +** If the user explicitly specified a likelihood() value for this term,
       112025  +** then the return value is the likelihood multiplied by the number of
       112026  +** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
       112027  +** has a likelihood of 0.50, and any other term a likelihood of 0.25.
       112028  +*/
       112029  +static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
       112030  +  LogEst nRet = nNew;
       112031  +  if( pTerm ){
       112032  +    if( pTerm->truthProb<=0 ){
       112033  +      nRet += pTerm->truthProb;
       112034  +    }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
       112035  +      nRet -= 20;        assert( 20==sqlite3LogEst(4) );
       112036  +    }
       112037  +  }
       112038  +  return nRet;
       112039  +}
111365 112040   
111366 112041   /*
111367 112042   ** This function is used to estimate the number of rows that will be visited
111368 112043   ** by scanning an index for a range of values. The range may have an upper
111369 112044   ** bound, a lower bound, or both. The WHERE clause terms that set the upper
111370 112045   ** and lower bounds are represented by pLower and pUpper respectively. For
111371 112046   ** example, assuming that index p is on t1(a):
................................................................................
111451 112126         aff = SQLITE_AFF_INTEGER;
111452 112127       }else{
111453 112128         aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
111454 112129       }
111455 112130       /* Determine iLower and iUpper using ($P) only. */
111456 112131       if( nEq==0 ){
111457 112132         iLower = 0;
111458         -      iUpper = p->aiRowEst[0];
       112133  +      iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
111459 112134       }else{
111460 112135         /* Note: this call could be optimized away - since the same values must 
111461 112136         ** have been requested when testing key $P in whereEqualScanEst().  */
111462 112137         whereKeyStats(pParse, p, pRec, 0, a);
111463 112138         iLower = a[0];
111464 112139         iUpper = a[0] + a[1];
111465 112140       }
................................................................................
111511 112186       }
111512 112187     }
111513 112188   #else
111514 112189     UNUSED_PARAMETER(pParse);
111515 112190     UNUSED_PARAMETER(pBuilder);
111516 112191   #endif
111517 112192     assert( pLower || pUpper );
111518         -  /* TUNING:  Each inequality constraint reduces the search space 4-fold.
111519         -  ** A BETWEEN operator, therefore, reduces the search space 16-fold */
111520         -  nNew = nOut;
111521         -  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
111522         -    nNew -= 20;        assert( 20==sqlite3LogEst(4) );
111523         -    nOut--;
111524         -  }
111525         -  if( pUpper ){
111526         -    nNew -= 20;        assert( 20==sqlite3LogEst(4) );
111527         -    nOut--;
111528         -  }
       112193  +  assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
       112194  +  nNew = whereRangeAdjust(pLower, nOut);
       112195  +  nNew = whereRangeAdjust(pUpper, nNew);
       112196  +
       112197  +  /* TUNING: If there is both an upper and lower limit, assume the range is
       112198  +  ** reduced by an additional 75%. This means that, by default, an open-ended
       112199  +  ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
       112200  +  ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
       112201  +  ** match 1/64 of the index. */ 
       112202  +  if( pLower && pUpper ) nNew -= 20;
       112203  +
       112204  +  nOut -= (pLower!=0) + (pUpper!=0);
111529 112205     if( nNew<10 ) nNew = 10;
111530 112206     if( nNew<nOut ) nOut = nNew;
111531 112207     pLoop->nOut = (LogEst)nOut;
111532 112208     return rc;
111533 112209   }
111534 112210   
111535 112211   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
................................................................................
111618 112294   static int whereInScanEst(
111619 112295     Parse *pParse,       /* Parsing & code generating context */
111620 112296     WhereLoopBuilder *pBuilder,
111621 112297     ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
111622 112298     tRowcnt *pnRow       /* Write the revised row estimate here */
111623 112299   ){
111624 112300     Index *p = pBuilder->pNew->u.btree.pIndex;
       112301  +  i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
111625 112302     int nRecValid = pBuilder->nRecValid;
111626 112303     int rc = SQLITE_OK;     /* Subfunction return code */
111627 112304     tRowcnt nEst;           /* Number of rows for a single term */
111628 112305     tRowcnt nRowEst = 0;    /* New estimate of the number of rows */
111629 112306     int i;                  /* Loop counter */
111630 112307   
111631 112308     assert( p->aSample!=0 );
111632 112309     for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
111633         -    nEst = p->aiRowEst[0];
       112310  +    nEst = nRow0;
111634 112311       rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
111635 112312       nRowEst += nEst;
111636 112313       pBuilder->nRecValid = nRecValid;
111637 112314     }
111638 112315   
111639 112316     if( rc==SQLITE_OK ){
111640         -    if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
       112317  +    if( nRowEst > nRow0 ) nRowEst = nRow0;
111641 112318       *pnRow = nRowEst;
111642 112319       WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst));
111643 112320     }
111644 112321     assert( pBuilder->nRecValid==nRecValid );
111645 112322     return rc;
111646 112323   }
111647 112324   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
................................................................................
112076 112753   
112077 112754       if( pItem->zAlias ){
112078 112755         zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
112079 112756       }
112080 112757       if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0
112081 112758        && ALWAYS(pLoop->u.btree.pIndex!=0)
112082 112759       ){
       112760  +      const char *zFmt;
       112761  +      Index *pIdx = pLoop->u.btree.pIndex;
112083 112762         char *zWhere = explainIndexRange(db, pLoop, pItem->pTab);
112084         -      zMsg = sqlite3MAppendf(db, zMsg,
112085         -               ((flags & WHERE_AUTO_INDEX) ? 
112086         -                   "%s USING AUTOMATIC %sINDEX%.0s%s" :
112087         -                   "%s USING %sINDEX %s%s"), 
112088         -               zMsg, ((flags & WHERE_IDX_ONLY) ? "COVERING " : ""),
112089         -               pLoop->u.btree.pIndex->zName, zWhere);
       112763  +      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
       112764  +      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
       112765  +        zFmt = zWhere ? "%s USING PRIMARY KEY%.0s%s" : "%s%.0s%s";
       112766  +      }else if( flags & WHERE_AUTO_INDEX ){
       112767  +        zFmt = "%s USING AUTOMATIC COVERING INDEX%.0s%s";
       112768  +      }else if( flags & WHERE_IDX_ONLY ){
       112769  +        zFmt = "%s USING COVERING INDEX %s%s";
       112770  +      }else{
       112771  +        zFmt = "%s USING INDEX %s%s";
       112772  +      }
       112773  +      zMsg = sqlite3MAppendf(db, zMsg, zFmt, zMsg, pIdx->zName, zWhere);
112090 112774         sqlite3DbFree(db, zWhere);
112091 112775       }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
112092 112776         zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
112093 112777   
112094 112778         if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
112095 112779           zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
112096 112780         }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
................................................................................
112225 112909           disableTerm(pLevel, pLoop->aLTerm[j]);
112226 112910         }
112227 112911       }
112228 112912       pLevel->op = OP_VNext;
112229 112913       pLevel->p1 = iCur;
112230 112914       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
112231 112915       sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
112232         -    sqlite3ExprCachePop(pParse, 1);
       112916  +    sqlite3ExprCachePop(pParse);
112233 112917     }else
112234 112918   #endif /* SQLITE_OMIT_VIRTUALTABLE */
112235 112919   
112236 112920     if( (pLoop->wsFlags & WHERE_IPK)!=0
112237 112921      && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
112238 112922     ){
112239 112923       /* Case 2:  We can directly reference a single row using an
................................................................................
112421 113105       ** was passed to this function to implement a "SELECT min(x) ..." 
112422 113106       ** query, then the caller will only allow the loop to run for
112423 113107       ** a single iteration. This means that the first row returned
112424 113108       ** should not have a NULL value stored in 'x'. If column 'x' is
112425 113109       ** the first one after the nEq equality constraints in the index,
112426 113110       ** this requires some special handling.
112427 113111       */
       113112  +    assert( pWInfo->pOrderBy==0
       113113  +         || pWInfo->pOrderBy->nExpr==1
       113114  +         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
112428 113115       if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
112429         -     && (pWInfo->bOBSat!=0)
       113116  +     && pWInfo->nOBSat>0
112430 113117        && (pIdx->nKeyCol>nEq)
112431 113118       ){
112432 113119         assert( pLoop->u.btree.nSkip==0 );
112433 113120         bSeekPastNull = 1;
112434 113121         nExtraReg = 1;
112435 113122       }
112436 113123   
................................................................................
112571 113258       if( omitTable ){
112572 113259         /* pIdx is a covering index.  No need to access the main table. */
112573 113260       }else if( HasRowid(pIdx->pTable) ){
112574 113261         iRowidReg = ++pParse->nMem;
112575 113262         sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
112576 113263         sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
112577 113264         sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
112578         -    }else{
       113265  +    }else if( iCur!=iIdxCur ){
112579 113266         Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
112580 113267         iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
112581 113268         for(j=0; j<pPk->nKeyCol; j++){
112582 113269           k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
112583 113270           sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
112584 113271         }
112585 113272         sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
................................................................................
112593 113280         pLevel->op = OP_Noop;
112594 113281       }else if( bRev ){
112595 113282         pLevel->op = OP_Prev;
112596 113283       }else{
112597 113284         pLevel->op = OP_Next;
112598 113285       }
112599 113286       pLevel->p1 = iIdxCur;
112600         -    assert( (WHERE_UNQ_WANTED>>16)==1 );
112601         -    pLevel->p3 = (pLoop->wsFlags>>16)&1;
       113287  +    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
112602 113288       if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
112603 113289         pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
112604 113290       }else{
112605 113291         assert( pLevel->p5==0 );
112606 113292       }
112607 113293     }else
112608 113294   
................................................................................
112642 113328       **
112643 113329       **       A: <loop body>                 # Return data, whatever.
112644 113330       **
112645 113331       **          Return     2                # Jump back to the Gosub
112646 113332       **
112647 113333       **       B: <after the loop>
112648 113334       **
       113335  +    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
       113336  +    ** use an ephermeral index instead of a RowSet to record the primary
       113337  +    ** keys of the rows we have already seen.
       113338  +    **
112649 113339       */
112650 113340       WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
112651 113341       SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
112652 113342       Index *pCov = 0;             /* Potential covering index (or NULL) */
112653 113343       int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
112654 113344   
112655 113345       int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
................................................................................
112656 113346       int regRowset = 0;                        /* Register for RowSet object */
112657 113347       int regRowid = 0;                         /* Register holding rowid */
112658 113348       int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
112659 113349       int iRetInit;                             /* Address of regReturn init */
112660 113350       int untestedTerms = 0;             /* Some terms not completely tested */
112661 113351       int ii;                            /* Loop counter */
112662 113352       Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
       113353  +    Table *pTab = pTabItem->pTab;
112663 113354      
112664 113355       pTerm = pLoop->aLTerm[0];
112665 113356       assert( pTerm!=0 );
112666 113357       assert( pTerm->eOperator & WO_OR );
112667 113358       assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
112668 113359       pOrWc = &pTerm->u.pOrInfo->wc;
112669 113360       pLevel->op = OP_Return;
................................................................................
112688 113379           memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
112689 113380         }
112690 113381       }else{
112691 113382         pOrTab = pWInfo->pTabList;
112692 113383       }
112693 113384   
112694 113385       /* Initialize the rowset register to contain NULL. An SQL NULL is 
112695         -    ** equivalent to an empty rowset.
       113386  +    ** equivalent to an empty rowset.  Or, create an ephermeral index
       113387  +    ** capable of holding primary keys in the case of a WITHOUT ROWID.
112696 113388       **
112697 113389       ** Also initialize regReturn to contain the address of the instruction 
112698 113390       ** immediately following the OP_Return at the bottom of the loop. This
112699 113391       ** is required in a few obscure LEFT JOIN cases where control jumps
112700 113392       ** over the top of the loop into the body of it. In this case the 
112701 113393       ** correct response for the end-of-loop code (the OP_Return) is to 
112702 113394       ** fall through to the next instruction, just as an OP_Next does if
112703 113395       ** called on an uninitialized cursor.
112704 113396       */
112705 113397       if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
       113398  +      if( HasRowid(pTab) ){
112706 113399         regRowset = ++pParse->nMem;
112707         -      regRowid = ++pParse->nMem;
112708 113400         sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
       113401  +      }else{
       113402  +        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
       113403  +        regRowset = pParse->nTab++;
       113404  +        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
       113405  +        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
       113406  +      }
       113407  +      regRowid = ++pParse->nMem;
112709 113408       }
112710 113409       iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
112711 113410   
112712 113411       /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
112713 113412       ** Then for every term xN, evaluate as the subexpression: xN AND z
112714 113413       ** That way, terms in y that are factored into the disjunction will
112715 113414       ** be picked up by the recursive calls to sqlite3WhereBegin() below.
................................................................................
112737 113436           pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
112738 113437         }
112739 113438         if( pAndExpr ){
112740 113439           pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
112741 113440         }
112742 113441       }
112743 113442   
       113443  +    /* Run a separate WHERE clause for each term of the OR clause.  After
       113444  +    ** eliminating duplicates from other WHERE clauses, the action for each
       113445  +    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
       113446  +    */
112744 113447       for(ii=0; ii<pOrWc->nTerm; ii++){
112745 113448         WhereTerm *pOrTerm = &pOrWc->a[ii];
112746 113449         if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
112747 113450           WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
112748         -        Expr *pOrExpr = pOrTerm->pExpr;
       113451  +        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
       113452  +        int j1 = 0;                     /* Address of jump operation */
112749 113453           if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
112750 113454             pAndExpr->pLeft = pOrExpr;
112751 113455             pOrExpr = pAndExpr;
112752 113456           }
112753 113457           /* Loop through table entries that match term pOrTerm. */
112754 113458           pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
112755 113459                           WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
................................................................................
112756 113460                           WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
112757 113461           assert( pSubWInfo || pParse->nErr || db->mallocFailed );
112758 113462           if( pSubWInfo ){
112759 113463             WhereLoop *pSubLoop;
112760 113464             explainOneScan(
112761 113465                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
112762 113466             );
       113467  +          /* This is the sub-WHERE clause body.  First skip over
       113468  +          ** duplicate rows from prior sub-WHERE clauses, and record the
       113469  +          ** rowid (or PRIMARY KEY) for the current row so that the same
       113470  +          ** row will be skipped in subsequent sub-WHERE clauses.
       113471  +          */
112763 113472             if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
112764         -            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
112765 113473               int r;
112766         -            r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
112767         -                                         regRowid, 0);
112768         -            sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
112769         -                                 sqlite3VdbeCurrentAddr(v)+2, r, iSet);
       113474  +            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
       113475  +            if( HasRowid(pTab) ){
       113476  +              r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
       113477  +              j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
       113478  +              VdbeCoverage(v);
       113479  +            }else{
       113480  +              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
       113481  +              int nPk = pPk->nKeyCol;
       113482  +              int iPk;
       113483  +
       113484  +              /* Read the PK into an array of temp registers. */
       113485  +              r = sqlite3GetTempRange(pParse, nPk);
       113486  +              for(iPk=0; iPk<nPk; iPk++){
       113487  +                int iCol = pPk->aiColumn[iPk];
       113488  +                sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
       113489  +              }
       113490  +
       113491  +              /* Check if the temp table already contains this key. If so,
       113492  +              ** the row has already been included in the result set and
       113493  +              ** can be ignored (by jumping past the Gosub below). Otherwise,
       113494  +              ** insert the key into the temp table and proceed with processing
       113495  +              ** the row.
       113496  +              **
       113497  +              ** Use some of the same optimizations as OP_RowSetTest: If iSet
       113498  +              ** is zero, assume that the key cannot already be present in
       113499  +              ** the temp table. And if iSet is -1, assume that there is no 
       113500  +              ** need to insert the key into the temp table, as it will never 
       113501  +              ** be tested for.  */ 
       113502  +              if( iSet ){
       113503  +                j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
112770 113504               VdbeCoverage(v);
112771 113505             }
       113506  +              if( iSet>=0 ){
       113507  +                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
       113508  +                sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
       113509  +                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
       113510  +              }
       113511  +
       113512  +              /* Release the array of temp registers */
       113513  +              sqlite3ReleaseTempRange(pParse, r, nPk);
       113514  +            }
       113515  +          }
       113516  +
       113517  +          /* Invoke the main loop body as a subroutine */
112772 113518             sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
       113519  +
       113520  +          /* Jump here (skipping the main loop body subroutine) if the
       113521  +          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
       113522  +          if( j1 ) sqlite3VdbeJumpHere(v, j1);
112773 113523   
112774 113524             /* The pSubWInfo->untestedTerms flag means that this OR term
112775 113525             ** contained one or more AND term from a notReady table.  The
112776 113526             ** terms from the notReady table could not be tested and will
112777 113527             ** need to be tested later.
112778 113528             */
112779 113529             if( pSubWInfo->untestedTerms ) untestedTerms = 1;
................................................................................
112790 113540             ** pCov to NULL to indicate that no candidate covering index will 
112791 113541             ** be available.
112792 113542             */
112793 113543             pSubLoop = pSubWInfo->a[0].pWLoop;
112794 113544             assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
112795 113545             if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
112796 113546              && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
       113547  +           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
112797 113548             ){
112798 113549               assert( pSubWInfo->a[0].iIdxCur==iCovCur );
112799 113550               pCov = pSubLoop->u.btree.pIndex;
112800 113551             }else{
112801 113552               pCov = 0;
112802 113553             }
112803 113554   
................................................................................
113092 113843         WhereLoop *p = pWInfo->pLoops;
113093 113844         pWInfo->pLoops = p->pNextLoop;
113094 113845         whereLoopDelete(db, p);
113095 113846       }
113096 113847       sqlite3DbFree(db, pWInfo);
113097 113848     }
113098 113849   }
       113850  +
       113851  +/*
       113852  +** Return TRUE if both of the following are true:
       113853  +**
       113854  +**   (1)  X has the same or lower cost that Y
       113855  +**   (2)  X is a proper subset of Y
       113856  +**
       113857  +** By "proper subset" we mean that X uses fewer WHERE clause terms
       113858  +** than Y and that every WHERE clause term used by X is also used
       113859  +** by Y.
       113860  +**
       113861  +** If X is a proper subset of Y then Y is a better choice and ought
       113862  +** to have a lower cost.  This routine returns TRUE when that cost 
       113863  +** relationship is inverted and needs to be adjusted.
       113864  +*/
       113865  +static int whereLoopCheaperProperSubset(
       113866  +  const WhereLoop *pX,       /* First WhereLoop to compare */
       113867  +  const WhereLoop *pY        /* Compare against this WhereLoop */
       113868  +){
       113869  +  int i, j;
       113870  +  if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */
       113871  +  if( pX->rRun >= pY->rRun ){
       113872  +    if( pX->rRun > pY->rRun ) return 0;    /* X costs more than Y */
       113873  +    if( pX->nOut > pY->nOut ) return 0;    /* X costs more than Y */
       113874  +  }
       113875  +  for(i=pX->nLTerm-1; i>=0; i--){
       113876  +    for(j=pY->nLTerm-1; j>=0; j--){
       113877  +      if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
       113878  +    }
       113879  +    if( j<0 ) return 0;  /* X not a subset of Y since term X[i] not used by Y */
       113880  +  }
       113881  +  return 1;  /* All conditions meet */
       113882  +}
       113883  +
       113884  +/*
       113885  +** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so
       113886  +** that:
       113887  +**
       113888  +**   (1) pTemplate costs less than any other WhereLoops that are a proper
       113889  +**       subset of pTemplate
       113890  +**
       113891  +**   (2) pTemplate costs more than any other WhereLoops for which pTemplate
       113892  +**       is a proper subset.
       113893  +**
       113894  +** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
       113895  +** WHERE clause terms than Y and that every WHERE clause term used by X is
       113896  +** also used by Y.
       113897  +**
       113898  +** This adjustment is omitted for SKIPSCAN loops.  In a SKIPSCAN loop, the
       113899  +** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE
       113900  +** clause terms covered, since some of the first nLTerm entries in aLTerm[]
       113901  +** will be NULL (because they are skipped).  That makes it more difficult
       113902  +** to compare the loops.  We could add extra code to do the comparison, and
       113903  +** perhaps we will someday.  But SKIPSCAN is sufficiently uncommon, and this
       113904  +** adjustment is sufficient minor, that it is very difficult to construct
       113905  +** a test case where the extra code would improve the query plan.  Better
       113906  +** to avoid the added complexity and just omit cost adjustments to SKIPSCAN
       113907  +** loops.
       113908  +*/
       113909  +static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
       113910  +  if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
       113911  +  if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return;
       113912  +  for(; p; p=p->pNextLoop){
       113913  +    if( p->iTab!=pTemplate->iTab ) continue;
       113914  +    if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
       113915  +    if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue;
       113916  +    if( whereLoopCheaperProperSubset(p, pTemplate) ){
       113917  +      /* Adjust pTemplate cost downward so that it is cheaper than its 
       113918  +      ** subset p */
       113919  +      pTemplate->rRun = p->rRun;
       113920  +      pTemplate->nOut = p->nOut - 1;
       113921  +    }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
       113922  +      /* Adjust pTemplate cost upward so that it is costlier than p since
       113923  +      ** pTemplate is a proper subset of p */
       113924  +      pTemplate->rRun = p->rRun;
       113925  +      pTemplate->nOut = p->nOut + 1;
       113926  +    }
       113927  +  }
       113928  +}
       113929  +
       113930  +/*
       113931  +** Search the list of WhereLoops in *ppPrev looking for one that can be
       113932  +** supplanted by pTemplate.
       113933  +**
       113934  +** Return NULL if the WhereLoop list contains an entry that can supplant
       113935  +** pTemplate, in other words if pTemplate does not belong on the list.
       113936  +**
       113937  +** If pX is a WhereLoop that pTemplate can supplant, then return the
       113938  +** link that points to pX.
       113939  +**
       113940  +** If pTemplate cannot supplant any existing element of the list but needs
       113941  +** to be added to the list, then return a pointer to the tail of the list.
       113942  +*/
       113943  +static WhereLoop **whereLoopFindLesser(
       113944  +  WhereLoop **ppPrev,
       113945  +  const WhereLoop *pTemplate
       113946  +){
       113947  +  WhereLoop *p;
       113948  +  for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
       113949  +    if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
       113950  +      /* If either the iTab or iSortIdx values for two WhereLoop are different
       113951  +      ** then those WhereLoops need to be considered separately.  Neither is
       113952  +      ** a candidate to replace the other. */
       113953  +      continue;
       113954  +    }
       113955  +    /* In the current implementation, the rSetup value is either zero
       113956  +    ** or the cost of building an automatic index (NlogN) and the NlogN
       113957  +    ** is the same for compatible WhereLoops. */
       113958  +    assert( p->rSetup==0 || pTemplate->rSetup==0 
       113959  +                 || p->rSetup==pTemplate->rSetup );
       113960  +
       113961  +    /* whereLoopAddBtree() always generates and inserts the automatic index
       113962  +    ** case first.  Hence compatible candidate WhereLoops never have a larger
       113963  +    ** rSetup. Call this SETUP-INVARIANT */
       113964  +    assert( p->rSetup>=pTemplate->rSetup );
       113965  +
       113966  +    /* If existing WhereLoop p is better than pTemplate, pTemplate can be
       113967  +    ** discarded.  WhereLoop p is better if:
       113968  +    **   (1)  p has no more dependencies than pTemplate, and
       113969  +    **   (2)  p has an equal or lower cost than pTemplate
       113970  +    */
       113971  +    if( (p->prereq & pTemplate->prereq)==p->prereq    /* (1)  */
       113972  +     && p->rSetup<=pTemplate->rSetup                  /* (2a) */
       113973  +     && p->rRun<=pTemplate->rRun                      /* (2b) */
       113974  +     && p->nOut<=pTemplate->nOut                      /* (2c) */
       113975  +    ){
       113976  +      return 0;  /* Discard pTemplate */
       113977  +    }
       113978  +
       113979  +    /* If pTemplate is always better than p, then cause p to be overwritten
       113980  +    ** with pTemplate.  pTemplate is better than p if:
       113981  +    **   (1)  pTemplate has no more dependences than p, and
       113982  +    **   (2)  pTemplate has an equal or lower cost than p.
       113983  +    */
       113984  +    if( (p->prereq & pTemplate->prereq)==pTemplate->prereq   /* (1)  */
       113985  +     && p->rRun>=pTemplate->rRun                             /* (2a) */
       113986  +     && p->nOut>=pTemplate->nOut                             /* (2b) */
       113987  +    ){
       113988  +      assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
       113989  +      break;   /* Cause p to be overwritten by pTemplate */
       113990  +    }
       113991  +  }
       113992  +  return ppPrev;
       113993  +}
113099 113994   
113100 113995   /*
113101 113996   ** Insert or replace a WhereLoop entry using the template supplied.
113102 113997   **
113103 113998   ** An existing WhereLoop entry might be overwritten if the new template
113104 113999   ** is better and has fewer dependencies.  Or the template will be ignored
113105 114000   ** and no insert will occur if an existing WhereLoop is faster and has
113106 114001   ** fewer dependencies than the template.  Otherwise a new WhereLoop is
113107 114002   ** added based on the template.
113108 114003   **
113109         -** If pBuilder->pOrSet is not NULL then we only care about only the
       114004  +** If pBuilder->pOrSet is not NULL then we care about only the
113110 114005   ** prerequisites and rRun and nOut costs of the N best loops.  That
113111 114006   ** information is gathered in the pBuilder->pOrSet object.  This special
113112 114007   ** processing mode is used only for OR clause processing.
113113 114008   **
113114 114009   ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
113115 114010   ** still might overwrite similar loops with the new template if the
113116         -** template is better.  Loops may be overwritten if the following 
       114011  +** new template is better.  Loops may be overwritten if the following 
113117 114012   ** conditions are met:
113118 114013   **
113119 114014   **    (1)  They have the same iTab.
113120 114015   **    (2)  They have the same iSortIdx.
113121 114016   **    (3)  The template has same or fewer dependencies than the current loop
113122 114017   **    (4)  The template has the same or lower cost than the current loop
113123         -**    (5)  The template uses more terms of the same index but has no additional
113124         -**         dependencies          
113125 114018   */
113126 114019   static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
113127         -  WhereLoop **ppPrev, *p, *pNext = 0;
       114020  +  WhereLoop **ppPrev, *p;
113128 114021     WhereInfo *pWInfo = pBuilder->pWInfo;
113129 114022     sqlite3 *db = pWInfo->pParse->db;
113130 114023   
113131 114024     /* If pBuilder->pOrSet is defined, then only keep track of the costs
113132 114025     ** and prereqs.
113133 114026     */
113134 114027     if( pBuilder->pOrSet!=0 ){
................................................................................
113143 114036         sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
113144 114037         whereLoopPrint(pTemplate, pBuilder->pWC);
113145 114038       }
113146 114039   #endif
113147 114040       return SQLITE_OK;
113148 114041     }
113149 114042   
113150         -  /* Search for an existing WhereLoop to overwrite, or which takes
113151         -  ** priority over pTemplate.
113152         -  */
113153         -  for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){
113154         -    if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
113155         -      /* If either the iTab or iSortIdx values for two WhereLoop are different
113156         -      ** then those WhereLoops need to be considered separately.  Neither is
113157         -      ** a candidate to replace the other. */
113158         -      continue;
113159         -    }
113160         -    /* In the current implementation, the rSetup value is either zero
113161         -    ** or the cost of building an automatic index (NlogN) and the NlogN
113162         -    ** is the same for compatible WhereLoops. */
113163         -    assert( p->rSetup==0 || pTemplate->rSetup==0 
113164         -                 || p->rSetup==pTemplate->rSetup );
113165         -
113166         -    /* whereLoopAddBtree() always generates and inserts the automatic index
113167         -    ** case first.  Hence compatible candidate WhereLoops never have a larger
113168         -    ** rSetup. Call this SETUP-INVARIANT */
113169         -    assert( p->rSetup>=pTemplate->rSetup );
113170         -
113171         -    if( (p->prereq & pTemplate->prereq)==p->prereq
113172         -     && p->rSetup<=pTemplate->rSetup
113173         -     && p->rRun<=pTemplate->rRun
113174         -     && p->nOut<=pTemplate->nOut
113175         -    ){
113176         -      /* This branch taken when p is equal or better than pTemplate in 
113177         -      ** all of (1) dependencies (2) setup-cost, (3) run-cost, and
113178         -      ** (4) number of output rows. */
113179         -      assert( p->rSetup==pTemplate->rSetup );
113180         -      if( p->prereq==pTemplate->prereq
113181         -       && p->nLTerm<pTemplate->nLTerm
113182         -       && (p->wsFlags & pTemplate->wsFlags & WHERE_INDEXED)!=0
113183         -       && (p->u.btree.pIndex==pTemplate->u.btree.pIndex
113184         -          || pTemplate->rRun+p->nLTerm<=p->rRun+pTemplate->nLTerm)
113185         -      ){
113186         -        /* Overwrite an existing WhereLoop with an similar one that uses
113187         -        ** more terms of the index */
113188         -        pNext = p->pNextLoop;
113189         -        break;
113190         -      }else{
113191         -        /* pTemplate is not helpful.
113192         -        ** Return without changing or adding anything */
113193         -        goto whereLoopInsert_noop;
113194         -      }
113195         -    }
113196         -    if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
113197         -     && p->rRun>=pTemplate->rRun
113198         -     && p->nOut>=pTemplate->nOut
113199         -    ){
113200         -      /* Overwrite an existing WhereLoop with a better one: one that is
113201         -      ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost
113202         -      ** or (4) number of output rows, and is no worse in any of those
113203         -      ** categories. */
113204         -      assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
113205         -      pNext = p->pNextLoop;
113206         -      break;
113207         -    }
       114043  +  /* Look for an existing WhereLoop to replace with pTemplate
       114044  +  */
       114045  +  whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
       114046  +  ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
       114047  +
       114048  +  if( ppPrev==0 ){
       114049  +    /* There already exists a WhereLoop on the list that is better
       114050  +    ** than pTemplate, so just ignore pTemplate */
       114051  +#if WHERETRACE_ENABLED /* 0x8 */
       114052  +    if( sqlite3WhereTrace & 0x8 ){
       114053  +      sqlite3DebugPrintf("ins-noop: ");
       114054  +      whereLoopPrint(pTemplate, pBuilder->pWC);
       114055  +    }
       114056  +#endif
       114057  +    return SQLITE_OK;  
       114058  +  }else{
       114059  +    p = *ppPrev;
113208 114060     }
113209 114061   
113210 114062     /* If we reach this point it means that either p[] should be overwritten
113211 114063     ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
113212 114064     ** WhereLoop and insert it.
113213 114065     */
113214 114066   #if WHERETRACE_ENABLED /* 0x8 */
................................................................................
113218 114070         whereLoopPrint(p, pBuilder->pWC);
113219 114071       }
113220 114072       sqlite3DebugPrintf("ins-new:  ");
113221 114073       whereLoopPrint(pTemplate, pBuilder->pWC);
113222 114074     }
113223 114075   #endif
113224 114076     if( p==0 ){
113225         -    p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
       114077  +    /* Allocate a new WhereLoop to add to the end of the list */
       114078  +    *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
113226 114079       if( p==0 ) return SQLITE_NOMEM;
113227 114080       whereLoopInit(p);
       114081  +    p->pNextLoop = 0;
       114082  +  }else{
       114083  +    /* We will be overwriting WhereLoop p[].  But before we do, first
       114084  +    ** go through the rest of the list and delete any other entries besides
       114085  +    ** p[] that are also supplated by pTemplate */
       114086  +    WhereLoop **ppTail = &p->pNextLoop;
       114087  +    WhereLoop *pToDel;
       114088  +    while( *ppTail ){
       114089  +      ppTail = whereLoopFindLesser(ppTail, pTemplate);
       114090  +      if( NEVER(ppTail==0) ) break;
       114091  +      pToDel = *ppTail;
       114092  +      if( pToDel==0 ) break;
       114093  +      *ppTail = pToDel->pNextLoop;
       114094  +#if WHERETRACE_ENABLED /* 0x8 */
       114095  +      if( sqlite3WhereTrace & 0x8 ){
       114096  +        sqlite3DebugPrintf("ins-del: ");
       114097  +        whereLoopPrint(pToDel, pBuilder->pWC);
       114098  +      }
       114099  +#endif
       114100  +      whereLoopDelete(db, pToDel);
       114101  +    }
113228 114102     }
113229 114103     whereLoopXfer(db, p, pTemplate);
113230         -  p->pNextLoop = pNext;
113231         -  *ppPrev = p;
113232 114104     if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
113233 114105       Index *pIndex = p->u.btree.pIndex;
113234 114106       if( pIndex && pIndex->tnum==0 ){
113235 114107         p->u.btree.pIndex = 0;
113236 114108       }
113237 114109     }
113238         -  return SQLITE_OK;
113239         -
113240         -  /* Jump here if the insert is a no-op */
113241         -whereLoopInsert_noop:
113242         -#if WHERETRACE_ENABLED /* 0x8 */
113243         -  if( sqlite3WhereTrace & 0x8 ){
113244         -    sqlite3DebugPrintf("ins-noop: ");
113245         -    whereLoopPrint(pTemplate, pBuilder->pWC);
113246         -  }
113247         -#endif
113248 114110     return SQLITE_OK;  
113249 114111   }
113250 114112   
113251 114113   /*
113252 114114   ** Adjust the WhereLoop.nOut value downward to account for terms of the
113253 114115   ** WHERE clause that reference the loop but which are not used by an
113254 114116   ** index.
................................................................................
113271 114133       if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
113272 114134       for(j=pLoop->nLTerm-1; j>=0; j--){
113273 114135         pX = pLoop->aLTerm[j];
113274 114136         if( pX==0 ) continue;
113275 114137         if( pX==pTerm ) break;
113276 114138         if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
113277 114139       }
113278         -    if( j<0 ) pLoop->nOut += pTerm->truthProb;
       114140  +    if( j<0 ){
       114141  +      pLoop->nOut += (pTerm->truthProb<=0 ? pTerm->truthProb : -1);
       114142  +    }
113279 114143     }
113280 114144   }
113281 114145   
113282 114146   /*
113283         -** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex.
113284         -** Try to match one more.
       114147  +** We have so far matched pBuilder->pNew->u.btree.nEq terms of the 
       114148  +** index pIndex. Try to match one more.
       114149  +**
       114150  +** When this function is called, pBuilder->pNew->nOut contains the 
       114151  +** number of rows expected to be visited by filtering using the nEq 
       114152  +** terms only. If it is modified, this value is restored before this 
       114153  +** function returns.
113285 114154   **
113286 114155   ** If pProbe->tnum==0, that means pIndex is a fake index used for the
113287 114156   ** INTEGER PRIMARY KEY.
113288 114157   */
113289 114158   static int whereLoopAddBtreeIndex(
113290 114159     WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
113291 114160     struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
................................................................................
113303 114172     u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
113304 114173     u16 saved_nEq;                  /* Original value of pNew->u.btree.nEq */
113305 114174     u16 saved_nSkip;                /* Original value of pNew->u.btree.nSkip */
113306 114175     u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
113307 114176     LogEst saved_nOut;              /* Original value of pNew->nOut */
113308 114177     int iCol;                       /* Index of the column in the table */
113309 114178     int rc = SQLITE_OK;             /* Return code */
113310         -  LogEst nRowEst;                 /* Estimated index selectivity */
113311 114179     LogEst rLogSize;                /* Logarithm of table size */
113312 114180     WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
113313 114181   
113314 114182     pNew = pBuilder->pNew;
113315 114183     if( db->mallocFailed ) return SQLITE_NOMEM;
113316 114184   
113317 114185     assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
................................................................................
113324 114192       opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
113325 114193     }
113326 114194     if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
113327 114195   
113328 114196     assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
113329 114197     if( pNew->u.btree.nEq < pProbe->nKeyCol ){
113330 114198       iCol = pProbe->aiColumn[pNew->u.btree.nEq];
113331         -    nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
113332         -    if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
113333 114199     }else{
113334 114200       iCol = -1;
113335         -    nRowEst = 0;
113336 114201     }
113337 114202     pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
113338 114203                           opMask, pProbe);
113339 114204     saved_nEq = pNew->u.btree.nEq;
113340 114205     saved_nSkip = pNew->u.btree.nSkip;
113341 114206     saved_nLTerm = pNew->nLTerm;
113342 114207     saved_wsFlags = pNew->wsFlags;
113343 114208     saved_prereq = pNew->prereq;
113344 114209     saved_nOut = pNew->nOut;
113345 114210     pNew->rSetup = 0;
113346         -  rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
       114211  +  rLogSize = estLog(pProbe->aiRowLogEst[0]);
113347 114212   
113348 114213     /* Consider using a skip-scan if there are no WHERE clause constraints
113349 114214     ** available for the left-most terms of the index, and if the average
113350         -  ** number of repeats in the left-most terms is at least 18.  The magic
113351         -  ** number 18 was found by experimentation to be the payoff point where
113352         -  ** skip-scan become faster than a full-scan.
113353         -  */
       114215  +  ** number of repeats in the left-most terms is at least 18. 
       114216  +  **
       114217  +  ** The magic number 18 is selected on the basis that scanning 17 rows
       114218  +  ** is almost always quicker than an index seek (even though if the index
       114219  +  ** contains fewer than 2^17 rows we assume otherwise in other parts of
       114220  +  ** the code). And, even if it is not, it should not be too much slower. 
       114221  +  ** On the other hand, the extra seeks could end up being significantly
       114222  +  ** more expensive.  */
       114223  +  assert( 42==sqlite3LogEst(18) );
113354 114224     if( pTerm==0
113355 114225      && saved_nEq==saved_nSkip
113356 114226      && saved_nEq+1<pProbe->nKeyCol
113357         -   && pProbe->aiRowEst[saved_nEq+1]>=18  /* TUNING: Minimum for skip-scan */
       114227  +   && pProbe->aiRowLogEst[saved_nEq+1]>=42  /* TUNING: Minimum for skip-scan */
113358 114228      && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
113359 114229     ){
113360 114230       LogEst nIter;
113361 114231       pNew->u.btree.nEq++;
113362 114232       pNew->u.btree.nSkip++;
113363 114233       pNew->aLTerm[pNew->nLTerm++] = 0;
113364 114234       pNew->wsFlags |= WHERE_SKIPSCAN;
113365         -    nIter = sqlite3LogEst(pProbe->aiRowEst[0]/pProbe->aiRowEst[saved_nEq+1]);
113366         -    pNew->rRun = rLogSize + nIter;
113367         -    pNew->nOut += nIter;
113368         -    whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter);
       114235  +    nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
       114236  +    pNew->nOut -= nIter;
       114237  +    whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
113369 114238       pNew->nOut = saved_nOut;
113370 114239     }
113371 114240     for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
       114241  +    u16 eOp = pTerm->eOperator;   /* Shorthand for pTerm->eOperator */
       114242  +    LogEst rCostIdx;
       114243  +    LogEst nOutUnadjusted;        /* nOut before IN() and WHERE adjustments */
113372 114244       int nIn = 0;
113373 114245   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113374 114246       int nRecValid = pBuilder->nRecValid;
113375 114247   #endif
113376         -    if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
       114248  +    if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
113377 114249        && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
113378 114250       ){
113379 114251         continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
113380 114252       }
113381 114253       if( pTerm->prereqRight & pNew->maskSelf ) continue;
113382 114254   
113383         -    assert( pNew->nOut==saved_nOut );
113384         -
113385 114255       pNew->wsFlags = saved_wsFlags;
113386 114256       pNew->u.btree.nEq = saved_nEq;
113387 114257       pNew->nLTerm = saved_nLTerm;
113388 114258       if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
113389 114259       pNew->aLTerm[pNew->nLTerm++] = pTerm;
113390 114260       pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
113391         -    pNew->rRun = rLogSize; /* Baseline cost is log2(N).  Adjustments below */
113392         -    if( pTerm->eOperator & WO_IN ){
       114261  +
       114262  +    assert( nInMul==0
       114263  +        || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 
       114264  +        || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 
       114265  +        || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 
       114266  +    );
       114267  +
       114268  +    if( eOp & WO_IN ){
113393 114269         Expr *pExpr = pTerm->pExpr;
113394 114270         pNew->wsFlags |= WHERE_COLUMN_IN;
113395 114271         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
113396 114272           /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
113397 114273           nIn = 46;  assert( 46==sqlite3LogEst(25) );
113398 114274         }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
113399 114275           /* "x IN (value, value, ...)" */
113400 114276           nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
113401 114277         }
113402         -      pNew->rRun += nIn;
113403         -      pNew->u.btree.nEq++;
113404         -      pNew->nOut = nRowEst + nInMul + nIn;
113405         -    }else if( pTerm->eOperator & (WO_EQ) ){
113406         -      assert(
113407         -        (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN|WHERE_SKIPSCAN))!=0
113408         -        || nInMul==0
113409         -      );
       114278  +      assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
       114279  +                        ** changes "x IN (?)" into "x=?". */
       114280  +
       114281  +    }else if( eOp & (WO_EQ) ){
113410 114282         pNew->wsFlags |= WHERE_COLUMN_EQ;
113411 114283         if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1)){
113412         -        assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 );
113413 114284           if( iCol>=0 && pProbe->onError==OE_None ){
113414 114285             pNew->wsFlags |= WHERE_UNQ_WANTED;
113415 114286           }else{
113416 114287             pNew->wsFlags |= WHERE_ONEROW;
113417 114288           }
113418 114289         }
113419         -      pNew->u.btree.nEq++;
113420         -      pNew->nOut = nRowEst + nInMul;
113421         -    }else if( pTerm->eOperator & (WO_ISNULL) ){
       114290  +    }else if( eOp & WO_ISNULL ){
113422 114291         pNew->wsFlags |= WHERE_COLUMN_NULL;
113423         -      pNew->u.btree.nEq++;
113424         -      /* TUNING: IS NULL selects 2 rows */
113425         -      nIn = 10;  assert( 10==sqlite3LogEst(2) );
113426         -      pNew->nOut = nRowEst + nInMul + nIn;
113427         -    }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
113428         -      testcase( pTerm->eOperator & WO_GT );
113429         -      testcase( pTerm->eOperator & WO_GE );
       114292  +    }else if( eOp & (WO_GT|WO_GE) ){
       114293  +      testcase( eOp & WO_GT );
       114294  +      testcase( eOp & WO_GE );
113430 114295         pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
113431 114296         pBtm = pTerm;
113432 114297         pTop = 0;
113433 114298       }else{
113434         -      assert( pTerm->eOperator & (WO_LT|WO_LE) );
113435         -      testcase( pTerm->eOperator & WO_LT );
113436         -      testcase( pTerm->eOperator & WO_LE );
       114299  +      assert( eOp & (WO_LT|WO_LE) );
       114300  +      testcase( eOp & WO_LT );
       114301  +      testcase( eOp & WO_LE );
113437 114302         pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
113438 114303         pTop = pTerm;
113439 114304         pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
113440 114305                        pNew->aLTerm[pNew->nLTerm-2] : 0;
113441 114306       }
113442         -    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
113443         -      /* Adjust nOut and rRun for STAT3 range values */
       114307  +
       114308  +    /* At this point pNew->nOut is set to the number of rows expected to
       114309  +    ** be visited by the index scan before considering term pTerm, or the
       114310  +    ** values of nIn and nInMul. In other words, assuming that all 
       114311  +    ** "x IN(...)" terms are replaced with "x = ?". This block updates
       114312  +    ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul).  */
113444 114313         assert( pNew->nOut==saved_nOut );
       114314  +    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
       114315  +      /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
       114316  +      ** data, using some other estimate.  */
113445 114317         whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
113446         -    }
       114318  +    }else{
       114319  +      int nEq = ++pNew->u.btree.nEq;
       114320  +      assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) );
       114321  +
       114322  +      assert( pNew->nOut==saved_nOut );
       114323  +      if( pTerm->truthProb<=0 && iCol>=0 ){
       114324  +        assert( (eOp & WO_IN) || nIn==0 );
       114325  +        testcase( eOp & WO_IN );
       114326  +        pNew->nOut += pTerm->truthProb;
       114327  +        pNew->nOut -= nIn;
       114328  +      }else{
113447 114329   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
       114330  +        tRowcnt nOut = 0;
113448 114331       if( nInMul==0 
113449 114332        && pProbe->nSample 
113450 114333        && pNew->u.btree.nEq<=pProbe->nSampleCol
113451 114334        && OptimizationEnabled(db, SQLITE_Stat3) 
       114335  +         && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
113452 114336       ){
113453 114337         Expr *pExpr = pTerm->pExpr;
113454         -      tRowcnt nOut = 0;
113455         -      if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
113456         -        testcase( pTerm->eOperator & WO_EQ );
113457         -        testcase( pTerm->eOperator & WO_ISNULL );
       114338  +          if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
       114339  +            testcase( eOp & WO_EQ );
       114340  +            testcase( eOp & WO_ISNULL );
113458 114341           rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
113459         -      }else if( (pTerm->eOperator & WO_IN)
113460         -             &&  !ExprHasProperty(pExpr, EP_xIsSelect)  ){
       114342  +          }else{
113461 114343           rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
113462 114344         }
113463         -      assert( nOut==0 || rc==SQLITE_OK );
       114345  +          assert( rc!=SQLITE_OK || nOut>0 );
       114346  +          if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
       114347  +          if( rc!=SQLITE_OK ) break;          /* Jump out of the pTerm loop */
113464 114348         if( nOut ){
113465 114349           pNew->nOut = sqlite3LogEst(nOut);
113466 114350           if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
       114351  +            pNew->nOut -= nIn;
113467 114352         }
113468 114353       }
       114354  +        if( nOut==0 )
113469 114355   #endif
       114356  +        {
       114357  +          pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
       114358  +          if( eOp & WO_ISNULL ){
       114359  +            /* TUNING: If there is no likelihood() value, assume that a 
       114360  +            ** "col IS NULL" expression matches twice as many rows 
       114361  +            ** as (col=?). */
       114362  +            pNew->nOut += 10;
       114363  +          }
       114364  +        }
       114365  +      }
       114366  +    }
       114367  +
       114368  +    /* Set rCostIdx to the cost of visiting selected rows in index. Add
       114369  +    ** it to pNew->rRun, which is currently set to the cost of the index
       114370  +    ** seek only. Then, if this is a non-covering index, add the cost of
       114371  +    ** visiting the rows in the main table.  */
       114372  +    rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
       114373  +    pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
113470 114374       if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
113471         -      /* Each row involves a step of the index, then a binary search of
113472         -      ** the main table */
113473         -      pNew->rRun =  sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
       114375  +      pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
113474 114376       }
113475         -    /* Step cost for each output row */
113476         -    pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
       114377  +
       114378  +    nOutUnadjusted = pNew->nOut;
       114379  +    pNew->rRun += nInMul + nIn;
       114380  +    pNew->nOut += nInMul + nIn;
113477 114381       whereLoopOutputAdjust(pBuilder->pWC, pNew);
113478 114382       rc = whereLoopInsert(pBuilder, pNew);
       114383  +
       114384  +    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
       114385  +      pNew->nOut = saved_nOut;
       114386  +    }else{
       114387  +      pNew->nOut = nOutUnadjusted;
       114388  +    }
       114389  +
113479 114390       if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
113480 114391        && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
113481 114392       ){
113482 114393         whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
113483 114394       }
113484 114395       pNew->nOut = saved_nOut;
113485 114396   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
................................................................................
113555 114466     return 0;
113556 114467   }
113557 114468   
113558 114469   /*
113559 114470   ** Add all WhereLoop objects for a single table of the join where the table
113560 114471   ** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
113561 114472   ** a b-tree table, not a virtual table.
       114473  +**
       114474  +** The costs (WhereLoop.rRun) of the b-tree loops added by this function
       114475  +** are calculated as follows:
       114476  +**
       114477  +** For a full scan, assuming the table (or index) contains nRow rows:
       114478  +**
       114479  +**     cost = nRow * 3.0                    // full-table scan
       114480  +**     cost = nRow * K                      // scan of covering index
       114481  +**     cost = nRow * (K+3.0)                // scan of non-covering index
       114482  +**
       114483  +** where K is a value between 1.1 and 3.0 set based on the relative 
       114484  +** estimated average size of the index and table records.
       114485  +**
       114486  +** For an index scan, where nVisit is the number of index rows visited
       114487  +** by the scan, and nSeek is the number of seek operations required on 
       114488  +** the index b-tree:
       114489  +**
       114490  +**     cost = nSeek * (log(nRow) + K * nVisit)          // covering index
       114491  +**     cost = nSeek * (log(nRow) + (K+3.0) * nVisit)    // non-covering index
       114492  +**
       114493  +** Normally, nSeek is 1. nSeek values greater than 1 come about if the 
       114494  +** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when 
       114495  +** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
113562 114496   */
113563 114497   static int whereLoopAddBtree(
113564 114498     WhereLoopBuilder *pBuilder, /* WHERE clause information */
113565 114499     Bitmask mExtra              /* Extra prerequesites for using this table */
113566 114500   ){
113567 114501     WhereInfo *pWInfo;          /* WHERE analysis context */
113568 114502     Index *pProbe;              /* An index we are evaluating */
113569 114503     Index sPk;                  /* A fake index object for the primary key */
113570         -  tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
       114504  +  LogEst aiRowEstPk[2];       /* The aiRowLogEst[] value for the sPk index */
113571 114505     i16 aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
113572 114506     SrcList *pTabList;          /* The FROM clause */
113573 114507     struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
113574 114508     WhereLoop *pNew;            /* Template WhereLoop object */
113575 114509     int rc = SQLITE_OK;         /* Return code */
113576 114510     int iSortIdx = 1;           /* Index number */
113577 114511     int b;                      /* A boolean value */
................................................................................
113598 114532       ** variable sPk to represent the rowid primary key index.  Make this
113599 114533       ** fake index the first in a chain of Index objects with all of the real
113600 114534       ** indices to follow */
113601 114535       Index *pFirst;                  /* First of real indices on the table */
113602 114536       memset(&sPk, 0, sizeof(Index));
113603 114537       sPk.nKeyCol = 1;
113604 114538       sPk.aiColumn = &aiColumnPk;
113605         -    sPk.aiRowEst = aiRowEstPk;
       114539  +    sPk.aiRowLogEst = aiRowEstPk;
113606 114540       sPk.onError = OE_Replace;
113607 114541       sPk.pTable = pTab;
113608         -    aiRowEstPk[0] = pTab->nRowEst;
113609         -    aiRowEstPk[1] = 1;
       114542  +    sPk.szIdxRow = pTab->szTabRow;
       114543  +    aiRowEstPk[0] = pTab->nRowLogEst;
       114544  +    aiRowEstPk[1] = 0;
113610 114545       pFirst = pSrc->pTab->pIndex;
113611 114546       if( pSrc->notIndexed==0 ){
113612 114547         /* The real indices of the table are only considered if the
113613 114548         ** NOT INDEXED qualifier is omitted from the FROM clause */
113614 114549         sPk.pNext = pFirst;
113615 114550       }
113616 114551       pProbe = &sPk;
113617 114552     }
113618         -  rSize = sqlite3LogEst(pTab->nRowEst);
       114553  +  rSize = pTab->nRowLogEst;
113619 114554     rLogSize = estLog(rSize);
113620 114555   
113621 114556   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
113622 114557     /* Automatic indexes */
113623 114558     if( !pBuilder->pOrSet
113624 114559      && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
113625 114560      && pSrc->pIndex==0
................................................................................
113661 114596     /* Loop over all indices
113662 114597     */
113663 114598     for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
113664 114599       if( pProbe->pPartIdxWhere!=0
113665 114600        && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
113666 114601         continue;  /* Partial index inappropriate for this query */
113667 114602       }
       114603  +    rSize = pProbe->aiRowLogEst[0];
113668 114604       pNew->u.btree.nEq = 0;
113669 114605       pNew->u.btree.nSkip = 0;
113670 114606       pNew->nLTerm = 0;
113671 114607       pNew->iSortIdx = 0;
113672 114608       pNew->rSetup = 0;
113673 114609       pNew->prereq = mExtra;
113674 114610       pNew->nOut = rSize;
................................................................................
113678 114614       assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
113679 114615       if( pProbe->tnum<=0 ){
113680 114616         /* Integer primary key index */
113681 114617         pNew->wsFlags = WHERE_IPK;
113682 114618   
113683 114619         /* Full table scan */
113684 114620         pNew->iSortIdx = b ? iSortIdx : 0;
113685         -      /* TUNING: Cost of full table scan is 3*(N + log2(N)).
113686         -      **  +  The extra 3 factor is to encourage the use of indexed lookups
113687         -      **     over full scans.  FIXME */
113688         -      pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
       114621  +      /* TUNING: Cost of full table scan is (N*3.0). */
       114622  +      pNew->rRun = rSize + 16;
113689 114623         whereLoopOutputAdjust(pWC, pNew);
113690 114624         rc = whereLoopInsert(pBuilder, pNew);
113691 114625         pNew->nOut = rSize;
113692 114626         if( rc ) break;
113693 114627       }else{
113694 114628         Bitmask m;
113695 114629         if( pProbe->isCovering ){
................................................................................
113708 114642            && (pProbe->szIdxRow<pTab->szTabRow)
113709 114643            && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
113710 114644            && sqlite3GlobalConfig.bUseCis
113711 114645            && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
113712 114646             )
113713 114647         ){
113714 114648           pNew->iSortIdx = b ? iSortIdx : 0;
113715         -        if( m==0 ){
113716         -          /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
113717         -          **  +  The extra factor K of between 1.1 and 3.0 that depends
113718         -          **     on the relative sizes of the table and the index.  K
113719         -          **     is smaller for smaller indices, thus favoring them.
113720         -          */
113721         -          pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 1 +
113722         -                        (15*pProbe->szIdxRow)/pTab->szTabRow;
113723         -        }else{
113724         -          /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
113725         -          ** which we will simplify to just N*log2(N) */
113726         -          pNew->rRun = rSize + rLogSize;
       114649  +
       114650  +        /* The cost of visiting the index rows is N*K, where K is
       114651  +        ** between 1.1 and 3.0, depending on the relative sizes of the
       114652  +        ** index and table rows. If this is a non-covering index scan,
       114653  +        ** also add the cost of visiting table rows (N*3.0).  */
       114654  +        pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
       114655  +        if( m!=0 ){
       114656  +          pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
113727 114657           }
       114658  +
113728 114659           whereLoopOutputAdjust(pWC, pNew);
113729 114660           rc = whereLoopInsert(pBuilder, pNew);
113730 114661           pNew->nOut = rSize;
113731 114662           if( rc ) break;
113732 114663         }
113733 114664       }
113734 114665   
................................................................................
113891 114822       if( i>=nConstraint ){
113892 114823         pNew->nLTerm = mxTerm+1;
113893 114824         assert( pNew->nLTerm<=pNew->nLSlot );
113894 114825         pNew->u.vtab.idxNum = pIdxInfo->idxNum;
113895 114826         pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
113896 114827         pIdxInfo->needToFreeIdxStr = 0;
113897 114828         pNew->u.vtab.idxStr = pIdxInfo->idxStr;
113898         -      pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
113899         -                                     && pIdxInfo->orderByConsumed);
       114829  +      pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
       114830  +                                      pIdxInfo->nOrderBy : 0);
113900 114831         pNew->rSetup = 0;
113901 114832         pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
113902 114833         pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
113903 114834         whereLoopInsert(pBuilder, pNew);
113904 114835         if( pNew->u.vtab.needFree ){
113905 114836           sqlite3_free(pNew->u.vtab.idxStr);
113906 114837           pNew->u.vtab.needFree = 0;
................................................................................
113924 114855     WhereClause *pWC;
113925 114856     WhereLoop *pNew;
113926 114857     WhereTerm *pTerm, *pWCEnd;
113927 114858     int rc = SQLITE_OK;
113928 114859     int iCur;
113929 114860     WhereClause tempWC;
113930 114861     WhereLoopBuilder sSubBuild;
113931         -  WhereOrSet sSum, sCur, sPrev;
       114862  +  WhereOrSet sSum, sCur;
113932 114863     struct SrcList_item *pItem;
113933 114864     
113934 114865     pWC = pBuilder->pWC;
113935 114866     if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
113936 114867     pWCEnd = pWC->a + pWC->nTerm;
113937 114868     pNew = pBuilder->pNew;
113938 114869     memset(&sSum, 0, sizeof(sSum));
113939 114870     pItem = pWInfo->pTabList->a + pNew->iTab;
113940         -  if( !HasRowid(pItem->pTab) ) return SQLITE_OK;
113941 114871     iCur = pItem->iCursor;
113942 114872   
113943 114873     for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
113944 114874       if( (pTerm->eOperator & WO_OR)!=0
113945 114875        && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 
113946 114876       ){
113947 114877         WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
................................................................................
113980 114910           if( sCur.n==0 ){
113981 114911             sSum.n = 0;
113982 114912             break;
113983 114913           }else if( once ){
113984 114914             whereOrMove(&sSum, &sCur);
113985 114915             once = 0;
113986 114916           }else{
       114917  +          WhereOrSet sPrev;
113987 114918             whereOrMove(&sPrev, &sSum);
113988 114919             sSum.n = 0;
113989 114920             for(i=0; i<sPrev.n; i++){
113990 114921               for(j=0; j<sCur.n; j++){
113991 114922                 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
113992 114923                               sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
113993 114924                               sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
................................................................................
113998 114929         pNew->nLTerm = 1;
113999 114930         pNew->aLTerm[0] = pTerm;
114000 114931         pNew->wsFlags = WHERE_MULTI_OR;
114001 114932         pNew->rSetup = 0;
114002 114933         pNew->iSortIdx = 0;
114003 114934         memset(&pNew->u, 0, sizeof(pNew->u));
114004 114935         for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
114005         -        /* TUNING: Multiple by 3.5 for the secondary table lookup */
114006         -        pNew->rRun = sSum.a[i].rRun + 18;
       114936  +        /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
       114937  +        ** of all sub-scans required by the OR-scan. However, due to rounding
       114938  +        ** errors, it may be that the cost of the OR-scan is equal to its
       114939  +        ** most expensive sub-scan. Add the smallest possible penalty 
       114940  +        ** (equivalent to multiplying the cost by 1.07) to ensure that 
       114941  +        ** this does not happen. Otherwise, for WHERE clauses such as the
       114942  +        ** following where there is an index on "y":
       114943  +        **
       114944  +        **     WHERE likelihood(x=?, 0.99) OR y=?
       114945  +        **
       114946  +        ** the planner may elect to "OR" together a full-table scan and an
       114947  +        ** index lookup. And other similarly odd results.  */
       114948  +        pNew->rRun = sSum.a[i].rRun + 1;
114007 114949           pNew->nOut = sSum.a[i].nOut;
114008 114950           pNew->prereq = sSum.a[i].prereq;
114009 114951           rc = whereLoopInsert(pBuilder, pNew);
114010 114952         }
114011 114953       }
114012 114954     }
114013 114955     return rc;
................................................................................
114053 114995     whereLoopClear(db, pNew);
114054 114996     return rc;
114055 114997   }
114056 114998   
114057 114999   /*
114058 115000   ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
114059 115001   ** parameters) to see if it outputs rows in the requested ORDER BY
114060         -** (or GROUP BY) without requiring a separate sort operation.  Return:
       115002  +** (or GROUP BY) without requiring a separate sort operation.  Return N:
114061 115003   ** 
114062         -**    0:  ORDER BY is not satisfied.  Sorting required
114063         -**    1:  ORDER BY is satisfied.      Omit sorting
114064         -**   -1:  Unknown at this time
       115004  +**   N>0:   N terms of the ORDER BY clause are satisfied
       115005  +**   N==0:  No terms of the ORDER BY clause are satisfied
       115006  +**   N<0:   Unknown yet how many terms of ORDER BY might be satisfied.   
114065 115007   **
114066 115008   ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
114067 115009   ** strict.  With GROUP BY and DISTINCT the only requirement is that
114068 115010   ** equivalent rows appear immediately adjacent to one another.  GROUP BY
114069         -** and DISTINT do not require rows to appear in any particular order as long
       115011  +** and DISTINCT do not require rows to appear in any particular order as long
114070 115012   ** as equivelent rows are grouped together.  Thus for GROUP BY and DISTINCT
114071 115013   ** the pOrderBy terms can be matched in any order.  With ORDER BY, the 
114072 115014   ** pOrderBy terms must be matched in strict left-to-right order.
114073 115015   */
114074         -static int wherePathSatisfiesOrderBy(
       115016  +static i8 wherePathSatisfiesOrderBy(
114075 115017     WhereInfo *pWInfo,    /* The WHERE clause */
114076 115018     ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
114077 115019     WherePath *pPath,     /* The WherePath to check */
114078 115020     u16 wctrlFlags,       /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
114079 115021     u16 nLoop,            /* Number of entries in pPath->aLoop[] */
114080 115022     WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
114081 115023     Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
................................................................................
114123 115065     **
114124 115066     ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
114125 115067     ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
114126 115068     ** automatically order-distinct.
114127 115069     */
114128 115070   
114129 115071     assert( pOrderBy!=0 );
114130         -
114131         -  /* Sortability of virtual tables is determined by the xBestIndex method
114132         -  ** of the virtual table itself */
114133         -  if( pLast->wsFlags & WHERE_VIRTUALTABLE ){
114134         -    testcase( nLoop>0 );  /* True when outer loops are one-row and match 
114135         -                          ** no ORDER BY terms */
114136         -    return pLast->u.vtab.isOrdered;
114137         -  }
114138 115072     if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
114139 115073   
114140 115074     nOrderBy = pOrderBy->nExpr;
114141 115075     testcase( nOrderBy==BMS-1 );
114142 115076     if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
114143 115077     isOrderDistinct = 1;
114144 115078     obDone = MASKBIT(nOrderBy)-1;
114145 115079     orderDistinctMask = 0;
114146 115080     ready = 0;
114147 115081     for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
114148 115082       if( iLoop>0 ) ready |= pLoop->maskSelf;
114149 115083       pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
114150         -    assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
       115084  +    if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
       115085  +      if( pLoop->u.vtab.isOrdered ) obSat = obDone;
       115086  +      break;
       115087  +    }
114151 115088       iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
114152 115089   
114153 115090       /* Mark off any ORDER BY term X that is a column in the table of
114154 115091       ** the current loop for which there is term in the WHERE
114155 115092       ** clause of the form X IS NULL or X=? that reference only outer
114156 115093       ** loops.
114157 115094       */
................................................................................
114231 115168            && j>=pLoop->u.btree.nEq
114232 115169            && pIndex->pTable->aCol[iColumn].notNull==0
114233 115170           ){
114234 115171             isOrderDistinct = 0;
114235 115172           }
114236 115173   
114237 115174           /* Find the ORDER BY term that corresponds to the j-th column
114238         -        ** of the index and and mark that ORDER BY term off 
       115175  +        ** of the index and mark that ORDER BY term off 
114239 115176           */
114240 115177           bOnce = 1;
114241 115178           isMatch = 0;
114242 115179           for(i=0; bOnce && i<nOrderBy; i++){
114243 115180             if( MASKBIT(i) & obSat ) continue;
114244 115181             pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
114245 115182             testcase( wctrlFlags & WHERE_GROUPBY );
................................................................................
114252 115189               pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
114253 115190               if( !pColl ) pColl = db->pDfltColl;
114254 115191               if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
114255 115192             }
114256 115193             isMatch = 1;
114257 115194             break;
114258 115195           }
114259         -        if( isMatch ){
114260         -          if( iColumn<0 ){
114261         -            testcase( distinctColumns==0 );
114262         -            distinctColumns = 1;
114263         -          }
114264         -          obSat |= MASKBIT(i);
114265         -          if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
       115196  +        if( isMatch && (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
114266 115197               /* Make sure the sort order is compatible in an ORDER BY clause.
114267 115198               ** Sort order is irrelevant for a GROUP BY clause. */
114268 115199               if( revSet ){
114269         -              if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0;
       115200  +            if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0;
114270 115201               }else{
114271 115202                 rev = revIdx ^ pOrderBy->a[i].sortOrder;
114272 115203                 if( rev ) *pRevMask |= MASKBIT(iLoop);
114273 115204                 revSet = 1;
114274 115205               }
114275 115206             }
       115207  +        if( isMatch ){
       115208  +          if( iColumn<0 ){
       115209  +            testcase( distinctColumns==0 );
       115210  +            distinctColumns = 1;
       115211  +          }
       115212  +          obSat |= MASKBIT(i);
114276 115213           }else{
114277 115214             /* No match found */
114278 115215             if( j==0 || j<nKeyCol ){
114279 115216               testcase( isOrderDistinct!=0 );
114280 115217               isOrderDistinct = 0;
114281 115218             }
114282 115219             break;
................................................................................
114300 115237           if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
114301 115238           if( (mTerm&~orderDistinctMask)==0 ){
114302 115239             obSat |= MASKBIT(i);
114303 115240           }
114304 115241         }
114305 115242       }
114306 115243     } /* End the loop over all WhereLoops from outer-most down to inner-most */
114307         -  if( obSat==obDone ) return 1;
114308         -  if( !isOrderDistinct ) return 0;
       115244  +  if( obSat==obDone ) return (i8)nOrderBy;
       115245  +  if( !isOrderDistinct ){
       115246  +    for(i=nOrderBy-1; i>0; i--){
       115247  +      Bitmask m = MASKBIT(i) - 1;
       115248  +      if( (obSat&m)==m ) return i;
       115249  +    }
       115250  +    return 0;
       115251  +  }
114309 115252     return -1;
114310 115253   }
114311 115254   
       115255  +
       115256  +/*
       115257  +** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
       115258  +** the planner assumes that the specified pOrderBy list is actually a GROUP
       115259  +** BY clause - and so any order that groups rows as required satisfies the
       115260  +** request.
       115261  +**
       115262  +** Normally, in this case it is not possible for the caller to determine
       115263  +** whether or not the rows are really being delivered in sorted order, or
       115264  +** just in some other order that provides the required grouping. However,
       115265  +** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
       115266  +** this function may be called on the returned WhereInfo object. It returns
       115267  +** true if the rows really will be sorted in the specified order, or false
       115268  +** otherwise.
       115269  +**
       115270  +** For example, assuming:
       115271  +**
       115272  +**   CREATE INDEX i1 ON t1(x, Y);
       115273  +**
       115274  +** then
       115275  +**
       115276  +**   SELECT * FROM t1 GROUP BY x,y ORDER BY x,y;   -- IsSorted()==1
       115277  +**   SELECT * FROM t1 GROUP BY y,x ORDER BY y,x;   -- IsSorted()==0
       115278  +*/
       115279  +SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
       115280  +  assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
       115281  +  assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
       115282  +  return pWInfo->sorted;
       115283  +}
       115284  +
114312 115285   #ifdef WHERETRACE_ENABLED
114313 115286   /* For debugging use only: */
114314 115287   static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
114315 115288     static char zName[65];
114316 115289     int i;
114317 115290     for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
114318 115291     if( pLast ) zName[i++] = pLast->cId;
114319 115292     zName[i] = 0;
114320 115293     return zName;
114321 115294   }
114322 115295   #endif
114323         -
114324 115296   
114325 115297   /*
114326 115298   ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
114327 115299   ** attempts to find the lowest cost path that visits each WhereLoop
114328 115300   ** once.  This path is then loaded into the pWInfo->a[].pWLoop fields.
114329 115301   **
114330 115302   ** Assume that the total number of output rows that will need to be sorted
................................................................................
114338 115310     int mxChoice;             /* Maximum number of simultaneous paths tracked */
114339 115311     int nLoop;                /* Number of terms in the join */
114340 115312     Parse *pParse;            /* Parsing context */
114341 115313     sqlite3 *db;              /* The database connection */
114342 115314     int iLoop;                /* Loop counter over the terms of the join */
114343 115315     int ii, jj;               /* Loop counters */
114344 115316     int mxI = 0;              /* Index of next entry to replace */
       115317  +  int nOrderBy;             /* Number of ORDER BY clause terms */
114345 115318     LogEst rCost;             /* Cost of a path */
114346 115319     LogEst nOut;              /* Number of outputs */
114347 115320     LogEst mxCost = 0;        /* Maximum cost of a set of paths */
114348 115321     LogEst mxOut = 0;         /* Maximum nOut value on the set of paths */
114349         -  LogEst rSortCost;         /* Cost to do a sort */
114350 115322     int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
114351 115323     WherePath *aFrom;         /* All nFrom paths at the previous level */
114352 115324     WherePath *aTo;           /* The nTo best paths at the current level */
114353 115325     WherePath *pFrom;         /* An element of aFrom[] that we are working on */
114354 115326     WherePath *pTo;           /* An element of aTo[] that we are working on */
114355 115327     WhereLoop *pWLoop;        /* One of the WhereLoop objects */
114356 115328     WhereLoop **pX;           /* Used to divy up the pSpace memory */
................................................................................
114358 115330   
114359 115331     pParse = pWInfo->pParse;
114360 115332     db = pParse->db;
114361 115333     nLoop = pWInfo->nLevel;
114362 115334     /* TUNING: For simple queries, only the best path is tracked.
114363 115335     ** For 2-way joins, the 5 best paths are followed.
114364 115336     ** For joins of 3 or more tables, track the 10 best paths */
114365         -  mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10);
       115337  +  mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
114366 115338     assert( nLoop<=pWInfo->pTabList->nSrc );
114367 115339     WHERETRACE(0x002, ("---- begin solver\n"));
114368 115340   
114369 115341     /* Allocate and initialize space for aTo and aFrom */
114370 115342     ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
114371 115343     pSpace = sqlite3DbMallocRaw(db, ii);
114372 115344     if( pSpace==0 ) return SQLITE_NOMEM;
................................................................................
114384 115356     ** of computing an automatic index is not paid back within the first 25
114385 115357     ** rows, then do not use the automatic index. */
114386 115358     aFrom[0].nRow = MIN(pParse->nQueryLoop, 46);  assert( 46==sqlite3LogEst(25) );
114387 115359     nFrom = 1;
114388 115360   
114389 115361     /* Precompute the cost of sorting the final result set, if the caller
114390 115362     ** to sqlite3WhereBegin() was concerned about sorting */
114391         -  rSortCost = 0;
114392 115363     if( pWInfo->pOrderBy==0 || nRowEst==0 ){
114393         -    aFrom[0].isOrderedValid = 1;
       115364  +    aFrom[0].isOrdered = 0;
       115365  +    nOrderBy = 0;
114394 115366     }else{
114395         -    /* TUNING: Estimated cost of sorting is 48*N*log2(N) where N is the
114396         -    ** number of output rows. The 48 is the expected size of a row to sort. 
114397         -    ** FIXME:  compute a better estimate of the 48 multiplier based on the
114398         -    ** result set expressions. */
114399         -    rSortCost = nRowEst + estLog(nRowEst);
114400         -    WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
       115367  +    aFrom[0].isOrdered = nLoop>0 ? -1 : 1;
       115368  +    nOrderBy = pWInfo->pOrderBy->nExpr;
114401 115369     }
114402 115370   
114403 115371     /* Compute successively longer WherePaths using the previous generation
114404 115372     ** of WherePaths as the basis for the next.  Keep track of the mxChoice
114405 115373     ** best paths at each generation */
114406 115374     for(iLoop=0; iLoop<nLoop; iLoop++){
114407 115375       nTo = 0;
114408 115376       for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
114409 115377         for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
114410 115378           Bitmask maskNew;
114411 115379           Bitmask revMask = 0;
114412         -        u8 isOrderedValid = pFrom->isOrderedValid;
114413         -        u8 isOrdered = pFrom->isOrdered;
       115380  +        i8 isOrdered = pFrom->isOrdered;
114414 115381           if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
114415 115382           if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
114416 115383           /* At this point, pWLoop is a candidate to be the next loop. 
114417 115384           ** Compute its cost */
114418 115385           rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
114419 115386           rCost = sqlite3LogEstAdd(rCost, pFrom->rCost);
114420 115387           nOut = pFrom->nRow + pWLoop->nOut;
114421 115388           maskNew = pFrom->maskLoop | pWLoop->maskSelf;
114422         -        if( !isOrderedValid ){
114423         -          switch( wherePathSatisfiesOrderBy(pWInfo,
       115389  +        if( isOrdered<0 ){
       115390  +          isOrdered = wherePathSatisfiesOrderBy(pWInfo,
114424 115391                          pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
114425         -                       iLoop, pWLoop, &revMask) ){
114426         -            case 1:  /* Yes.  pFrom+pWLoop does satisfy the ORDER BY clause */
114427         -              isOrdered = 1;
114428         -              isOrderedValid = 1;
114429         -              break;
114430         -            case 0:  /* No.  pFrom+pWLoop will require a separate sort */
114431         -              isOrdered = 0;
114432         -              isOrderedValid = 1;
       115392  +                       iLoop, pWLoop, &revMask);
       115393  +          if( isOrdered>=0 && isOrdered<nOrderBy ){
       115394  +            /* TUNING: Estimated cost of a full external sort, where N is 
       115395  +            ** the number of rows to sort is:
       115396  +            **
       115397  +            **   cost = (3.0 * N * log(N)).
       115398  +            ** 
       115399  +            ** Or, if the order-by clause has X terms but only the last Y 
       115400  +            ** terms are out of order, then block-sorting will reduce the 
       115401  +            ** sorting cost to:
       115402  +            **
       115403  +            **   cost = (3.0 * N * log(N)) * (Y/X)
       115404  +            **
       115405  +            ** The (Y/X) term is implemented using stack variable rScale
       115406  +            ** below.  */
       115407  +            LogEst rScale, rSortCost;
       115408  +            assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
       115409  +            rScale = sqlite3LogEst((nOrderBy-isOrdered)*100/nOrderBy) - 66;
       115410  +            rSortCost = nRowEst + estLog(nRowEst) + rScale + 16;
       115411  +
       115412  +            /* TUNING: The cost of implementing DISTINCT using a B-TREE is
       115413  +            ** similar but with a larger constant of proportionality. 
       115414  +            ** Multiply by an additional factor of 3.0.  */
       115415  +            if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
       115416  +              rSortCost += 16;
       115417  +            }
       115418  +            WHERETRACE(0x002,
       115419  +               ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
       115420  +                rSortCost, (nOrderBy-isOrdered), nOrderBy, rCost,
       115421  +                sqlite3LogEstAdd(rCost,rSortCost)));
114433 115422                 rCost = sqlite3LogEstAdd(rCost, rSortCost);
114434         -              break;
114435         -            default: /* Cannot tell yet.  Try again on the next iteration */
114436         -              break;
114437 115423             }
114438 115424           }else{
114439 115425             revMask = pFrom->revLoop;
114440 115426           }
114441 115427           /* Check to see if pWLoop should be added to the mxChoice best so far */
114442 115428           for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
114443 115429             if( pTo->maskLoop==maskNew
114444         -           && pTo->isOrderedValid==isOrderedValid
       115430  +           && ((pTo->isOrdered^isOrdered)&80)==0
114445 115431              && ((pTo->rCost<=rCost && pTo->nRow<=nOut) ||
114446 115432                   (pTo->rCost>=rCost && pTo->nRow>=nOut))
114447 115433             ){
114448 115434               testcase( jj==nTo-1 );
114449 115435               break;
114450 115436             }
114451 115437           }
114452 115438           if( jj>=nTo ){
114453 115439             if( nTo>=mxChoice && rCost>=mxCost ){
114454 115440   #ifdef WHERETRACE_ENABLED /* 0x4 */
114455 115441               if( sqlite3WhereTrace&0x4 ){
114456 115442                 sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d order=%c\n",
114457 115443                     wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
114458         -                  isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
       115444  +                  isOrdered>=0 ? isOrdered+'0' : '?');
114459 115445               }
114460 115446   #endif
114461 115447               continue;
114462 115448             }
114463 115449             /* Add a new Path to the aTo[] set */
114464 115450             if( nTo<mxChoice ){
114465 115451               /* Increase the size of the aTo set by one */
................................................................................
114469 115455               jj = mxI;
114470 115456             }
114471 115457             pTo = &aTo[jj];
114472 115458   #ifdef WHERETRACE_ENABLED /* 0x4 */
114473 115459             if( sqlite3WhereTrace&0x4 ){
114474 115460               sqlite3DebugPrintf("New    %s cost=%-3d,%3d order=%c\n",
114475 115461                   wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
114476         -                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
       115462  +                isOrdered>=0 ? isOrdered+'0' : '?');
114477 115463             }
114478 115464   #endif
114479 115465           }else{
114480 115466             if( pTo->rCost<=rCost && pTo->nRow<=nOut ){
114481 115467   #ifdef WHERETRACE_ENABLED /* 0x4 */
114482 115468               if( sqlite3WhereTrace&0x4 ){
114483 115469                 sqlite3DebugPrintf(
114484 115470                     "Skip   %s cost=%-3d,%3d order=%c",
114485 115471                     wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
114486         -                  isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
       115472  +                  isOrdered>=0 ? isOrdered+'0' : '?');
114487 115473                 sqlite3DebugPrintf("   vs %s cost=%-3d,%d order=%c\n",
114488 115474                     wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
114489         -                  pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
       115475  +                  pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
114490 115476               }
114491 115477   #endif
114492 115478               testcase( pTo->rCost==rCost );
114493 115479               continue;
114494 115480             }
114495 115481             testcase( pTo->rCost==rCost+1 );
114496 115482             /* A new and better score for a previously created equivalent path */
114497 115483   #ifdef WHERETRACE_ENABLED /* 0x4 */
114498 115484             if( sqlite3WhereTrace&0x4 ){
114499 115485               sqlite3DebugPrintf(
114500 115486                   "Update %s cost=%-3d,%3d order=%c",
114501 115487                   wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
114502         -                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
       115488  +                isOrdered>=0 ? isOrdered+'0' : '?');
114503 115489               sqlite3DebugPrintf("  was %s cost=%-3d,%3d order=%c\n",
114504 115490                   wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
114505         -                pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
       115491  +                pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
114506 115492             }
114507 115493   #endif
114508 115494           }
114509 115495           /* pWLoop is a winner.  Add it to the set of best so far */
114510 115496           pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
114511 115497           pTo->revLoop = revMask;
114512 115498           pTo->nRow = nOut;
114513 115499           pTo->rCost = rCost;
114514         -        pTo->isOrderedValid = isOrderedValid;
114515 115500           pTo->isOrdered = isOrdered;
114516 115501           memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
114517 115502           pTo->aLoop[iLoop] = pWLoop;
114518 115503           if( nTo>=mxChoice ){
114519 115504             mxI = 0;
114520 115505             mxCost = aTo[0].rCost;
114521 115506             mxOut = aTo[0].nRow;
................................................................................
114532 115517   
114533 115518   #ifdef WHERETRACE_ENABLED  /* >=2 */
114534 115519       if( sqlite3WhereTrace>=2 ){
114535 115520         sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
114536 115521         for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
114537 115522           sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
114538 115523              wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
114539         -           pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
114540         -        if( pTo->isOrderedValid && pTo->isOrdered ){
       115524  +           pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
       115525  +        if( pTo->isOrdered>0 ){
114541 115526             sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
114542 115527           }else{
114543 115528             sqlite3DebugPrintf("\n");
114544 115529           }
114545 115530         }
114546 115531       }
114547 115532   #endif
................................................................................
114576 115561      && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
114577 115562      && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
114578 115563      && nRowEst
114579 115564     ){
114580 115565       Bitmask notUsed;
114581 115566       int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
114582 115567                    WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
114583         -    if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
       115568  +    if( rc==pWInfo->pResultSet->nExpr ){
       115569  +      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
114584 115570     }
114585         -  if( pFrom->isOrdered ){
       115571  +  }
       115572  +  if( pWInfo->pOrderBy ){
114586 115573       if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
       115574  +      if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
114587 115575         pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
       115576  +      }
114588 115577       }else{
114589         -      pWInfo->bOBSat = 1;
       115578  +      pWInfo->nOBSat = pFrom->isOrdered;
       115579  +      if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
114590 115580         pWInfo->revMask = pFrom->revLoop;
114591 115581       }
       115582  +    if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
       115583  +        && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr
       115584  +    ){
       115585  +      Bitmask notUsed = 0;
       115586  +      int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, 
       115587  +          pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed
       115588  +      );
       115589  +      assert( pWInfo->sorted==0 );
       115590  +      pWInfo->sorted = (nOrder==pWInfo->pOrderBy->nExpr);
114592 115591     }
       115592  +  }
       115593  +
       115594  +
114593 115595     pWInfo->nRowOut = pFrom->nRow;
114594 115596   
114595 115597     /* Free temporary memory and return success */
114596 115598     sqlite3DbFree(db, pSpace);
114597 115599     return SQLITE_OK;
114598 115600   }
114599 115601   
................................................................................
114667 115669     }
114668 115670     if( pLoop->wsFlags ){
114669 115671       pLoop->nOut = (LogEst)1;
114670 115672       pWInfo->a[0].pWLoop = pLoop;
114671 115673       pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
114672 115674       pWInfo->a[0].iTabCur = iCur;
114673 115675       pWInfo->nRowOut = 1;
114674         -    if( pWInfo->pOrderBy ) pWInfo->bOBSat =  1;
       115676  +    if( pWInfo->pOrderBy ) pWInfo->nOBSat =  pWInfo->pOrderBy->nExpr;
114675 115677       if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
114676 115678         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
114677 115679       }
114678 115680   #ifdef SQLITE_DEBUG
114679 115681       pLoop->cId = '0';
114680 115682   #endif
114681 115683       return 1;
................................................................................
114771 115773   ** be used to compute the appropriate cursor depending on which index is
114772 115774   ** used.
114773 115775   */
114774 115776   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
114775 115777     Parse *pParse,        /* The parser context */
114776 115778     SrcList *pTabList,    /* FROM clause: A list of all tables to be scanned */
114777 115779     Expr *pWhere,         /* The WHERE clause */
114778         -  ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
       115780  +  ExprList *pOrderBy,   /* An ORDER BY (or GROUP BY) clause, or NULL */
114779 115781     ExprList *pResultSet, /* Result set of the query */
114780 115782     u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
114781 115783     int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
114782 115784   ){
114783 115785     int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
114784 115786     int nTabList;              /* Number of elements in pTabList */
114785 115787     WhereInfo *pWInfo;         /* Will become the return value of this function */
................................................................................
114793 115795     sqlite3 *db;               /* Database connection */
114794 115796     int rc;                    /* Return code */
114795 115797   
114796 115798   
114797 115799     /* Variable initialization */
114798 115800     db = pParse->db;
114799 115801     memset(&sWLB, 0, sizeof(sWLB));
       115802  +
       115803  +  /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
       115804  +  testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
       115805  +  if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
114800 115806     sWLB.pOrderBy = pOrderBy;
114801 115807   
114802 115808     /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
114803 115809     ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
114804 115810     if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
114805 115811       wctrlFlags &= ~WHERE_WANT_DISTINCT;
114806 115812     }
................................................................................
114837 115843     }
114838 115844     pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
114839 115845     pWInfo->nLevel = nTabList;
114840 115846     pWInfo->pParse = pParse;
114841 115847     pWInfo->pTabList = pTabList;
114842 115848     pWInfo->pOrderBy = pOrderBy;
114843 115849     pWInfo->pResultSet = pResultSet;
114844         -  pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
       115850  +  pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
114845 115851     pWInfo->wctrlFlags = wctrlFlags;
114846 115852     pWInfo->savedNQueryLoop = pParse->nQueryLoop;
114847 115853     pMaskSet = &pWInfo->sMaskSet;
114848 115854     sWLB.pWInfo = pWInfo;
114849 115855     sWLB.pWC = &pWInfo->sWC;
114850 115856     sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
114851 115857     assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
................................................................................
114871 115877         sWLB.pWC->a[ii].wtFlags |= TERM_CODED;
114872 115878       }
114873 115879     }
114874 115880   
114875 115881     /* Special case: No FROM clause
114876 115882     */
114877 115883     if( nTabList==0 ){
114878         -    if( pOrderBy ) pWInfo->bOBSat = 1;
       115884  +    if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
114879 115885       if( wctrlFlags & WHERE_WANT_DISTINCT ){
114880 115886         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
114881 115887       }
114882 115888     }
114883 115889   
114884 115890     /* Assign a bit from the bitmask to every term in the FROM clause.
114885 115891     **
................................................................................
114982 115988     if( pParse->nErr || NEVER(db->mallocFailed) ){
114983 115989       goto whereBeginError;
114984 115990     }
114985 115991   #ifdef WHERETRACE_ENABLED /* !=0 */
114986 115992     if( sqlite3WhereTrace ){
114987 115993       int ii;
114988 115994       sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
114989         -    if( pWInfo->bOBSat ){
114990         -      sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask);
       115995  +    if( pWInfo->nOBSat>0 ){
       115996  +      sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
114991 115997       }
114992 115998       switch( pWInfo->eDistinct ){
114993 115999         case WHERE_DISTINCT_UNIQUE: {
114994 116000           sqlite3DebugPrintf("  DISTINCT=unique");
114995 116001           break;
114996 116002         }
114997 116003         case WHERE_DISTINCT_ORDERED: {
................................................................................
115106 116112       }
115107 116113       if( pLoop->wsFlags & WHERE_INDEXED ){
115108 116114         Index *pIx = pLoop->u.btree.pIndex;
115109 116115         int iIndexCur;
115110 116116         int op = OP_OpenRead;
115111 116117         /* iIdxCur is always set if to a positive value if ONEPASS is possible */
115112 116118         assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
115113         -      if( pWInfo->okOnePass ){
       116119  +      if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
       116120  +       && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0
       116121  +      ){
       116122  +        /* This is one term of an OR-optimization using the PRIMARY KEY of a
       116123  +        ** WITHOUT ROWID table.  No need for a separate index */
       116124  +        iIndexCur = pLevel->iTabCur;
       116125  +        op = 0;
       116126  +      }else if( pWInfo->okOnePass ){
115114 116127           Index *pJ = pTabItem->pTab->pIndex;
115115 116128           iIndexCur = iIdxCur;
115116 116129           assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
115117 116130           while( ALWAYS(pJ) && pJ!=pIx ){
115118 116131             iIndexCur++;
115119 116132             pJ = pJ->pNext;
115120 116133           }
................................................................................
115124 116137           iIndexCur = iIdxCur;
115125 116138         }else{
115126 116139           iIndexCur = pParse->nTab++;
115127 116140         }
115128 116141         pLevel->iIdxCur = iIndexCur;
115129 116142         assert( pIx->pSchema==pTab->pSchema );
115130 116143         assert( iIndexCur>=0 );
       116144  +      if( op ){
115131 116145         sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
115132 116146         sqlite3VdbeSetP4KeyInfo(pParse, pIx);
115133 116147         VdbeComment((v, "%s", pIx->zName));
115134 116148       }
       116149  +    }
115135 116150       if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
115136 116151       notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
115137 116152     }
115138 116153     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
115139 116154     if( db->mallocFailed ) goto whereBeginError;
115140 116155   
115141 116156     /* Generate the code to do the search.  Each iteration of the for
................................................................................
118213 119228         **      expr1 NOT IN ()
118214 119229         **
118215 119230         ** simplify to constants 0 (false) and 1 (true), respectively,
118216 119231         ** regardless of the value of expr1.
118217 119232         */
118218 119233         yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy328]);
118219 119234         sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy346.pExpr);
       119235  +    }else if( yymsp[-1].minor.yy14->nExpr==1 ){
       119236  +      /* Expressions of the form:
       119237  +      **
       119238  +      **      expr1 IN (?1)
       119239  +      **      expr1 NOT IN (?2)
       119240  +      **
       119241  +      ** with exactly one value on the RHS can be simplified to something
       119242  +      ** like this:
       119243  +      **
       119244  +      **      expr1 == ?1
       119245  +      **      expr1 <> ?2
       119246  +      **
       119247  +      ** But, the RHS of the == or <> is marked with the EP_Generic flag
       119248  +      ** so that it may not contribute to the computation of comparison
       119249  +      ** affinity or the collating sequence to use for comparison.  Otherwise,
       119250  +      ** the semantics would be subtly different from IN or NOT IN.
       119251  +      */
       119252  +      Expr *pRHS = yymsp[-1].minor.yy14->a[0].pExpr;
       119253  +      yymsp[-1].minor.yy14->a[0].pExpr = 0;
       119254  +      sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
       119255  +      /* pRHS cannot be NULL because a malloc error would have been detected
       119256  +      ** before now and control would have never reached this point */
       119257  +      if( ALWAYS(pRHS) ){
       119258  +        pRHS->flags &= ~EP_Collate;
       119259  +        pRHS->flags |= EP_Generic;
       119260  +      }
       119261  +      yygotominor.yy346.pExpr = sqlite3PExpr(pParse, yymsp[-3].minor.yy328 ? TK_NE : TK_EQ, yymsp[-4].minor.yy346.pExpr, pRHS, 0);
118220 119262       }else{
118221 119263         yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
118222 119264         if( yygotominor.yy346.pExpr ){
118223 119265           yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy14;
118224 119266           sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
118225 119267         }else{
118226 119268           sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
................................................................................
120833 121875         HashElem *p;
120834 121876         for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
120835 121877           Table *pTab = (Table *)sqliteHashData(p);
120836 121878           if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
120837 121879         }
120838 121880       }
120839 121881     }
       121882  +  sqlite3VtabUnlockList(db);
120840 121883     sqlite3BtreeLeaveAll(db);
120841 121884   #else
120842 121885     UNUSED_PARAMETER(db);
120843 121886   #endif
120844 121887   }
120845 121888   
120846 121889   /*
................................................................................
123145 124188       */
123146 124189       case SQLITE_TESTCTRL_BITVEC_TEST: {
123147 124190         int sz = va_arg(ap, int);
123148 124191         int *aProg = va_arg(ap, int*);
123149 124192         rc = sqlite3BitvecBuiltinTest(sz, aProg);
123150 124193         break;
123151 124194       }
       124195  +
       124196  +    /*
       124197  +    **  sqlite3_test_control(FAULT_INSTALL, xCallback)
       124198  +    **
       124199  +    ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
       124200  +    ** if xCallback is not NULL.
       124201  +    **
       124202  +    ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
       124203  +    ** is called immediately after installing the new callback and the return
       124204  +    ** value from sqlite3FaultSim(0) becomes the return from
       124205  +    ** sqlite3_test_control().
       124206  +    */
       124207  +    case SQLITE_TESTCTRL_FAULT_INSTALL: {
       124208  +      /* MSVC is picky about pulling func ptrs from va lists.
       124209  +      ** http://support.microsoft.com/kb/47961
       124210  +      ** sqlite3Config.xTestCallback = va_arg(ap, int(*)(int));
       124211  +      */
       124212  +      typedef int(*TESTCALLBACKFUNC_t)(int);
       124213  +      sqlite3Config.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
       124214  +      rc = sqlite3FaultSim(0);
       124215  +      break;
       124216  +    }
123152 124217   
123153 124218       /*
123154 124219       **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
123155 124220       **
123156 124221       ** Register hooks to call to indicate which malloc() failures 
123157 124222       ** are benign.
123158 124223       */
................................................................................
123236 124301       **    }
123237 124302       */
123238 124303       case SQLITE_TESTCTRL_ALWAYS: {
123239 124304         int x = va_arg(ap,int);
123240 124305         rc = ALWAYS(x);
123241 124306         break;
123242 124307       }
       124308  +
       124309  +    /*
       124310  +    **   sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
       124311  +    **
       124312  +    ** The integer returned reveals the byte-order of the computer on which
       124313  +    ** SQLite is running:
       124314  +    **
       124315  +    **       1     big-endian,    determined at run-time
       124316  +    **      10     little-endian, determined at run-time
       124317  +    **  432101     big-endian,    determined at compile-time
       124318  +    **  123410     little-endian, determined at compile-time
       124319  +    */ 
       124320  +    case SQLITE_TESTCTRL_BYTEORDER: {
       124321  +      rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
       124322  +      break;
       124323  +    }
123243 124324   
123244 124325       /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
123245 124326       **
123246 124327       ** Set the nReserve size to N for the main database on the database
123247 124328       ** connection db.
123248 124329       */
123249 124330       case SQLITE_TESTCTRL_RESERVE: {
................................................................................
123439 124520   
123440 124521   /*
123441 124522   ** Return 1 if database is read-only or 0 if read/write.  Return -1 if
123442 124523   ** no such database exists.
123443 124524   */
123444 124525   SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
123445 124526     Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
123446         -  return pBt ? sqlite3PagerIsreadonly(sqlite3BtreePager(pBt)) : -1;
       124527  +  return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
123447 124528   }
123448 124529   
123449 124530   /************** End of main.c ************************************************/
123450 124531   /************** Begin file notify.c ******************************************/
123451 124532   /*
123452 124533   ** 2009 March 3
123453 124534   **
................................................................................
124559 125640     const char *zName;              /* virtual table name */
124560 125641     int nColumn;                    /* number of named columns in virtual table */
124561 125642     char **azColumn;                /* column names.  malloced */
124562 125643     u8 *abNotindexed;               /* True for 'notindexed' columns */
124563 125644     sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
124564 125645     char *zContentTbl;              /* content=xxx option, or NULL */
124565 125646     char *zLanguageid;              /* languageid=xxx option, or NULL */
124566         -  u8 bAutoincrmerge;              /* True if automerge=1 */
       125647  +  int nAutoincrmerge;             /* Value configured by 'automerge' */
124567 125648     u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
124568 125649   
124569 125650     /* Precompiled statements used by the implementation. Each of these 
124570 125651     ** statements is run and reset within a single virtual table API call. 
124571 125652     */
124572         -  sqlite3_stmt *aStmt[37];
       125653  +  sqlite3_stmt *aStmt[40];
124573 125654   
124574 125655     char *zReadExprlist;
124575 125656     char *zWriteExprlist;
124576 125657   
124577 125658     int nNodeSize;                  /* Soft limit for node size */
124578 125659     u8 bFts4;                       /* True for FTS4, false for FTS3 */
124579         -  u8 bHasStat;                    /* True if %_stat table exists */
       125660  +  u8 bHasStat;                    /* True if %_stat table exists (2==unknown) */
124580 125661     u8 bHasDocsize;                 /* True if %_docsize table exists */
124581 125662     u8 bDescIdx;                    /* True if doclists are in reverse order */
124582 125663     u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
124583 125664     int nPgsz;                      /* Page size for host database */
124584 125665     char *zSegmentsTbl;             /* Name of %_segments table */
124585 125666     sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
124586 125667   
................................................................................
125987 127068     p->azColumn = (char **)&p[1];
125988 127069     p->pTokenizer = pTokenizer;
125989 127070     p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
125990 127071     p->bHasDocsize = (isFts4 && bNoDocsize==0);
125991 127072     p->bHasStat = isFts4;
125992 127073     p->bFts4 = isFts4;
125993 127074     p->bDescIdx = bDescIdx;
125994         -  p->bAutoincrmerge = 0xff;   /* 0xff means setting unknown */
       127075  +  p->nAutoincrmerge = 0xff;   /* 0xff means setting unknown */
125995 127076     p->zContentTbl = zContent;
125996 127077     p->zLanguageid = zLanguageid;
125997 127078     zContent = 0;
125998 127079     zLanguageid = 0;
125999 127080     TESTONLY( p->inTransaction = -1 );
126000 127081     TESTONLY( p->mxSavepoint = -1 );
126001 127082   
................................................................................
126030 127111     }
126031 127112   
126032 127113     /* Fill in the abNotindexed array */
126033 127114     for(iCol=0; iCol<nCol; iCol++){
126034 127115       int n = (int)strlen(p->azColumn[iCol]);
126035 127116       for(i=0; i<nNotindexed; i++){
126036 127117         char *zNot = azNotindexed[i];
126037         -      if( zNot && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n) ){
       127118  +      if( zNot && n==(int)strlen(zNot)
       127119  +       && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n) 
       127120  +      ){
126038 127121           p->abNotindexed[iCol] = 1;
126039 127122           sqlite3_free(zNot);
126040 127123           azNotindexed[i] = 0;
126041 127124         }
126042 127125       }
126043 127126     }
126044 127127     for(i=0; i<nNotindexed; i++){
................................................................................
126064 127147       rc = fts3CreateTables(p);
126065 127148     }
126066 127149   
126067 127150     /* Check to see if a legacy fts3 table has been "upgraded" by the
126068 127151     ** addition of a %_stat table so that it can use incremental merge.
126069 127152     */
126070 127153     if( !isFts4 && !isCreate ){
126071         -    int rc2 = SQLITE_OK;
126072         -    fts3DbExec(&rc2, db, "SELECT 1 FROM %Q.'%q_stat' WHERE id=2",
126073         -               p->zDb, p->zName);
126074         -    if( rc2==SQLITE_OK ) p->bHasStat = 1;
       127154  +    p->bHasStat = 2;
126075 127155     }
126076 127156   
126077 127157     /* Figure out the page-size for the database. This is required in order to
126078 127158     ** estimate the cost of loading large doclists from the database.  */
126079 127159     fts3DatabasePageSize(&rc, p);
126080 127160     p->nNodeSize = p->nPgsz-35;
126081 127161   
................................................................................
127959 129039     ** segments.
127960 129040     */
127961 129041     const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
127962 129042   
127963 129043     Fts3Table *p = (Fts3Table*)pVtab;
127964 129044     int rc = sqlite3Fts3PendingTermsFlush(p);
127965 129045   
127966         -  if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
       129046  +  if( rc==SQLITE_OK 
       129047  +   && p->nLeafAdd>(nMinMerge/16) 
       129048  +   && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
       129049  +  ){
127967 129050       int mxLevel = 0;              /* Maximum relative level value in db */
127968 129051       int A;                        /* Incr-merge parameter A */
127969 129052   
127970 129053       rc = sqlite3Fts3MaxLevel(p, &mxLevel);
127971 129054       assert( rc==SQLITE_OK || mxLevel==0 );
127972 129055       A = p->nLeafAdd * mxLevel;
127973 129056       A += (A/2);
127974         -    if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
       129057  +    if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
127975 129058     }
127976 129059     sqlite3Fts3SegmentsClose(p);
127977 129060     return rc;
127978 129061   }
127979 129062   
127980 129063   /*
127981         -** Implementation of xBegin() method. This is a no-op.
       129064  +** If it is currently unknown whether or not the FTS table has an %_stat
       129065  +** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
       129066  +** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
       129067  +** if an error occurs.
       129068  +*/
       129069  +static int fts3SetHasStat(Fts3Table *p){
       129070  +  int rc = SQLITE_OK;
       129071  +  if( p->bHasStat==2 ){
       129072  +    const char *zFmt ="SELECT 1 FROM %Q.sqlite_master WHERE tbl_name='%q_stat'";
       129073  +    char *zSql = sqlite3_mprintf(zFmt, p->zDb, p->zName);
       129074  +    if( zSql ){
       129075  +      sqlite3_stmt *pStmt = 0;
       129076  +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
       129077  +      if( rc==SQLITE_OK ){
       129078  +        int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
       129079  +        rc = sqlite3_finalize(pStmt);
       129080  +        if( rc==SQLITE_OK ) p->bHasStat = bHasStat;
       129081  +      }
       129082  +      sqlite3_free(zSql);
       129083  +    }else{
       129084  +      rc = SQLITE_NOMEM;
       129085  +    }
       129086  +  }
       129087  +  return rc;
       129088  +}
       129089  +
       129090  +/*
       129091  +** Implementation of xBegin() method. 
127982 129092   */
127983 129093   static int fts3BeginMethod(sqlite3_vtab *pVtab){
127984 129094     Fts3Table *p = (Fts3Table*)pVtab;
127985 129095     UNUSED_PARAMETER(pVtab);
127986 129096     assert( p->pSegments==0 );
127987 129097     assert( p->nPendingData==0 );
127988 129098     assert( p->inTransaction!=1 );
127989 129099     TESTONLY( p->inTransaction = 1 );
127990 129100     TESTONLY( p->mxSavepoint = -1; );
127991 129101     p->nLeafAdd = 0;
127992         -  return SQLITE_OK;
       129102  +  return fts3SetHasStat(p);
127993 129103   }
127994 129104   
127995 129105   /*
127996 129106   ** Implementation of xCommit() method. This is a no-op. The contents of
127997 129107   ** the pending-terms hash-table have already been flushed into the database
127998 129108   ** by fts3SyncMethod().
127999 129109   */
................................................................................
128233 129343   static int fts3RenameMethod(
128234 129344     sqlite3_vtab *pVtab,            /* Virtual table handle */
128235 129345     const char *zName               /* New name of table */
128236 129346   ){
128237 129347     Fts3Table *p = (Fts3Table *)pVtab;
128238 129348     sqlite3 *db = p->db;            /* Database connection */
128239 129349     int rc;                         /* Return Code */
       129350  +
       129351  +  /* At this point it must be known if the %_stat table exists or not.
       129352  +  ** So bHasStat may not be 2.  */
       129353  +  rc = fts3SetHasStat(p);
128240 129354   
128241 129355     /* As it happens, the pending terms table is always empty here. This is
128242 129356     ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction 
128243 129357     ** always opens a savepoint transaction. And the xSavepoint() method 
128244 129358     ** flushes the pending terms table. But leave the (no-op) call to
128245 129359     ** PendingTermsFlush() in in case that changes.
128246 129360     */
128247 129361     assert( p->nPendingData==0 );
       129362  +  if( rc==SQLITE_OK ){
128248 129363     rc = sqlite3Fts3PendingTermsFlush(p);
       129364  +  }
128249 129365   
128250 129366     if( p->zContentTbl==0 ){
128251 129367       fts3DbExec(&rc, db,
128252 129368         "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
128253 129369         p->zDb, p->zName, zName
128254 129370       );
128255 129371     }
................................................................................
131157 132273     int *pnConsumed                         /* OUT: Number of bytes consumed */
131158 132274   ){
131159 132275     sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
131160 132276     sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
131161 132277     int rc;
131162 132278     sqlite3_tokenizer_cursor *pCursor;
131163 132279     Fts3Expr *pRet = 0;
131164         -  int nConsumed = 0;
       132280  +  int i = 0;
131165 132281   
131166         -  rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
       132282  +  /* Set variable i to the maximum number of bytes of input to tokenize. */
       132283  +  for(i=0; i<n; i++){
       132284  +    if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
       132285  +    if( z[i]=='*' || z[i]=='"' ) break;
       132286  +  }
       132287  +
       132288  +  *pnConsumed = i;
       132289  +  rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
131167 132290     if( rc==SQLITE_OK ){
131168 132291       const char *zToken;
131169 132292       int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
131170 132293       int nByte;                               /* total space to allocate */
131171 132294   
131172 132295       rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
131173         -
131174         -    if( (rc==SQLITE_OK || rc==SQLITE_DONE) && sqlite3_fts3_enable_parentheses ){
131175         -      int i;
131176         -      if( rc==SQLITE_DONE ) iStart = n;
131177         -      for(i=0; i<iStart; i++){
131178         -        if( z[i]=='(' ){
131179         -          pParse->nNest++;
131180         -          rc = fts3ExprParse(pParse, &z[i+1], n-i-1, &pRet, &nConsumed);
131181         -          if( rc==SQLITE_OK && !pRet ){
131182         -            rc = SQLITE_DONE;
131183         -          }
131184         -          nConsumed = (int)(i + 1 + nConsumed);
131185         -          break;
131186         -        }
131187         -
131188         -        if( z[i]==')' ){
131189         -          rc = SQLITE_DONE;
131190         -          pParse->nNest--;
131191         -          nConsumed = i+1;
131192         -          break;
131193         -        }
131194         -      }
131195         -    }
131196         -
131197         -    if( nConsumed==0 && rc==SQLITE_OK ){
       132296  +    if( rc==SQLITE_OK ){
131198 132297         nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
131199 132298         pRet = (Fts3Expr *)fts3MallocZero(nByte);
131200 132299         if( !pRet ){
131201 132300           rc = SQLITE_NOMEM;
131202 132301         }else{
131203 132302           pRet->eType = FTSQUERY_PHRASE;
131204 132303           pRet->pPhrase = (Fts3Phrase *)&pRet[1];
................................................................................
131224 132323               iStart--;
131225 132324             }else{
131226 132325               break;
131227 132326             }
131228 132327           }
131229 132328   
131230 132329         }
131231         -      nConsumed = iEnd;
       132330  +      *pnConsumed = iEnd;
       132331  +    }else if( i && rc==SQLITE_DONE ){
       132332  +      rc = SQLITE_OK;
131232 132333       }
131233 132334   
131234 132335       pModule->xClose(pCursor);
131235 132336     }
131236 132337     
131237         -  *pnConsumed = nConsumed;
131238 132338     *ppExpr = pRet;
131239 132339     return rc;
131240 132340   }
131241 132341   
131242 132342   
131243 132343   /*
131244 132344   ** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
................................................................................
131480 132580       *pnConsumed = (int)((zInput - z) + ii + 1);
131481 132581       if( ii==nInput ){
131482 132582         return SQLITE_ERROR;
131483 132583       }
131484 132584       return getNextString(pParse, &zInput[1], ii-1, ppExpr);
131485 132585     }
131486 132586   
       132587  +  if( sqlite3_fts3_enable_parentheses ){
       132588  +    if( *zInput=='(' ){
       132589  +      int nConsumed = 0;
       132590  +      pParse->nNest++;
       132591  +      rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
       132592  +      if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; }
       132593  +      *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
       132594  +      return rc;
       132595  +    }else if( *zInput==')' ){
       132596  +      pParse->nNest--;
       132597  +      *pnConsumed = (int)((zInput - z) + 1);
       132598  +      *ppExpr = 0;
       132599  +      return SQLITE_DONE;
       132600  +    }
       132601  +  }
131487 132602   
131488 132603     /* If control flows to this point, this must be a regular token, or 
131489 132604     ** the end of the input. Read a regular token using the sqlite3_tokenizer
131490 132605     ** interface. Before doing so, figure out if there is an explicit
131491 132606     ** column specifier for the token. 
131492 132607     **
131493 132608     ** TODO: Strangely, it is not possible to associate a column specifier
................................................................................
131598 132713     const char *zIn = z;
131599 132714     int rc = SQLITE_OK;
131600 132715     int isRequirePhrase = 1;
131601 132716   
131602 132717     while( rc==SQLITE_OK ){
131603 132718       Fts3Expr *p = 0;
131604 132719       int nByte = 0;
       132720  +
131605 132721       rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
       132722  +    assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
131606 132723       if( rc==SQLITE_OK ){
       132724  +      if( p ){
131607 132725         int isPhrase;
131608 132726   
131609 132727         if( !sqlite3_fts3_enable_parentheses 
131610 132728          && p->eType==FTSQUERY_PHRASE && pParse->isNot 
131611 132729         ){
131612 132730           /* Create an implicit NOT operator. */
131613 132731           Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
................................................................................
131682 132800               pRet = p;
131683 132801             }
131684 132802           }else{
131685 132803             insertBinaryOperator(&pRet, pPrev, p);
131686 132804           }
131687 132805           isRequirePhrase = !isPhrase;
131688 132806         }
       132807  +        pPrev = p;
       132808  +      }
131689 132809         assert( nByte>0 );
131690 132810       }
131691 132811       assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
131692 132812       nIn -= nByte;
131693 132813       zIn += nByte;
131694         -    pPrev = p;
131695 132814     }
131696 132815   
131697 132816     if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
131698 132817       rc = SQLITE_ERROR;
131699 132818     }
131700 132819   
131701 132820     if( rc==SQLITE_DONE ){
................................................................................
134675 135794     char *zTerm;                    /* Pointer to previous term buffer */
134676 135795     int nTerm;                      /* Number of bytes in zTerm */
134677 135796     int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
134678 135797     char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
134679 135798     int nSize;                      /* Size of allocation at aData */
134680 135799     int nData;                      /* Bytes of data in aData */
134681 135800     char *aData;                    /* Pointer to block from malloc() */
       135801  +  i64 nLeafData;                  /* Number of bytes of leaf data written */
134682 135802   };
134683 135803   
134684 135804   /*
134685 135805   ** Type SegmentNode is used by the following three functions to create
134686 135806   ** the interior part of the segment b+-tree structures (everything except
134687 135807   ** the leaf nodes). These functions and type are only ever used by code
134688 135808   ** within the fts3SegWriterXXX() family of functions described above.
................................................................................
134749 135869   #define SQL_DELETE_SEGDIR_ENTRY       30
134750 135870   #define SQL_SHIFT_SEGDIR_ENTRY        31
134751 135871   #define SQL_SELECT_SEGDIR             32
134752 135872   #define SQL_CHOMP_SEGDIR              33
134753 135873   #define SQL_SEGMENT_IS_APPENDABLE     34
134754 135874   #define SQL_SELECT_INDEXES            35
134755 135875   #define SQL_SELECT_MXLEVEL            36
       135876  +
       135877  +#define SQL_SELECT_LEVEL_RANGE2       37
       135878  +#define SQL_UPDATE_LEVEL_IDX          38
       135879  +#define SQL_UPDATE_LEVEL              39
134756 135880   
134757 135881   /*
134758 135882   ** This function is used to obtain an SQLite prepared statement handle
134759 135883   ** for the statement identified by the second argument. If successful,
134760 135884   ** *pp is set to the requested statement handle and SQLITE_OK returned.
134761 135885   ** Otherwise, an SQLite error code is returned and *pp is set to 0.
134762 135886   **
................................................................................
134851 135975   
134852 135976   /* SQL_SELECT_INDEXES
134853 135977   **   Return the list of valid segment indexes for absolute level ?  */
134854 135978   /* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
134855 135979   
134856 135980   /* SQL_SELECT_MXLEVEL
134857 135981   **   Return the largest relative level in the FTS index or indexes.  */
134858         -/* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
       135982  +/* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
       135983  +
       135984  +          /* Return segments in order from oldest to newest.*/ 
       135985  +/* 37 */  "SELECT level, idx, end_block "
       135986  +            "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
       135987  +            "ORDER BY level DESC, idx ASC",
       135988  +
       135989  +          /* Update statements used while promoting segments */
       135990  +/* 38 */  "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
       135991  +            "WHERE level=? AND idx=?",
       135992  +/* 39 */  "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
       135993  +
134859 135994     };
134860 135995     int rc = SQLITE_OK;
134861 135996     sqlite3_stmt *pStmt;
134862 135997   
134863 135998     assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
134864 135999     assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
134865 136000     
................................................................................
136392 137527   static int fts3WriteSegdir(
136393 137528     Fts3Table *p,                   /* Virtual table handle */
136394 137529     sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
136395 137530     int iIdx,                       /* Value for "idx" field */
136396 137531     sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
136397 137532     sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
136398 137533     sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
       137534  +  sqlite3_int64 nLeafData,        /* Bytes of leaf data in segment */
136399 137535     char *zRoot,                    /* Blob value for "root" field */
136400 137536     int nRoot                       /* Number of bytes in buffer zRoot */
136401 137537   ){
136402 137538     sqlite3_stmt *pStmt;
136403 137539     int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
136404 137540     if( rc==SQLITE_OK ){
136405 137541       sqlite3_bind_int64(pStmt, 1, iLevel);
136406 137542       sqlite3_bind_int(pStmt, 2, iIdx);
136407 137543       sqlite3_bind_int64(pStmt, 3, iStartBlock);
136408 137544       sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
       137545  +    if( nLeafData==0 ){
136409 137546       sqlite3_bind_int64(pStmt, 5, iEndBlock);
       137547  +    }else{
       137548  +      char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
       137549  +      if( !zEnd ) return SQLITE_NOMEM;
       137550  +      sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
       137551  +    }
136410 137552       sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
136411 137553       sqlite3_step(pStmt);
136412 137554       rc = sqlite3_reset(pStmt);
136413 137555     }
136414 137556     return rc;
136415 137557   }
136416 137558   
................................................................................
136727 137869       nSuffix = nTerm;
136728 137870       nReq = 1 +                              /* varint containing prefix size */
136729 137871         sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
136730 137872         nTerm +                               /* Term suffix */
136731 137873         sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
136732 137874         nDoclist;                             /* Doclist data */
136733 137875     }
       137876  +
       137877  +  /* Increase the total number of bytes written to account for the new entry. */
       137878  +  pWriter->nLeafData += nReq;
136734 137879   
136735 137880     /* If the buffer currently allocated is too small for this entry, realloc
136736 137881     ** the buffer to make it large enough.
136737 137882     */
136738 137883     if( nReq>pWriter->nSize ){
136739 137884       char *aNew = sqlite3_realloc(pWriter->aData, nReq);
136740 137885       if( !aNew ) return SQLITE_NOMEM;
................................................................................
136799 137944       iLastLeaf = pWriter->iFree;
136800 137945       rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
136801 137946       if( rc==SQLITE_OK ){
136802 137947         rc = fts3NodeWrite(p, pWriter->pTree, 1,
136803 137948             pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
136804 137949       }
136805 137950       if( rc==SQLITE_OK ){
136806         -      rc = fts3WriteSegdir(
136807         -          p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
       137951  +      rc = fts3WriteSegdir(p, iLevel, iIdx, 
       137952  +          pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
136808 137953       }
136809 137954     }else{
136810 137955       /* The entire tree fits on the root node. Write it to the segdir table. */
136811         -    rc = fts3WriteSegdir(
136812         -        p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
       137956  +    rc = fts3WriteSegdir(p, iLevel, iIdx, 
       137957  +        0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
136813 137958     }
136814 137959     p->nLeafAdd++;
136815 137960     return rc;
136816 137961   }
136817 137962   
136818 137963   /*
136819 137964   ** Release all memory held by the SegmentWriter object passed as the 
................................................................................
136888 138033         getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
136889 138034     );
136890 138035     if( SQLITE_ROW==sqlite3_step(pStmt) ){
136891 138036       *pnMax = sqlite3_column_int64(pStmt, 0);
136892 138037     }
136893 138038     return sqlite3_reset(pStmt);
136894 138039   }
       138040  +
       138041  +/*
       138042  +** iAbsLevel is an absolute level that may be assumed to exist within
       138043  +** the database. This function checks if it is the largest level number
       138044  +** within its index. Assuming no error occurs, *pbMax is set to 1 if
       138045  +** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
       138046  +** is returned. If an error occurs, an error code is returned and the
       138047  +** final value of *pbMax is undefined.
       138048  +*/
       138049  +static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
       138050  +
       138051  +  /* Set pStmt to the compiled version of:
       138052  +  **
       138053  +  **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
       138054  +  **
       138055  +  ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
       138056  +  */
       138057  +  sqlite3_stmt *pStmt;
       138058  +  int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
       138059  +  if( rc!=SQLITE_OK ) return rc;
       138060  +  sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
       138061  +  sqlite3_bind_int64(pStmt, 2, 
       138062  +      ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
       138063  +  );
       138064  +
       138065  +  *pbMax = 0;
       138066  +  if( SQLITE_ROW==sqlite3_step(pStmt) ){
       138067  +    *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
       138068  +  }
       138069  +  return sqlite3_reset(pStmt);
       138070  +}
136895 138071   
136896 138072   /*
136897 138073   ** Delete all entries in the %_segments table associated with the segment
136898 138074   ** opened with seg-reader pSeg. This function does not affect the contents
136899 138075   ** of the %_segdir table.
136900 138076   */
136901 138077   static int fts3DeleteSegment(
................................................................................
137423 138599       sqlite3_free(pCsr->aBuffer);
137424 138600   
137425 138601       pCsr->nSegment = 0;
137426 138602       pCsr->apSegment = 0;
137427 138603       pCsr->aBuffer = 0;
137428 138604     }
137429 138605   }
       138606  +
       138607  +/*
       138608  +** Decode the "end_block" field, selected by column iCol of the SELECT 
       138609  +** statement passed as the first argument. 
       138610  +**
       138611  +** The "end_block" field may contain either an integer, or a text field
       138612  +** containing the text representation of two non-negative integers separated 
       138613  +** by one or more space (0x20) characters. In the first case, set *piEndBlock 
       138614  +** to the integer value and *pnByte to zero before returning. In the second, 
       138615  +** set *piEndBlock to the first value and *pnByte to the second.
       138616  +*/
       138617  +static void fts3ReadEndBlockField(
       138618  +  sqlite3_stmt *pStmt, 
       138619  +  int iCol, 
       138620  +  i64 *piEndBlock,
       138621  +  i64 *pnByte
       138622  +){
       138623  +  const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
       138624  +  if( zText ){
       138625  +    int i;
       138626  +    int iMul = 1;
       138627  +    i64 iVal = 0;
       138628  +    for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
       138629  +      iVal = iVal*10 + (zText[i] - '0');
       138630  +    }
       138631  +    *piEndBlock = iVal;
       138632  +    while( zText[i]==' ' ) i++;
       138633  +    iVal = 0;
       138634  +    if( zText[i]=='-' ){
       138635  +      i++;
       138636  +      iMul = -1;
       138637  +    }
       138638  +    for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
       138639  +      iVal = iVal*10 + (zText[i] - '0');
       138640  +    }
       138641  +    *pnByte = (iVal * (i64)iMul);
       138642  +  }
       138643  +}
       138644  +
       138645  +
       138646  +/*
       138647  +** A segment of size nByte bytes has just been written to absolute level
       138648  +** iAbsLevel. Promote any segments that should be promoted as a result.
       138649  +*/
       138650  +static int fts3PromoteSegments(
       138651  +  Fts3Table *p,                   /* FTS table handle */
       138652  +  sqlite3_int64 iAbsLevel,        /* Absolute level just updated */
       138653  +  sqlite3_int64 nByte             /* Size of new segment at iAbsLevel */
       138654  +){
       138655  +  int rc = SQLITE_OK;
       138656  +  sqlite3_stmt *pRange;
       138657  +
       138658  +  rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
       138659  +
       138660  +  if( rc==SQLITE_OK ){
       138661  +    int bOk = 0;
       138662  +    i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
       138663  +    i64 nLimit = (nByte*3)/2;
       138664  +
       138665  +    /* Loop through all entries in the %_segdir table corresponding to 
       138666  +    ** segments in this index on levels greater than iAbsLevel. If there is
       138667  +    ** at least one such segment, and it is possible to determine that all 
       138668  +    ** such segments are smaller than nLimit bytes in size, they will be 
       138669  +    ** promoted to level iAbsLevel.  */
       138670  +    sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
       138671  +    sqlite3_bind_int64(pRange, 2, iLast);
       138672  +    while( SQLITE_ROW==sqlite3_step(pRange) ){
       138673  +      i64 nSize = 0, dummy;
       138674  +      fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
       138675  +      if( nSize<=0 || nSize>nLimit ){
       138676  +        /* If nSize==0, then the %_segdir.end_block field does not not 
       138677  +        ** contain a size value. This happens if it was written by an
       138678  +        ** old version of FTS. In this case it is not possible to determine
       138679  +        ** the size of the segment, and so segment promotion does not
       138680  +        ** take place.  */
       138681  +        bOk = 0;
       138682  +        break;
       138683  +      }
       138684  +      bOk = 1;
       138685  +    }
       138686  +    rc = sqlite3_reset(pRange);
       138687  +
       138688  +    if( bOk ){
       138689  +      int iIdx = 0;
       138690  +      sqlite3_stmt *pUpdate1;
       138691  +      sqlite3_stmt *pUpdate2;
       138692  +
       138693  +      if( rc==SQLITE_OK ){
       138694  +        rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
       138695  +      }
       138696  +      if( rc==SQLITE_OK ){
       138697  +        rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
       138698  +      }
       138699  +
       138700  +      if( rc==SQLITE_OK ){
       138701  +
       138702  +        /* Loop through all %_segdir entries for segments in this index with
       138703  +        ** levels equal to or greater than iAbsLevel. As each entry is visited,
       138704  +        ** updated it to set (level = -1) and (idx = N), where N is 0 for the
       138705  +        ** oldest segment in the range, 1 for the next oldest, and so on.
       138706  +        **
       138707  +        ** In other words, move all segments being promoted to level -1,
       138708  +        ** setting the "idx" fields as appropriate to keep them in the same
       138709  +        ** order. The contents of level -1 (which is never used, except
       138710  +        ** transiently here), will be moved back to level iAbsLevel below.  */
       138711  +        sqlite3_bind_int64(pRange, 1, iAbsLevel);
       138712  +        while( SQLITE_ROW==sqlite3_step(pRange) ){
       138713  +          sqlite3_bind_int(pUpdate1, 1, iIdx++);
       138714  +          sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
       138715  +          sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
       138716  +          sqlite3_step(pUpdate1);
       138717  +          rc = sqlite3_reset(pUpdate1);
       138718  +          if( rc!=SQLITE_OK ){
       138719  +            sqlite3_reset(pRange);
       138720  +            break;
       138721  +          }
       138722  +        }
       138723  +      }
       138724  +      if( rc==SQLITE_OK ){
       138725  +        rc = sqlite3_reset(pRange);
       138726  +      }
       138727  +
       138728  +      /* Move level -1 to level iAbsLevel */
       138729  +      if( rc==SQLITE_OK ){
       138730  +        sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
       138731  +        sqlite3_step(pUpdate2);
       138732  +        rc = sqlite3_reset(pUpdate2);
       138733  +      }
       138734  +    }
       138735  +  }
       138736  +
       138737  +
       138738  +  return rc;
       138739  +}
137430 138740   
137431 138741   /*
137432 138742   ** Merge all level iLevel segments in the database into a single 
137433 138743   ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
137434 138744   ** single segment with a level equal to the numerically largest level 
137435 138745   ** currently present in the database.
137436 138746   **
................................................................................
137448 138758     int rc;                         /* Return code */
137449 138759     int iIdx = 0;                   /* Index of new segment */
137450 138760     sqlite3_int64 iNewLevel = 0;    /* Level/index to create new segment at */
137451 138761     SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
137452 138762     Fts3SegFilter filter;           /* Segment term filter condition */
137453 138763     Fts3MultiSegReader csr;         /* Cursor to iterate through level(s) */
137454 138764     int bIgnoreEmpty = 0;           /* True to ignore empty segments */
       138765  +  i64 iMaxLevel = 0;              /* Max level number for this index/langid */
137455 138766   
137456 138767     assert( iLevel==FTS3_SEGCURSOR_ALL
137457 138768          || iLevel==FTS3_SEGCURSOR_PENDING
137458 138769          || iLevel>=0
137459 138770     );
137460 138771     assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
137461 138772     assert( iIndex>=0 && iIndex<p->nIndex );
137462 138773   
137463 138774     rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
137464 138775     if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
       138776  +
       138777  +  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
       138778  +    rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
       138779  +    if( rc!=SQLITE_OK ) goto finished;
       138780  +  }
137465 138781   
137466 138782     if( iLevel==FTS3_SEGCURSOR_ALL ){
137467 138783       /* This call is to merge all segments in the database to a single
137468 138784       ** segment. The level of the new segment is equal to the numerically
137469 138785       ** greatest segment level currently present in the database for this
137470 138786       ** index. The idx of the new segment is always 0.  */
137471 138787       if( csr.nSegment==1 ){
137472 138788         rc = SQLITE_DONE;
137473 138789         goto finished;
137474 138790       }
137475         -    rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iNewLevel);
       138791  +    iNewLevel = iMaxLevel;
137476 138792       bIgnoreEmpty = 1;
137477 138793   
137478         -  }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
137479         -    iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, 0);
137480         -    rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, 0, &iIdx);
137481 138794     }else{
137482 138795       /* This call is to merge all segments at level iLevel. find the next
137483 138796       ** available segment index at level iLevel+1. The call to
137484 138797       ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to 
137485 138798       ** a single iLevel+2 segment if necessary.  */
137486         -    rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
       138799  +    assert( FTS3_SEGCURSOR_PENDING==-1 );
137487 138800       iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
       138801  +    rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
       138802  +    bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
137488 138803     }
137489 138804     if( rc!=SQLITE_OK ) goto finished;
       138805  +
137490 138806     assert( csr.nSegment>0 );
137491 138807     assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
137492 138808     assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
137493 138809   
137494 138810     memset(&filter, 0, sizeof(Fts3SegFilter));
137495 138811     filter.flags = FTS3_SEGMENT_REQUIRE_POS;
137496 138812     filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
................................................................................
137499 138815     while( SQLITE_OK==rc ){
137500 138816       rc = sqlite3Fts3SegReaderStep(p, &csr);
137501 138817       if( rc!=SQLITE_ROW ) break;
137502 138818       rc = fts3SegWriterAdd(p, &pWriter, 1, 
137503 138819           csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
137504 138820     }
137505 138821     if( rc!=SQLITE_OK ) goto finished;
137506         -  assert( pWriter );
       138822  +  assert( pWriter || bIgnoreEmpty );
137507 138823   
137508 138824     if( iLevel!=FTS3_SEGCURSOR_PENDING ){
137509 138825       rc = fts3DeleteSegdir(
137510 138826           p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
137511 138827       );
137512 138828       if( rc!=SQLITE_OK ) goto finished;
137513 138829     }
       138830  +  if( pWriter ){
137514 138831     rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
       138832  +    if( rc==SQLITE_OK ){
       138833  +      if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
       138834  +        rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
       138835  +      }
       138836  +    }
       138837  +  }
137515 138838   
137516 138839    finished:
137517 138840     fts3SegWriterFree(pWriter);
137518 138841     sqlite3Fts3SegReaderFinish(&csr);
137519 138842     return rc;
137520 138843   }
137521 138844   
................................................................................
137533 138856     }
137534 138857     sqlite3Fts3PendingTermsClear(p);
137535 138858   
137536 138859     /* Determine the auto-incr-merge setting if unknown.  If enabled,
137537 138860     ** estimate the number of leaf blocks of content to be written
137538 138861     */
137539 138862     if( rc==SQLITE_OK && p->bHasStat
137540         -   && p->bAutoincrmerge==0xff && p->nLeafAdd>0
       138863  +   && p->nAutoincrmerge==0xff && p->nLeafAdd>0
137541 138864     ){
137542 138865       sqlite3_stmt *pStmt = 0;
137543 138866       rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
137544 138867       if( rc==SQLITE_OK ){
137545 138868         sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
137546 138869         rc = sqlite3_step(pStmt);
137547         -      p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
       138870  +      if( rc==SQLITE_ROW ){
       138871  +        p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
       138872  +        if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
       138873  +      }else if( rc==SQLITE_DONE ){
       138874  +        p->nAutoincrmerge = 0;
       138875  +      }
137548 138876         rc = sqlite3_reset(pStmt);
137549 138877       }
137550 138878     }
137551 138879     return rc;
137552 138880   }
137553 138881   
137554 138882   /*
................................................................................
137908 139236   struct IncrmergeWriter {
137909 139237     int nLeafEst;                   /* Space allocated for leaf blocks */
137910 139238     int nWork;                      /* Number of leaf pages flushed */
137911 139239     sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
137912 139240     int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
137913 139241     sqlite3_int64 iStart;           /* Block number of first allocated block */
137914 139242     sqlite3_int64 iEnd;             /* Block number of last allocated block */
       139243  +  sqlite3_int64 nLeafData;        /* Bytes of leaf page data so far */
       139244  +  u8 bNoLeafData;                 /* If true, store 0 for segment size */
137915 139245     NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
137916 139246   };
137917 139247   
137918 139248   /*
137919 139249   ** An object of the following type is used to read data from a single
137920 139250   ** FTS segment node. See the following functions:
137921 139251   **
................................................................................
138246 139576   
138247 139577       nSuffix = nTerm;
138248 139578       nSpace  = 1;
138249 139579       nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
138250 139580       nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
138251 139581     }
138252 139582   
       139583  +  pWriter->nLeafData += nSpace;
138253 139584     blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
138254         -
138255 139585     if( rc==SQLITE_OK ){
138256 139586       if( pLeaf->block.n==0 ){
138257 139587         pLeaf->block.n = 1;
138258 139588         pLeaf->block.a[0] = '\0';
138259 139589       }
138260 139590       rc = fts3AppendToNode(
138261 139591           &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
................................................................................
138346 139676     if( rc==SQLITE_OK ){
138347 139677       rc = fts3WriteSegdir(p, 
138348 139678           pWriter->iAbsLevel+1,               /* level */
138349 139679           pWriter->iIdx,                      /* idx */
138350 139680           pWriter->iStart,                    /* start_block */
138351 139681           pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
138352 139682           pWriter->iEnd,                      /* end_block */
       139683  +        (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0),   /* end_block */
138353 139684           pRoot->block.a, pRoot->block.n      /* root */
138354 139685       );
138355 139686     }
138356 139687     sqlite3_free(pRoot->block.a);
138357 139688     sqlite3_free(pRoot->key.a);
138358 139689   
138359 139690     *pRc = rc;
................................................................................
138447 139778   
138448 139779       /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
138449 139780       sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
138450 139781       sqlite3_bind_int(pSelect, 2, iIdx);
138451 139782       if( sqlite3_step(pSelect)==SQLITE_ROW ){
138452 139783         iStart = sqlite3_column_int64(pSelect, 1);
138453 139784         iLeafEnd = sqlite3_column_int64(pSelect, 2);
138454         -      iEnd = sqlite3_column_int64(pSelect, 3);
       139785  +      fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
       139786  +      if( pWriter->nLeafData<0 ){
       139787  +        pWriter->nLeafData = pWriter->nLeafData * -1;
       139788  +      }
       139789  +      pWriter->bNoLeafData = (pWriter->nLeafData==0);
138455 139790         nRoot = sqlite3_column_bytes(pSelect, 4);
138456 139791         aRoot = sqlite3_column_blob(pSelect, 4);
138457 139792       }else{
138458 139793         return sqlite3_reset(pSelect);
138459 139794       }
138460 139795   
138461 139796       /* Check for the zero-length marker in the %_segments table */
................................................................................
139048 140383     return SQLITE_OK;
139049 140384   }
139050 140385   
139051 140386   
139052 140387   /*
139053 140388   ** Attempt an incremental merge that writes nMerge leaf blocks.
139054 140389   **
139055         -** Incremental merges happen nMin segments at a time. The two
139056         -** segments to be merged are the nMin oldest segments (the ones with
139057         -** the smallest indexes) in the highest level that contains at least
139058         -** nMin segments. Multiple merges might occur in an attempt to write the 
139059         -** quota of nMerge leaf blocks.
       140390  +** Incremental merges happen nMin segments at a time. The segments 
       140391  +** to be merged are the nMin oldest segments (the ones with the smallest 
       140392  +** values for the _segdir.idx field) in the highest level that contains 
       140393  +** at least nMin segments. Multiple merges might occur in an attempt to 
       140394  +** write the quota of nMerge leaf blocks.
139060 140395   */
139061 140396   SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
139062 140397     int rc;                         /* Return code */
139063 140398     int nRem = nMerge;              /* Number of leaf pages yet to  be written */
139064 140399     Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
139065 140400     Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
139066 140401     IncrmergeWriter *pWriter;       /* Writer object */
................................................................................
139077 140412     pCsr = (Fts3MultiSegReader *)&pFilter[1];
139078 140413   
139079 140414     rc = fts3IncrmergeHintLoad(p, &hint);
139080 140415     while( rc==SQLITE_OK && nRem>0 ){
139081 140416       const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
139082 140417       sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
139083 140418       int bUseHint = 0;             /* True if attempting to append */
       140419  +    int iIdx = 0;                 /* Largest idx in level (iAbsLevel+1) */
139084 140420   
139085 140421       /* Search the %_segdir table for the absolute level with the smallest
139086 140422       ** relative level number that contains at least nMin segments, if any.
139087 140423       ** If one is found, set iAbsLevel to the absolute level number and
139088 140424       ** nSeg to nMin. If no level with at least nMin segments can be found, 
139089 140425       ** set nSeg to -1.
139090 140426       */
................................................................................
139130 140466       ** indexes of absolute level iAbsLevel. If this cursor is opened using 
139131 140467       ** the 'hint' parameters, it is possible that there are less than nSeg
139132 140468       ** segments available in level iAbsLevel. In this case, no work is
139133 140469       ** done on iAbsLevel - fall through to the next iteration of the loop 
139134 140470       ** to start work on some other level.  */
139135 140471       memset(pWriter, 0, nAlloc);
139136 140472       pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
       140473  +
       140474  +    if( rc==SQLITE_OK ){
       140475  +      rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
       140476  +      assert( bUseHint==1 || bUseHint==0 );
       140477  +      if( iIdx==0 || (bUseHint && iIdx==1) ){
       140478  +        int bIgnore = 0;
       140479  +        rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
       140480  +        if( bIgnore ){
       140481  +          pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
       140482  +        }
       140483  +      }
       140484  +    }
       140485  +
139137 140486       if( rc==SQLITE_OK ){
139138 140487         rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
139139 140488       }
139140 140489       if( SQLITE_OK==rc && pCsr->nSegment==nSeg
139141 140490        && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
139142 140491        && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
139143 140492       ){
139144         -      int iIdx = 0;               /* Largest idx in level (iAbsLevel+1) */
139145         -      rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
139146         -      if( rc==SQLITE_OK ){
139147 140493           if( bUseHint && iIdx>0 ){
139148 140494             const char *zKey = pCsr->zTerm;
139149 140495             int nKey = pCsr->nTerm;
139150 140496             rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
139151 140497           }else{
139152 140498             rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
139153 140499           }
139154         -      }
139155 140500   
139156 140501         if( rc==SQLITE_OK && pWriter->nLeafEst ){
139157 140502           fts3LogMerge(nSeg, iAbsLevel);
139158 140503           do {
139159 140504             rc = fts3IncrmergeAppend(p, pWriter, pCsr);
139160 140505             if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
139161 140506             if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
................................................................................
139168 140513             if( nSeg!=0 ){
139169 140514               bDirtyHint = 1;
139170 140515               fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
139171 140516             }
139172 140517           }
139173 140518         }
139174 140519   
       140520  +      if( nSeg!=0 ){
       140521  +        pWriter->nLeafData = pWriter->nLeafData * -1;
       140522  +      }
139175 140523         fts3IncrmergeRelease(p, pWriter, &rc);
       140524  +      if( nSeg==0 && pWriter->bNoLeafData==0 ){
       140525  +        fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
       140526  +      }
139176 140527       }
139177 140528   
139178 140529       sqlite3Fts3SegReaderFinish(pCsr);
139179 140530     }
139180 140531   
139181 140532     /* Write the hint values into the %_stat table for the next incr-merger */
139182 140533     if( bDirtyHint && rc==SQLITE_OK ){
................................................................................
139255 140606   */
139256 140607   static int fts3DoAutoincrmerge(
139257 140608     Fts3Table *p,                   /* FTS3 table handle */
139258 140609     const char *zParam              /* Nul-terminated string containing boolean */
139259 140610   ){
139260 140611     int rc = SQLITE_OK;
139261 140612     sqlite3_stmt *pStmt = 0;
139262         -  p->bAutoincrmerge = fts3Getint(&zParam)!=0;
       140613  +  p->nAutoincrmerge = fts3Getint(&zParam);
       140614  +  if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
       140615  +    p->nAutoincrmerge = 8;
       140616  +  }
139263 140617     if( !p->bHasStat ){
139264 140618       assert( p->bFts4==0 );
139265 140619       sqlite3Fts3CreateStatTable(&rc, p);
139266 140620       if( rc ) return rc;
139267 140621     }
139268 140622     rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
139269 140623     if( rc ) return rc;
139270 140624     sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
139271         -  sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
       140625  +  sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
139272 140626     sqlite3_step(pStmt);
139273 140627     rc = sqlite3_reset(pStmt);
139274 140628     return rc;
139275 140629   }
139276 140630   
139277 140631   /*
139278 140632   ** Return a 64-bit checksum for the FTS index entry specified by the
................................................................................
139752 141106     Fts3Table *p = (Fts3Table *)pVtab;
139753 141107     int rc = SQLITE_OK;             /* Return Code */
139754 141108     int isRemove = 0;               /* True for an UPDATE or DELETE */
139755 141109     u32 *aSzIns = 0;                /* Sizes of inserted documents */
139756 141110     u32 *aSzDel = 0;                /* Sizes of deleted documents */
139757 141111     int nChng = 0;                  /* Net change in number of documents */
139758 141112     int bInsertDone = 0;
       141113  +
       141114  +  /* At this point it must be known if the %_stat table exists or not.
       141115  +  ** So bHasStat may not be 2.  */
       141116  +  assert( p->bHasStat==0 || p->bHasStat==1 );
139759 141117   
139760 141118     assert( p->pSegments==0 );
139761 141119     assert( 
139762 141120         nArg==1                     /* DELETE operations */
139763 141121      || nArg==(2 + p->nColumn + 3)  /* INSERT or UPDATE operations */
139764 141122     );
139765 141123   
................................................................................
142243 143601   **      of 4-byte coordinates. For leaf nodes the integer is the rowid
142244 143602   **      of a record. For internal nodes it is the node number of a
142245 143603   **      child page.
142246 143604   */
142247 143605   
142248 143606   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
142249 143607   
142250         -/*
142251         -** This file contains an implementation of a couple of different variants
142252         -** of the r-tree algorithm. See the README file for further details. The 
142253         -** same data-structure is used for all, but the algorithms for insert and
142254         -** delete operations vary. The variants used are selected at compile time 
142255         -** by defining the following symbols:
142256         -*/
142257         -
142258         -/* Either, both or none of the following may be set to activate 
142259         -** r*tree variant algorithms.
142260         -*/
142261         -#define VARIANT_RSTARTREE_CHOOSESUBTREE 0
142262         -#define VARIANT_RSTARTREE_REINSERT      1
142263         -
142264         -/* 
142265         -** Exactly one of the following must be set to 1.
142266         -*/
142267         -#define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
142268         -#define VARIANT_GUTTMAN_LINEAR_SPLIT    0
142269         -#define VARIANT_RSTARTREE_SPLIT         1
142270         -
142271         -#define VARIANT_GUTTMAN_SPLIT \
142272         -        (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
142273         -
142274         -#if VARIANT_GUTTMAN_QUADRATIC_SPLIT
142275         -  #define PickNext QuadraticPickNext
142276         -  #define PickSeeds QuadraticPickSeeds
142277         -  #define AssignCells splitNodeGuttman
142278         -#endif
142279         -#if VARIANT_GUTTMAN_LINEAR_SPLIT
142280         -  #define PickNext LinearPickNext
142281         -  #define PickSeeds LinearPickSeeds
142282         -  #define AssignCells splitNodeGuttman
142283         -#endif
142284         -#if VARIANT_RSTARTREE_SPLIT
142285         -  #define AssignCells splitNodeStartree
142286         -#endif
142287         -
142288         -#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
142289         -# define NDEBUG 1
142290         -#endif
142291         -
142292 143608   #ifndef SQLITE_CORE
142293 143609     SQLITE_EXTENSION_INIT1
142294 143610   #else
142295 143611   #endif
142296 143612   
142297 143613   /* #include <string.h> */
142298 143614   /* #include <assert.h> */
       143615  +/* #include <stdio.h> */
142299 143616   
142300 143617   #ifndef SQLITE_AMALGAMATION
142301 143618   #include "sqlite3rtree.h"
142302 143619   typedef sqlite3_int64 i64;
142303 143620   typedef unsigned char u8;
       143621  +typedef unsigned short u16;
142304 143622   typedef unsigned int u32;
142305 143623   #endif
142306 143624   
142307 143625   /*  The following macro is used to suppress compiler warnings.
142308 143626   */
142309 143627   #ifndef UNUSED_PARAMETER
142310 143628   # define UNUSED_PARAMETER(x) (void)(x)
................................................................................
142314 143632   typedef struct RtreeCursor RtreeCursor;
142315 143633   typedef struct RtreeNode RtreeNode;
142316 143634   typedef struct RtreeCell RtreeCell;
142317 143635   typedef struct RtreeConstraint RtreeConstraint;
142318 143636   typedef struct RtreeMatchArg RtreeMatchArg;
142319 143637   typedef struct RtreeGeomCallback RtreeGeomCallback;
142320 143638   typedef union RtreeCoord RtreeCoord;
       143639  +typedef struct RtreeSearchPoint RtreeSearchPoint;
142321 143640   
142322 143641   /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
142323 143642   #define RTREE_MAX_DIMENSIONS 5
142324 143643   
142325 143644   /* Size of hash table Rtree.aHash. This hash table is not expected to
142326 143645   ** ever contain very many entries, so a fixed number of buckets is 
142327 143646   ** used.
142328 143647   */
142329         -#define HASHSIZE 128
       143648  +#define HASHSIZE 97
142330 143649   
142331 143650   /* The xBestIndex method of this virtual table requires an estimate of
142332 143651   ** the number of rows in the virtual table to calculate the costs of
142333 143652   ** various strategies. If possible, this estimate is loaded from the
142334 143653   ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
142335 143654   ** Otherwise, if no sqlite_stat1 entry is available, use 
142336 143655   ** RTREE_DEFAULT_ROWEST.
................................................................................
142338 143657   #define RTREE_DEFAULT_ROWEST 1048576
142339 143658   #define RTREE_MIN_ROWEST         100
142340 143659   
142341 143660   /* 
142342 143661   ** An rtree virtual-table object.
142343 143662   */
142344 143663   struct Rtree {
142345         -  sqlite3_vtab base;
       143664  +  sqlite3_vtab base;          /* Base class.  Must be first */
142346 143665     sqlite3 *db;                /* Host database connection */
142347 143666     int iNodeSize;              /* Size in bytes of each node in the node table */
142348         -  int nDim;                   /* Number of dimensions */
142349         -  int nBytesPerCell;          /* Bytes consumed per cell */
       143667  +  u8 nDim;                    /* Number of dimensions */
       143668  +  u8 eCoordType;              /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
       143669  +  u8 nBytesPerCell;           /* Bytes consumed per cell */
142350 143670     int iDepth;                 /* Current depth of the r-tree structure */
142351 143671     char *zDb;                  /* Name of database containing r-tree table */
142352 143672     char *zName;                /* Name of r-tree table */ 
142353         -  RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
142354 143673     int nBusy;                  /* Current number of users of this structure */
142355 143674     i64 nRowEst;                /* Estimated number of rows in this table */
142356 143675   
142357 143676     /* List of nodes removed during a CondenseTree operation. List is
142358 143677     ** linked together via the pointer normally used for hash chains -
142359 143678     ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
142360 143679     ** headed by the node (leaf nodes have RtreeNode.iNode==0).
................................................................................
142373 143692     sqlite3_stmt *pDeleteRowid;
142374 143693   
142375 143694     /* Statements to read/write/delete a record from xxx_parent */
142376 143695     sqlite3_stmt *pReadParent;
142377 143696     sqlite3_stmt *pWriteParent;
142378 143697     sqlite3_stmt *pDeleteParent;
142379 143698   
142380         -  int eCoordType;
       143699  +  RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
142381 143700   };
142382 143701   
142383         -/* Possible values for eCoordType: */
       143702  +/* Possible values for Rtree.eCoordType: */
142384 143703   #define RTREE_COORD_REAL32 0
142385 143704   #define RTREE_COORD_INT32  1
142386 143705   
142387 143706   /*
142388 143707   ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
142389 143708   ** only deal with integer coordinates.  No floating point operations
142390 143709   ** will be done.
142391 143710   */
142392 143711   #ifdef SQLITE_RTREE_INT_ONLY
142393 143712     typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
142394 143713     typedef int RtreeValue;                  /* Low accuracy coordinate */
       143714  +# define RTREE_ZERO 0
142395 143715   #else
142396 143716     typedef double RtreeDValue;              /* High accuracy coordinate */
142397 143717     typedef float RtreeValue;                /* Low accuracy coordinate */
       143718  +# define RTREE_ZERO 0.0
142398 143719   #endif
       143720  +
       143721  +/*
       143722  +** When doing a search of an r-tree, instances of the following structure
       143723  +** record intermediate results from the tree walk.
       143724  +**
       143725  +** The id is always a node-id.  For iLevel>=1 the id is the node-id of
       143726  +** the node that the RtreeSearchPoint represents.  When iLevel==0, however,
       143727  +** the id is of the parent node and the cell that RtreeSearchPoint
       143728  +** represents is the iCell-th entry in the parent node.
       143729  +*/
       143730  +struct RtreeSearchPoint {
       143731  +  RtreeDValue rScore;    /* The score for this node.  Smallest goes first. */
       143732  +  sqlite3_int64 id;      /* Node ID */
       143733  +  u8 iLevel;             /* 0=entries.  1=leaf node.  2+ for higher */
       143734  +  u8 eWithin;            /* PARTLY_WITHIN or FULLY_WITHIN */
       143735  +  u8 iCell;              /* Cell index within the node */
       143736  +};
142399 143737   
142400 143738   /*
142401 143739   ** The minimum number of cells allowed for a node is a third of the 
142402 143740   ** maximum. In Gutman's notation:
142403 143741   **
142404 143742   **     m = M/3
142405 143743   **
................................................................................
142415 143753   ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
142416 143754   ** Therefore all non-root nodes must contain at least 3 entries. Since 
142417 143755   ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
142418 143756   ** 40 or less.
142419 143757   */
142420 143758   #define RTREE_MAX_DEPTH 40
142421 143759   
       143760  +
       143761  +/*
       143762  +** Number of entries in the cursor RtreeNode cache.  The first entry is
       143763  +** used to cache the RtreeNode for RtreeCursor.sPoint.  The remaining
       143764  +** entries cache the RtreeNode for the first elements of the priority queue.
       143765  +*/
       143766  +#define RTREE_CACHE_SZ  5
       143767  +
142422 143768   /* 
142423 143769   ** An rtree cursor object.
142424 143770   */
142425 143771   struct RtreeCursor {
142426         -  sqlite3_vtab_cursor base;
142427         -  RtreeNode *pNode;                 /* Node cursor is currently pointing at */
142428         -  int iCell;                        /* Index of current cell in pNode */
       143772  +  sqlite3_vtab_cursor base;         /* Base class.  Must be first */
       143773  +  u8 atEOF;                         /* True if at end of search */
       143774  +  u8 bPoint;                        /* True if sPoint is valid */
142429 143775     int iStrategy;                    /* Copy of idxNum search parameter */
142430 143776     int nConstraint;                  /* Number of entries in aConstraint */
142431 143777     RtreeConstraint *aConstraint;     /* Search constraints. */
       143778  +  int nPointAlloc;                  /* Number of slots allocated for aPoint[] */
       143779  +  int nPoint;                       /* Number of slots used in aPoint[] */
       143780  +  int mxLevel;                      /* iLevel value for root of the tree */
       143781  +  RtreeSearchPoint *aPoint;         /* Priority queue for search points */
       143782  +  RtreeSearchPoint sPoint;          /* Cached next search point */
       143783  +  RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
       143784  +  u32 anQueue[RTREE_MAX_DEPTH+1];   /* Number of queued entries by iLevel */
142432 143785   };
142433 143786   
       143787  +/* Return the Rtree of a RtreeCursor */
       143788  +#define RTREE_OF_CURSOR(X)   ((Rtree*)((X)->base.pVtab))
       143789  +
       143790  +/*
       143791  +** A coordinate can be either a floating point number or a integer.  All
       143792  +** coordinates within a single R-Tree are always of the same time.
       143793  +*/
142434 143794   union RtreeCoord {
142435         -  RtreeValue f;
142436         -  int i;
       143795  +  RtreeValue f;      /* Floating point value */
       143796  +  int i;             /* Integer value */
       143797  +  u32 u;             /* Unsigned for byte-order conversions */
142437 143798   };
142438 143799   
142439 143800   /*
142440 143801   ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
142441 143802   ** formatted as a RtreeDValue (double or int64). This macro assumes that local
142442 143803   ** variable pRtree points to the Rtree structure associated with the
142443 143804   ** RtreeCoord.
................................................................................
142454 143815   
142455 143816   /*
142456 143817   ** A search constraint.
142457 143818   */
142458 143819   struct RtreeConstraint {
142459 143820     int iCoord;                     /* Index of constrained coordinate */
142460 143821     int op;                         /* Constraining operation */
       143822  +  union {
142461 143823     RtreeDValue rValue;             /* Constraint value. */
142462 143824     int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
142463         -  sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
       143825  +    int (*xQueryFunc)(sqlite3_rtree_query_info*);
       143826  +  } u;
       143827  +  sqlite3_rtree_query_info *pInfo;  /* xGeom and xQueryFunc argument */
142464 143828   };
142465 143829   
142466 143830   /* Possible values for RtreeConstraint.op */
142467         -#define RTREE_EQ    0x41
142468         -#define RTREE_LE    0x42
142469         -#define RTREE_LT    0x43
142470         -#define RTREE_GE    0x44
142471         -#define RTREE_GT    0x45
142472         -#define RTREE_MATCH 0x46
       143831  +#define RTREE_EQ    0x41  /* A */
       143832  +#define RTREE_LE    0x42  /* B */
       143833  +#define RTREE_LT    0x43  /* C */
       143834  +#define RTREE_GE    0x44  /* D */
       143835  +#define RTREE_GT    0x45  /* E */
       143836  +#define RTREE_MATCH 0x46  /* F: Old-style sqlite3_rtree_geometry_callback() */
       143837  +#define RTREE_QUERY 0x47  /* G: New-style sqlite3_rtree_query_callback() */
       143838  +
142473 143839   
142474 143840   /* 
142475 143841   ** An rtree structure node.
142476 143842   */
142477 143843   struct RtreeNode {
142478 143844     RtreeNode *pParent;               /* Parent node */
142479         -  i64 iNode;
142480         -  int nRef;
142481         -  int isDirty;
142482         -  u8 *zData;
142483         -  RtreeNode *pNext;                 /* Next node in this hash chain */
       143845  +  i64 iNode;                  /* The node number */
       143846  +  int nRef;                   /* Number of references to this node */
       143847  +  int isDirty;                /* True if the node needs to be written to disk */
       143848  +  u8 *zData;                  /* Content of the node, as should be on disk */
       143849  +  RtreeNode *pNext;           /* Next node in this hash collision chain */
142484 143850   };
       143851  +
       143852  +/* Return the number of cells in a node  */
142485 143853   #define NCELL(pNode) readInt16(&(pNode)->zData[2])
142486 143854   
142487 143855   /* 
142488         -** Structure to store a deserialized rtree record.
       143856  +** A single cell from a node, deserialized
142489 143857   */
142490 143858   struct RtreeCell {
142491         -  i64 iRowid;
142492         -  RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
       143859  +  i64 iRowid;                                 /* Node or entry ID */
       143860  +  RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];  /* Bounding box coordinates */
       143861  +};
       143862  +
       143863  +
       143864  +/*
       143865  +** This object becomes the sqlite3_user_data() for the SQL functions
       143866  +** that are created by sqlite3_rtree_geometry_callback() and
       143867  +** sqlite3_rtree_query_callback() and which appear on the right of MATCH
       143868  +** operators in order to constrain a search.
       143869  +**
       143870  +** xGeom and xQueryFunc are the callback functions.  Exactly one of 
       143871  +** xGeom and xQueryFunc fields is non-NULL, depending on whether the
       143872  +** SQL function was created using sqlite3_rtree_geometry_callback() or
       143873  +** sqlite3_rtree_query_callback().
       143874  +** 
       143875  +** This object is deleted automatically by the destructor mechanism in
       143876  +** sqlite3_create_function_v2().
       143877  +*/
       143878  +struct RtreeGeomCallback {
       143879  +  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
       143880  +  int (*xQueryFunc)(sqlite3_rtree_query_info*);
       143881  +  void (*xDestructor)(void*);
       143882  +  void *pContext;
142493 143883   };
142494 143884   
142495 143885   
142496 143886   /*
142497 143887   ** Value for the first field of every RtreeMatchArg object. The MATCH
142498 143888   ** operator tests that the first field of a blob operand matches this
142499 143889   ** value to avoid operating on invalid blobs (which could cause a segfault).
142500 143890   */
142501 143891   #define RTREE_GEOMETRY_MAGIC 0x891245AB
142502 143892   
142503 143893   /*
142504         -** An instance of this structure must be supplied as a blob argument to
142505         -** the right-hand-side of an SQL MATCH operator used to constrain an
142506         -** r-tree query.
       143894  +** An instance of this structure (in the form of a BLOB) is returned by
       143895  +** the SQL functions that sqlite3_rtree_geometry_callback() and
       143896  +** sqlite3_rtree_query_callback() create, and is read as the right-hand
       143897  +** operand to the MATCH operator of an R-Tree.
142507 143898   */
142508 143899   struct RtreeMatchArg {
142509 143900     u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
142510         -  int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
142511         -  void *pContext;
142512         -  int nParam;
142513         -  RtreeDValue aParam[1];
142514         -};
142515         -
142516         -/*
142517         -** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
142518         -** a single instance of the following structure is allocated. It is used
142519         -** as the context for the user-function created by by s_r_g_c(). The object
142520         -** is eventually deleted by the destructor mechanism provided by
142521         -** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
142522         -** the geometry callback function).
142523         -*/
142524         -struct RtreeGeomCallback {
142525         -  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
142526         -  void *pContext;
       143901  +  RtreeGeomCallback cb;       /* Info about the callback functions */
       143902  +  int nParam;                 /* Number of parameters to the SQL function */
       143903  +  RtreeDValue aParam[1];      /* Values for parameters to the SQL function */
142527 143904   };
142528 143905   
142529 143906   #ifndef MAX
142530 143907   # define MAX(x,y) ((x) < (y) ? (y) : (x))
142531 143908   #endif
142532 143909   #ifndef MIN
142533 143910   # define MIN(x,y) ((x) > (y) ? (y) : (x))
................................................................................
142613 143990   }
142614 143991   
142615 143992   /*
142616 143993   ** Given a node number iNode, return the corresponding key to use
142617 143994   ** in the Rtree.aHash table.
142618 143995   */
142619 143996   static int nodeHash(i64 iNode){
142620         -  return (
142621         -    (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^ 
142622         -    (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
142623         -  ) % HASHSIZE;
       143997  +  return iNode % HASHSIZE;
142624 143998   }
142625 143999   
142626 144000   /*
142627 144001   ** Search the node hash table for node iNode. If found, return a pointer
142628 144002   ** to it. Otherwise, return 0.
142629 144003   */
142630 144004   static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
................................................................................
142676 144050     }
142677 144051     return pNode;
142678 144052   }
142679 144053   
142680 144054   /*
142681 144055   ** Obtain a reference to an r-tree node.
142682 144056   */
142683         -static int
142684         -nodeAcquire(
       144057  +static int nodeAcquire(
142685 144058     Rtree *pRtree,             /* R-tree structure */
142686 144059     i64 iNode,                 /* Node number to load */
142687 144060     RtreeNode *pParent,        /* Either the parent node or NULL */
142688 144061     RtreeNode **ppNode         /* OUT: Acquired node */
142689 144062   ){
142690 144063     int rc;
142691 144064     int rc2 = SQLITE_OK;
................................................................................
142766 144139     return rc;
142767 144140   }
142768 144141   
142769 144142   /*
142770 144143   ** Overwrite cell iCell of node pNode with the contents of pCell.
142771 144144   */
142772 144145   static void nodeOverwriteCell(
142773         -  Rtree *pRtree, 
142774         -  RtreeNode *pNode,  
142775         -  RtreeCell *pCell, 
142776         -  int iCell
       144146  +  Rtree *pRtree,             /* The overall R-Tree */
       144147  +  RtreeNode *pNode,          /* The node into which the cell is to be written */
       144148  +  RtreeCell *pCell,          /* The cell to write */
       144149  +  int iCell                  /* Index into pNode into which pCell is written */
142777 144150   ){
142778 144151     int ii;
142779 144152     u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
142780 144153     p += writeInt64(p, pCell->iRowid);
142781 144154     for(ii=0; ii<(pRtree->nDim*2); ii++){
142782 144155       p += writeCoord(p, &pCell->aCoord[ii]);
142783 144156     }
142784 144157     pNode->isDirty = 1;
142785 144158   }
142786 144159   
142787 144160   /*
142788         -** Remove cell the cell with index iCell from node pNode.
       144161  +** Remove the cell with index iCell from node pNode.
142789 144162   */
142790 144163   static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
142791 144164     u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
142792 144165     u8 *pSrc = &pDst[pRtree->nBytesPerCell];
142793 144166     int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
142794 144167     memmove(pDst, pSrc, nByte);
142795 144168     writeInt16(&pNode->zData[2], NCELL(pNode)-1);
................................................................................
142798 144171   
142799 144172   /*
142800 144173   ** Insert the contents of cell pCell into node pNode. If the insert
142801 144174   ** is successful, return SQLITE_OK.
142802 144175   **
142803 144176   ** If there is not enough free space in pNode, return SQLITE_FULL.
142804 144177   */
142805         -static int
142806         -nodeInsertCell(
142807         -  Rtree *pRtree, 
142808         -  RtreeNode *pNode, 
142809         -  RtreeCell *pCell 
       144178  +static int nodeInsertCell(
       144179  +  Rtree *pRtree,                /* The overall R-Tree */
       144180  +  RtreeNode *pNode,             /* Write new cell into this node */
       144181  +  RtreeCell *pCell              /* The cell to be inserted */
142810 144182   ){
142811 144183     int nCell;                    /* Current number of cells in pNode */
142812 144184     int nMaxCell;                 /* Maximum number of cells for pNode */
142813 144185   
142814 144186     nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
142815 144187     nCell = NCELL(pNode);
142816 144188   
................................................................................
142823 144195   
142824 144196     return (nCell==nMaxCell);
142825 144197   }
142826 144198   
142827 144199   /*
142828 144200   ** If the node is dirty, write it out to the database.
142829 144201   */
142830         -static int
142831         -nodeWrite(Rtree *pRtree, RtreeNode *pNode){
       144202  +static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
142832 144203     int rc = SQLITE_OK;
142833 144204     if( pNode->isDirty ){
142834 144205       sqlite3_stmt *p = pRtree->pWriteNode;
142835 144206       if( pNode->iNode ){
142836 144207         sqlite3_bind_int64(p, 1, pNode->iNode);
142837 144208       }else{
142838 144209         sqlite3_bind_null(p, 1);
................................................................................
142849 144220     return rc;
142850 144221   }
142851 144222   
142852 144223   /*
142853 144224   ** Release a reference to a node. If the node is dirty and the reference
142854 144225   ** count drops to zero, the node data is written to the database.
142855 144226   */
142856         -static int
142857         -nodeRelease(Rtree *pRtree, RtreeNode *pNode){
       144227  +static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
142858 144228     int rc = SQLITE_OK;
142859 144229     if( pNode ){
142860 144230       assert( pNode->nRef>0 );
142861 144231       pNode->nRef--;
142862 144232       if( pNode->nRef==0 ){
142863 144233         if( pNode->iNode==1 ){
142864 144234           pRtree->iDepth = -1;
................................................................................
142878 144248   
142879 144249   /*
142880 144250   ** Return the 64-bit integer value associated with cell iCell of
142881 144251   ** node pNode. If pNode is a leaf node, this is a rowid. If it is
142882 144252   ** an internal node, then the 64-bit integer is a child page number.
142883 144253   */
142884 144254   static i64 nodeGetRowid(
142885         -  Rtree *pRtree, 
142886         -  RtreeNode *pNode, 
142887         -  int iCell
       144255  +  Rtree *pRtree,       /* The overall R-Tree */
       144256  +  RtreeNode *pNode,    /* The node from which to extract the ID */
       144257  +  int iCell            /* The cell index from which to extract the ID */
142888 144258   ){
142889 144259     assert( iCell<NCELL(pNode) );
142890 144260     return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
142891 144261   }
142892 144262   
142893 144263   /*
142894 144264   ** Return coordinate iCoord from cell iCell in node pNode.
142895 144265   */
142896 144266   static void nodeGetCoord(
142897         -  Rtree *pRtree, 
142898         -  RtreeNode *pNode, 
142899         -  int iCell,
142900         -  int iCoord,
142901         -  RtreeCoord *pCoord           /* Space to write result to */
       144267  +  Rtree *pRtree,               /* The overall R-Tree */
       144268  +  RtreeNode *pNode,            /* The node from which to extract a coordinate */
       144269  +  int iCell,                   /* The index of the cell within the node */
       144270  +  int iCoord,                  /* Which coordinate to extract */
       144271  +  RtreeCoord *pCoord           /* OUT: Space to write result to */
142902 144272   ){
142903 144273     readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
142904 144274   }
142905 144275   
142906 144276   /*
142907 144277   ** Deserialize cell iCell of node pNode. Populate the structure pointed
142908 144278   ** to by pCell with the results.
142909 144279   */
142910 144280   static void nodeGetCell(
142911         -  Rtree *pRtree, 
142912         -  RtreeNode *pNode, 
142913         -  int iCell,
142914         -  RtreeCell *pCell
       144281  +  Rtree *pRtree,               /* The overall R-Tree */
       144282  +  RtreeNode *pNode,            /* The node containing the cell to be read */
       144283  +  int iCell,                   /* Index of the cell within the node */
       144284  +  RtreeCell *pCell             /* OUT: Write the cell contents here */
142915 144285   ){
142916         -  int ii;
       144286  +  u8 *pData;
       144287  +  u8 *pEnd;
       144288  +  RtreeCoord *pCoord;
142917 144289     pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
142918         -  for(ii=0; ii<pRtree->nDim*2; ii++){
142919         -    nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
       144290  +  pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
       144291  +  pEnd = pData + pRtree->nDim*8;
       144292  +  pCoord = pCell->aCoord;
       144293  +  for(; pData<pEnd; pData+=4, pCoord++){
       144294  +    readCoord(pData, pCoord);
142920 144295     }
142921 144296   }
142922 144297   
142923 144298   
142924 144299   /* Forward declaration for the function that does the work of
142925 144300   ** the virtual table module xCreate() and xConnect() methods.
142926 144301   */
................................................................................
143038 144413   /*
143039 144414   ** Free the RtreeCursor.aConstraint[] array and its contents.
143040 144415   */
143041 144416   static void freeCursorConstraints(RtreeCursor *pCsr){
143042 144417     if( pCsr->aConstraint ){
143043 144418       int i;                        /* Used to iterate through constraint array */
143044 144419       for(i=0; i<pCsr->nConstraint; i++){
143045         -      sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
143046         -      if( pGeom ){
143047         -        if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
143048         -        sqlite3_free(pGeom);
       144420  +      sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
       144421  +      if( pInfo ){
       144422  +        if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
       144423  +        sqlite3_free(pInfo);
143049 144424         }
143050 144425       }
143051 144426       sqlite3_free(pCsr->aConstraint);
143052 144427       pCsr->aConstraint = 0;
143053 144428     }
143054 144429   }
143055 144430   
143056 144431   /* 
143057 144432   ** Rtree virtual table module xClose method.
143058 144433   */
143059 144434   static int rtreeClose(sqlite3_vtab_cursor *cur){
143060 144435     Rtree *pRtree = (Rtree *)(cur->pVtab);
143061         -  int rc;
       144436  +  int ii;
143062 144437     RtreeCursor *pCsr = (RtreeCursor *)cur;
143063 144438     freeCursorConstraints(pCsr);
143064         -  rc = nodeRelease(pRtree, pCsr->pNode);
       144439  +  sqlite3_free(pCsr->aPoint);
       144440  +  for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
143065 144441     sqlite3_free(pCsr);
143066         -  return rc;
       144442  +  return SQLITE_OK;
143067 144443   }
143068 144444   
143069 144445   /*
143070 144446   ** Rtree virtual table module xEof method.
143071 144447   **
143072 144448   ** Return non-zero if the cursor does not currently point to a valid 
143073 144449   ** record (i.e if the scan has finished), or zero otherwise.
143074 144450   */
143075 144451   static int rtreeEof(sqlite3_vtab_cursor *cur){
143076 144452     RtreeCursor *pCsr = (RtreeCursor *)cur;
143077         -  return (pCsr->pNode==0);
143078         -}
143079         -
143080         -/*
143081         -** The r-tree constraint passed as the second argument to this function is
143082         -** guaranteed to be a MATCH constraint.
143083         -*/
143084         -static int testRtreeGeom(
143085         -  Rtree *pRtree,                  /* R-Tree object */
143086         -  RtreeConstraint *pConstraint,   /* MATCH constraint to test */
143087         -  RtreeCell *pCell,               /* Cell to test */
143088         -  int *pbRes                      /* OUT: Test result */
143089         -){
143090         -  int i;
143091         -  RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
143092         -  int nCoord = pRtree->nDim*2;
143093         -
143094         -  assert( pConstraint->op==RTREE_MATCH );
143095         -  assert( pConstraint->pGeom );
143096         -
143097         -  for(i=0; i<nCoord; i++){
143098         -    aCoord[i] = DCOORD(pCell->aCoord[i]);
143099         -  }
143100         -  return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
143101         -}
143102         -
143103         -/* 
143104         -** Cursor pCursor currently points to a cell in a non-leaf page.
143105         -** Set *pbEof to true if the sub-tree headed by the cell is filtered
143106         -** (excluded) by the constraints in the pCursor->aConstraint[] 
143107         -** array, or false otherwise.
143108         -**
143109         -** Return SQLITE_OK if successful or an SQLite error code if an error
143110         -** occurs within a geometry callback.
143111         -*/
143112         -static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
143113         -  RtreeCell cell;
143114         -  int ii;
143115         -  int bRes = 0;
143116         -  int rc = SQLITE_OK;
143117         -
143118         -  nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
143119         -  for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
143120         -    RtreeConstraint *p = &pCursor->aConstraint[ii];
143121         -    RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
143122         -    RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
       144453  +  return pCsr->atEOF;
       144454  +}
       144455  +
       144456  +/*
       144457  +** Convert raw bits from the on-disk RTree record into a coordinate value.
       144458  +** The on-disk format is big-endian and needs to be converted for little-
       144459  +** endian platforms.  The on-disk record stores integer coordinates if
       144460  +** eInt is true and it stores 32-bit floating point records if eInt is
       144461  +** false.  a[] is the four bytes of the on-disk record to be decoded.
       144462  +** Store the results in "r".
       144463  +**
       144464  +** There are three versions of this macro, one each for little-endian and
       144465  +** big-endian processors and a third generic implementation.  The endian-
       144466  +** specific implementations are much faster and are preferred if the
       144467  +** processor endianness is known at compile-time.  The SQLITE_BYTEORDER
       144468  +** macro is part of sqliteInt.h and hence the endian-specific
       144469  +** implementation will only be used if this module is compiled as part
       144470  +** of the amalgamation.
       144471  +*/
       144472  +#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
       144473  +#define RTREE_DECODE_COORD(eInt, a, r) {                        \
       144474  +    RtreeCoord c;    /* Coordinate decoded */                   \
       144475  +    memcpy(&c.u,a,4);                                           \
       144476  +    c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)|                   \
       144477  +          ((c.u&0xff)<<24)|((c.u&0xff00)<<8);                   \
       144478  +    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
       144479  +}
       144480  +#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
       144481  +#define RTREE_DECODE_COORD(eInt, a, r) {                        \
       144482  +    RtreeCoord c;    /* Coordinate decoded */                   \
       144483  +    memcpy(&c.u,a,4);                                           \
       144484  +    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
       144485  +}
       144486  +#else
       144487  +#define RTREE_DECODE_COORD(eInt, a, r) {                        \
       144488  +    RtreeCoord c;    /* Coordinate decoded */                   \
       144489  +    c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16)                     \
       144490  +           +((u32)a[2]<<8) + a[3];                              \
       144491  +    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
       144492  +}
       144493  +#endif
       144494  +
       144495  +/*
       144496  +** Check the RTree node or entry given by pCellData and p against the MATCH
       144497  +** constraint pConstraint.  
       144498  +*/
       144499  +static int rtreeCallbackConstraint(
       144500  +  RtreeConstraint *pConstraint,  /* The constraint to test */
       144501  +  int eInt,                      /* True if RTree holding integer coordinates */
       144502  +  u8 *pCellData,                 /* Raw cell content */
       144503  +  RtreeSearchPoint *pSearch,     /* Container of this cell */
       144504  +  sqlite3_rtree_dbl *prScore,    /* OUT: score for the cell */
       144505  +  int *peWithin                  /* OUT: visibility of the cell */
       144506  +){
       144507  +  int i;                                                /* Loop counter */
       144508  +  sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
       144509  +  int nCoord = pInfo->nCoord;                           /* No. of coordinates */
       144510  +  int rc;                                             /* Callback return code */
       144511  +  sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2];   /* Decoded coordinates */
       144512  +
       144513  +  assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
       144514  +  assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
       144515  +
       144516  +  if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
       144517  +    pInfo->iRowid = readInt64(pCellData);
       144518  +  }
       144519  +  pCellData += 8;
       144520  +  for(i=0; i<nCoord; i++, pCellData += 4){
       144521  +    RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
       144522  +  }
       144523  +  if( pConstraint->op==RTREE_MATCH ){
       144524  +    rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
       144525  +                              nCoord, aCoord, &i);
       144526  +    if( i==0 ) *peWithin = NOT_WITHIN;
       144527  +    *prScore = RTREE_ZERO;
       144528  +  }else{
       144529  +    pInfo->aCoord = aCoord;
       144530  +    pInfo->iLevel = pSearch->iLevel - 1;
       144531  +    pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
       144532  +    pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
       144533  +    rc = pConstraint->u.xQueryFunc(pInfo);
       144534  +    if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
       144535  +    if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
       144536  +      *prScore = pInfo->rScore;
       144537  +    }
       144538  +  }
       144539  +  return rc;
       144540  +}
       144541  +
       144542  +/* 
       144543  +** Check the internal RTree node given by pCellData against constraint p.
       144544  +** If this constraint cannot be satisfied by any child within the node,
       144545  +** set *peWithin to NOT_WITHIN.
       144546  +*/
       144547  +static void rtreeNonleafConstraint(
       144548  +  RtreeConstraint *p,        /* The constraint to test */
       144549  +  int eInt,                  /* True if RTree holds integer coordinates */
       144550  +  u8 *pCellData,             /* Raw cell content as appears on disk */
       144551  +  int *peWithin              /* Adjust downward, as appropriate */
       144552  +){
       144553  +  sqlite3_rtree_dbl val;     /* Coordinate value convert to a double */
       144554  +
       144555  +  /* p->iCoord might point to either a lower or upper bound coordinate
       144556  +  ** in a coordinate pair.  But make pCellData point to the lower bound.
       144557  +  */
       144558  +  pCellData += 8 + 4*(p->iCoord&0xfe);
       144559  +
       144560  +  assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
       144561  +      || p->op==RTREE_GT || p->op==RTREE_EQ );
       144562  +  switch( p->op ){
       144563  +    case RTREE_LE:
       144564  +    case RTREE_LT:
       144565  +    case RTREE_EQ:
       144566  +      RTREE_DECODE_COORD(eInt, pCellData, val);
       144567  +      /* val now holds the lower bound of the coordinate pair */
       144568  +      if( p->u.rValue>=val ) return;
       144569  +      if( p->op!=RTREE_EQ ) break;  /* RTREE_LE and RTREE_LT end here */
       144570  +      /* Fall through for the RTREE_EQ case */
       144571  +
       144572  +    default: /* RTREE_GT or RTREE_GE,  or fallthrough of RTREE_EQ */
       144573  +      pCellData += 4;
       144574  +      RTREE_DECODE_COORD(eInt, pCellData, val);
       144575  +      /* val now holds the upper bound of the coordinate pair */
       144576  +      if( p->u.rValue<=val ) return;
       144577  +  }
       144578  +  *peWithin = NOT_WITHIN;
       144579  +}
       144580  +
       144581  +/*
       144582  +** Check the leaf RTree cell given by pCellData against constraint p.
       144583  +** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
       144584  +** If the constraint is satisfied, leave *peWithin unchanged.
       144585  +**
       144586  +** The constraint is of the form:  xN op $val
       144587  +**
       144588  +** The op is given by p->op.  The xN is p->iCoord-th coordinate in
       144589  +** pCellData.  $val is given by p->u.rValue.
       144590  +*/
       144591  +static void rtreeLeafConstraint(
       144592  +  RtreeConstraint *p,        /* The constraint to test */
       144593  +  int eInt,                  /* True if RTree holds integer coordinates */
       144594  +  u8 *pCellData,             /* Raw cell content as appears on disk */
       144595  +  int *peWithin              /* Adjust downward, as appropriate */
       144596  +){
       144597  +  RtreeDValue xN;      /* Coordinate value converted to a double */
143123 144598   
143124 144599       assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
143125         -        || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
143126         -    );
143127         -
       144600  +      || p->op==RTREE_GT || p->op==RTREE_EQ );
       144601  +  pCellData += 8 + p->iCoord*4;
       144602  +  RTREE_DECODE_COORD(eInt, pCellData, xN);
143128 144603       switch( p->op ){
143129         -      case RTREE_LE: case RTREE_LT: 
143130         -        bRes = p->rValue<cell_min; 
143131         -        break;
143132         -
143133         -      case RTREE_GE: case RTREE_GT: 
143134         -        bRes = p->rValue>cell_max; 
143135         -        break;
143136         -
143137         -      case RTREE_EQ:
143138         -        bRes = (p->rValue>cell_max || p->rValue<cell_min);
143139         -        break;
143140         -
143141         -      default: {
143142         -        assert( p->op==RTREE_MATCH );
143143         -        rc = testRtreeGeom(pRtree, p, &cell, &bRes);
143144         -        bRes = !bRes;
143145         -        break;
143146         -      }
143147         -    }
143148         -  }
143149         -
143150         -  *pbEof = bRes;
143151         -  return rc;
143152         -}
143153         -
143154         -/* 
143155         -** Test if the cell that cursor pCursor currently points to
143156         -** would be filtered (excluded) by the constraints in the 
143157         -** pCursor->aConstraint[] array. If so, set *pbEof to true before
143158         -** returning. If the cell is not filtered (excluded) by the constraints,
143159         -** set pbEof to zero.
143160         -**
143161         -** Return SQLITE_OK if successful or an SQLite error code if an error
143162         -** occurs within a geometry callback.
143163         -**
143164         -** This function assumes that the cell is part of a leaf node.
143165         -*/
143166         -static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
143167         -  RtreeCell cell;
143168         -  int ii;
143169         -  *pbEof = 0;
143170         -
143171         -  nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
143172         -  for(ii=0; ii<pCursor->nConstraint; ii++){
143173         -    RtreeConstraint *p = &pCursor->aConstraint[ii];
143174         -    RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
143175         -    int res;
143176         -    assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
143177         -        || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
143178         -    );
143179         -    switch( p->op ){
143180         -      case RTREE_LE: res = (coord<=p->rValue); break;
143181         -      case RTREE_LT: res = (coord<p->rValue);  break;
143182         -      case RTREE_GE: res = (coord>=p->rValue); break;
143183         -      case RTREE_GT: res = (coord>p->rValue);  break;
143184         -      case RTREE_EQ: res = (coord==p->rValue); break;
143185         -      default: {
143186         -        int rc;
143187         -        assert( p->op==RTREE_MATCH );
143188         -        rc = testRtreeGeom(pRtree, p, &cell, &res);
143189         -        if( rc!=SQLITE_OK ){
143190         -          return rc;
143191         -        }
143192         -        break;
143193         -      }
143194         -    }
143195         -
143196         -    if( !res ){
143197         -      *pbEof = 1;
143198         -      return SQLITE_OK;
143199         -    }
143200         -  }
143201         -
143202         -  return SQLITE_OK;
143203         -}
143204         -
143205         -/*
143206         -** Cursor pCursor currently points at a node that heads a sub-tree of
143207         -** height iHeight (if iHeight==0, then the node is a leaf). Descend
143208         -** to point to the left-most cell of the sub-tree that matches the 
143209         -** configured constraints.
143210         -*/
143211         -static int descendToCell(
143212         -  Rtree *pRtree, 
143213         -  RtreeCursor *pCursor, 
143214         -  int iHeight,
143215         -  int *pEof                 /* OUT: Set to true if cannot descend */
143216         -){
143217         -  int isEof;
143218         -  int rc;
143219         -  int ii;
143220         -  RtreeNode *pChild;
143221         -  sqlite3_int64 iRowid;
143222         -
143223         -  RtreeNode *pSavedNode = pCursor->pNode;
143224         -  int iSavedCell = pCursor->iCell;
143225         -
143226         -  assert( iHeight>=0 );
143227         -
143228         -  if( iHeight==0 ){
143229         -    rc = testRtreeEntry(pRtree, pCursor, &isEof);
143230         -  }else{
143231         -    rc = testRtreeCell(pRtree, pCursor, &isEof);
143232         -  }
143233         -  if( rc!=SQLITE_OK || isEof || iHeight==0 ){
143234         -    goto descend_to_cell_out;
143235         -  }
143236         -
143237         -  iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
143238         -  rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
143239         -  if( rc!=SQLITE_OK ){
143240         -    goto descend_to_cell_out;
143241         -  }
143242         -
143243         -  nodeRelease(pRtree, pCursor->pNode);
143244         -  pCursor->pNode = pChild;
143245         -  isEof = 1;
143246         -  for(ii=0; isEof && ii<NCELL(pChild); ii++){
143247         -    pCursor->iCell = ii;
143248         -    rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
143249         -    if( rc!=SQLITE_OK ){
143250         -      goto descend_to_cell_out;
143251         -    }
143252         -  }
143253         -
143254         -  if( isEof ){
143255         -    assert( pCursor->pNode==pChild );
143256         -    nodeReference(pSavedNode);
143257         -    nodeRelease(pRtree, pChild);
143258         -    pCursor->pNode = pSavedNode;
143259         -    pCursor->iCell = iSavedCell;
143260         -  }
143261         -
143262         -descend_to_cell_out:
143263         -  *pEof = isEof;
143264         -  return rc;
       144604  +    case RTREE_LE: if( xN <= p->u.rValue ) return;  break;
       144605  +    case RTREE_LT: if( xN <  p->u.rValue ) return;  break;
       144606  +    case RTREE_GE: if( xN >= p->u.rValue ) return;  break;
       144607  +    case RTREE_GT: if( xN >  p->u.rValue ) return;  break;
       144608  +    default:       if( xN == p->u.rValue ) return;  break;
       144609  +  }
       144610  +  *peWithin = NOT_WITHIN;
143265 144611   }
143266 144612   
143267 144613   /*
143268 144614   ** One of the cells in node pNode is guaranteed to have a 64-bit 
143269 144615   ** integer value equal to iRowid. Return the index of this cell.
143270 144616   */
143271 144617   static int nodeRowidIndex(
................................................................................
143272 144618     Rtree *pRtree, 
143273 144619     RtreeNode *pNode, 
143274 144620     i64 iRowid,
143275 144621     int *piIndex
143276 144622   ){
143277 144623     int ii;
143278 144624     int nCell = NCELL(pNode);
       144625  +  assert( nCell<200 );
143279 144626     for(ii=0; ii<nCell; ii++){
143280 144627       if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
143281 144628         *piIndex = ii;
143282 144629         return SQLITE_OK;
143283 144630       }
143284 144631     }
143285 144632     return SQLITE_CORRUPT_VTAB;
................................................................................
143293 144640     RtreeNode *pParent = pNode->pParent;
143294 144641     if( pParent ){
143295 144642       return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
143296 144643     }
143297 144644     *piIndex = -1;
143298 144645     return SQLITE_OK;
143299 144646   }
       144647  +
       144648  +/*
       144649  +** Compare two search points.  Return negative, zero, or positive if the first
       144650  +** is less than, equal to, or greater than the second.
       144651  +**
       144652  +** The rScore is the primary key.  Smaller rScore values come first.
       144653  +** If the rScore is a tie, then use iLevel as the tie breaker with smaller
       144654  +** iLevel values coming first.  In this way, if rScore is the same for all
       144655  +** SearchPoints, then iLevel becomes the deciding factor and the result
       144656  +** is a depth-first search, which is the desired default behavior.
       144657  +*/
       144658  +static int rtreeSearchPointCompare(
       144659  +  const RtreeSearchPoint *pA,
       144660  +  const RtreeSearchPoint *pB
       144661  +){
       144662  +  if( pA->rScore<pB->rScore ) return -1;
       144663  +  if( pA->rScore>pB->rScore ) return +1;
       144664  +  if( pA->iLevel<pB->iLevel ) return -1;
       144665  +  if( pA->iLevel>pB->iLevel ) return +1;
       144666  +  return 0;
       144667  +}
       144668  +
       144669  +/*
       144670  +** Interchange to search points in a cursor.
       144671  +*/
       144672  +static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
       144673  +  RtreeSearchPoint t = p->aPoint[i];
       144674  +  assert( i<j );
       144675  +  p->aPoint[i] = p->aPoint[j];
       144676  +  p->aPoint[j] = t;
       144677  +  i++; j++;
       144678  +  if( i<RTREE_CACHE_SZ ){
       144679  +    if( j>=RTREE_CACHE_SZ ){
       144680  +      nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
       144681  +      p->aNode[i] = 0;
       144682  +    }else{
       144683  +      RtreeNode *pTemp = p->aNode[i];
       144684  +      p->aNode[i] = p->aNode[j];
       144685  +      p->aNode[j] = pTemp;
       144686  +    }
       144687  +  }
       144688  +}
       144689  +
       144690  +/*
       144691  +** Return the search point with the lowest current score.
       144692  +*/
       144693  +static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
       144694  +  return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
       144695  +}
       144696  +
       144697  +/*
       144698  +** Get the RtreeNode for the search point with the lowest score.
       144699  +*/
       144700  +static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
       144701  +  sqlite3_int64 id;
       144702  +  int ii = 1 - pCur->bPoint;
       144703  +  assert( ii==0 || ii==1 );
       144704  +  assert( pCur->bPoint || pCur->nPoint );
       144705  +  if( pCur->aNode[ii]==0 ){
       144706  +    assert( pRC!=0 );
       144707  +    id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
       144708  +    *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
       144709  +  }
       144710  +  return pCur->aNode[ii];
       144711  +}
       144712  +
       144713  +/*
       144714  +** Push a new element onto the priority queue
       144715  +*/
       144716  +static RtreeSearchPoint *rtreeEnqueue(
       144717  +  RtreeCursor *pCur,    /* The cursor */
       144718  +  RtreeDValue rScore,   /* Score for the new search point */
       144719  +  u8 iLevel             /* Level for the new search point */
       144720  +){
       144721  +  int i, j;
       144722  +  RtreeSearchPoint *pNew;
       144723  +  if( pCur->nPoint>=pCur->nPointAlloc ){
       144724  +    int nNew = pCur->nPointAlloc*2 + 8;
       144725  +    pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
       144726  +    if( pNew==0 ) return 0;
       144727  +    pCur->aPoint = pNew;
       144728  +    pCur->nPointAlloc = nNew;
       144729  +  }
       144730  +  i = pCur->nPoint++;
       144731  +  pNew = pCur->aPoint + i;
       144732  +  pNew->rScore = rScore;
       144733  +  pNew->iLevel = iLevel;
       144734  +  assert( iLevel>=0 && iLevel<=RTREE_MAX_DEPTH );
       144735  +  while( i>0 ){
       144736  +    RtreeSearchPoint *pParent;
       144737  +    j = (i-1)/2;
       144738  +    pParent = pCur->aPoint + j;
       144739  +    if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
       144740  +    rtreeSearchPointSwap(pCur, j, i);
       144741  +    i = j;
       144742  +    pNew = pParent;
       144743  +  }
       144744  +  return pNew;
       144745  +}
       144746  +
       144747  +/*
       144748  +** Allocate a new RtreeSearchPoint and return a pointer to it.  Return
       144749  +** NULL if malloc fails.
       144750  +*/
       144751  +static RtreeSearchPoint *rtreeSearchPointNew(
       144752  +  RtreeCursor *pCur,    /* The cursor */
       144753  +  RtreeDValue rScore,   /* Score for the new search point */
       144754  +  u8 iLevel             /* Level for the new search point */
       144755  +){
       144756  +  RtreeSearchPoint *pNew, *pFirst;
       144757  +  pFirst = rtreeSearchPointFirst(pCur);
       144758  +  pCur->anQueue[iLevel]++;
       144759  +  if( pFirst==0
       144760  +   || pFirst->rScore>rScore 
       144761  +   || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
       144762  +  ){
       144763  +    if( pCur->bPoint ){
       144764  +      int ii;
       144765  +      pNew = rtreeEnqueue(pCur, rScore, iLevel);
       144766  +      if( pNew==0 ) return 0;
       144767  +      ii = (int)(pNew - pCur->aPoint) + 1;
       144768  +      if( ii<RTREE_CACHE_SZ ){
       144769  +        assert( pCur->aNode[ii]==0 );
       144770  +        pCur->aNode[ii] = pCur->aNode[0];
       144771  +       }else{
       144772  +        nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
       144773  +      }
       144774  +      pCur->aNode[0] = 0;
       144775  +      *pNew = pCur->sPoint;
       144776  +    }
       144777  +    pCur->sPoint.rScore = rScore;
       144778  +    pCur->sPoint.iLevel = iLevel;
       144779  +    pCur->bPoint = 1;
       144780  +    return &pCur->sPoint;
       144781  +  }else{
       144782  +    return rtreeEnqueue(pCur, rScore, iLevel);
       144783  +  }
       144784  +}
       144785  +
       144786  +#if 0
       144787  +/* Tracing routines for the RtreeSearchPoint queue */
       144788  +static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
       144789  +  if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
       144790  +  printf(" %d.%05lld.%02d %g %d",
       144791  +    p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
       144792  +  );
       144793  +  idx++;
       144794  +  if( idx<RTREE_CACHE_SZ ){
       144795  +    printf(" %p\n", pCur->aNode[idx]);
       144796  +  }else{
       144797  +    printf("\n");
       144798  +  }
       144799  +}
       144800  +static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
       144801  +  int ii;
       144802  +  printf("=== %9s ", zPrefix);
       144803  +  if( pCur->bPoint ){
       144804  +    tracePoint(&pCur->sPoint, -1, pCur);
       144805  +  }
       144806  +  for(ii=0; ii<pCur->nPoint; ii++){
       144807  +    if( ii>0 || pCur->bPoint ) printf("              ");
       144808  +    tracePoint(&pCur->aPoint[ii], ii, pCur);
       144809  +  }
       144810  +}
       14