System.Data.SQLite
Check-in [15b1d1d825]
Not logged in

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

Overview
Comment:Pickup SQLite core library with disposable vtable support from upstream. More work on integration of the SQLiteModuleBase class. Add initial tests.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | virtualTables
Files: files | file ages | folders
SHA1: 15b1d1d825ec5082495e1eb9d629e0cfe52d84f2
User & Date: mistachkin 2013-06-19 07:02:25
Context
2013-06-20
01:40
More work on disposable vtable integration. check-in: 5a2bdec84f user: mistachkin tags: virtualTables
2013-06-19
07:02
Pickup SQLite core library with disposable vtable support from upstream. More work on integration of the SQLiteModuleBase class. Add initial tests. check-in: 15b1d1d825 user: mistachkin tags: virtualTables
2013-06-18
21:15
Move marshalling utility methods into the internal SQLiteMarshal class. check-in: 89613e2645 user: mistachkin tags: virtualTables
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

   350    350   # define SQLITE_PTR_TO_INT(X)  ((int)(X))
   351    351   #endif
   352    352   
   353    353   /*
   354    354   ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
   355    355   ** 0 means mutexes are permanently disable and the library is never
   356    356   ** threadsafe.  1 means the library is serialized which is the highest
   357         -** level of threadsafety.  2 means the libary is multithreaded - multiple
          357  +** level of threadsafety.  2 means the library is multithreaded - multiple
   358    358   ** threads can use SQLite as long as no two threads try to use the same
   359    359   ** database connection at the same time.
   360    360   **
   361    361   ** Older versions of SQLite used an optional THREADSAFE macro.
   362    362   ** We support that for legacy.
   363    363   */
   364    364   #if !defined(SQLITE_THREADSAFE)
................................................................................
   429    429   */
   430    430   #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
   431    431   # define SQLITE_MALLOC_SOFT_LIMIT 1024
   432    432   #endif
   433    433   
   434    434   /*
   435    435   ** We need to define _XOPEN_SOURCE as follows in order to enable
   436         -** recursive mutexes on most Unix systems.  But Mac OS X is different.
   437         -** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
   438         -** so it is omitted there.  See ticket #2673.
   439         -**
   440         -** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
   441         -** implemented on some systems.  So we avoid defining it at all
   442         -** if it is already defined or if it is unneeded because we are
   443         -** not doing a threadsafe build.  Ticket #2681.
   444         -**
   445         -** See also ticket #2741.
          436  +** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
          437  +** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
          438  +** it.
   446    439   */
   447         -#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) \
   448         - && !defined(__APPLE__) && SQLITE_THREADSAFE
   449         -#  define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
          440  +#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
          441  +#  define _XOPEN_SOURCE 600
   450    442   #endif
   451    443   
   452    444   /*
   453    445   ** The TCL headers are only needed when compiling the TCL bindings.
   454    446   */
   455    447   #if defined(SQLITE_TCL) || defined(TCLSH)
   456    448   # include <tcl.h>
................................................................................
   676    668   **
   677    669   ** See also: [sqlite3_libversion()],
   678    670   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   679    671   ** [sqlite_version()] and [sqlite_source_id()].
   680    672   */
   681    673   #define SQLITE_VERSION        "3.7.17"
   682    674   #define SQLITE_VERSION_NUMBER 3007017
   683         -#define SQLITE_SOURCE_ID      "2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668"
          675  +#define SQLITE_SOURCE_ID      "2013-06-13 00:32:29 10bba8d0821159a45c6a0d6c3cef897cb4d4e9a6"
   684    676   
   685    677   /*
   686    678   ** CAPI3REF: Run-Time Library Version Numbers
   687    679   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   688    680   **
   689    681   ** These interfaces provide the same information as the [SQLITE_VERSION],
   690    682   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  5085   5077   ** The code to implement this API is not available in the public release
  5086   5078   ** of SQLite.
  5087   5079   */
  5088   5080   SQLITE_API int sqlite3_key(
  5089   5081     sqlite3 *db,                   /* Database to be rekeyed */
  5090   5082     const void *pKey, int nKey     /* The key */
  5091   5083   );
         5084  +SQLITE_API int sqlite3_key_v2(
         5085  +  sqlite3 *db,                   /* Database to be rekeyed */
         5086  +  const char *zDbName,           /* Name of the database */
         5087  +  const void *pKey, int nKey     /* The key */
         5088  +);
  5092   5089   
  5093   5090   /*
  5094   5091   ** Change the key on an open database.  If the current database is not
  5095   5092   ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
  5096   5093   ** database is decrypted.
  5097   5094   **
  5098   5095   ** The code to implement this API is not available in the public release
  5099   5096   ** of SQLite.
  5100   5097   */
  5101   5098   SQLITE_API int sqlite3_rekey(
  5102   5099     sqlite3 *db,                   /* Database to be rekeyed */
  5103   5100     const void *pKey, int nKey     /* The new key */
  5104   5101   );
         5102  +SQLITE_API int sqlite3_rekey_v2(
         5103  +  sqlite3 *db,                   /* Database to be rekeyed */
         5104  +  const char *zDbName,           /* Name of the database */
         5105  +  const void *pKey, int nKey     /* The new key */
         5106  +);
  5105   5107   
  5106   5108   /*
  5107   5109   ** Specify the activation key for a SEE database.  Unless 
  5108   5110   ** activated, none of the SEE routines will work.
  5109   5111   */
  5110   5112   SQLITE_API void sqlite3_activate_see(
  5111   5113     const char *zPassPhrase        /* Activation phrase */
................................................................................
  8474   8476   typedef struct Trigger Trigger;
  8475   8477   typedef struct TriggerPrg TriggerPrg;
  8476   8478   typedef struct TriggerStep TriggerStep;
  8477   8479   typedef struct UnpackedRecord UnpackedRecord;
  8478   8480   typedef struct VTable VTable;
  8479   8481   typedef struct VtabCtx VtabCtx;
  8480   8482   typedef struct Walker Walker;
  8481         -typedef struct WherePlan WherePlan;
  8482   8483   typedef struct WhereInfo WhereInfo;
  8483         -typedef struct WhereLevel WhereLevel;
  8484   8484   
  8485   8485   /*
  8486   8486   ** Defer sourcing vdbe.h and btree.h until after the "u8" and 
  8487   8487   ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
  8488   8488   ** pointer types (i.e. FuncDef) defined above.
  8489   8489   */
  8490   8490   /************** Include btree.h in the middle of sqliteInt.h *****************/
................................................................................
  9913   9913   ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
  9914   9914   ** aDb[1] is the database file used to hold temporary tables.  Additional
  9915   9915   ** databases may be attached.
  9916   9916   */
  9917   9917   struct Db {
  9918   9918     char *zName;         /* Name of this database */
  9919   9919     Btree *pBt;          /* The B*Tree structure for this database file */
  9920         -  u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
  9921   9920     u8 safety_level;     /* How aggressive at syncing data to disk */
  9922   9921     Schema *pSchema;     /* Pointer to database schema (possibly shared) */
  9923   9922   };
  9924   9923   
  9925   9924   /*
  9926   9925   ** An instance of the following structure stores a database schema.
  9927   9926   **
................................................................................
 10711  10710     u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
 10712  10711     char **azColl;           /* Array of collation sequence names for index */
 10713  10712     int tnum;                /* DB Page containing root of this index */
 10714  10713     u16 nColumn;             /* Number of columns in table used by this index */
 10715  10714     u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 10716  10715     unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
 10717  10716     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
        10717  +  unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
 10718  10718   #ifdef SQLITE_ENABLE_STAT3
 10719  10719     int nSample;             /* Number of elements in aSample[] */
 10720  10720     tRowcnt avgEq;           /* Average nEq value for key values not in aSample */
 10721  10721     IndexSample *aSample;    /* Samples of the left-most key */
 10722  10722   #endif
 10723  10723   };
 10724  10724   
................................................................................
 11056  11056   typedef u64 Bitmask;
 11057  11057   
 11058  11058   /*
 11059  11059   ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
 11060  11060   */
 11061  11061   #define BMS  ((int)(sizeof(Bitmask)*8))
 11062  11062   
        11063  +/*
        11064  +** A bit in a Bitmask
        11065  +*/
        11066  +#define MASKBIT(n)   (((Bitmask)1)<<(n))
        11067  +
 11063  11068   /*
 11064  11069   ** The following structure describes the FROM clause of a SELECT statement.
 11065  11070   ** Each table or subquery in the FROM clause is a separate element of
 11066  11071   ** the SrcList.a[] array.
 11067  11072   **
 11068  11073   ** With the addition of multiple database support, the following structure
 11069  11074   ** can also be used to describe a particular table such as the table that
................................................................................
 11076  11081   ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
 11077  11082   ** jointype expresses the join between the table and the previous table.
 11078  11083   **
 11079  11084   ** In the colUsed field, the high-order bit (bit 63) is set if the table
 11080  11085   ** contains more than 63 columns and the 64-th or later column is used.
 11081  11086   */
 11082  11087   struct SrcList {
 11083         -  i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
 11084         -  i16 nAlloc;      /* Number of entries allocated in a[] below */
        11088  +  u8 nSrc;        /* Number of tables or subqueries in the FROM clause */
        11089  +  u8 nAlloc;      /* Number of entries allocated in a[] below */
 11085  11090     struct SrcList_item {
 11086  11091       Schema *pSchema;  /* Schema to which this item is fixed */
 11087  11092       char *zDatabase;  /* Name of database holding this table */
 11088  11093       char *zName;      /* Name of the table */
 11089  11094       char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
 11090  11095       Table *pTab;      /* An SQL table corresponding to zName */
 11091  11096       Select *pSelect;  /* A SELECT statement used in place of a table name */
................................................................................
 11115  11120   #define JT_NATURAL   0x0004    /* True for a "natural" join */
 11116  11121   #define JT_LEFT      0x0008    /* Left outer join */
 11117  11122   #define JT_RIGHT     0x0010    /* Right outer join */
 11118  11123   #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
 11119  11124   #define JT_ERROR     0x0040    /* unknown or unsupported join type */
 11120  11125   
 11121  11126   
 11122         -/*
 11123         -** A WherePlan object holds information that describes a lookup
 11124         -** strategy.
 11125         -**
 11126         -** This object is intended to be opaque outside of the where.c module.
 11127         -** It is included here only so that that compiler will know how big it
 11128         -** is.  None of the fields in this object should be used outside of
 11129         -** the where.c module.
 11130         -**
 11131         -** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
 11132         -** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
 11133         -** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
 11134         -** case that more than one of these conditions is true.
 11135         -*/
 11136         -struct WherePlan {
 11137         -  u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
 11138         -  u16 nEq;                       /* Number of == constraints */
 11139         -  u16 nOBSat;                    /* Number of ORDER BY terms satisfied */
 11140         -  double nRow;                   /* Estimated number of rows (for EQP) */
 11141         -  union {
 11142         -    Index *pIdx;                   /* Index when WHERE_INDEXED is true */
 11143         -    struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
 11144         -    sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
 11145         -  } u;
 11146         -};
 11147         -
 11148         -/*
 11149         -** For each nested loop in a WHERE clause implementation, the WhereInfo
 11150         -** structure contains a single instance of this structure.  This structure
 11151         -** is intended to be private to the where.c module and should not be
 11152         -** access or modified by other modules.
 11153         -**
 11154         -** The pIdxInfo field is used to help pick the best index on a
 11155         -** virtual table.  The pIdxInfo pointer contains indexing
 11156         -** information for the i-th table in the FROM clause before reordering.
 11157         -** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
 11158         -** All other information in the i-th WhereLevel object for the i-th table
 11159         -** after FROM clause ordering.
 11160         -*/
 11161         -struct WhereLevel {
 11162         -  WherePlan plan;       /* query plan for this element of the FROM clause */
 11163         -  int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
 11164         -  int iTabCur;          /* The VDBE cursor used to access the table */
 11165         -  int iIdxCur;          /* The VDBE cursor used to access pIdx */
 11166         -  int addrBrk;          /* Jump here to break out of the loop */
 11167         -  int addrNxt;          /* Jump here to start the next IN combination */
 11168         -  int addrCont;         /* Jump here to continue with the next loop cycle */
 11169         -  int addrFirst;        /* First instruction of interior of the loop */
 11170         -  u8 iFrom;             /* Which entry in the FROM clause */
 11171         -  u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
 11172         -  int p1, p2;           /* Operands of the opcode used to ends the loop */
 11173         -  union {               /* Information that depends on plan.wsFlags */
 11174         -    struct {
 11175         -      int nIn;              /* Number of entries in aInLoop[] */
 11176         -      struct InLoop {
 11177         -        int iCur;              /* The VDBE cursor used by this IN operator */
 11178         -        int addrInTop;         /* Top of the IN loop */
 11179         -        u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
 11180         -      } *aInLoop;           /* Information about each nested IN operator */
 11181         -    } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
 11182         -    Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
 11183         -  } u;
 11184         -  double rOptCost;      /* "Optimal" cost for this level */
 11185         -
 11186         -  /* The following field is really not part of the current level.  But
 11187         -  ** we need a place to cache virtual table index information for each
 11188         -  ** virtual table in the FROM clause and the WhereLevel structure is
 11189         -  ** a convenient place since there is one WhereLevel for each FROM clause
 11190         -  ** element.
 11191         -  */
 11192         -  sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
 11193         -};
 11194         -
 11195  11127   /*
 11196  11128   ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
 11197  11129   ** and the WhereInfo.wctrlFlags member.
 11198  11130   */
 11199  11131   #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
 11200  11132   #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
 11201  11133   #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
 11202  11134   #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
 11203  11135   #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
 11204  11136   #define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
 11205  11137   #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
 11206  11138   #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
 11207  11139   #define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
        11140  +#define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
        11141  +#define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
 11208  11142   
 11209         -/*
 11210         -** The WHERE clause processing routine has two halves.  The
 11211         -** first part does the start of the WHERE loop and the second
 11212         -** half does the tail of the WHERE loop.  An instance of
 11213         -** this structure is returned by the first half and passed
 11214         -** into the second half to give some continuity.
        11143  +/* Allowed return values from sqlite3WhereIsDistinct()
 11215  11144   */
 11216         -struct WhereInfo {
 11217         -  Parse *pParse;            /* Parsing and code generating context */
 11218         -  SrcList *pTabList;        /* List of tables in the join */
 11219         -  u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
 11220         -  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
 11221         -  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
 11222         -  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
 11223         -  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
 11224         -  int iTop;                 /* The very beginning of the WHERE loop */
 11225         -  int iContinue;            /* Jump here to continue with next record */
 11226         -  int iBreak;               /* Jump here to break out of the loop */
 11227         -  int nLevel;               /* Number of nested loop */
 11228         -  struct WhereClause *pWC;  /* Decomposition of the WHERE clause */
 11229         -  double savedNQueryLoop;   /* pParse->nQueryLoop outside the WHERE loop */
 11230         -  double nRowOut;           /* Estimated number of output rows */
 11231         -  WhereLevel a[1];          /* Information about each nest loop in WHERE */
 11232         -};
 11233         -
 11234         -/* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
 11235  11145   #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
 11236  11146   #define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
 11237  11147   #define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
 11238  11148   #define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
 11239  11149   
 11240  11150   /*
 11241  11151   ** A NameContext defines a context in which to resolve table and column
................................................................................
 11301  11211   */
 11302  11212   struct Select {
 11303  11213     ExprList *pEList;      /* The fields of the result */
 11304  11214     u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
 11305  11215     u16 selFlags;          /* Various SF_* values */
 11306  11216     int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
 11307  11217     int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
 11308         -  double nSelectRow;     /* Estimated number of result rows */
        11218  +  u64 nSelectRow;        /* Estimated number of result rows */
 11309  11219     SrcList *pSrc;         /* The FROM clause */
 11310  11220     Expr *pWhere;          /* The WHERE clause */
 11311  11221     ExprList *pGroupBy;    /* The GROUP BY clause */
 11312  11222     Expr *pHaving;         /* The HAVING clause */
 11313  11223     ExprList *pOrderBy;    /* The ORDER BY clause */
 11314  11224     Select *pPrior;        /* Prior select in a compound select statement */
 11315  11225     Select *pNext;         /* Next select to the left in a compound */
................................................................................
 11485  11395     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 11486  11396   #endif
 11487  11397     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 11488  11398   
 11489  11399     /* Information used while coding trigger programs. */
 11490  11400     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
 11491  11401     Table *pTriggerTab;  /* Table triggers are being coded for */
 11492         -  double nQueryLoop;   /* Estimated number of iterations of a query */
        11402  +  u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
 11493  11403     u32 oldmask;         /* Mask of old.* columns referenced */
 11494  11404     u32 newmask;         /* Mask of new.* columns referenced */
 11495  11405     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 11496  11406     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 11497  11407     u8 disableTriggers;  /* True to disable triggers */
 11498  11408   
 11499  11409     /* Above is constant between recursions.  Below is reset before and after
................................................................................
 12055  11965   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 12056  11966   SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
 12057  11967   #endif
 12058  11968   SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
 12059  11969   SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
 12060  11970   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 12061  11971   SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
        11972  +SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
        11973  +SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
        11974  +SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
        11975  +SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
        11976  +SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
        11977  +SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*);
 12062  11978   SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
 12063  11979   SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
 12064  11980   SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
 12065  11981   SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
 12066  11982   SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
 12067  11983   SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
 12068  11984   SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
................................................................................
 19958  19874             prefix = '-';
 19959  19875           }else{
 19960  19876             if( flag_plussign )          prefix = '+';
 19961  19877             else if( flag_blanksign )    prefix = ' ';
 19962  19878             else                         prefix = 0;
 19963  19879           }
 19964  19880           if( xtype==etGENERIC && precision>0 ) precision--;
 19965         -#if 0
 19966         -        /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
 19967         -        for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
 19968         -#else
 19969         -        /* It makes more sense to use 0.5 */
 19970  19881           for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
 19971         -#endif
 19972  19882           if( xtype==etFLOAT ) realvalue += rounder;
 19973  19883           /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
 19974  19884           exp = 0;
 19975  19885           if( sqlite3IsNaN((double)realvalue) ){
 19976  19886             bufpt = "NaN";
 19977  19887             length = 3;
 19978  19888             break;
................................................................................
 26864  26774           unixGetTempname(pFile->pVfs->mxPathname, zTFile);
 26865  26775           *(char**)pArg = zTFile;
 26866  26776         }
 26867  26777         return SQLITE_OK;
 26868  26778       }
 26869  26779       case SQLITE_FCNTL_MMAP_SIZE: {
 26870  26780         i64 newLimit = *(i64*)pArg;
        26781  +      int rc = SQLITE_OK;
 26871  26782         if( newLimit>sqlite3GlobalConfig.mxMmap ){
 26872  26783           newLimit = sqlite3GlobalConfig.mxMmap;
 26873  26784         }
 26874  26785         *(i64*)pArg = pFile->mmapSizeMax;
 26875         -      if( newLimit>=0 ){
        26786  +      if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
 26876  26787           pFile->mmapSizeMax = newLimit;
 26877         -        if( newLimit<pFile->mmapSize ) pFile->mmapSize = newLimit;
        26788  +        if( pFile->mmapSize>0 ){
        26789  +          unixUnmapfile(pFile);
        26790  +          rc = unixMapfile(pFile, -1);
        26791  +        }
 26878  26792         }
 26879         -      return SQLITE_OK;
        26793  +      return rc;
 26880  26794       }
 26881  26795   #ifdef SQLITE_DEBUG
 26882  26796       /* The pager calls this method to signal that it has done
 26883  26797       ** a rollback and that the database is therefore unchanged and
 26884  26798       ** it hence it is OK for the transaction change counter to be
 26885  26799       ** unchanged.
 26886  26800       */
................................................................................
 33529  33443     }else{
 33530  33444       pFile->ctrlFlags |= mask;
 33531  33445     }
 33532  33446   }
 33533  33447   
 33534  33448   /* Forward declaration */
 33535  33449   static int getTempname(int nBuf, char *zBuf);
        33450  +#if SQLITE_MAX_MMAP_SIZE>0
        33451  +static int winMapfile(winFile*, sqlite3_int64);
        33452  +#endif
 33536  33453   
 33537  33454   /*
 33538  33455   ** Control and query of the open file handle.
 33539  33456   */
 33540  33457   static int winFileControl(sqlite3_file *id, int op, void *pArg){
 33541  33458     winFile *pFile = (winFile*)id;
 33542  33459     OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
................................................................................
 33612  33529         }
 33613  33530         OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 33614  33531         return SQLITE_OK;
 33615  33532       }
 33616  33533   #if SQLITE_MAX_MMAP_SIZE>0
 33617  33534       case SQLITE_FCNTL_MMAP_SIZE: {
 33618  33535         i64 newLimit = *(i64*)pArg;
        33536  +      int rc = SQLITE_OK;
 33619  33537         if( newLimit>sqlite3GlobalConfig.mxMmap ){
 33620  33538           newLimit = sqlite3GlobalConfig.mxMmap;
 33621  33539         }
 33622  33540         *(i64*)pArg = pFile->mmapSizeMax;
 33623         -      if( newLimit>=0 ) pFile->mmapSizeMax = newLimit;
 33624         -      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 33625         -      return SQLITE_OK;
        33541  +      if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
        33542  +        pFile->mmapSizeMax = newLimit;
        33543  +        if( pFile->mmapSize>0 ){
        33544  +          (void)winUnmapfile(pFile);
        33545  +          rc = winMapfile(pFile, -1);
        33546  +        }
        33547  +      }
        33548  +      OSTRACE(("FCNTL file=%p, rc=%d\n", pFile->h, rc));
        33549  +      return rc;
 33626  33550       }
 33627  33551   #endif
 33628  33552     }
 33629  33553     OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
 33630  33554     return SQLITE_NOTFOUND;
 33631  33555   }
 33632  33556   
................................................................................
 37218  37142   */
 37219  37143   static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
 37220  37144     PCache1 *pCache;      /* The newly created page cache */
 37221  37145     PGroup *pGroup;       /* The group the new page cache will belong to */
 37222  37146     int sz;               /* Bytes of memory required to allocate the new cache */
 37223  37147   
 37224  37148     /*
 37225         -  ** The seperateCache variable is true if each PCache has its own private
        37149  +  ** The separateCache variable is true if each PCache has its own private
 37226  37150     ** PGroup.  In other words, separateCache is true for mode (1) where no
 37227  37151     ** mutexing is required.
 37228  37152     **
 37229  37153     **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
 37230  37154     **
 37231  37155     **   *  Always use a unified cache in single-threaded applications
 37232  37156     **
................................................................................
 42542  42466     }
 42543  42467   
 42544  42468     /* Before the first write, give the VFS a hint of what the final
 42545  42469     ** file size will be.
 42546  42470     */
 42547  42471     assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
 42548  42472     if( rc==SQLITE_OK 
 42549         -   && (pList->pDirty ? pPager->dbSize : pList->pgno+1)>pPager->dbHintSize 
        42473  +   && pPager->dbHintSize<pPager->dbSize
        42474  +   && (pList->pDirty || pList->pgno>pPager->dbHintSize)
 42550  42475     ){
 42551  42476       sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
 42552  42477       sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
 42553  42478       pPager->dbHintSize = pPager->dbSize;
 42554  42479     }
 42555  42480   
 42556  42481     while( rc==SQLITE_OK && pList ){
................................................................................
 43507  43432   ** If the database image is smaller than the requested page or if a 
 43508  43433   ** non-zero value is passed as the noContent parameter and the 
 43509  43434   ** requested page is not already stored in the cache, then no 
 43510  43435   ** actual disk read occurs. In this case the memory image of the 
 43511  43436   ** page is initialized to all zeros. 
 43512  43437   **
 43513  43438   ** If noContent is true, it means that we do not care about the contents
 43514         -** of the page. This occurs in two seperate scenarios:
        43439  +** of the page. This occurs in two scenarios:
 43515  43440   **
 43516  43441   **   a) When reading a free-list leaf page from the database, and
 43517  43442   **
 43518  43443   **   b) When a savepoint is being rolled back and we need to load
 43519  43444   **      a new page into the cache to be filled with the data read
 43520  43445   **      from the savepoint journal.
 43521  43446   **
................................................................................
 44917  44842     pPager->xCodecFree = xCodecFree;
 44918  44843     pPager->pCodec = pCodec;
 44919  44844     pagerReportSize(pPager);
 44920  44845   }
 44921  44846   SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
 44922  44847     return pPager->pCodec;
 44923  44848   }
 44924         -#endif
        44849  +
        44850  +/*
        44851  +** This function is called by the wal module when writing page content
        44852  +** into the log file.
        44853  +**
        44854  +** This function returns a pointer to a buffer containing the encrypted
        44855  +** page content. If a malloc fails, this function may return NULL.
        44856  +*/
        44857  +SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
        44858  +  void *aData = 0;
        44859  +  CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
        44860  +  return aData;
        44861  +}
        44862  +
        44863  +/*
        44864  +** Return the current pager state
        44865  +*/
        44866  +SQLITE_PRIVATE int sqlite3PagerState(Pager *pPager){
        44867  +  return pPager->eState;
        44868  +}
        44869  +#endif /* SQLITE_HAS_CODEC */
 44925  44870   
 44926  44871   #ifndef SQLITE_OMIT_AUTOVACUUM
 44927  44872   /*
 44928  44873   ** Move the page pPg to location pgno in the file.
 44929  44874   **
 44930  44875   ** There must be no references to the page previously located at
 44931  44876   ** pgno (which we call pPgOld) though that page is allowed to be
................................................................................
 45472  45417   */
 45473  45418   SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
 45474  45419     assert( pPager->eState==PAGER_READER );
 45475  45420     return sqlite3WalFramesize(pPager->pWal);
 45476  45421   }
 45477  45422   #endif
 45478  45423   
 45479         -#ifdef SQLITE_HAS_CODEC
 45480         -/*
 45481         -** This function is called by the wal module when writing page content
 45482         -** into the log file.
 45483         -**
 45484         -** This function returns a pointer to a buffer containing the encrypted
 45485         -** page content. If a malloc fails, this function may return NULL.
 45486         -*/
 45487         -SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
 45488         -  void *aData = 0;
 45489         -  CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
 45490         -  return aData;
 45491         -}
 45492         -#endif /* SQLITE_HAS_CODEC */
 45493         -
 45494  45424   #endif /* SQLITE_OMIT_DISKIO */
 45495  45425   
 45496  45426   /************** End of pager.c ***********************************************/
 45497  45427   /************** Begin file wal.c *********************************************/
 45498  45428   /*
 45499  45429   ** 2010 February 1
 45500  45430   **
................................................................................
 50757  50687       /* Always defragment highly fragmented pages */
 50758  50688       rc = defragmentPage(pPage);
 50759  50689       if( rc ) return rc;
 50760  50690       top = get2byteNotZero(&data[hdr+5]);
 50761  50691     }else if( gap+2<=top ){
 50762  50692       /* Search the freelist looking for a free slot big enough to satisfy 
 50763  50693       ** the request. The allocation is made from the first free slot in 
 50764         -    ** the list that is large enough to accomadate it.
        50694  +    ** the list that is large enough to accommodate it.
 50765  50695       */
 50766  50696       int pc, addr;
 50767  50697       for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
 50768  50698         int size;            /* Size of the free slot */
 50769  50699         if( pc>usableSize-4 || pc<addr+4 ){
 50770  50700           return SQLITE_CORRUPT_BKPT;
 50771  50701         }
................................................................................
 52700  52630     }
 52701  52631     sqlite3BtreeLeave(p);
 52702  52632     return rc;
 52703  52633   }
 52704  52634   
 52705  52635   /*
 52706  52636   ** This routine is called prior to sqlite3PagerCommit when a transaction
 52707         -** is commited for an auto-vacuum database.
        52637  +** is committed for an auto-vacuum database.
 52708  52638   **
 52709  52639   ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
 52710  52640   ** the database file should be truncated to during the commit process. 
 52711  52641   ** i.e. the database has been reorganized so that only the first *pnTrunc
 52712  52642   ** pages are in use.
 52713  52643   */
 52714  52644   static int autoVacuumCommit(BtShared *pBt){
................................................................................
 61940  61870   #endif
 61941  61871   
 61942  61872   /*
 61943  61873   ** If the Vdbe passed as the first argument opened a statement-transaction,
 61944  61874   ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
 61945  61875   ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
 61946  61876   ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
 61947         -** statement transaction is commtted.
        61877  +** statement transaction is committed.
 61948  61878   **
 61949  61879   ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
 61950  61880   ** Otherwise SQLITE_OK.
 61951  61881   */
 61952  61882   SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
 61953  61883     sqlite3 *const db = p->db;
 61954  61884     int rc = SQLITE_OK;
................................................................................
 64009  63939   #endif /* SQLITE_OMIT_UTF16 */
 64010  63940   SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
 64011  63941     int iType = sqlite3_value_type( columnMem(pStmt,i) );
 64012  63942     columnMallocFailure(pStmt);
 64013  63943     return iType;
 64014  63944   }
 64015  63945   
 64016         -/* The following function is experimental and subject to change or
 64017         -** removal */
 64018         -/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
 64019         -**  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
 64020         -**}
 64021         -*/
 64022         -
 64023  63946   /*
 64024  63947   ** Convert the N-th element of pStmt->pColName[] into a string using
 64025  63948   ** xFunc() then return that string.  If N is out of range, return 0.
 64026  63949   **
 64027  63950   ** There are up to 5 names for each column.  useType determines which
 64028  63951   ** name is returned.  Here are the names:
 64029  63952   **
................................................................................
 68267  68190   ** attached databases.
 68268  68191   **
 68269  68192   ** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
 68270  68193   ** obtained on the database file when a write-transaction is started.  No
 68271  68194   ** other process can start another write transaction while this transaction is
 68272  68195   ** underway.  Starting a write transaction also creates a rollback journal. A
 68273  68196   ** write transaction must be started before any changes can be made to the
 68274         -** database.  If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
 68275         -** on the file.
        68197  +** database.  If P2 is greater than or equal to 2 then an EXCLUSIVE lock is
        68198  +** also obtained on the file.
 68276  68199   **
 68277  68200   ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
 68278  68201   ** true (this flag is set if the Vdbe may modify more than one row and may
 68279  68202   ** throw an ABORT exception), a statement transaction may also be opened.
 68280  68203   ** More specifically, a statement transaction is opened iff the database
 68281  68204   ** connection is currently not in autocommit mode, or if there are other
 68282  68205   ** active statements. A statement transaction allows the changes made by this
................................................................................
 72222  72145   ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
 72223  72146   ** aTree element is set to the index of it. 
 72224  72147   **
 72225  72148   ** For the purposes of this comparison, EOF is considered greater than any
 72226  72149   ** other key value. If the keys are equal (only possible with two EOF
 72227  72150   ** values), it doesn't matter which index is stored.
 72228  72151   **
 72229         -** The (N/4) elements of aTree[] that preceed the final (N/2) described 
        72152  +** The (N/4) elements of aTree[] that precede the final (N/2) described 
 72230  72153   ** above contains the index of the smallest of each block of 4 iterators.
 72231  72154   ** And so on. So that aTree[1] contains the index of the iterator that 
 72232  72155   ** currently points to the smallest key value. aTree[0] is unused.
 72233  72156   **
 72234  72157   ** Example:
 72235  72158   **
 72236  72159   **     aIter[0] -> Banana
................................................................................
 76815  76738       }
 76816  76739     }
 76817  76740   
 76818  76741     if( eType==0 ){
 76819  76742       /* Could not found an existing table or index to use as the RHS b-tree.
 76820  76743       ** We will have to generate an ephemeral table to do the job.
 76821  76744       */
 76822         -    double savedNQueryLoop = pParse->nQueryLoop;
        76745  +    u32 savedNQueryLoop = pParse->nQueryLoop;
 76823  76746       int rMayHaveNull = 0;
 76824  76747       eType = IN_INDEX_EPH;
 76825  76748       if( prNotFound ){
 76826  76749         *prNotFound = rMayHaveNull = ++pParse->nMem;
 76827  76750         sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
 76828  76751       }else{
 76829         -      testcase( pParse->nQueryLoop>(double)1 );
 76830         -      pParse->nQueryLoop = (double)1;
        76752  +      testcase( pParse->nQueryLoop>1 );
        76753  +      pParse->nQueryLoop = 1;
 76831  76754         if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
 76832  76755           eType = IN_INDEX_ROWID;
 76833  76756         }
 76834  76757       }
 76835  76758       sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
 76836  76759       pParse->nQueryLoop = savedNQueryLoop;
 76837  76760     }else{
................................................................................
 76865  76788   ** to iterate over the RHS of the IN operator in order to quickly locate
 76866  76789   ** all corresponding LHS elements.  All this routine does is initialize
 76867  76790   ** the register given by rMayHaveNull to NULL.  Calling routines will take
 76868  76791   ** care of changing this register value to non-NULL if the RHS is NULL-free.
 76869  76792   **
 76870  76793   ** If rMayHaveNull is zero, that means that the subquery is being used
 76871  76794   ** for membership testing only.  There is no need to initialize any
 76872         -** registers to indicate the presense or absence of NULLs on the RHS.
        76795  +** registers to indicate the presence or absence of NULLs on the RHS.
 76873  76796   **
 76874  76797   ** For a SELECT or EXISTS operator, return the register that holds the
 76875  76798   ** result.  For IN operators or if an error occurs, the return value is 0.
 76876  76799   */
 76877  76800   #ifndef SQLITE_OMIT_SUBQUERY
 76878  76801   SQLITE_PRIVATE int sqlite3CodeSubselect(
 76879  76802     Parse *pParse,          /* Parsing context */
................................................................................
 80265  80188   **    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
 80266  80189   **    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
 80267  80190   **
 80268  80191   ** Additional tables might be added in future releases of SQLite.
 80269  80192   ** The sqlite_stat2 table is not created or used unless the SQLite version
 80270  80193   ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
 80271  80194   ** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
 80272         -** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
        80195  +** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
 80273  80196   ** created and used by SQLite versions 3.7.9 and later and with
 80274  80197   ** SQLITE_ENABLE_STAT3 defined.  The fucntionality of sqlite_stat3
 80275  80198   ** is a superset of sqlite_stat2.  
 80276  80199   **
 80277  80200   ** Format of sqlite_stat1:
 80278  80201   **
 80279  80202   ** There is normally one row per index, with the index identified by the
................................................................................
 83453  83376     i = p->nCol-1;
 83454  83377     db = pParse->db;
 83455  83378     zColl = sqlite3NameFromToken(db, pToken);
 83456  83379     if( !zColl ) return;
 83457  83380   
 83458  83381     if( sqlite3LocateCollSeq(pParse, zColl) ){
 83459  83382       Index *pIdx;
        83383  +    sqlite3DbFree(db, p->aCol[i].zColl);
 83460  83384       p->aCol[i].zColl = zColl;
 83461  83385     
 83462  83386       /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
 83463  83387       ** then an index may have been created on this column before the
 83464  83388       ** collation type was added. Correct this if it is the case.
 83465  83389       */
 83466  83390       for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
................................................................................
 84872  84796     pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
 84873  84797     pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
 84874  84798     zExtra = (char *)(&pIndex->zName[nName+1]);
 84875  84799     memcpy(pIndex->zName, zName, nName+1);
 84876  84800     pIndex->pTable = pTab;
 84877  84801     pIndex->nColumn = pList->nExpr;
 84878  84802     pIndex->onError = (u8)onError;
        84803  +  pIndex->uniqNotNull = onError==OE_Abort;
 84879  84804     pIndex->autoIndex = (u8)(pName==0);
 84880  84805     pIndex->pSchema = db->aDb[iDb].pSchema;
 84881  84806     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 84882  84807   
 84883  84808     /* Check to see if we should honor DESC requests on index columns
 84884  84809     */
 84885  84810     if( pDb->pSchema->file_format>=4 ){
................................................................................
 84930  84855       }
 84931  84856       if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
 84932  84857         goto exit_create_index;
 84933  84858       }
 84934  84859       pIndex->azColl[i] = zColl;
 84935  84860       requestedSortOrder = pListItem->sortOrder & sortOrderMask;
 84936  84861       pIndex->aSortOrder[i] = (u8)requestedSortOrder;
        84862  +    if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
 84937  84863     }
 84938  84864     sqlite3DefaultRowEst(pIndex);
 84939  84865   
 84940  84866     if( pTab==pParse->pNewTable ){
 84941  84867       /* This routine has been called to create an automatic index as a
 84942  84868       ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
 84943  84869       ** a PRIMARY KEY or UNIQUE clause following the column definitions.
................................................................................
 87376  87302   ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
 87377  87303   ** p1 is 1-indexed.  So substr(x,1,1) returns the first character
 87378  87304   ** of x.  If x is text, then we actually count UTF-8 characters.
 87379  87305   ** If x is a blob, then we count bytes.
 87380  87306   **
 87381  87307   ** If p1 is negative, then we begin abs(p1) from the end of x[].
 87382  87308   **
 87383         -** If p2 is negative, return the p2 characters preceeding p1.
        87309  +** If p2 is negative, return the p2 characters preceding p1.
 87384  87310   */
 87385  87311   static void substrFunc(
 87386  87312     sqlite3_context *context,
 87387  87313     int argc,
 87388  87314     sqlite3_value **argv
 87389  87315   ){
 87390  87316     const unsigned char *z;
................................................................................
 88035  87961   ** digits. */
 88036  87962   static const char hexdigits[] = {
 88037  87963     '0', '1', '2', '3', '4', '5', '6', '7',
 88038  87964     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
 88039  87965   };
 88040  87966   
 88041  87967   /*
 88042         -** EXPERIMENTAL - This is not an official function.  The interface may
 88043         -** change.  This function may disappear.  Do not write code that depends
 88044         -** on this function.
 88045         -**
 88046  87968   ** Implementation of the QUOTE() function.  This function takes a single
 88047  87969   ** argument.  If the argument is numeric, the return value is the same as
 88048  87970   ** the argument.  If the argument is NULL, the return value is the string
 88049  87971   ** "NULL".  Otherwise, the argument is enclosed in single quotes with
 88050  87972   ** single-quote escapes.
 88051  87973   */
 88052  87974   static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
................................................................................
 88227  88149       sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
 88228  88150     }
 88229  88151   }
 88230  88152   
 88231  88153   /*
 88232  88154   ** The replace() function.  Three arguments are all strings: call
 88233  88155   ** them A, B, and C. The result is also a string which is derived
 88234         -** from A by replacing every occurance of B with C.  The match
        88156  +** from A by replacing every occurrence of B with C.  The match
 88235  88157   ** must be exact.  Collating sequences are not used.
 88236  88158   */
 88237  88159   static void replaceFunc(
 88238  88160     sqlite3_context *context,
 88239  88161     int argc,
 88240  88162     sqlite3_value **argv
 88241  88163   ){
................................................................................
 94145  94067         for(ii=db->nDb-1; ii>=0; ii--){
 94146  94068           if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
 94147  94069             sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
 94148  94070           }
 94149  94071         }
 94150  94072       }
 94151  94073       sz = -1;
 94152         -    if( sqlite3_file_control(db,zDb,SQLITE_FCNTL_MMAP_SIZE,&sz)==SQLITE_OK ){
        94074  +    rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
 94153  94075   #if SQLITE_MAX_MMAP_SIZE==0
 94154         -      sz = 0;
        94076  +    sz = 0;
 94155  94077   #endif
        94078  +    if( rc==SQLITE_OK ){
 94156  94079         returnSingleInt(pParse, "mmap_size", sz);
        94080  +    }else if( rc!=SQLITE_NOTFOUND ){
        94081  +      pParse->nErr++;
        94082  +      pParse->rc = rc;
 94157  94083       }
 94158  94084     }else
 94159  94085   
 94160  94086     /*
 94161  94087     **   PRAGMA temp_store
 94162  94088     **   PRAGMA temp_store = "default"|"memory"|"file"
 94163  94089     **
................................................................................
 94680  94606     }else
 94681  94607   
 94682  94608   #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
 94683  94609   # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
 94684  94610   #endif
 94685  94611   
 94686  94612   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 94687         -  /* Pragma "quick_check" is an experimental reduced version of 
        94613  +  /* Pragma "quick_check" is reduced version of 
 94688  94614     ** integrity_check designed to detect most database corruption
 94689  94615     ** without most of the overhead of a full integrity-check.
 94690  94616     */
 94691  94617     if( sqlite3StrICmp(zLeft, "integrity_check")==0
 94692  94618      || sqlite3StrICmp(zLeft, "quick_check")==0 
 94693  94619     ){
 94694  94620       int i, j, addr, mxErr;
................................................................................
 95138  95064       }
 95139  95065   
 95140  95066     }else
 95141  95067   #endif
 95142  95068   
 95143  95069   #ifdef SQLITE_HAS_CODEC
 95144  95070     if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
 95145         -    sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
        95071  +    sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
 95146  95072     }else
 95147  95073     if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
 95148         -    sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
        95074  +    sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
 95149  95075     }else
 95150  95076     if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
 95151  95077                    sqlite3StrICmp(zLeft, "hexrekey")==0) ){
 95152  95078       int i, h1, h2;
 95153  95079       char zKey[40];
 95154  95080       for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
 95155  95081         h1 += 9*(1&(h1>>6));
 95156  95082         h2 += 9*(1&(h2>>6));
 95157  95083         zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
 95158  95084       }
 95159  95085       if( (zLeft[3] & 0xf)==0xb ){
 95160         -      sqlite3_key(db, zKey, i/2);
        95086  +      sqlite3_key_v2(db, zDb, zKey, i/2);
 95161  95087       }else{
 95162         -      sqlite3_rekey(db, zKey, i/2);
        95088  +      sqlite3_rekey_v2(db, zDb, zKey, i/2);
 95163  95089       }
 95164  95090     }else
 95165  95091   #endif
 95166  95092   #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
 95167  95093     if( sqlite3StrICmp(zLeft, "activate_extensions")==0 && zRight ){
 95168  95094   #ifdef SQLITE_HAS_CODEC
 95169  95095       if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
................................................................................
 95790  95716         }
 95791  95717       }
 95792  95718     }
 95793  95719   
 95794  95720     sqlite3VtabUnlockList(db);
 95795  95721   
 95796  95722     pParse->db = db;
 95797         -  pParse->nQueryLoop = (double)1;
        95723  +  pParse->nQueryLoop = 1;
 95798  95724     if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
 95799  95725       char *zSqlCopy;
 95800  95726       int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
 95801  95727       testcase( nBytes==mxLen );
 95802  95728       testcase( nBytes==mxLen+1 );
 95803  95729       if( nBytes>mxLen ){
 95804  95730         sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
................................................................................
 95812  95738         pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
 95813  95739       }else{
 95814  95740         pParse->zTail = &zSql[nBytes];
 95815  95741       }
 95816  95742     }else{
 95817  95743       sqlite3RunParser(pParse, zSql, &zErrMsg);
 95818  95744     }
 95819         -  assert( 1==(int)pParse->nQueryLoop );
        95745  +  assert( 1==pParse->nQueryLoop );
 95820  95746   
 95821  95747     if( db->mallocFailed ){
 95822  95748       pParse->rc = SQLITE_NOMEM;
 95823  95749     }
 95824  95750     if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
 95825  95751     if( pParse->checkSchema ){
 95826  95752       schemaIsValid(pParse);
................................................................................
 96176  96102     if( p ){
 96177  96103       clearSelect(db, p);
 96178  96104       sqlite3DbFree(db, p);
 96179  96105     }
 96180  96106   }
 96181  96107   
 96182  96108   /*
 96183         -** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
        96109  +** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
 96184  96110   ** type of join.  Return an integer constant that expresses that type
 96185  96111   ** in terms of the following bit values:
 96186  96112   **
 96187  96113   **     JT_INNER
 96188  96114   **     JT_CROSS
 96189  96115   **     JT_OUTER
 96190  96116   **     JT_NATURAL
................................................................................
 97590  97516     int iLimit = 0;
 97591  97517     int iOffset;
 97592  97518     int addr1, n;
 97593  97519     if( p->iLimit ) return;
 97594  97520   
 97595  97521     /* 
 97596  97522     ** "LIMIT -1" always shows all rows.  There is some
 97597         -  ** contraversy about what the correct behavior should be.
        97523  +  ** controversy about what the correct behavior should be.
 97598  97524     ** The current implementation interprets "LIMIT 0" to mean
 97599  97525     ** no rows.
 97600  97526     */
 97601  97527     sqlite3ExprCacheClear(pParse);
 97602  97528     assert( p->pOffset==0 || p->pLimit!=0 );
 97603  97529     if( p->pLimit ){
 97604  97530       p->iLimit = iLimit = ++pParse->nMem;
................................................................................
 97606  97532       if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
 97607  97533       if( sqlite3ExprIsInteger(p->pLimit, &n) ){
 97608  97534         sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
 97609  97535         VdbeComment((v, "LIMIT counter"));
 97610  97536         if( n==0 ){
 97611  97537           sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
 97612  97538         }else{
 97613         -        if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n;
        97539  +        if( p->nSelectRow > n ) p->nSelectRow = n;
 97614  97540         }
 97615  97541       }else{
 97616  97542         sqlite3ExprCode(pParse, p->pLimit, iLimit);
 97617  97543         sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
 97618  97544         VdbeComment((v, "LIMIT counter"));
 97619  97545         sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
 97620  97546       }
................................................................................
 97800  97726         rc = sqlite3Select(pParse, p, &dest);
 97801  97727         testcase( rc!=SQLITE_OK );
 97802  97728         pDelete = p->pPrior;
 97803  97729         p->pPrior = pPrior;
 97804  97730         p->nSelectRow += pPrior->nSelectRow;
 97805  97731         if( pPrior->pLimit
 97806  97732          && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
 97807         -       && p->nSelectRow > (double)nLimit 
        97733  +       && p->nSelectRow > nLimit 
 97808  97734         ){
 97809         -        p->nSelectRow = (double)nLimit;
        97735  +        p->nSelectRow = nLimit;
 97810  97736         }
 97811  97737         if( addr ){
 97812  97738           sqlite3VdbeJumpHere(v, addr);
 97813  97739         }
 97814  97740         break;
 97815  97741       }
 97816  97742       case TK_EXCEPT:
................................................................................
 99951  99877   #ifndef SQLITE_OMIT_EXPLAIN
 99952  99878   static void explainSimpleCount(
 99953  99879     Parse *pParse,                  /* Parse context */
 99954  99880     Table *pTab,                    /* Table being queried */
 99955  99881     Index *pIdx                     /* Index used to optimize scan, or NULL */
 99956  99882   ){
 99957  99883     if( pParse->explain==2 ){
 99958         -    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)",
        99884  +    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
 99959  99885           pTab->zName, 
 99960         -        pIdx ? "USING COVERING INDEX " : "",
 99961         -        pIdx ? pIdx->zName : "",
 99962         -        pTab->nRowEst
        99886  +        pIdx ? " USING COVERING INDEX " : "",
        99887  +        pIdx ? pIdx->zName : ""
 99963  99888       );
 99964  99889       sqlite3VdbeAddOp4(
 99965  99890           pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
 99966  99891       );
 99967  99892     }
 99968  99893   }
 99969  99894   #else
................................................................................
100113 100038         if( pItem->viaCoroutine==0 ){
100114 100039           sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
100115 100040         }
100116 100041         continue;
100117 100042       }
100118 100043   
100119 100044       /* Increment Parse.nHeight by the height of the largest expression
100120         -    ** tree refered to by this, the parent select. The child select
       100045  +    ** tree referred to by this, the parent select. The child select
100121 100046       ** may contain expression trees of at most
100122 100047       ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
100123 100048       ** more conservative than necessary, but much easier than enforcing
100124 100049       ** an exact limit.
100125 100050       */
100126 100051       pParse->nHeight += sqlite3SelectExprHeight(p);
100127 100052   
................................................................................
100306 100231     if( pDest->eDest==SRT_EphemTab ){
100307 100232       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
100308 100233     }
100309 100234   
100310 100235     /* Set the limiter.
100311 100236     */
100312 100237     iEnd = sqlite3VdbeMakeLabel(v);
100313         -  p->nSelectRow = (double)LARGEST_INT64;
       100238  +  p->nSelectRow = LARGEST_INT64;
100314 100239     computeLimitRegisters(pParse, p, iEnd);
100315 100240     if( p->iLimit==0 && addrSortIndex>=0 ){
100316 100241       sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
100317 100242       p->selFlags |= SF_UseSorter;
100318 100243     }
100319 100244   
100320 100245     /* Open a virtual index to use for the distinct set.
................................................................................
100334 100259     if( !isAgg && pGroupBy==0 ){
100335 100260       /* No aggregate functions and no GROUP BY clause */
100336 100261       ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0);
100337 100262   
100338 100263       /* Begin the database scan. */
100339 100264       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0);
100340 100265       if( pWInfo==0 ) goto select_end;
100341         -    if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
100342         -    if( pWInfo->eDistinct ) sDistinct.eTnctType = pWInfo->eDistinct;
100343         -    if( pOrderBy && pWInfo->nOBSat==pOrderBy->nExpr ) pOrderBy = 0;
       100266  +    if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
       100267  +      p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
       100268  +    }
       100269  +    if( sqlite3WhereIsDistinct(pWInfo) ){
       100270  +      sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
       100271  +    }
       100272  +    if( pOrderBy && sqlite3WhereIsOrdered(pWInfo) ) pOrderBy = 0;
100344 100273   
100345 100274       /* If sorting index that was created by a prior OP_OpenEphemeral 
100346 100275       ** instruction ended up not being needed, then change the OP_OpenEphemeral
100347 100276       ** into an OP_Noop.
100348 100277       */
100349 100278       if( addrSortIndex>=0 && pOrderBy==0 ){
100350 100279         sqlite3VdbeChangeToNoop(v, addrSortIndex);
100351 100280         p->addrOpenEphm[2] = -1;
100352 100281       }
100353 100282   
100354 100283       /* Use the standard inner loop. */
100355 100284       selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
100356         -                    pWInfo->iContinue, pWInfo->iBreak);
       100285  +                    sqlite3WhereContinueLabel(pWInfo),
       100286  +                    sqlite3WhereBreakLabel(pWInfo));
100357 100287   
100358 100288       /* End the database scan loop.
100359 100289       */
100360 100290       sqlite3WhereEnd(pWInfo);
100361 100291     }else{
100362 100292       /* This case when there exist aggregate functions or a GROUP BY clause
100363 100293       ** or both */
................................................................................
100382 100312   
100383 100313         for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
100384 100314           pItem->iAlias = 0;
100385 100315         }
100386 100316         for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
100387 100317           pItem->iAlias = 0;
100388 100318         }
100389         -      if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100;
       100319  +      if( p->nSelectRow>100 ) p->nSelectRow = 100;
100390 100320       }else{
100391         -      p->nSelectRow = (double)1;
       100321  +      p->nSelectRow = 1;
100392 100322       }
100393 100323   
100394 100324    
100395 100325       /* Create a label to jump to when we want to abort the query */
100396 100326       addrEnd = sqlite3VdbeMakeLabel(v);
100397 100327   
100398 100328       /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
................................................................................
100466 100396         ** This might involve two separate loops with an OP_Sort in between, or
100467 100397         ** it might be a single loop that uses an index to extract information
100468 100398         ** in the right order to begin with.
100469 100399         */
100470 100400         sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
100471 100401         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0);
100472 100402         if( pWInfo==0 ) goto select_end;
100473         -      if( pWInfo->nOBSat==pGroupBy->nExpr ){
       100403  +      if( sqlite3WhereIsOrdered(pWInfo) ){
100474 100404           /* The optimizer is able to deliver rows in group by order so
100475 100405           ** we do not have to sort.  The OP_OpenEphemeral table will be
100476 100406           ** cancelled later because we still need to use the pKeyInfo
100477 100407           */
100478 100408           groupBySort = 0;
100479 100409         }else{
100480 100410           /* Rows are coming out in undetermined order.  We have to push
................................................................................
100747 100677           pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
100748 100678           if( pWInfo==0 ){
100749 100679             sqlite3ExprListDelete(db, pDel);
100750 100680             goto select_end;
100751 100681           }
100752 100682           updateAccumulator(pParse, &sAggInfo);
100753 100683           assert( pMinMax==0 || pMinMax->nExpr==1 );
100754         -        if( pWInfo->nOBSat>0 ){
100755         -          sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
       100684  +        if( sqlite3WhereIsOrdered(pWInfo) ){
       100685  +          sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo));
100756 100686             VdbeComment((v, "%s() by index",
100757 100687                   (flag==WHERE_ORDERBY_MIN?"min":"max")));
100758 100688           }
100759 100689           sqlite3WhereEnd(pWInfo);
100760 100690           finalizeAggFunctions(pParse, &sAggInfo);
100761 100691         }
100762 100692   
................................................................................
102107 102037       sqlite3VdbeChangeP5(v, (u8)bRecursive);
102108 102038     }
102109 102039   }
102110 102040   
102111 102041   /*
102112 102042   ** This is called to code the required FOR EACH ROW triggers for an operation
102113 102043   ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
102114         -** is given by the op paramater. The tr_tm parameter determines whether the
       102044  +** is given by the op parameter. The tr_tm parameter determines whether the
102115 102045   ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
102116 102046   ** parameter pChanges is passed the list of columns being modified.
102117 102047   **
102118 102048   ** If there are no triggers that fire at the specified time for the specified
102119 102049   ** operation on pTab, this function is a no-op.
102120 102050   **
102121 102051   ** The reg argument is the address of the first in an array of registers 
................................................................................
102558 102488     /* Begin the database scan
102559 102489     */
102560 102490     sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
102561 102491     pWInfo = sqlite3WhereBegin(
102562 102492         pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0
102563 102493     );
102564 102494     if( pWInfo==0 ) goto update_cleanup;
102565         -  okOnePass = pWInfo->okOnePass;
       102495  +  okOnePass = sqlite3WhereOkOnePass(pWInfo);
102566 102496   
102567 102497     /* Remember the rowid of every item to be updated.
102568 102498     */
102569 102499     sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
102570 102500     if( !okOnePass ){
102571 102501       sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
102572 102502     }
................................................................................
104395 104325   ** Trace output macros
104396 104326   */
104397 104327   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
104398 104328   /***/ int sqlite3WhereTrace = 0;
104399 104329   #endif
104400 104330   #if defined(SQLITE_DEBUG) \
104401 104331       && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
104402         -# define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
       104332  +# define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
       104333  +# define WHERETRACE_ENABLED 1
104403 104334   #else
104404         -# define WHERETRACE(X)
       104335  +# define WHERETRACE(K,X)
104405 104336   #endif
104406 104337   
104407 104338   /* Forward reference
104408 104339   */
104409 104340   typedef struct WhereClause WhereClause;
104410 104341   typedef struct WhereMaskSet WhereMaskSet;
104411 104342   typedef struct WhereOrInfo WhereOrInfo;
104412 104343   typedef struct WhereAndInfo WhereAndInfo;
104413         -typedef struct WhereCost WhereCost;
       104344  +typedef struct WhereLevel WhereLevel;
       104345  +typedef struct WhereLoop WhereLoop;
       104346  +typedef struct WherePath WherePath;
       104347  +typedef struct WhereTerm WhereTerm;
       104348  +typedef struct WhereLoopBuilder WhereLoopBuilder;
       104349  +typedef struct WhereScan WhereScan;
       104350  +
       104351  +/*
       104352  +** Cost X is tracked as 10*log2(X) stored in a 16-bit integer.  The
       104353  +** maximum cost for ordinary tables is 64*(2**63) which becomes 6900.
       104354  +** (Virtual tables can return a larger cost, but let's assume they do not.)
       104355  +** So all costs can be stored in a 16-bit unsigned integer without risk
       104356  +** of overflow.
       104357  +**
       104358  +** Costs are estimates, so don't go to the computational trouble to compute
       104359  +** 10*log2(X) exactly.  Instead, a close estimate is used.  Any value of
       104360  +** X<=1 is stored as 0.  X=2 is 10.  X=3 is 16.  X=1000 is 99. etc.
       104361  +**
       104362  +*/
       104363  +typedef unsigned short int WhereCost;
       104364  +
       104365  +/*
       104366  +** This object contains information needed to implement a single nestd
       104367  +** loop in WHERE clause.
       104368  +**
       104369  +** Contrast this object with WhereLoop.  This object describes the
       104370  +** implementation of the loop.  WhereLoop describes the algorithm.
       104371  +** This object contains a pointer to the WhereLoop algorithm as one of
       104372  +** its elements.
       104373  +**
       104374  +** The WhereInfo object contains a single instance of this object for
       104375  +** each term in the FROM clause (which is to say, for each of the
       104376  +** nested loops as implemented).  The order of WhereLevel objects determines
       104377  +** the loop nested order, with WhereInfo.a[0] being the outer loop and
       104378  +** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
       104379  +*/
       104380  +struct WhereLevel {
       104381  +  int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
       104382  +  int iTabCur;          /* The VDBE cursor used to access the table */
       104383  +  int iIdxCur;          /* The VDBE cursor used to access pIdx */
       104384  +  int addrBrk;          /* Jump here to break out of the loop */
       104385  +  int addrNxt;          /* Jump here to start the next IN combination */
       104386  +  int addrCont;         /* Jump here to continue with the next loop cycle */
       104387  +  int addrFirst;        /* First instruction of interior of the loop */
       104388  +  u8 iFrom;             /* Which entry in the FROM clause */
       104389  +  u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
       104390  +  int p1, p2;           /* Operands of the opcode used to ends the loop */
       104391  +  union {               /* Information that depends on pWLoop->wsFlags */
       104392  +    struct {
       104393  +      int nIn;              /* Number of entries in aInLoop[] */
       104394  +      struct InLoop {
       104395  +        int iCur;              /* The VDBE cursor used by this IN operator */
       104396  +        int addrInTop;         /* Top of the IN loop */
       104397  +        u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
       104398  +      } *aInLoop;           /* Information about each nested IN operator */
       104399  +    } in;                 /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
       104400  +    Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
       104401  +  } u;
       104402  +  struct WhereLoop *pWLoop;  /* The selected WhereLoop object */
       104403  +};
       104404  +
       104405  +/*
       104406  +** Each instance of this object represents an algorithm for evaluating one
       104407  +** term of a join.  Every term of the FROM clause will have at least
       104408  +** one corresponding WhereLoop object (unless INDEXED BY constraints
       104409  +** prevent a query solution - which is an error) and many terms of the
       104410  +** FROM clause will have multiple WhereLoop objects, each describing a
       104411  +** potential way of implementing that FROM-clause term, together with
       104412  +** dependencies and cost estimates for using the chosen algorithm.
       104413  +**
       104414  +** Query planning consists of building up a collection of these WhereLoop
       104415  +** objects, then computing a particular sequence of WhereLoop objects, with
       104416  +** one WhereLoop object per FROM clause term, that satisfy all dependencies
       104417  +** and that minimize the overall cost.
       104418  +*/
       104419  +struct WhereLoop {
       104420  +  Bitmask prereq;       /* Bitmask of other loops that must run first */
       104421  +  Bitmask maskSelf;     /* Bitmask identifying table iTab */
       104422  +#ifdef SQLITE_DEBUG
       104423  +  char cId;             /* Symbolic ID of this loop for debugging use */
       104424  +#endif
       104425  +  u8 iTab;              /* Position in FROM clause of table for this loop */
       104426  +  u8 iSortIdx;          /* Sorting index number.  0==None */
       104427  +  WhereCost rSetup;     /* One-time setup cost (ex: create transient index) */
       104428  +  WhereCost rRun;       /* Cost of running each loop */
       104429  +  WhereCost nOut;       /* Estimated number of output rows */
       104430  +  union {
       104431  +    struct {               /* Information for internal btree tables */
       104432  +      int nEq;               /* Number of equality constraints */
       104433  +      Index *pIndex;         /* Index used, or NULL */
       104434  +    } btree;
       104435  +    struct {               /* Information for virtual tables */
       104436  +      int idxNum;            /* Index number */
       104437  +      u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
       104438  +      u8 isOrdered;          /* True if satisfies ORDER BY */
       104439  +      u16 omitMask;          /* Terms that may be omitted */
       104440  +      char *idxStr;          /* Index identifier string */
       104441  +    } vtab;
       104442  +  } u;
       104443  +  u32 wsFlags;          /* WHERE_* flags describing the plan */
       104444  +  u16 nLTerm;           /* Number of entries in aLTerm[] */
       104445  +  /**** whereLoopXfer() copies fields above ***********************/
       104446  +# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
       104447  +  u16 nLSlot;           /* Number of slots allocated for aLTerm[] */
       104448  +  WhereTerm **aLTerm;   /* WhereTerms used */
       104449  +  WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
       104450  +  WhereTerm *aLTermSpace[4];  /* Initial aLTerm[] space */
       104451  +};
       104452  +
       104453  +/* Forward declaration of methods */
       104454  +static int whereLoopResize(sqlite3*, WhereLoop*, int);
       104455  +
       104456  +/*
       104457  +** Each instance of this object holds a sequence of WhereLoop objects
       104458  +** that implement some or all of a query plan.
       104459  +**
       104460  +** Think of each WhereLoop objects as a node in a graph, which arcs
       104461  +** showing dependences and costs for travelling between nodes.  (That is
       104462  +** not a completely accurate description because WhereLoop costs are a
       104463  +** vector, not a scalar, and because dependences are many-to-one, not
       104464  +** one-to-one as are graph nodes.  But it is a useful visualization aid.)
       104465  +** Then a WherePath object is a path through the graph that visits some
       104466  +** or all of the WhereLoop objects once.
       104467  +**
       104468  +** The "solver" works by creating the N best WherePath objects of length
       104469  +** 1.  Then using those as a basis to compute the N best WherePath objects
       104470  +** of length 2.  And so forth until the length of WherePaths equals the
       104471  +** number of nodes in the FROM clause.  The best (lowest cost) WherePath
       104472  +** at the end is the choosen query plan.
       104473  +*/
       104474  +struct WherePath {
       104475  +  Bitmask maskLoop;     /* Bitmask of all WhereLoop objects in this path */
       104476  +  Bitmask revLoop;      /* aLoop[]s that should be reversed for ORDER BY */
       104477  +  WhereCost nRow;       /* Estimated number of rows generated by this path */
       104478  +  WhereCost rCost;      /* Total cost of this path */
       104479  +  u8 isOrdered;         /* True if this path satisfies ORDER BY */
       104480  +  u8 isOrderedValid;    /* True if the isOrdered field is valid */
       104481  +  WhereLoop **aLoop;    /* Array of WhereLoop objects implementing this path */
       104482  +};
104414 104483   
104415 104484   /*
104416 104485   ** The query generator uses an array of instances of this structure to
104417 104486   ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
104418 104487   ** clause subexpression is separated from the others by AND operators,
104419 104488   ** usually, or sometimes subexpressions separated by OR.
104420 104489   **
................................................................................
104459 104528   ** bits in the Bitmask.  So, in the example above, the cursor numbers
104460 104529   ** would be mapped into integers 0 through 7.
104461 104530   **
104462 104531   ** The number of terms in a join is limited by the number of bits
104463 104532   ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
104464 104533   ** is only able to process joins with 64 or fewer tables.
104465 104534   */
104466         -typedef struct WhereTerm WhereTerm;
104467 104535   struct WhereTerm {
104468 104536     Expr *pExpr;            /* Pointer to the subexpression that is this term */
104469 104537     int iParent;            /* Disable pWC->a[iParent] when this term disabled */
104470 104538     int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
104471 104539     union {
104472 104540       int leftColumn;         /* Column number of X in "X <op> <expr>" */
104473 104541       WhereOrInfo *pOrInfo;   /* Extra information if (eOperator & WO_OR)!=0 */
................................................................................
104493 104561   #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
104494 104562   #ifdef SQLITE_ENABLE_STAT3
104495 104563   #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
104496 104564   #else
104497 104565   #  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
104498 104566   #endif
104499 104567   
       104568  +/*
       104569  +** An instance of the WhereScan object is used as an iterator for locating
       104570  +** terms in the WHERE clause that are useful to the query planner.
       104571  +*/
       104572  +struct WhereScan {
       104573  +  WhereClause *pOrigWC;      /* Original, innermost WhereClause */
       104574  +  WhereClause *pWC;          /* WhereClause currently being scanned */
       104575  +  char *zCollName;           /* Required collating sequence, if not NULL */
       104576  +  char idxaff;               /* Must match this affinity, if zCollName!=NULL */
       104577  +  unsigned char nEquiv;      /* Number of entries in aEquiv[] */
       104578  +  unsigned char iEquiv;      /* Next unused slot in aEquiv[] */
       104579  +  u32 opMask;                /* Acceptable operators */
       104580  +  int k;                     /* Resume scanning at this->pWC->a[this->k] */
       104581  +  int aEquiv[22];            /* Cursor,Column pairs for equivalence classes */
       104582  +};
       104583  +
104500 104584   /*
104501 104585   ** An instance of the following structure holds all information about a
104502 104586   ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
104503 104587   **
104504 104588   ** Explanation of pOuter:  For a WHERE clause of the form
104505 104589   **
104506 104590   **           a AND ((b AND c) OR (d AND e)) AND f
104507 104591   **
104508 104592   ** There are separate WhereClause objects for the whole clause and for
104509 104593   ** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
104510 104594   ** subclauses points to the WhereClause object for the whole clause.
104511 104595   */
104512 104596   struct WhereClause {
104513         -  Parse *pParse;           /* The parser context */
104514         -  WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
       104597  +  WhereInfo *pWInfo;       /* WHERE clause processing context */
104515 104598     WhereClause *pOuter;     /* Outer conjunction */
104516 104599     u8 op;                   /* Split operator.  TK_AND or TK_OR */
104517         -  u16 wctrlFlags;          /* Might include WHERE_AND_ONLY */
104518 104600     int nTerm;               /* Number of terms */
104519 104601     int nSlot;               /* Number of entries in a[] */
104520 104602     WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
104521 104603   #if defined(SQLITE_SMALL_STACK)
104522 104604     WhereTerm aStatic[1];    /* Initial static space for a[] */
104523 104605   #else
104524 104606     WhereTerm aStatic[8];    /* Initial static space for a[] */
................................................................................
104570 104652   */
104571 104653   struct WhereMaskSet {
104572 104654     int n;                        /* Number of assigned cursor values */
104573 104655     int ix[BMS];                  /* Cursor assigned to each bit */
104574 104656   };
104575 104657   
104576 104658   /*
104577         -** A WhereCost object records a lookup strategy and the estimated
104578         -** cost of pursuing that strategy.
104579         -*/
104580         -struct WhereCost {
104581         -  WherePlan plan;    /* The lookup strategy */
104582         -  double rCost;      /* Overall cost of pursuing this search strategy */
104583         -  Bitmask used;      /* Bitmask of cursors used by this plan */
104584         -};
104585         -
104586         -/*
104587         -** Bitmasks for the operators that indices are able to exploit.  An
       104659  +** This object is a convenience wrapper holding all information needed
       104660  +** to construct WhereLoop objects for a particular query.
       104661  +*/
       104662  +struct WhereLoopBuilder {
       104663  +  WhereInfo *pWInfo;        /* Information about this WHERE */
       104664  +  WhereClause *pWC;         /* WHERE clause terms */
       104665  +  ExprList *pOrderBy;       /* ORDER BY clause */
       104666  +  WhereLoop *pNew;          /* Template WhereLoop */
       104667  +  WhereLoop *pBest;         /* If non-NULL, store single best loop here */
       104668  +};
       104669  +
       104670  +/*
       104671  +** The WHERE clause processing routine has two halves.  The
       104672  +** first part does the start of the WHERE loop and the second
       104673  +** half does the tail of the WHERE loop.  An instance of
       104674  +** this structure is returned by the first half and passed
       104675  +** into the second half to give some continuity.
       104676  +**
       104677  +** An instance of this object holds the complete state of the query
       104678  +** planner.
       104679  +*/
       104680  +struct WhereInfo {
       104681  +  Parse *pParse;            /* Parsing and code generating context */
       104682  +  SrcList *pTabList;        /* List of tables in the join */
       104683  +  ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
       104684  +  ExprList *pDistinct;      /* DISTINCT ON values, or NULL */
       104685  +  WhereLoop *pLoops;        /* List of all WhereLoop objects */
       104686  +  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
       104687  +  WhereCost nRowOut;        /* Estimated number of output rows */
       104688  +  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
       104689  +  u8 bOBSat;                /* ORDER BY satisfied by indices */
       104690  +  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
       104691  +  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
       104692  +  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
       104693  +  int iTop;                 /* The very beginning of the WHERE loop */
       104694  +  int iContinue;            /* Jump here to continue with next record */
       104695  +  int iBreak;               /* Jump here to break out of the loop */
       104696  +  int nLevel;               /* Number of nested loop */
       104697  +  int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
       104698  +  WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
       104699  +  WhereClause sWC;          /* Decomposition of the WHERE clause */
       104700  +  WhereLevel a[1];          /* Information about each nest loop in WHERE */
       104701  +};
       104702  +
       104703  +/*
       104704  +** Bitmasks for the operators on WhereTerm objects.  These are all
       104705  +** operators that are of interest to the query planner.  An
104588 104706   ** OR-ed combination of these values can be used when searching for
104589         -** terms in the where clause.
       104707  +** particular WhereTerms within a WhereClause.
104590 104708   */
104591 104709   #define WO_IN     0x001
104592 104710   #define WO_EQ     0x002
104593 104711   #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
104594 104712   #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
104595 104713   #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
104596 104714   #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
................................................................................
104601 104719   #define WO_EQUIV  0x400       /* Of the form A==B, both columns */
104602 104720   #define WO_NOOP   0x800       /* This term does not restrict search space */
104603 104721   
104604 104722   #define WO_ALL    0xfff       /* Mask of all possible WO_* values */
104605 104723   #define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
104606 104724   
104607 104725   /*
104608         -** Value for wsFlags returned by bestIndex() and stored in
104609         -** WhereLevel.wsFlags.  These flags determine which search
104610         -** strategies are appropriate.
104611         -**
104612         -** The least significant 12 bits is reserved as a mask for WO_ values above.
104613         -** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
104614         -** But if the table is the right table of a left join, WhereLevel.wsFlags
104615         -** is set to WO_IN|WO_EQ.  The WhereLevel.wsFlags field can then be used as
104616         -** the "op" parameter to findTerm when we are resolving equality constraints.
104617         -** ISNULL constraints will then not be used on the right table of a left
104618         -** join.  Tickets #2177 and #2189.
104619         -*/
104620         -#define WHERE_ROWID_EQ     0x00001000  /* rowid=EXPR or rowid IN (...) */
104621         -#define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
104622         -#define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
104623         -#define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
104624         -#define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
104625         -#define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
104626         -#define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
104627         -#define WHERE_NOT_FULLSCAN 0x100f3000  /* Does not do a full table scan */
104628         -#define WHERE_IN_ABLE      0x080f1000  /* Able to support an IN operator */
104629         -#define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
104630         -#define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
104631         -#define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
104632         -#define WHERE_IDX_ONLY     0x00400000  /* Use index only - omit table */
104633         -#define WHERE_ORDERED      0x00800000  /* Output will appear in correct order */
104634         -#define WHERE_REVERSE      0x01000000  /* Scan in reverse order */
104635         -#define WHERE_UNIQUE       0x02000000  /* Selects no more than one row */
104636         -#define WHERE_ALL_UNIQUE   0x04000000  /* This and all prior have one row */
104637         -#define WHERE_OB_UNIQUE    0x00004000  /* Values in ORDER BY columns are 
104638         -                                       ** different for every output row */
104639         -#define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
104640         -#define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
104641         -#define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
104642         -#define WHERE_DISTINCT     0x40000000  /* Correct order for DISTINCT */
104643         -#define WHERE_COVER_SCAN   0x80000000  /* Full scan of a covering index */
104644         -
104645         -/*
104646         -** This module contains many separate subroutines that work together to
104647         -** find the best indices to use for accessing a particular table in a query.
104648         -** An instance of the following structure holds context information about the
104649         -** index search so that it can be more easily passed between the various
104650         -** routines.
104651         -*/
104652         -typedef struct WhereBestIdx WhereBestIdx;
104653         -struct WhereBestIdx {
104654         -  Parse *pParse;                  /* Parser context */
104655         -  WhereClause *pWC;               /* The WHERE clause */
104656         -  struct SrcList_item *pSrc;      /* The FROM clause term to search */
104657         -  Bitmask notReady;               /* Mask of cursors not available */
104658         -  Bitmask notValid;               /* Cursors not available for any purpose */
104659         -  ExprList *pOrderBy;             /* The ORDER BY clause */
104660         -  ExprList *pDistinct;            /* The select-list if query is DISTINCT */
104661         -  sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
104662         -  int i, n;                       /* Which loop is being coded; # of loops */
104663         -  WhereLevel *aLevel;             /* Info about outer loops */
104664         -  WhereCost cost;                 /* Lowest cost query plan */
104665         -};
104666         -
104667         -/*
104668         -** Return TRUE if the probe cost is less than the baseline cost
104669         -*/
104670         -static int compareCost(const WhereCost *pProbe, const WhereCost *pBaseline){
104671         -  if( pProbe->rCost<pBaseline->rCost ) return 1;
104672         -  if( pProbe->rCost>pBaseline->rCost ) return 0;
104673         -  if( pProbe->plan.nOBSat>pBaseline->plan.nOBSat ) return 1;
104674         -  if( pProbe->plan.nRow<pBaseline->plan.nRow ) return 1;
104675         -  return 0;
       104726  +** These are definitions of bits in the WhereLoop.wsFlags field.
       104727  +** The particular combination of bits in each WhereLoop help to
       104728  +** determine the algorithm that WhereLoop represents.
       104729  +*/
       104730  +#define WHERE_COLUMN_EQ    0x00000001  /* x=EXPR or x IN (...) or x IS NULL */
       104731  +#define WHERE_COLUMN_RANGE 0x00000002  /* x<EXPR and/or x>EXPR */
       104732  +#define WHERE_COLUMN_IN    0x00000004  /* x IN (...) */
       104733  +#define WHERE_COLUMN_NULL  0x00000008  /* x IS NULL */
       104734  +#define WHERE_CONSTRAINT   0x0000000f  /* Any of the WHERE_COLUMN_xxx values */
       104735  +#define WHERE_TOP_LIMIT    0x00000010  /* x<EXPR or x<=EXPR constraint */
       104736  +#define WHERE_BTM_LIMIT    0x00000020  /* x>EXPR or x>=EXPR constraint */
       104737  +#define WHERE_BOTH_LIMIT   0x00000030  /* Both x>EXPR and x<EXPR */
       104738  +#define WHERE_IDX_ONLY     0x00000040  /* Use index only - omit table */
       104739  +#define WHERE_IPK          0x00000100  /* x is the INTEGER PRIMARY KEY */
       104740  +#define WHERE_INDEXED      0x00000200  /* WhereLoop.u.btree.pIndex is valid */
       104741  +#define WHERE_VIRTUALTABLE 0x00000400  /* WhereLoop.u.vtab is valid */
       104742  +#define WHERE_IN_ABLE      0x00000800  /* Able to support an IN operator */
       104743  +#define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
       104744  +#define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
       104745  +#define WHERE_TEMP_INDEX   0x00004000  /* Uses an ephemeral index */
       104746  +#define WHERE_COVER_SCAN   0x00008000  /* Full scan of a covering index */
       104747  +
       104748  +
       104749  +/* Convert a WhereCost value (10 times log2(X)) into its integer value X.
       104750  +** A rough approximation is used.  The value returned is not exact.
       104751  +*/
       104752  +static u64 whereCostToInt(WhereCost x){
       104753  +  u64 n;
       104754  +  if( x<=10 ) return 1;
       104755  +  n = x%10;
       104756  +  x /= 10;
       104757  +  if( n>=5 ) n -= 2;
       104758  +  else if( n>=1 ) n -= 1;
       104759  +  if( x>=3 ) return (n+8)<<(x-3);
       104760  +  return (n+8)>>(3-x);
       104761  +}
       104762  +
       104763  +/*
       104764  +** Return the estimated number of output rows from a WHERE clause
       104765  +*/
       104766  +SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
       104767  +  return whereCostToInt(pWInfo->nRowOut);
       104768  +}
       104769  +
       104770  +/*
       104771  +** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
       104772  +** WHERE clause returns outputs for DISTINCT processing.
       104773  +*/
       104774  +SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
       104775  +  return pWInfo->eDistinct;
       104776  +}
       104777  +
       104778  +/*
       104779  +** Return TRUE if the WHERE clause returns rows in ORDER BY order.
       104780  +** Return FALSE if the output needs to be sorted.
       104781  +*/
       104782  +SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
       104783  +  return pWInfo->bOBSat!=0;
       104784  +}
       104785  +
       104786  +/*
       104787  +** Return the VDBE address or label to jump to in order to continue
       104788  +** immediately with the next row of a WHERE clause.
       104789  +*/
       104790  +SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
       104791  +  return pWInfo->iContinue;
       104792  +}
       104793  +
       104794  +/*
       104795  +** Return the VDBE address or label to jump to in order to break
       104796  +** out of a WHERE loop.
       104797  +*/
       104798  +SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
       104799  +  return pWInfo->iBreak;
       104800  +}
       104801  +
       104802  +/*
       104803  +** Return TRUE if an UPDATE or DELETE statement can operate directly on
       104804  +** the rowids returned by a WHERE clause.  Return FALSE if doing an
       104805  +** UPDATE or DELETE might change subsequent WHERE clause results.
       104806  +*/
       104807  +SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo){
       104808  +  return pWInfo->okOnePass;
104676 104809   }
104677 104810   
104678 104811   /*
104679 104812   ** Initialize a preallocated WhereClause structure.
104680 104813   */
104681 104814   static void whereClauseInit(
104682 104815     WhereClause *pWC,        /* The WhereClause to be initialized */
104683         -  Parse *pParse,           /* The parsing context */
104684         -  WhereMaskSet *pMaskSet,  /* Mapping from table cursor numbers to bitmasks */
104685         -  u16 wctrlFlags           /* Might include WHERE_AND_ONLY */
       104816  +  WhereInfo *pWInfo        /* The WHERE processing context */
104686 104817   ){
104687         -  pWC->pParse = pParse;
104688         -  pWC->pMaskSet = pMaskSet;
       104818  +  pWC->pWInfo = pWInfo;
104689 104819     pWC->pOuter = 0;
104690 104820     pWC->nTerm = 0;
104691 104821     pWC->nSlot = ArraySize(pWC->aStatic);
104692 104822     pWC->a = pWC->aStatic;
104693         -  pWC->wctrlFlags = wctrlFlags;
104694 104823   }
104695 104824   
104696 104825   /* Forward reference */
104697 104826   static void whereClauseClear(WhereClause*);
104698 104827   
104699 104828   /*
104700 104829   ** Deallocate all memory associated with a WhereOrInfo object.
................................................................................
104715 104844   /*
104716 104845   ** Deallocate a WhereClause structure.  The WhereClause structure
104717 104846   ** itself is not freed.  This routine is the inverse of whereClauseInit().
104718 104847   */
104719 104848   static void whereClauseClear(WhereClause *pWC){
104720 104849     int i;
104721 104850     WhereTerm *a;
104722         -  sqlite3 *db = pWC->pParse->db;
       104851  +  sqlite3 *db = pWC->pWInfo->pParse->db;
104723 104852     for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
104724 104853       if( a->wtFlags & TERM_DYNAMIC ){
104725 104854         sqlite3ExprDelete(db, a->pExpr);
104726 104855       }
104727 104856       if( a->wtFlags & TERM_ORINFO ){
104728 104857         whereOrInfoDelete(db, a->u.pOrInfo);
104729 104858       }else if( a->wtFlags & TERM_ANDINFO ){
................................................................................
104756 104885   */
104757 104886   static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
104758 104887     WhereTerm *pTerm;
104759 104888     int idx;
104760 104889     testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
104761 104890     if( pWC->nTerm>=pWC->nSlot ){
104762 104891       WhereTerm *pOld = pWC->a;
104763         -    sqlite3 *db = pWC->pParse->db;
       104892  +    sqlite3 *db = pWC->pWInfo->pParse->db;
104764 104893       pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
104765 104894       if( pWC->a==0 ){
104766 104895         if( wtFlags & TERM_DYNAMIC ){
104767 104896           sqlite3ExprDelete(db, p);
104768 104897         }
104769 104898         pWC->a = pOld;
104770 104899         return 0;
................................................................................
104808 104937     }else{
104809 104938       whereSplit(pWC, pExpr->pLeft, op);
104810 104939       whereSplit(pWC, pExpr->pRight, op);
104811 104940     }
104812 104941   }
104813 104942   
104814 104943   /*
104815         -** Initialize an expression mask set (a WhereMaskSet object)
       104944  +** Initialize a WhereMaskSet object
104816 104945   */
104817         -#define initMaskSet(P)  memset(P, 0, sizeof(*P))
       104946  +#define initMaskSet(P)  (P)->n=0
104818 104947   
104819 104948   /*
104820 104949   ** Return the bitmask for the given cursor number.  Return 0 if
104821 104950   ** iCursor is not in the set.
104822 104951   */
104823 104952   static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
104824 104953     int i;
104825 104954     assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
104826 104955     for(i=0; i<pMaskSet->n; i++){
104827 104956       if( pMaskSet->ix[i]==iCursor ){
104828         -      return ((Bitmask)1)<<i;
       104957  +      return MASKBIT(i);
104829 104958       }
104830 104959     }
104831 104960     return 0;
104832 104961   }
104833 104962   
104834 104963   /*
104835 104964   ** Create a new mask for cursor iCursor.
................................................................................
104841 104970   */
104842 104971   static void createMask(WhereMaskSet *pMaskSet, int iCursor){
104843 104972     assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
104844 104973     pMaskSet->ix[pMaskSet->n++] = iCursor;
104845 104974   }
104846 104975   
104847 104976   /*
104848         -** This routine walks (recursively) an expression tree and generates
       104977  +** These routine walk (recursively) an expression tree and generates
104849 104978   ** a bitmask indicating which tables are used in that expression
104850 104979   ** tree.
104851         -**
104852         -** In order for this routine to work, the calling function must have
104853         -** previously invoked sqlite3ResolveExprNames() on the expression.  See
104854         -** the header comment on that routine for additional information.
104855         -** The sqlite3ResolveExprNames() routines looks for column names and
104856         -** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
104857         -** the VDBE cursor number of the table.  This routine just has to
104858         -** translate the cursor numbers into bitmask values and OR all
104859         -** the bitmasks together.
104860 104980   */
104861 104981   static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
104862 104982   static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
104863 104983   static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
104864 104984     Bitmask mask = 0;
104865 104985     if( p==0 ) return 0;
104866 104986     if( p->op==TK_COLUMN ){
................................................................................
104906 105026     }
104907 105027     return mask;
104908 105028   }
104909 105029   
104910 105030   /*
104911 105031   ** Return TRUE if the given operator is one of the operators that is
104912 105032   ** allowed for an indexable WHERE clause term.  The allowed operators are
104913         -** "=", "<", ">", "<=", ">=", and "IN".
       105033  +** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
104914 105034   **
104915 105035   ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
104916 105036   ** of one of the following forms: column = expression column > expression
104917 105037   ** column >= expression column < expression column <= expression
104918 105038   ** expression = column expression > column expression >= column
104919 105039   ** expression < column expression <= column column IN
104920 105040   ** (expression-list) column IN (subquery) column IS NULL
................................................................................
104933 105053   #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
104934 105054   
104935 105055   /*
104936 105056   ** Commute a comparison operator.  Expressions of the form "X op Y"
104937 105057   ** are converted into "Y op X".
104938 105058   **
104939 105059   ** If left/right precedence rules come into play when determining the
104940         -** collating
104941         -** side of the comparison, it remains associated with the same side after
104942         -** the commutation. So "Y collate NOCASE op X" becomes 
104943         -** "X op Y". This is because any collation sequence on
       105060  +** collating sequence, then COLLATE operators are adjusted to ensure
       105061  +** that the collating sequence does not change.  For example:
       105062  +** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
104944 105063   ** the left hand side of a comparison overrides any collation sequence 
104945 105064   ** attached to the right. For the same reason the EP_Collate flag
104946 105065   ** is not commuted.
104947 105066   */
104948 105067   static void exprCommute(Parse *pParse, Expr *pExpr){
104949 105068     u16 expRight = (pExpr->pRight->flags & EP_Collate);
104950 105069     u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
................................................................................
104992 105111     assert( op!=TK_EQ || c==WO_EQ );
104993 105112     assert( op!=TK_LT || c==WO_LT );
104994 105113     assert( op!=TK_LE || c==WO_LE );
104995 105114     assert( op!=TK_GT || c==WO_GT );
104996 105115     assert( op!=TK_GE || c==WO_GE );
104997 105116     return c;
104998 105117   }
       105118  +
       105119  +/*
       105120  +** Advance to the next WhereTerm that matches according to the criteria
       105121  +** established when the pScan object was initialized by whereScanInit().
       105122  +** Return NULL if there are no more matching WhereTerms.
       105123  +*/
       105124  +WhereTerm *whereScanNext(WhereScan *pScan){
       105125  +  int iCur;            /* The cursor on the LHS of the term */
       105126  +  int iColumn;         /* The column on the LHS of the term.  -1 for IPK */
       105127  +  Expr *pX;            /* An expression being tested */
       105128  +  WhereClause *pWC;    /* Shorthand for pScan->pWC */
       105129  +  WhereTerm *pTerm;    /* The term being tested */
       105130  +  int k = pScan->k;    /* Where to start scanning */
       105131  +
       105132  +  while( pScan->iEquiv<=pScan->nEquiv ){
       105133  +    iCur = pScan->aEquiv[pScan->iEquiv-2];
       105134  +    iColumn = pScan->aEquiv[pScan->iEquiv-1];
       105135  +    while( (pWC = pScan->pWC)!=0 ){
       105136  +      for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
       105137  +        if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){
       105138  +          if( (pTerm->eOperator & WO_EQUIV)!=0
       105139  +           && pScan->nEquiv<ArraySize(pScan->aEquiv)
       105140  +          ){
       105141  +            int j;
       105142  +            pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
       105143  +            assert( pX->op==TK_COLUMN );
       105144  +            for(j=0; j<pScan->nEquiv; j+=2){
       105145  +              if( pScan->aEquiv[j]==pX->iTable
       105146  +               && pScan->aEquiv[j+1]==pX->iColumn ){
       105147  +                  break;
       105148  +              }
       105149  +            }
       105150  +            if( j==pScan->nEquiv ){
       105151  +              pScan->aEquiv[j] = pX->iTable;
       105152  +              pScan->aEquiv[j+1] = pX->iColumn;
       105153  +              pScan->nEquiv += 2;
       105154  +            }
       105155  +          }
       105156  +          if( (pTerm->eOperator & pScan->opMask)!=0 ){
       105157  +            /* Verify the affinity and collating sequence match */
       105158  +            if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
       105159  +              CollSeq *pColl;
       105160  +              Parse *pParse = pWC->pWInfo->pParse;
       105161  +              pX = pTerm->pExpr;
       105162  +              if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
       105163  +                continue;
       105164  +              }
       105165  +              assert(pX->pLeft);
       105166  +              pColl = sqlite3BinaryCompareCollSeq(pParse,
       105167  +                                                  pX->pLeft, pX->pRight);
       105168  +              if( pColl==0 ) pColl = pParse->db->pDfltColl;
       105169  +              if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
       105170  +                continue;
       105171  +              }
       105172  +            }
       105173  +            if( (pTerm->eOperator & WO_EQ)!=0
       105174  +             && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
       105175  +             && pX->iTable==pScan->aEquiv[0]
       105176  +             && pX->iColumn==pScan->aEquiv[1]
       105177  +            ){
       105178  +              continue;
       105179  +            }
       105180  +            pScan->k = k+1;
       105181  +            return pTerm;
       105182  +          }
       105183  +        }
       105184  +      }
       105185  +      pWC = pScan->pWC = pScan->pWC->pOuter;
       105186  +      k = 0;
       105187  +    }
       105188  +    pScan->pWC = pScan->pOrigWC;
       105189  +    k = 0;
       105190  +    pScan->iEquiv += 2;
       105191  +  }
       105192  +  return 0;
       105193  +}
       105194  +
       105195  +/*
       105196  +** Initialize a WHERE clause scanner object.  Return a pointer to the
       105197  +** first match.  Return NULL if there are no matches.
       105198  +**
       105199  +** The scanner will be searching the WHERE clause pWC.  It will look
       105200  +** for terms of the form "X <op> <expr>" where X is column iColumn of table
       105201  +** iCur.  The <op> must be one of the operators described by opMask.
       105202  +**
       105203  +** If the search is for X and the WHERE clause contains terms of the
       105204  +** form X=Y then this routine might also return terms of the form
       105205  +** "Y <op> <expr>".  The number of levels of transitivity is limited,
       105206  +** but is enough to handle most commonly occurring SQL statements.
       105207  +**
       105208  +** If X is not the INTEGER PRIMARY KEY then X must be compatible with
       105209  +** index pIdx.
       105210  +*/
       105211  +WhereTerm *whereScanInit(
       105212  +  WhereScan *pScan,       /* The WhereScan object being initialized */
       105213  +  WhereClause *pWC,       /* The WHERE clause to be scanned */
       105214  +  int iCur,               /* Cursor to scan for */
       105215  +  int iColumn,            /* Column to scan for */
       105216  +  u32 opMask,             /* Operator(s) to scan for */
       105217  +  Index *pIdx             /* Must be compatible with this index */
       105218  +){
       105219  +  int j;
       105220  +
       105221  +  /* memset(pScan, 0, sizeof(*pScan)); */
       105222  +  pScan->pOrigWC = pWC;
       105223  +  pScan->pWC = pWC;
       105224  +  if( pIdx && iColumn>=0 ){
       105225  +    pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
       105226  +    for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
       105227  +      if( NEVER(j>=pIdx->nColumn) ) return 0;
       105228  +    }
       105229  +    pScan->zCollName = pIdx->azColl[j];
       105230  +  }else{
       105231  +    pScan->idxaff = 0;
       105232  +    pScan->zCollName = 0;
       105233  +  }
       105234  +  pScan->opMask = opMask;
       105235  +  pScan->k = 0;
       105236  +  pScan->aEquiv[0] = iCur;
       105237  +  pScan->aEquiv[1] = iColumn;
       105238  +  pScan->nEquiv = 2;
       105239  +  pScan->iEquiv = 2;
       105240  +  return whereScanNext(pScan);
       105241  +}
104999 105242   
105000 105243   /*
105001 105244   ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
105002 105245   ** where X is a reference to the iColumn of table iCur and <op> is one of
105003 105246   ** the WO_xx operator codes specified by the op parameter.
105004 105247   ** Return a pointer to the term.  Return 0 if not found.
105005 105248   **
................................................................................
105024 105267     WhereClause *pWC,     /* The WHERE clause to be searched */
105025 105268     int iCur,             /* Cursor number of LHS */
105026 105269     int iColumn,          /* Column number of LHS */
105027 105270     Bitmask notReady,     /* RHS must not overlap with this mask */
105028 105271     u32 op,               /* Mask of WO_xx values describing operator */
105029 105272     Index *pIdx           /* Must be compatible with this index, if not NULL */
105030 105273   ){
105031         -  WhereTerm *pTerm;            /* Term being examined as possible result */
105032         -  WhereTerm *pResult = 0;      /* The answer to return */
105033         -  WhereClause *pWCOrig = pWC;  /* Original pWC value */
105034         -  int j, k;                    /* Loop counters */
105035         -  Expr *pX;                /* Pointer to an expression */
105036         -  Parse *pParse;           /* Parsing context */
105037         -  int iOrigCol = iColumn;  /* Original value of iColumn */
105038         -  int nEquiv = 2;          /* Number of entires in aEquiv[] */
105039         -  int iEquiv = 2;          /* Number of entries of aEquiv[] processed so far */
105040         -  int aEquiv[22];          /* iCur,iColumn and up to 10 other equivalents */
105041         -
105042         -  assert( iCur>=0 );
105043         -  aEquiv[0] = iCur;
105044         -  aEquiv[1] = iColumn;
105045         -  for(;;){
105046         -    for(pWC=pWCOrig; pWC; pWC=pWC->pOuter){
105047         -      for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
105048         -        if( pTerm->leftCursor==iCur
105049         -          && pTerm->u.leftColumn==iColumn
105050         -        ){
105051         -          if( (pTerm->prereqRight & notReady)==0
105052         -           && (pTerm->eOperator & op & WO_ALL)!=0
105053         -          ){
105054         -            if( iOrigCol>=0 && pIdx && (pTerm->eOperator & WO_ISNULL)==0 ){
105055         -              CollSeq *pColl;
105056         -              char idxaff;
105057         -      
105058         -              pX = pTerm->pExpr;
105059         -              pParse = pWC->pParse;
105060         -              idxaff = pIdx->pTable->aCol[iOrigCol].affinity;
105061         -              if( !sqlite3IndexAffinityOk(pX, idxaff) ){
105062         -                continue;
105063         -              }
105064         -      
105065         -              /* Figure out the collation sequence required from an index for
105066         -              ** it to be useful for optimising expression pX. Store this
105067         -              ** value in variable pColl.
105068         -              */
105069         -              assert(pX->pLeft);
105070         -              pColl = sqlite3BinaryCompareCollSeq(pParse,pX->pLeft,pX->pRight);
105071         -              if( pColl==0 ) pColl = pParse->db->pDfltColl;
105072         -      
105073         -              for(j=0; pIdx->aiColumn[j]!=iOrigCol; j++){
105074         -                if( NEVER(j>=pIdx->nColumn) ) return 0;
105075         -              }
105076         -              if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ){
105077         -                continue;
105078         -              }
105079         -            }
105080         -            if( pTerm->prereqRight==0 && (pTerm->eOperator&WO_EQ)!=0 ){
105081         -              pResult = pTerm;
105082         -              goto findTerm_success;
105083         -            }else if( pResult==0 ){
105084         -              pResult = pTerm;
105085         -            }
105086         -          }
105087         -          if( (pTerm->eOperator & WO_EQUIV)!=0
105088         -           && nEquiv<ArraySize(aEquiv)
105089         -          ){
105090         -            pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
105091         -            assert( pX->op==TK_COLUMN );
105092         -            for(j=0; j<nEquiv; j+=2){
105093         -              if( aEquiv[j]==pX->iTable && aEquiv[j+1]==pX->iColumn ) break;
105094         -            }
105095         -            if( j==nEquiv ){
105096         -              aEquiv[j] = pX->iTable;
105097         -              aEquiv[j+1] = pX->iColumn;
105098         -              nEquiv += 2;
105099         -            }
105100         -          }
105101         -        }
105102         -      }
105103         -    }
105104         -    if( iEquiv>=nEquiv ) break;
105105         -    iCur = aEquiv[iEquiv++];
105106         -    iColumn = aEquiv[iEquiv++];
105107         -  }
105108         -findTerm_success:
       105274  +  WhereTerm *pResult = 0;
       105275  +  WhereTerm *p;
       105276  +  WhereScan scan;
       105277  +
       105278  +  p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
       105279  +  while( p ){
       105280  +    if( (p->prereqRight & notReady)==0 ){
       105281  +      if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){
       105282  +        return p;
       105283  +      }
       105284  +      if( pResult==0 ) pResult = p;
       105285  +    }
       105286  +    p = whereScanNext(&scan);
       105287  +  }
105109 105288     return pResult;
105110 105289   }
105111 105290   
105112 105291   /* Forward reference */
105113 105292   static void exprAnalyze(SrcList*, WhereClause*, int);
105114 105293   
105115 105294   /*
105116 105295   ** Call exprAnalyze on all terms in a WHERE clause.  
105117         -**
105118         -**
105119 105296   */
105120 105297   static void exprAnalyzeAll(
105121 105298     SrcList *pTabList,       /* the FROM clause */
105122 105299     WhereClause *pWC         /* the WHERE clause to be analyzed */
105123 105300   ){
105124 105301     int i;
105125 105302     for(i=pWC->nTerm-1; i>=0; i--){
................................................................................
105343 105520   ** zero.  This term is not useful for search.
105344 105521   */
105345 105522   static void exprAnalyzeOrTerm(
105346 105523     SrcList *pSrc,            /* the FROM clause */
105347 105524     WhereClause *pWC,         /* the complete WHERE clause */
105348 105525     int idxTerm               /* Index of the OR-term to be analyzed */
105349 105526   ){
105350         -  Parse *pParse = pWC->pParse;            /* Parser context */
       105527  +  WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
       105528  +  Parse *pParse = pWInfo->pParse;         /* Parser context */
105351 105529     sqlite3 *db = pParse->db;               /* Database connection */
105352 105530     WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
105353 105531     Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
105354         -  WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
105355 105532     int i;                                  /* Loop counters */
105356 105533     WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
105357 105534     WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
105358 105535     WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
105359 105536     Bitmask chngToIN;         /* Tables that might satisfy case 1 */
105360 105537     Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
105361 105538   
................................................................................
105366 105543     */
105367 105544     assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
105368 105545     assert( pExpr->op==TK_OR );
105369 105546     pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
105370 105547     if( pOrInfo==0 ) return;
105371 105548     pTerm->wtFlags |= TERM_ORINFO;
105372 105549     pOrWc = &pOrInfo->wc;
105373         -  whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
       105550  +  whereClauseInit(pOrWc, pWInfo);
105374 105551     whereSplit(pOrWc, pExpr, TK_OR);
105375 105552     exprAnalyzeAll(pSrc, pOrWc);
105376 105553     if( db->mallocFailed ) return;
105377 105554     assert( pOrWc->nTerm>=2 );
105378 105555   
105379 105556     /*
105380 105557     ** Compute the set of tables that might satisfy cases 1 or 2.
................................................................................
105392 105569           WhereTerm *pAndTerm;
105393 105570           int j;
105394 105571           Bitmask b = 0;
105395 105572           pOrTerm->u.pAndInfo = pAndInfo;
105396 105573           pOrTerm->wtFlags |= TERM_ANDINFO;
105397 105574           pOrTerm->eOperator = WO_AND;
105398 105575           pAndWC = &pAndInfo->wc;
105399         -        whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
       105576  +        whereClauseInit(pAndWC, pWC->pWInfo);
105400 105577           whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
105401 105578           exprAnalyzeAll(pSrc, pAndWC);
105402 105579           pAndWC->pOuter = pWC;
105403 105580           testcase( db->mallocFailed );
105404 105581           if( !db->mallocFailed ){
105405 105582             for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
105406 105583               assert( pAndTerm->pExpr );
105407 105584               if( allowedOp(pAndTerm->pExpr->op) ){
105408         -              b |= getMask(pMaskSet, pAndTerm->leftCursor);
       105585  +              b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
105409 105586               }
105410 105587             }
105411 105588           }
105412 105589           indexable &= b;
105413 105590         }
105414 105591       }else if( pOrTerm->wtFlags & TERM_COPIED ){
105415 105592         /* Skip this term for now.  We revisit it when we process the
105416 105593         ** corresponding TERM_VIRTUAL term */
105417 105594       }else{
105418 105595         Bitmask b;
105419         -      b = getMask(pMaskSet, pOrTerm->leftCursor);
       105596  +      b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
105420 105597         if( pOrTerm->wtFlags & TERM_VIRTUAL ){
105421 105598           WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
105422         -        b |= getMask(pMaskSet, pOther->leftCursor);
       105599  +        b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
105423 105600         }
105424 105601         indexable &= b;
105425 105602         if( (pOrTerm->eOperator & WO_EQ)==0 ){
105426 105603           chngToIN = 0;
105427 105604         }else{
105428 105605           chngToIN &= b;
105429 105606         }
................................................................................
105477 105654           pOrTerm->wtFlags &= ~TERM_OR_OK;
105478 105655           if( pOrTerm->leftCursor==iCursor ){
105479 105656             /* This is the 2-bit case and we are on the second iteration and
105480 105657             ** current term is from the first iteration.  So skip this term. */
105481 105658             assert( j==1 );
105482 105659             continue;
105483 105660           }
105484         -        if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
       105661  +        if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
105485 105662             /* This term must be of the form t1.a==t2.b where t2 is in the
105486 105663             ** chngToIN set but t1 is not.  This term will be either preceeded
105487 105664             ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
105488 105665             ** and use its inversion. */
105489 105666             testcase( pOrTerm->wtFlags & TERM_COPIED );
105490 105667             testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
105491 105668             assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
................................................................................
105496 105673           break;
105497 105674         }
105498 105675         if( i<0 ){
105499 105676           /* No candidate table+column was found.  This can only occur
105500 105677           ** on the second iteration */
105501 105678           assert( j==1 );
105502 105679           assert( IsPowerOfTwo(chngToIN) );
105503         -        assert( chngToIN==getMask(pMaskSet, iCursor) );
       105680  +        assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
105504 105681           break;
105505 105682         }
105506 105683         testcase( j==1 );
105507 105684   
105508 105685         /* We have found a candidate table and column.  Check to see if that
105509 105686         ** table and column is common to every term in the OR clause */
105510 105687         okToChngToIN = 1;
................................................................................
105545 105722   
105546 105723         for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
105547 105724           if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
105548 105725           assert( pOrTerm->eOperator & WO_EQ );
105549 105726           assert( pOrTerm->leftCursor==iCursor );
105550 105727           assert( pOrTerm->u.leftColumn==iColumn );
105551 105728           pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
105552         -        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
       105729  +        pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
105553 105730           pLeft = pOrTerm->pExpr->pLeft;
105554 105731         }
105555 105732         assert( pLeft!=0 );
105556 105733         pDup = sqlite3ExprDup(db, pLeft, 0);
105557 105734         pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
105558 105735         if( pNew ){
105559 105736           int idxNew;
................................................................................
105594 105771   ** and the copy has idxParent set to the index of the original term.
105595 105772   */
105596 105773   static void exprAnalyze(
105597 105774     SrcList *pSrc,            /* the FROM clause */
105598 105775     WhereClause *pWC,         /* the WHERE clause */
105599 105776     int idxTerm               /* Index of the term to be analyzed */
105600 105777   ){
       105778  +  WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
105601 105779     WhereTerm *pTerm;                /* The term to be analyzed */
105602 105780     WhereMaskSet *pMaskSet;          /* Set of table index masks */
105603 105781     Expr *pExpr;                     /* The expression to be analyzed */
105604 105782     Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
105605 105783     Bitmask prereqAll;               /* Prerequesites of pExpr */
105606 105784     Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
105607 105785     Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
105608 105786     int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
105609 105787     int noCase = 0;                  /* LIKE/GLOB distinguishes case */
105610 105788     int op;                          /* Top-level operator.  pExpr->op */
105611         -  Parse *pParse = pWC->pParse;     /* Parsing context */
       105789  +  Parse *pParse = pWInfo->pParse;  /* Parsing context */
105612 105790     sqlite3 *db = pParse->db;        /* Database connection */
105613 105791   
105614 105792     if( db->mallocFailed ){
105615 105793       return;
105616 105794     }
105617 105795     pTerm = &pWC->a[idxTerm];
105618         -  pMaskSet = pWC->pMaskSet;
       105796  +  pMaskSet = &pWInfo->sMaskSet;
105619 105797     pExpr = pTerm->pExpr;
105620 105798     assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
105621 105799     prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
105622 105800     op = pExpr->op;
105623 105801     if( op==TK_IN ){
105624 105802       assert( pExpr->pRight==0 );
105625 105803       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
................................................................................
105889 106067     /* Prevent ON clause terms of a LEFT JOIN from being used to drive
105890 106068     ** an index for tables to the left of the join.
105891 106069     */
105892 106070     pTerm->prereqRight |= extraRight;
105893 106071   }
105894 106072   
105895 106073   /*
105896         -** This function searches the expression list passed as the second argument
105897         -** for an expression of type TK_COLUMN that refers to the same column and
105898         -** uses the same collation sequence as the iCol'th column of index pIdx.
105899         -** Argument iBase is the cursor number used for the table that pIdx refers
105900         -** to.
       106074  +** This function searches pList for a entry that matches the iCol-th column
       106075  +** of index pIdx.
105901 106076   **
105902 106077   ** If such an expression is found, its index in pList->a[] is returned. If
105903 106078   ** no expression is found, -1 is returned.
105904 106079   */
105905 106080   static int findIndexCol(
105906 106081     Parse *pParse,                  /* Parse context */
105907 106082     ExprList *pList,                /* Expression list to search */
................................................................................
105923 106098           return i;
105924 106099         }
105925 106100       }
105926 106101     }
105927 106102   
105928 106103     return -1;
105929 106104   }
105930         -
105931         -/*
105932         -** This routine determines if pIdx can be used to assist in processing a
105933         -** DISTINCT qualifier. In other words, it tests whether or not using this
105934         -** index for the outer loop guarantees that rows with equal values for
105935         -** all expressions in the pDistinct list are delivered grouped together.
105936         -**
105937         -** For example, the query 
105938         -**
105939         -**   SELECT DISTINCT a, b, c FROM tbl WHERE a = ?
105940         -**
105941         -** can benefit from any index on columns "b" and "c".
105942         -*/
105943         -static int isDistinctIndex(
105944         -  Parse *pParse,                  /* Parsing context */
105945         -  WhereClause *pWC,               /* The WHERE clause */
105946         -  Index *pIdx,                    /* The index being considered */
105947         -  int base,                       /* Cursor number for the table pIdx is on */
105948         -  ExprList *pDistinct,            /* The DISTINCT expressions */
105949         -  int nEqCol                      /* Number of index columns with == */
105950         -){
105951         -  Bitmask mask = 0;               /* Mask of unaccounted for pDistinct exprs */
105952         -  int i;                          /* Iterator variable */
105953         -
105954         -  assert( pDistinct!=0 );
105955         -  if( pIdx->zName==0 || pDistinct->nExpr>=BMS ) return 0;
105956         -  testcase( pDistinct->nExpr==BMS-1 );
105957         -
105958         -  /* Loop through all the expressions in the distinct list. If any of them
105959         -  ** are not simple column references, return early. Otherwise, test if the
105960         -  ** WHERE clause contains a "col=X" clause. If it does, the expression
105961         -  ** can be ignored. If it does not, and the column does not belong to the
105962         -  ** same table as index pIdx, return early. Finally, if there is no
105963         -  ** matching "col=X" expression and the column is on the same table as pIdx,
105964         -  ** set the corresponding bit in variable mask.
105965         -  */
105966         -  for(i=0; i<pDistinct->nExpr; i++){
105967         -    WhereTerm *pTerm;
105968         -    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
105969         -    if( p->op!=TK_COLUMN ) return 0;
105970         -    pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
105971         -    if( pTerm ){
105972         -      Expr *pX = pTerm->pExpr;
105973         -      CollSeq *p1 = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
105974         -      CollSeq *p2 = sqlite3ExprCollSeq(pParse, p);
105975         -      if( p1==p2 ) continue;
105976         -    }
105977         -    if( p->iTable!=base ) return 0;
105978         -    mask |= (((Bitmask)1) << i);
105979         -  }
105980         -
105981         -  for(i=nEqCol; mask && i<pIdx->nColumn; i++){
105982         -    int iExpr = findIndexCol(pParse, pDistinct, base, pIdx, i);
105983         -    if( iExpr<0 ) break;
105984         -    mask &= ~(((Bitmask)1) << iExpr);
105985         -  }
105986         -
105987         -  return (mask==0);
105988         -}
105989         -
105990 106105   
105991 106106   /*
105992 106107   ** Return true if the DISTINCT expression-list passed as the third argument
105993         -** is redundant. A DISTINCT list is redundant if the database contains a
105994         -** UNIQUE index that guarantees that the result of the query will be distinct
105995         -** anyway.
       106108  +** is redundant.
       106109  +**
       106110  +** A DISTINCT list is redundant if the database contains some subset of
       106111  +** columns that are unique and non-null.
105996 106112   */
105997 106113   static int isDistinctRedundant(
105998         -  Parse *pParse,
105999         -  SrcList *pTabList,
106000         -  WhereClause *pWC,
106001         -  ExprList *pDistinct
       106114  +  Parse *pParse,            /* Parsing context */
       106115  +  SrcList *pTabList,        /* The FROM clause */
       106116  +  WhereClause *pWC,         /* The WHERE clause */
       106117  +  ExprList *pDistinct       /* The result set that needs to be DISTINCT */
106002 106118   ){
106003 106119     Table *pTab;
106004 106120     Index *pIdx;
106005 106121     int i;                          
106006 106122     int iBase;
106007 106123   
106008 106124     /* If there is more than one table or sub-select in the FROM clause of
................................................................................
106050 106166         return 1;
106051 106167       }
106052 106168     }
106053 106169   
106054 106170     return 0;
106055 106171   }
106056 106172   
106057         -/*
106058         -** Prepare a crude estimate of the logarithm of the input value.
106059         -** The results need not be exact.  This is only used for estimating
106060         -** the total cost of performing operations with O(logN) or O(NlogN)
106061         -** complexity.  Because N is just a guess, it is no great tragedy if
106062         -** logN is a little off.
106063         -*/
106064         -static double estLog(double N){
106065         -  double logN = 1;
106066         -  double x = 10;
106067         -  while( N>x ){
106068         -    logN += 1;
106069         -    x *= 10;
106070         -  }
106071         -  return logN;
       106173  +/* 
       106174  +** The (an approximate) sum of two WhereCosts.  This computation is
       106175  +** not a simple "+" operator because WhereCost is stored as a logarithmic
       106176  +** value.
       106177  +** 
       106178  +*/
       106179  +static WhereCost whereCostAdd(WhereCost a, WhereCost b){
       106180  +  static const unsigned char x[] = {
       106181  +     10, 10,                         /* 0,1 */
       106182  +      9, 9,                          /* 2,3 */
       106183  +      8, 8,                          /* 4,5 */
       106184  +      7, 7, 7,                       /* 6,7,8 */
       106185  +      6, 6, 6,                       /* 9,10,11 */
       106186  +      5, 5, 5,                       /* 12-14 */
       106187  +      4, 4, 4, 4,                    /* 15-18 */
       106188  +      3, 3, 3, 3, 3, 3,              /* 19-24 */
       106189  +      2, 2, 2, 2, 2, 2, 2,           /* 25-31 */
       106190  +  };
       106191  +  if( a>=b ){
       106192  +    if( a>b+49 ) return a;
       106193  +    if( a>b+31 ) return a+1;
       106194  +    return a+x[a-b];
       106195  +  }else{
       106196  +    if( b>a+49 ) return b;
       106197  +    if( b>a+31 ) return b+1;
       106198  +    return b+x[b-a];
       106199  +  }
       106200  +}
       106201  +
       106202  +/*
       106203  +** Convert an integer into a WhereCost.  In other words, compute a
       106204  +** good approximatation for 10*log2(x).
       106205  +*/
       106206  +static WhereCost whereCostFromInt(tRowcnt x){
       106207  +  static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
       106208  +  WhereCost y = 40;
       106209  +  if( x<8 ){
       106210  +    if( x<2 ) return 0;
       106211  +    while( x<8 ){  y -= 10; x <<= 1; }
       106212  +  }else{
       106213  +    while( x>255 ){ y += 40; x >>= 4; }
       106214  +    while( x>15 ){  y += 10; x >>= 1; }
       106215  +  }
       106216  +  return a[x&7] + y - 10;
       106217  +}
       106218  +
       106219  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       106220  +/*
       106221  +** Convert a double (as received from xBestIndex of a virtual table)
       106222  +** into a WhereCost.  In other words, compute an approximation for
       106223  +** 10*log2(x).
       106224  +*/
       106225  +static WhereCost whereCostFromDouble(double x){
       106226  +  u64 a;
       106227  +  WhereCost e;
       106228  +  assert( sizeof(x)==8 && sizeof(a)==8 );
       106229  +  if( x<=1 ) return 0;
       106230  +  if( x<=2000000000 ) return whereCostFromInt((tRowcnt)x);
       106231  +  memcpy(&a, &x, 8);
       106232  +  e = (a>>52) - 1022;
       106233  +  return e*10;
       106234  +}
       106235  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
       106236  +
       106237  +/*
       106238  +** Estimate the logarithm of the input value to base 2.
       106239  +*/
       106240  +static WhereCost estLog(WhereCost N){
       106241  +  WhereCost x = whereCostFromInt(N);
       106242  +  return x>33 ? x - 33 : 0;
106072 106243   }
106073 106244   
106074 106245   /*
106075 106246   ** Two routines for printing the content of an sqlite3_index_info
106076 106247   ** structure.  Used for testing and debugging only.  If neither
106077 106248   ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
106078 106249   ** are no-ops.
106079 106250   */
106080         -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
       106251  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
106081 106252   static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
106082 106253     int i;
106083 106254     if( !sqlite3WhereTrace ) return;
106084 106255     for(i=0; i<p->nConstraint; i++){
106085 106256       sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
106086 106257          i,
106087 106258          p->aConstraint[i].iColumn,
................................................................................
106111 106282     sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
106112 106283   }
106113 106284   #else
106114 106285   #define TRACE_IDX_INPUTS(A)
106115 106286   #define TRACE_IDX_OUTPUTS(A)
106116 106287   #endif
106117 106288   
106118         -/* 
106119         -** Required because bestIndex() is called by bestOrClauseIndex() 
106120         -*/
106121         -static void bestIndex(WhereBestIdx*);
106122         -
106123         -/*
106124         -** This routine attempts to find an scanning strategy that can be used 
106125         -** to optimize an 'OR' expression that is part of a WHERE clause. 
106126         -**
106127         -** The table associated with FROM clause term pSrc may be either a
106128         -** regular B-Tree table or a virtual table.
106129         -*/
106130         -static void bestOrClauseIndex(WhereBestIdx *p){
106131         -#ifndef SQLITE_OMIT_OR_OPTIMIZATION
106132         -  WhereClause *pWC = p->pWC;           /* The WHERE clause */
106133         -  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
106134         -  const int iCur = pSrc->iCursor;      /* The cursor of the table  */
106135         -  const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
106136         -  WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
106137         -  WhereTerm *pTerm;                    /* A single term of the WHERE clause */
106138         -
106139         -  /* The OR-clause optimization is disallowed if the INDEXED BY or
106140         -  ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
106141         -  if( pSrc->notIndexed || pSrc->pIndex!=0 ){
106142         -    return;
106143         -  }
106144         -  if( pWC->wctrlFlags & WHERE_AND_ONLY ){
106145         -    return;
106146         -  }
106147         -
106148         -  /* Search the WHERE clause terms for a usable WO_OR term. */
106149         -  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
106150         -    if( (pTerm->eOperator & WO_OR)!=0
106151         -     && ((pTerm->prereqAll & ~maskSrc) & p->notReady)==0
106152         -     && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
106153         -    ){
106154         -      WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
106155         -      WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
106156         -      WhereTerm *pOrTerm;
106157         -      int flags = WHERE_MULTI_OR;
106158         -      double rTotal = 0;
106159         -      double nRow = 0;
106160         -      Bitmask used = 0;
106161         -      WhereBestIdx sBOI;
106162         -
106163         -      sBOI = *p;
106164         -      sBOI.pOrderBy = 0;
106165         -      sBOI.pDistinct = 0;
106166         -      sBOI.ppIdxInfo = 0;
106167         -      for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
106168         -        WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
106169         -          (pOrTerm - pOrWC->a), (pTerm - pWC->a)
106170         -        ));
106171         -        if( (pOrTerm->eOperator& WO_AND)!=0 ){
106172         -          sBOI.pWC = &pOrTerm->u.pAndInfo->wc;
106173         -          bestIndex(&sBOI);
106174         -        }else if( pOrTerm->leftCursor==iCur ){
106175         -          WhereClause tempWC;
106176         -          tempWC.pParse = pWC->pParse;
106177         -          tempWC.pMaskSet = pWC->pMaskSet;
106178         -          tempWC.pOuter = pWC;
106179         -          tempWC.op = TK_AND;
106180         -          tempWC.a = pOrTerm;
106181         -          tempWC.wctrlFlags = 0;
106182         -          tempWC.nTerm = 1;
106183         -          sBOI.pWC = &tempWC;
106184         -          bestIndex(&sBOI);
106185         -        }else{
106186         -          continue;
106187         -        }
106188         -        rTotal += sBOI.cost.rCost;
106189         -        nRow += sBOI.cost.plan.nRow;
106190         -        used |= sBOI.cost.used;
106191         -        if( rTotal>=p->cost.rCost ) break;
106192         -      }
106193         -
106194         -      /* If there is an ORDER BY clause, increase the scan cost to account 
106195         -      ** for the cost of the sort. */
106196         -      if( p->pOrderBy!=0 ){
106197         -        WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
106198         -                    rTotal, rTotal+nRow*estLog(nRow)));
106199         -        rTotal += nRow*estLog(nRow);
106200         -      }
106201         -
106202         -      /* If the cost of scanning using this OR term for optimization is
106203         -      ** less than the current cost stored in pCost, replace the contents
106204         -      ** of pCost. */
106205         -      WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
106206         -      if( rTotal<p->cost.rCost ){
106207         -        p->cost.rCost = rTotal;
106208         -        p->cost.used = used;
106209         -        p->cost.plan.nRow = nRow;
106210         -        p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
106211         -        p->cost.plan.wsFlags = flags;
106212         -        p->cost.plan.u.pTerm = pTerm;
106213         -      }
106214         -    }
106215         -  }
106216         -#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
106217         -}
106218         -
106219 106289   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
106220 106290   /*
106221 106291   ** Return TRUE if the WHERE clause term pTerm is of a form where it
106222 106292   ** could be used with an index to access pSrc, assuming an appropriate
106223 106293   ** index existed.
106224 106294   */
106225 106295   static int termCanDriveIndex(
................................................................................
106227 106297     struct SrcList_item *pSrc,     /* Table we are trying to access */
106228 106298     Bitmask notReady               /* Tables in outer loops of the join */
106229 106299   ){
106230 106300     char aff;
106231 106301     if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
106232 106302     if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
106233 106303     if( (pTerm->prereqRight & notReady)!=0 ) return 0;
       106304  +  if( pTerm->u.leftColumn<0 ) return 0;
106234 106305     aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
106235 106306     if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
106236 106307     return 1;
106237 106308   }
106238 106309   #endif
106239 106310   
106240         -#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
106241         -/*
106242         -** If the query plan for pSrc specified in pCost is a full table scan
106243         -** and indexing is allows (if there is no NOT INDEXED clause) and it
106244         -** possible to construct a transient index that would perform better
106245         -** than a full table scan even when the cost of constructing the index
106246         -** is taken into account, then alter the query plan to use the
106247         -** transient index.
106248         -*/
106249         -static void bestAutomaticIndex(WhereBestIdx *p){
106250         -  Parse *pParse = p->pParse;            /* The parsing context */
106251         -  WhereClause *pWC = p->pWC;            /* The WHERE clause */
106252         -  struct SrcList_item *pSrc = p->pSrc;  /* The FROM clause term to search */
106253         -  double nTableRow;                     /* Rows in the input table */
106254         -  double logN;                          /* log(nTableRow) */
106255         -  double costTempIdx;         /* per-query cost of the transient index */
106256         -  WhereTerm *pTerm;           /* A single term of the WHERE clause */
106257         -  WhereTerm *pWCEnd;          /* End of pWC->a[] */
106258         -  Table *pTable;              /* Table tht might be indexed */
106259         -
106260         -  if( pParse->nQueryLoop<=(double)1 ){
106261         -    /* There is no point in building an automatic index for a single scan */
106262         -    return;
106263         -  }
106264         -  if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
106265         -    /* Automatic indices are disabled at run-time */
106266         -    return;
106267         -  }
106268         -  if( (p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0
106269         -   && (p->cost.plan.wsFlags & WHERE_COVER_SCAN)==0
106270         -  ){
106271         -    /* We already have some kind of index in use for this query. */
106272         -    return;
106273         -  }
106274         -  if( pSrc->viaCoroutine ){
106275         -    /* Cannot index a co-routine */
106276         -    return;
106277         -  }
106278         -  if( pSrc->notIndexed ){
106279         -    /* The NOT INDEXED clause appears in the SQL. */
106280         -    return;
106281         -  }
106282         -  if( pSrc->isCorrelated ){
106283         -    /* The source is a correlated sub-query. No point in indexing it. */
106284         -    return;
106285         -  }
106286         -
106287         -  assert( pParse->nQueryLoop >= (double)1 );
106288         -  pTable = pSrc->pTab;
106289         -  nTableRow = pTable->nRowEst;
106290         -  logN = estLog(nTableRow);
106291         -  costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
106292         -  if( costTempIdx>=p->cost.rCost ){
106293         -    /* The cost of creating the transient table would be greater than
106294         -    ** doing the full table scan */
106295         -    return;
106296         -  }
106297         -
106298         -  /* Search for any equality comparison term */
106299         -  pWCEnd = &pWC->a[pWC->nTerm];
106300         -  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
106301         -    if( termCanDriveIndex(pTerm, pSrc, p->notReady) ){
106302         -      WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
106303         -                    p->cost.rCost, costTempIdx));
106304         -      p->cost.rCost = costTempIdx;
106305         -      p->cost.plan.nRow = logN + 1;
106306         -      p->cost.plan.wsFlags = WHERE_TEMP_INDEX;
106307         -      p->cost.used = pTerm->prereqRight;
106308         -      break;
106309         -    }
106310         -  }
106311         -}
106312         -#else
106313         -# define bestAutomaticIndex(A)  /* no-op */
106314         -#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
106315         -
106316 106311   
106317 106312   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
106318 106313   /*
106319 106314   ** Generate code to construct the Index object for an automatic index
106320 106315   ** and to set up the WhereLevel object pLevel so that the code generator
106321 106316   ** makes use of the automatic index.
106322 106317   */
................................................................................
106338 106333     KeyInfo *pKeyinfo;          /* Key information for the index */   
106339 106334     int addrTop;                /* Top of the index fill loop */
106340 106335     int regRecord;              /* Register holding an index record */
106341 106336     int n;                      /* Column counter */
106342 106337     int i;                      /* Loop counter */
106343 106338     int mxBitCol;               /* Maximum column in pSrc->colUsed */
106344 106339     CollSeq *pColl;             /* Collating sequence to on a column */
       106340  +  WhereLoop *pLoop;           /* The Loop object */
106345 106341     Bitmask idxCols;            /* Bitmap of columns used for indexing */
106346 106342     Bitmask extraCols;          /* Bitmap of additional columns */
       106343  +  const int mxConstraint = 10; /* Maximum number of constraints */
106347 106344   
106348 106345     /* Generate code to skip over the creation and initialization of the
106349 106346     ** transient index on 2nd and subsequent iterations of the loop. */
106350 106347     v = pParse->pVdbe;
106351 106348     assert( v!=0 );
106352 106349     addrInit = sqlite3CodeOnce(pParse);
106353 106350   
106354 106351     /* Count the number of columns that will be added to the index
106355 106352     ** and used to match WHERE clause constraints */
106356 106353     nColumn = 0;
106357 106354     pTable = pSrc->pTab;
106358 106355     pWCEnd = &pWC->a[pWC->nTerm];
       106356  +  pLoop = pLevel->pWLoop;
106359 106357     idxCols = 0;
106360         -  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
       106358  +  for(pTerm=pWC->a; pTerm<pWCEnd && pLoop->nLTerm<mxConstraint; pTerm++){
106361 106359       if( termCanDriveIndex(pTerm, pSrc, notReady) ){
106362 106360         int iCol = pTerm->u.leftColumn;
106363         -      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
       106361  +      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
106364 106362         testcase( iCol==BMS );
106365 106363         testcase( iCol==BMS-1 );
106366 106364         if( (idxCols & cMask)==0 ){
106367         -        nColumn++;
       106365  +        if( whereLoopResize(pParse->db, pLoop, nColumn+1) ) return;
       106366  +        pLoop->aLTerm[nColumn++] = pTerm;
106368 106367           idxCols |= cMask;
106369 106368         }
106370 106369       }
106371 106370     }
106372 106371     assert( nColumn>0 );
106373         -  pLevel->plan.nEq = nColumn;
       106372  +  pLoop->u.btree.nEq = pLoop->nLTerm = nColumn;
       106373  +  pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
       106374  +                     | WHERE_TEMP_INDEX;
106374 106375   
106375 106376     /* Count the number of additional columns needed to create a
106376 106377     ** covering index.  A "covering index" is an index that contains all
106377 106378     ** columns that are needed by the query.  With a covering index, the
106378 106379     ** original table never needs to be accessed.  Automatic indices must
106379 106380     ** be a covering index because the index will not be updated if the
106380 106381     ** original table changes and the index and table cannot both be used
106381 106382     ** if they go out of sync.
106382 106383     */
106383         -  extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
       106384  +  extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
106384 106385     mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
106385 106386     testcase( pTable->nCol==BMS-1 );
106386 106387     testcase( pTable->nCol==BMS-2 );
106387 106388     for(i=0; i<mxBitCol; i++){
106388         -    if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
       106389  +    if( extraCols & MASKBIT(i) ) nColumn++;
106389 106390     }
106390         -  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
       106391  +  if( pSrc->colUsed & MASKBIT(BMS-1) ){
106391 106392       nColumn += pTable->nCol - BMS + 1;
106392 106393     }
106393         -  pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
       106394  +  pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;
106394 106395   
106395 106396     /* Construct the Index object to describe this index */
106396 106397     nByte = sizeof(Index);
106397 106398     nByte += nColumn*sizeof(int);     /* Index.aiColumn */
106398 106399     nByte += nColumn*sizeof(char*);   /* Index.azColl */
106399 106400     nByte += nColumn;                 /* Index.aSortOrder */
106400 106401     pIdx = sqlite3DbMallocZero(pParse->db, nByte);
106401 106402     if( pIdx==0 ) return;
106402         -  pLevel->plan.u.pIdx = pIdx;
       106403  +  pLoop->u.btree.pIndex = pIdx;
106403 106404     pIdx->azColl = (char**)&pIdx[1];
106404 106405     pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
106405 106406     pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
106406 106407     pIdx->zName = "auto-index";
106407 106408     pIdx->nColumn = nColumn;
106408 106409     pIdx->pTable = pTable;
106409 106410     n = 0;
106410 106411     idxCols = 0;
106411 106412     for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
106412 106413       if( termCanDriveIndex(pTerm, pSrc, notReady) ){
106413 106414         int iCol = pTerm->u.leftColumn;
106414         -      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
       106415  +      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
106415 106416         if( (idxCols & cMask)==0 ){
106416 106417           Expr *pX = pTerm->pExpr;
106417 106418           idxCols |= cMask;
106418 106419           pIdx->aiColumn[n] = pTerm->u.leftColumn;
106419 106420           pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
106420 106421           pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
106421 106422           n++;
106422 106423         }
106423 106424       }
106424 106425     }
106425         -  assert( (u32)n==pLevel->plan.nEq );
       106426  +  assert( (u32)n==pLoop->u.btree.nEq );
106426 106427   
106427 106428     /* Add additional columns needed to make the automatic index into
106428 106429     ** a covering index */
106429 106430     for(i=0; i<mxBitCol; i++){
106430         -    if( extraCols & (((Bitmask)1)<<i) ){
       106431  +    if( extraCols & MASKBIT(i) ){
106431 106432         pIdx->aiColumn[n] = i;
106432 106433         pIdx->azColl[n] = "BINARY";
106433 106434         n++;
106434 106435       }
106435 106436     }
106436         -  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
       106437  +  if( pSrc->colUsed & MASKBIT(BMS-1) ){
106437 106438       for(i=BMS-1; i<pTable->nCol; i++){
106438 106439         pIdx->aiColumn[n] = i;
106439 106440         pIdx->azColl[n] = "BINARY";
106440 106441         n++;
106441 106442       }
106442 106443     }
106443 106444     assert( n==nColumn );
106444 106445   
106445 106446     /* Create the automatic index */
106446 106447     pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
106447 106448     assert( pLevel->iIdxCur>=0 );
       106449  +  pLevel->iIdxCur = pParse->nTab++;
106448 106450     sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
106449 106451                       (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
106450 106452     VdbeComment((v, "for %s", pTable->zName));
106451 106453   
106452 106454     /* Fill the automatic index with content */
106453 106455     addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
106454 106456     regRecord = sqlite3GetTempReg(pParse);
................................................................................
106467 106469   
106468 106470   #ifndef SQLITE_OMIT_VIRTUALTABLE
106469 106471   /*
106470 106472   ** Allocate and populate an sqlite3_index_info structure. It is the 
106471 106473   ** responsibility of the caller to eventually release the structure
106472 106474   ** by passing the pointer returned by this function to sqlite3_free().
106473 106475   */
106474         -static sqlite3_index_info *allocateIndexInfo(WhereBestIdx *p){
106475         -  Parse *pParse = p->pParse; 
106476         -  WhereClause *pWC = p->pWC;
106477         -  struct SrcList_item *pSrc = p->pSrc;
106478         -  ExprList *pOrderBy = p->pOrderBy;
       106476  +static sqlite3_index_info *allocateIndexInfo(
       106477  +  Parse *pParse,
       106478  +  WhereClause *pWC,
       106479  +  struct SrcList_item *pSrc,
       106480  +  ExprList *pOrderBy
       106481  +){
106479 106482     int i, j;
106480 106483     int nTerm;
106481 106484     struct sqlite3_index_constraint *pIdxCons;
106482 106485     struct sqlite3_index_orderby *pIdxOrderBy;
106483 106486     struct sqlite3_index_constraint_usage *pUsage;
106484 106487     WhereTerm *pTerm;
106485 106488     int nOrderBy;
106486 106489     sqlite3_index_info *pIdxInfo;
106487 106490   
106488         -  WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
106489         -
106490 106491     /* Count the number of possible WHERE clause constraints referring
106491 106492     ** to this virtual table */
106492 106493     for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
106493 106494       if( pTerm->leftCursor != pSrc->iCursor ) continue;
106494 106495       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
106495 106496       testcase( pTerm->eOperator & WO_IN );
106496 106497       testcase( pTerm->eOperator & WO_ISNULL );
................................................................................
106518 106519     /* Allocate the sqlite3_index_info structure
106519 106520     */
106520 106521     pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
106521 106522                              + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
106522 106523                              + sizeof(*pIdxOrderBy)*nOrderBy );
106523 106524     if( pIdxInfo==0 ){
106524 106525       sqlite3ErrorMsg(pParse, "out of memory");
106525         -    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
106526 106526       return 0;
106527 106527     }
106528 106528   
106529 106529     /* Initialize the structure.  The sqlite3_index_info structure contains
106530 106530     ** many fields that are declared "const" to prevent xBestIndex from
106531 106531     ** changing them.  We have to do some funky casting in order to
106532 106532     ** initialize those fields.
................................................................................
106574 106574   
106575 106575     return pIdxInfo;
106576 106576   }
106577 106577   
106578 106578   /*
106579 106579   ** The table object reference passed as the second argument to this function
106580 106580   ** must represent a virtual table. This function invokes the xBestIndex()
106581         -** method of the virtual table with the sqlite3_index_info pointer passed
106582         -** as the argument.
       106581  +** method of the virtual table with the sqlite3_index_info object that
       106582  +** comes in as the 3rd argument to this function.
106583 106583   **
106584 106584   ** If an error occurs, pParse is populated with an error message and a
106585 106585   ** non-zero value is returned. Otherwise, 0 is returned and the output
106586 106586   ** part of the sqlite3_index_info structure is left populated.
106587 106587   **
106588 106588   ** Whether or not an error is returned, it is the responsibility of the
106589 106589   ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
................................................................................
106590 106590   ** that this is required.
106591 106591   */
106592 106592   static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
106593 106593     sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
106594 106594     int i;
106595 106595     int rc;
106596 106596   
106597         -  WHERETRACE(("xBestIndex for %s\n", pTab->zName));
106598 106597     TRACE_IDX_INPUTS(p);
106599 106598     rc = pVtab->pModule->xBestIndex(pVtab, p);
106600 106599     TRACE_IDX_OUTPUTS(p);
106601 106600   
106602 106601     if( rc!=SQLITE_OK ){
106603 106602       if( rc==SQLITE_NOMEM ){
106604 106603         pParse->db->mallocFailed = 1;
................................................................................
106616 106615         sqlite3ErrorMsg(pParse, 
106617 106616             "table %s: xBestIndex returned an invalid plan", pTab->zName);
106618 106617       }
106619 106618     }
106620 106619   
106621 106620     return pParse->nErr;
106622 106621   }
106623         -
106624         -
106625         -/*
106626         -** Compute the best index for a virtual table.
106627         -**
106628         -** The best index is computed by the xBestIndex method of the virtual
106629         -** table module.  This routine is really just a wrapper that sets up
106630         -** the sqlite3_index_info structure that is used to communicate with
106631         -** xBestIndex.
106632         -**
106633         -** In a join, this routine might be called multiple times for the
106634         -** same virtual table.  The sqlite3_index_info structure is created
106635         -** and initialized on the first invocation and reused on all subsequent
106636         -** invocations.  The sqlite3_index_info structure is also used when
106637         -** code is generated to access the virtual table.  The whereInfoDelete() 
106638         -** routine takes care of freeing the sqlite3_index_info structure after
106639         -** everybody has finished with it.
106640         -*/
106641         -static void bestVirtualIndex(WhereBestIdx *p){
106642         -  Parse *pParse = p->pParse;      /* The parsing context */
106643         -  WhereClause *pWC = p->pWC;      /* The WHERE clause */
106644         -  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
106645         -  Table *pTab = pSrc->pTab;
106646         -  sqlite3_index_info *pIdxInfo;
106647         -  struct sqlite3_index_constraint *pIdxCons;
106648         -  struct sqlite3_index_constraint_usage *pUsage;
106649         -  WhereTerm *pTerm;
106650         -  int i, j;
106651         -  int nOrderBy;
106652         -  int bAllowIN;                   /* Allow IN optimizations */
106653         -  double rCost;
106654         -
106655         -  /* Make sure wsFlags is initialized to some sane value. Otherwise, if the 
106656         -  ** malloc in allocateIndexInfo() fails and this function returns leaving
106657         -  ** wsFlags in an uninitialized state, the caller may behave unpredictably.
106658         -  */
106659         -  memset(&p->cost, 0, sizeof(p->cost));
106660         -  p->cost.plan.wsFlags = WHERE_VIRTUALTABLE;
106661         -
106662         -  /* If the sqlite3_index_info structure has not been previously
106663         -  ** allocated and initialized, then allocate and initialize it now.
106664         -  */
106665         -  pIdxInfo = *p->ppIdxInfo;
106666         -  if( pIdxInfo==0 ){
106667         -    *p->ppIdxInfo = pIdxInfo = allocateIndexInfo(p);
106668         -  }
106669         -  if( pIdxInfo==0 ){
106670         -    return;
106671         -  }
106672         -
106673         -  /* At this point, the sqlite3_index_info structure that pIdxInfo points
106674         -  ** to will have been initialized, either during the current invocation or
106675         -  ** during some prior invocation.  Now we just have to customize the
106676         -  ** details of pIdxInfo for the current invocation and pass it to
106677         -  ** xBestIndex.
106678         -  */
106679         -
106680         -  /* The module name must be defined. Also, by this point there must
106681         -  ** be a pointer to an sqlite3_vtab structure. Otherwise
106682         -  ** sqlite3ViewGetColumnNames() would have picked up the error. 
106683         -  */
106684         -  assert( pTab->azModuleArg && pTab->azModuleArg[0] );
106685         -  assert( sqlite3GetVTable(pParse->db, pTab) );
106686         -
106687         -  /* Try once or twice.  On the first attempt, allow IN optimizations.
106688         -  ** If an IN optimization is accepted by the virtual table xBestIndex
106689         -  ** method, but the  pInfo->aConstrainUsage.omit flag is not set, then
106690         -  ** the query will not work because it might allow duplicate rows in
106691         -  ** output.  In that case, run the xBestIndex method a second time
106692         -  ** without the IN constraints.  Usually this loop only runs once.
106693         -  ** The loop will exit using a "break" statement.
106694         -  */
106695         -  for(bAllowIN=1; 1; bAllowIN--){
106696         -    assert( bAllowIN==0 || bAllowIN==1 );
106697         -
106698         -    /* Set the aConstraint[].usable fields and initialize all 
106699         -    ** output variables to zero.
106700         -    **
106701         -    ** aConstraint[].usable is true for constraints where the right-hand
106702         -    ** side contains only references to tables to the left of the current
106703         -    ** table.  In other words, if the constraint is of the form:
106704         -    **
106705         -    **           column = expr
106706         -    **
106707         -    ** and we are evaluating a join, then the constraint on column is 
106708         -    ** only valid if all tables referenced in expr occur to the left
106709         -    ** of the table containing column.
106710         -    **
106711         -    ** The aConstraints[] array contains entries for all constraints
106712         -    ** on the current table.  That way we only have to compute it once
106713         -    ** even though we might try to pick the best index multiple times.
106714         -    ** For each attempt at picking an index, the order of tables in the
106715         -    ** join might be different so we have to recompute the usable flag
106716         -    ** each time.
106717         -    */
106718         -    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
106719         -    pUsage = pIdxInfo->aConstraintUsage;
106720         -    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
106721         -      j = pIdxCons->iTermOffset;
106722         -      pTerm = &pWC->a[j];
106723         -      if( (pTerm->prereqRight&p->notReady)==0
106724         -       && (bAllowIN || (pTerm->eOperator & WO_IN)==0)
106725         -      ){
106726         -        pIdxCons->usable = 1;
106727         -      }else{
106728         -        pIdxCons->usable = 0;
106729         -      }
106730         -    }
106731         -    memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
106732         -    if( pIdxInfo->needToFreeIdxStr ){
106733         -      sqlite3_free(pIdxInfo->idxStr);
106734         -    }
106735         -    pIdxInfo->idxStr = 0;
106736         -    pIdxInfo->idxNum = 0;
106737         -    pIdxInfo->needToFreeIdxStr = 0;
106738         -    pIdxInfo->orderByConsumed = 0;
106739         -    /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
106740         -    pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
106741         -    nOrderBy = pIdxInfo->nOrderBy;
106742         -    if( !p->pOrderBy ){
106743         -      pIdxInfo->nOrderBy = 0;
106744         -    }
106745         -  
106746         -    if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
106747         -      return;
106748         -    }
106749         -  
106750         -    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
106751         -    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
106752         -      if( pUsage[i].argvIndex>0 ){
106753         -        j = pIdxCons->iTermOffset;
106754         -        pTerm = &pWC->a[j];
106755         -        p->cost.used |= pTerm->prereqRight;
106756         -        if( (pTerm->eOperator & WO_IN)!=0 ){
106757         -          if( pUsage[i].omit==0 ){
106758         -            /* Do not attempt to use an IN constraint if the virtual table
106759         -            ** says that the equivalent EQ constraint cannot be safely omitted.
106760         -            ** If we do attempt to use such a constraint, some rows might be
106761         -            ** repeated in the output. */
106762         -            break;
106763         -          }
106764         -          /* A virtual table that is constrained by an IN clause may not
106765         -          ** consume the ORDER BY clause because (1) the order of IN terms
106766         -          ** is not necessarily related to the order of output terms and
106767         -          ** (2) Multiple outputs from a single IN value will not merge
106768         -          ** together.  */
106769         -          pIdxInfo->orderByConsumed = 0;
106770         -        }
106771         -      }
106772         -    }
106773         -    if( i>=pIdxInfo->nConstraint ) break;
106774         -  }
106775         -
106776         -  /* The orderByConsumed signal is only valid if all outer loops collectively
106777         -  ** generate just a single row of output.
106778         -  */
106779         -  if( pIdxInfo->orderByConsumed ){
106780         -    for(i=0; i<p->i; i++){
106781         -      if( (p->aLevel[i].plan.wsFlags & WHERE_UNIQUE)==0 ){
106782         -        pIdxInfo->orderByConsumed = 0;
106783         -      }
106784         -    }
106785         -  }
106786         -  
106787         -  /* If there is an ORDER BY clause, and the selected virtual table index
106788         -  ** does not satisfy it, increase the cost of the scan accordingly. This
106789         -  ** matches the processing for non-virtual tables in bestBtreeIndex().
106790         -  */
106791         -  rCost = pIdxInfo->estimatedCost;
106792         -  if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
106793         -    rCost += estLog(rCost)*rCost;
106794         -  }
106795         -
106796         -  /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
106797         -  ** inital value of lowestCost in this loop. If it is, then the
106798         -  ** (cost<lowestCost) test below will never be true.
106799         -  ** 
106800         -  ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT 
106801         -  ** is defined.
106802         -  */
106803         -  if( (SQLITE_BIG_DBL/((double)2))<rCost ){
106804         -    p->cost.rCost = (SQLITE_BIG_DBL/((double)2));
106805         -  }else{
106806         -    p->cost.rCost = rCost;
106807         -  }
106808         -  p->cost.plan.u.pVtabIdx = pIdxInfo;
106809         -  if( pIdxInfo->orderByConsumed ){
106810         -    p->cost.plan.wsFlags |= WHERE_ORDERED;
106811         -    p->cost.plan.nOBSat = nOrderBy;
106812         -  }else{
106813         -    p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
106814         -  }
106815         -  p->cost.plan.nEq = 0;
106816         -  pIdxInfo->nOrderBy = nOrderBy;
106817         -
106818         -  /* Try to find a more efficient access pattern by using multiple indexes
106819         -  ** to optimize an OR expression within the WHERE clause. 
106820         -  */
106821         -  bestOrClauseIndex(p);
106822         -}
106823         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
       106622  +#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
       106623  +
106824 106624   
106825 106625   #ifdef SQLITE_ENABLE_STAT3
106826 106626   /*
106827 106627   ** Estimate the location of a particular key among all keys in an
106828 106628   ** index.  Store the results in aStat as follows:
106829 106629   **
106830 106630   **    aStat[0]      Est. number of rows less than pVal
................................................................................
107058 106858   */
107059 106859   static int whereRangeScanEst(
107060 106860     Parse *pParse,       /* Parsing & code generating context */
107061 106861     Index *p,            /* The index containing the range-compared column; "x" */
107062 106862     int nEq,             /* index into p->aCol[] of the range-compared column */
107063 106863     WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
107064 106864     WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
107065         -  double *pRangeDiv   /* OUT: Reduce search space by this divisor */
       106865  +  WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */
107066 106866   ){
107067 106867     int rc = SQLITE_OK;
107068 106868   
107069 106869   #ifdef SQLITE_ENABLE_STAT3
107070 106870   
107071 106871     if( nEq==0 && p->nSample ){
107072 106872       sqlite3_value *pRangeVal;
................................................................................
107096 106896         ){
107097 106897           iUpper = a[0];
107098 106898           if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1];
107099 106899         }
107100 106900         sqlite3ValueFree(pRangeVal);
107101 106901       }
107102 106902       if( rc==SQLITE_OK ){
107103         -      if( iUpper<=iLower ){
107104         -        *pRangeDiv = (double)p->aiRowEst[0];
107105         -      }else{
107106         -        *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
       106903  +      WhereCost iBase = whereCostFromInt(p->aiRowEst[0]);
       106904  +      if( iUpper>iLower ){
       106905  +        iBase -= whereCostFromInt(iUpper - iLower);
107107 106906         }
107108         -      WHERETRACE(("range scan regions: %u..%u  div=%g\n",
107109         -                  (u32)iLower, (u32)iUpper, *pRangeDiv));
       106907  +      *pRangeDiv = iBase;
       106908  +      WHERETRACE(0x100, ("range scan regions: %u..%u  div=%d\n",
       106909  +                         (u32)iLower, (u32)iUpper, *pRangeDiv));
107110 106910         return SQLITE_OK;
107111 106911       }
107112 106912     }
107113 106913   #else
107114 106914     UNUSED_PARAMETER(pParse);
107115 106915     UNUSED_PARAMETER(p);
107116 106916     UNUSED_PARAMETER(nEq);
107117 106917   #endif
107118 106918     assert( pLower || pUpper );
107119         -  *pRangeDiv = (double)1;
107120         -  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
107121         -  if( pUpper ) *pRangeDiv *= (double)4;
       106919  +  *pRangeDiv = 0;
       106920  +  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
       106921  +    *pRangeDiv += 20;  assert( 20==whereCostFromInt(4) );
       106922  +  }
       106923  +  if( pUpper ){
       106924  +    *pRangeDiv += 20;  assert( 20==whereCostFromInt(4) );
       106925  +  }
107122 106926     return rc;
107123 106927   }
107124 106928   
107125 106929   #ifdef SQLITE_ENABLE_STAT3
107126 106930   /*
107127 106931   ** Estimate the number of rows that will be returned based on
107128 106932   ** an equality constraint x=VALUE and where that VALUE occurs in
................................................................................
107140 106944   ** for a UTF conversion required for comparison.  The error is stored
107141 106945   ** in the pParse structure.
107142 106946   */
107143 106947   static int whereEqualScanEst(
107144 106948     Parse *pParse,       /* Parsing & code generating context */
107145 106949     Index *p,            /* The index whose left-most column is pTerm */
107146 106950     Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
107147         -  double *pnRow        /* Write the revised row estimate here */
       106951  +  tRowcnt *pnRow       /* Write the revised row estimate here */
107148 106952   ){
107149 106953     sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
107150 106954     u8 aff;                   /* Column affinity */
107151 106955     int rc;                   /* Subfunction return code */
107152 106956     tRowcnt a[2];             /* Statistics */
107153 106957   
107154 106958     assert( p->aSample!=0 );
................................................................................
107159 106963       if( rc ) goto whereEqualScanEst_cancel;
107160 106964     }else{
107161 106965       pRhs = sqlite3ValueNew(pParse->db);
107162 106966     }
107163 106967     if( pRhs==0 ) return SQLITE_NOTFOUND;
107164 106968     rc = whereKeyStats(pParse, p, pRhs, 0, a);
107165 106969     if( rc==SQLITE_OK ){
107166         -    WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
       106970  +    WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
107167 106971       *pnRow = a[1];
107168 106972     }
107169 106973   whereEqualScanEst_cancel:
107170 106974     sqlite3ValueFree(pRhs);
107171 106975     return rc;
107172 106976   }
107173 106977   #endif /* defined(SQLITE_ENABLE_STAT3) */
................................................................................
107189 106993   ** for a UTF conversion required for comparison.  The error is stored
107190 106994   ** in the pParse structure.
107191 106995   */
107192 106996   static int whereInScanEst(
107193 106997     Parse *pParse,       /* Parsing & code generating context */
107194 106998     Index *p,            /* The index whose left-most column is pTerm */
107195 106999     ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
107196         -  double *pnRow        /* Write the revised row estimate here */
       107000  +  tRowcnt *pnRow       /* Write the revised row estimate here */
107197 107001   ){
107198         -  int rc = SQLITE_OK;         /* Subfunction return code */
107199         -  double nEst;                /* Number of rows for a single term */
107200         -  double nRowEst = (double)0; /* New estimate of the number of rows */
107201         -  int i;                      /* Loop counter */
       107002  +  int rc = SQLITE_OK;     /* Subfunction return code */
       107003  +  tRowcnt nEst;           /* Number of rows for a single term */
       107004  +  tRowcnt nRowEst = 0;    /* New estimate of the number of rows */
       107005  +  int i;                  /* Loop counter */
107202 107006   
107203 107007     assert( p->aSample!=0 );
107204 107008     for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
107205 107009       nEst = p->aiRowEst[0];
107206 107010       rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
107207 107011       nRowEst += nEst;
107208 107012     }
107209 107013     if( rc==SQLITE_OK ){
107210 107014       if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
107211 107015       *pnRow = nRowEst;
107212         -    WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
107213         -  }
107214         -  return rc;
107215         -}
107216         -#endif /* defined(SQLITE_ENABLE_STAT3) */
107217         -
107218         -/*
107219         -** Check to see if column iCol of the table with cursor iTab will appear
107220         -** in sorted order according to the current query plan.
107221         -**
107222         -** Return values:
107223         -**
107224         -**    0   iCol is not ordered
107225         -**    1   iCol has only a single value
107226         -**    2   iCol is in ASC order
107227         -**    3   iCol is in DESC order
107228         -*/
107229         -static int isOrderedColumn(
107230         -  WhereBestIdx *p,
107231         -  int iTab,
107232         -  int iCol
107233         -){
107234         -  int i, j;
107235         -  WhereLevel *pLevel = &p->aLevel[p->i-1];
107236         -  Index *pIdx;
107237         -  u8 sortOrder;
107238         -  for(i=p->i-1; i>=0; i--, pLevel--){
107239         -    if( pLevel->iTabCur!=iTab ) continue;
107240         -    if( (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
107241         -      return 1;
107242         -    }
107243         -    assert( (pLevel->plan.wsFlags & WHERE_ORDERED)!=0 );
107244         -    if( (pIdx = pLevel->plan.u.pIdx)!=0 ){
107245         -      if( iCol<0 ){
107246         -        sortOrder = 0;
107247         -        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
107248         -      }else{
107249         -        int n = pIdx->nColumn;
107250         -        for(j=0; j<n; j++){
107251         -          if( iCol==pIdx->aiColumn[j] ) break;
107252         -        }
107253         -        if( j>=n ) return 0;
107254         -        sortOrder = pIdx->aSortOrder[j];
107255         -        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
107256         -      }
107257         -    }else{
107258         -      if( iCol!=(-1) ) return 0;
107259         -      sortOrder = 0;
107260         -      testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
107261         -    }
107262         -    if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
107263         -      assert( sortOrder==0 || sortOrder==1 );
107264         -      testcase( sortOrder==1 );
107265         -      sortOrder = 1 - sortOrder;
107266         -    }
107267         -    return sortOrder+2;
107268         -  }
107269         -  return 0;
107270         -}
107271         -
107272         -/*
107273         -** This routine decides if pIdx can be used to satisfy the ORDER BY
107274         -** clause, either in whole or in part.  The return value is the 
107275         -** cumulative number of terms in the ORDER BY clause that are satisfied
107276         -** by the index pIdx and other indices in outer loops.
107277         -**
107278         -** The table being queried has a cursor number of "base".  pIdx is the
107279         -** index that is postulated for use to access the table.
107280         -**
107281         -** The *pbRev value is set to 0 order 1 depending on whether or not
107282         -** pIdx should be run in the forward order or in reverse order.
107283         -*/
107284         -static int isSortingIndex(
107285         -  WhereBestIdx *p,    /* Best index search context */
107286         -  Index *pIdx,        /* The index we are testing */
107287         -  int base,           /* Cursor number for the table to be sorted */
107288         -  int *pbRev,         /* Set to 1 for reverse-order scan of pIdx */
107289         -  int *pbObUnique     /* ORDER BY column values will different in every row */
107290         -){
107291         -  int i;                        /* Number of pIdx terms used */
107292         -  int j;                        /* Number of ORDER BY terms satisfied */
107293         -  int sortOrder = 2;            /* 0: forward.  1: backward.  2: unknown */
107294         -  int nTerm;                    /* Number of ORDER BY terms */
107295         -  struct ExprList_item *pOBItem;/* A term of the ORDER BY clause */
107296         -  Table *pTab = pIdx->pTable;   /* Table that owns index pIdx */
107297         -  ExprList *pOrderBy;           /* The ORDER BY clause */
107298         -  Parse *pParse = p->pParse;    /* Parser context */
107299         -  sqlite3 *db = pParse->db;     /* Database connection */
107300         -  int nPriorSat;                /* ORDER BY terms satisfied by outer loops */
107301         -  int seenRowid = 0;            /* True if an ORDER BY rowid term is seen */
107302         -  int uniqueNotNull;            /* pIdx is UNIQUE with all terms are NOT NULL */
107303         -  int outerObUnique;            /* Outer loops generate different values in
107304         -                                ** every row for the ORDER BY columns */
107305         -
107306         -  if( p->i==0 ){
107307         -    nPriorSat = 0;
107308         -    outerObUnique = 1;
107309         -  }else{
107310         -    u32 wsFlags = p->aLevel[p->i-1].plan.wsFlags;
107311         -    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
107312         -    if( (wsFlags & WHERE_ORDERED)==0 ){
107313         -      /* This loop cannot be ordered unless the next outer loop is
107314         -      ** also ordered */
107315         -      return nPriorSat;
107316         -    }
107317         -    if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ){
107318         -      /* Only look at the outer-most loop if the OrderByIdxJoin
107319         -      ** optimization is disabled */
107320         -      return nPriorSat;
107321         -    }
107322         -    testcase( wsFlags & WHERE_OB_UNIQUE );
107323         -    testcase( wsFlags & WHERE_ALL_UNIQUE );
107324         -    outerObUnique = (wsFlags & (WHERE_OB_UNIQUE|WHERE_ALL_UNIQUE))!=0;
107325         -  }
107326         -  pOrderBy = p->pOrderBy;
107327         -  assert( pOrderBy!=0 );
107328         -  if( pIdx->bUnordered ){
107329         -    /* Hash indices (indicated by the "unordered" tag on sqlite_stat1) cannot
107330         -    ** be used for sorting */
107331         -    return nPriorSat;
107332         -  }
107333         -  nTerm = pOrderBy->nExpr;
107334         -  uniqueNotNull = pIdx->onError!=OE_None;
107335         -  assert( nTerm>0 );
107336         -
107337         -  /* Argument pIdx must either point to a 'real' named index structure, 
107338         -  ** or an index structure allocated on the stack by bestBtreeIndex() to
107339         -  ** represent the rowid index that is part of every table.  */
107340         -  assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
107341         -
107342         -  /* Match terms of the ORDER BY clause against columns of
107343         -  ** the index.
107344         -  **
107345         -  ** Note that indices have pIdx->nColumn regular columns plus
107346         -  ** one additional column containing the rowid.  The rowid column
107347         -  ** of the index is also allowed to match against the ORDER BY
107348         -  ** clause.
107349         -  */
107350         -  j = nPriorSat;
107351         -  for(i=0,pOBItem=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
107352         -    Expr *pOBExpr;          /* The expression of the ORDER BY pOBItem */
107353         -    CollSeq *pColl;         /* The collating sequence of pOBExpr */
107354         -    int termSortOrder;      /* Sort order for this term */
107355         -    int iColumn;            /* The i-th column of the index.  -1 for rowid */
107356         -    int iSortOrder;         /* 1 for DESC, 0 for ASC on the i-th index term */
107357         -    int isEq;               /* Subject to an == or IS NULL constraint */
107358         -    int isMatch;            /* ORDER BY term matches the index term */
107359         -    const char *zColl;      /* Name of collating sequence for i-th index term */
107360         -    WhereTerm *pConstraint; /* A constraint in the WHERE clause */
107361         -
107362         -    /* If the next term of the ORDER BY clause refers to anything other than
107363         -    ** a column in the "base" table, then this index will not be of any
107364         -    ** further use in handling the ORDER BY. */
107365         -    pOBExpr = sqlite3ExprSkipCollate(pOBItem->pExpr);
107366         -    if( pOBExpr->op!=TK_COLUMN || pOBExpr->iTable!=base ){
107367         -      break;
107368         -    }
107369         -
107370         -    /* Find column number and collating sequence for the next entry
107371         -    ** in the index */
107372         -    if( pIdx->zName && i<pIdx->nColumn ){
107373         -      iColumn = pIdx->aiColumn[i];
107374         -      if( iColumn==pIdx->pTable->iPKey ){
107375         -        iColumn = -1;
107376         -      }
107377         -      iSortOrder = pIdx->aSortOrder[i];
107378         -      zColl = pIdx->azColl[i];
107379         -      assert( zColl!=0 );
107380         -    }else{
107381         -      iColumn = -1;
107382         -      iSortOrder = 0;
107383         -      zColl = 0;
107384         -    }
107385         -
107386         -    /* Check to see if the column number and collating sequence of the
107387         -    ** index match the column number and collating sequence of the ORDER BY
107388         -    ** clause entry.  Set isMatch to 1 if they both match. */
107389         -    if( pOBExpr->iColumn==iColumn ){
107390         -      if( zColl ){
107391         -        pColl = sqlite3ExprCollSeq(pParse, pOBItem->pExpr);
107392         -        if( !pColl ) pColl = db->pDfltColl;
107393         -        isMatch = sqlite3StrICmp(pColl->zName, zColl)==0;
107394         -      }else{
107395         -        isMatch = 1;
107396         -      }
107397         -    }else{
107398         -      isMatch = 0;
107399         -    }
107400         -
107401         -    /* termSortOrder is 0 or 1 for whether or not the access loop should
107402         -    ** run forward or backwards (respectively) in order to satisfy this 
107403         -    ** term of the ORDER BY clause. */
107404         -    assert( pOBItem->sortOrder==0 || pOBItem->sortOrder==1 );
107405         -    assert( iSortOrder==0 || iSortOrder==1 );
107406         -    termSortOrder = iSortOrder ^ pOBItem->sortOrder;
107407         -
107408         -    /* If X is the column in the index and ORDER BY clause, check to see
107409         -    ** if there are any X= or X IS NULL constraints in the WHERE clause. */
107410         -    pConstraint = findTerm(p->pWC, base, iColumn, p->notReady,
107411         -                           WO_EQ|WO_ISNULL|WO_IN, pIdx);
107412         -    if( pConstraint==0 ){
107413         -      isEq = 0;
107414         -    }else if( (pConstraint->eOperator & WO_IN)!=0 ){
107415         -      isEq = 0;
107416         -    }else if( (pConstraint->eOperator & WO_ISNULL)!=0 ){
107417         -      uniqueNotNull = 0;
107418         -      isEq = 1;  /* "X IS NULL" means X has only a single value */
107419         -    }else if( pConstraint->prereqRight==0 ){
107420         -      isEq = 1;  /* Constraint "X=constant" means X has only a single value */
107421         -    }else{
107422         -      Expr *pRight = pConstraint->pExpr->pRight;
107423         -      if( pRight->op==TK_COLUMN ){
107424         -        WHERETRACE(("       .. isOrderedColumn(tab=%d,col=%d)",
107425         -                    pRight->iTable, pRight->iColumn));
107426         -        isEq = isOrderedColumn(p, pRight->iTable, pRight->iColumn);
107427         -        WHERETRACE((" -> isEq=%d\n", isEq));
107428         -
107429         -        /* If the constraint is of the form X=Y where Y is an ordered value
107430         -        ** in an outer loop, then make sure the sort order of Y matches the
107431         -        ** sort order required for X. */
107432         -        if( isMatch && isEq>=2 && isEq!=pOBItem->sortOrder+2 ){
107433         -          testcase( isEq==2 );
107434         -          testcase( isEq==3 );
107435         -          break;
107436         -        }
107437         -      }else{
107438         -        isEq = 0;  /* "X=expr" places no ordering constraints on X */
107439         -      }
107440         -    }
107441         -    if( !isMatch ){
107442         -      if( isEq==0 ){
107443         -        break;
107444         -      }else{
107445         -        continue;
107446         -      }
107447         -    }else if( isEq!=1 ){
107448         -      if( sortOrder==2 ){
107449         -        sortOrder = termSortOrder;
107450         -      }else if( termSortOrder!=sortOrder ){
107451         -        break;
107452         -      }
107453         -    }
107454         -    j++;
107455         -    pOBItem++;
107456         -    if( iColumn<0 ){
107457         -      seenRowid = 1;
107458         -      break;
107459         -    }else if( pTab->aCol[iColumn].notNull==0 && isEq!=1 ){
107460         -      testcase( isEq==0 );
107461         -      testcase( isEq==2 );
107462         -      testcase( isEq==3 );
107463         -      uniqueNotNull = 0;
107464         -    }
107465         -  }
107466         -  if( seenRowid ){
107467         -    uniqueNotNull = 1;
107468         -  }else if( uniqueNotNull==0 || i<pIdx->nColumn ){
107469         -    uniqueNotNull = 0;
107470         -  }
107471         -
107472         -  /* If we have not found at least one ORDER BY term that matches the
107473         -  ** index, then show no progress. */
107474         -  if( pOBItem==&pOrderBy->a[nPriorSat] ) return nPriorSat;
107475         -
107476         -  /* Either the outer queries must generate rows where there are no two
107477         -  ** rows with the same values in all ORDER BY columns, or else this
107478         -  ** loop must generate just a single row of output.  Example:  Suppose
107479         -  ** the outer loops generate A=1 and A=1, and this loop generates B=3
107480         -  ** and B=4.  Then without the following test, ORDER BY A,B would 
107481         -  ** generate the wrong order output: 1,3 1,4 1,3 1,4
107482         -  */
107483         -  if( outerObUnique==0 && uniqueNotNull==0 ) return nPriorSat;
107484         -  *pbObUnique = uniqueNotNull;
107485         -
107486         -  /* Return the necessary scan order back to the caller */
107487         -  *pbRev = sortOrder & 1;
107488         -
107489         -  /* If there was an "ORDER BY rowid" term that matched, or it is only
107490         -  ** possible for a single row from this table to match, then skip over
107491         -  ** any additional ORDER BY terms dealing with this table.
107492         -  */
107493         -  if( uniqueNotNull ){
107494         -    /* Advance j over additional ORDER BY terms associated with base */
107495         -    WhereMaskSet *pMS = p->pWC->pMaskSet;
107496         -    Bitmask m = ~getMask(pMS, base);
107497         -    while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
107498         -      j++;
107499         -    }
107500         -  }
107501         -  return j;
107502         -}
107503         -
107504         -/*
107505         -** Find the best query plan for accessing a particular table.  Write the
107506         -** best query plan and its cost into the p->cost.
107507         -**
107508         -** The lowest cost plan wins.  The cost is an estimate of the amount of
107509         -** CPU and disk I/O needed to process the requested result.
107510         -** Factors that influence cost include:
107511         -**
107512         -**    *  The estimated number of rows that will be retrieved.  (The
107513         -**       fewer the better.)
107514         -**
107515         -**    *  Whether or not sorting must occur.
107516         -**
107517         -**    *  Whether or not there must be separate lookups in the
107518         -**       index and in the main table.
107519         -**
107520         -** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
107521         -** the SQL statement, then this function only considers plans using the 
107522         -** named index. If no such plan is found, then the returned cost is
107523         -** SQLITE_BIG_DBL. If a plan is found that uses the named index, 
107524         -** then the cost is calculated in the usual way.
107525         -**
107526         -** If a NOT INDEXED clause was attached to the table 
107527         -** in the SELECT statement, then no indexes are considered. However, the 
107528         -** selected plan may still take advantage of the built-in rowid primary key
107529         -** index.
107530         -*/
107531         -static void bestBtreeIndex(WhereBestIdx *p){
107532         -  Parse *pParse = p->pParse;  /* The parsing context */
107533         -  WhereClause *pWC = p->pWC;  /* The WHERE clause */
107534         -  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
107535         -  int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
107536         -  Index *pProbe;              /* An index we are evaluating */
107537         -  Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
107538         -  int eqTermMask;             /* Current mask of valid equality operators */
107539         -  int idxEqTermMask;          /* Index mask of valid equality operators */
107540         -  Index sPk;                  /* A fake index object for the primary key */
107541         -  tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
107542         -  int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
107543         -  int wsFlagMask;             /* Allowed flags in p->cost.plan.wsFlag */
107544         -  int nPriorSat;              /* ORDER BY terms satisfied by outer loops */
107545         -  int nOrderBy;               /* Number of ORDER BY terms */
107546         -  char bSortInit;             /* Initializer for bSort in inner loop */
107547         -  char bDistInit;             /* Initializer for bDist in inner loop */
107548         -
107549         -
107550         -  /* Initialize the cost to a worst-case value */
107551         -  memset(&p->cost, 0, sizeof(p->cost));
107552         -  p->cost.rCost = SQLITE_BIG_DBL;
107553         -
107554         -  /* If the pSrc table is the right table of a LEFT JOIN then we may not
107555         -  ** use an index to satisfy IS NULL constraints on that table.  This is
107556         -  ** because columns might end up being NULL if the table does not match -
107557         -  ** a circumstance which the index cannot help us discover.  Ticket #2177.
107558         -  */
107559         -  if( pSrc->jointype & JT_LEFT ){
107560         -    idxEqTermMask = WO_EQ|WO_IN;
107561         -  }else{
107562         -    idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
107563         -  }
107564         -
107565         -  if( pSrc->pIndex ){
107566         -    /* An INDEXED BY clause specifies a particular index to use */
107567         -    pIdx = pProbe = pSrc->pIndex;
107568         -    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
107569         -    eqTermMask = idxEqTermMask;
107570         -  }else{
107571         -    /* There is no INDEXED BY clause.  Create a fake Index object in local
107572         -    ** variable sPk to represent the rowid primary key index.  Make this
107573         -    ** fake index the first in a chain of Index objects with all of the real
107574         -    ** indices to follow */
107575         -    Index *pFirst;                  /* First of real indices on the table */
107576         -    memset(&sPk, 0, sizeof(Index));
107577         -    sPk.nColumn = 1;
107578         -    sPk.aiColumn = &aiColumnPk;
107579         -    sPk.aiRowEst = aiRowEstPk;
107580         -    sPk.onError = OE_Replace;
107581         -    sPk.pTable = pSrc->pTab;
107582         -    aiRowEstPk[0] = pSrc->pTab->nRowEst;
107583         -    aiRowEstPk[1] = 1;
107584         -    pFirst = pSrc->pTab->pIndex;
107585         -    if( pSrc->notIndexed==0 ){
107586         -      /* The real indices of the table are only considered if the
107587         -      ** NOT INDEXED qualifier is omitted from the FROM clause */
107588         -      sPk.pNext = pFirst;
107589         -    }
107590         -    pProbe = &sPk;
107591         -    wsFlagMask = ~(
107592         -        WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
107593         -    );
107594         -    eqTermMask = WO_EQ|WO_IN;
107595         -    pIdx = 0;
107596         -  }
107597         -
107598         -  nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
107599         -  if( p->i ){
107600         -    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
107601         -    bSortInit = nPriorSat<nOrderBy;
107602         -    bDistInit = 0;
107603         -  }else{
107604         -    nPriorSat = 0;
107605         -    bSortInit = nOrderBy>0;
107606         -    bDistInit = p->pDistinct!=0;
107607         -  }
107608         -
107609         -  /* Loop over all indices looking for the best one to use
107610         -  */
107611         -  for(; pProbe; pIdx=pProbe=pProbe->pNext){
107612         -    const tRowcnt * const aiRowEst = pProbe->aiRowEst;
107613         -    WhereCost pc;               /* Cost of using pProbe */
107614         -    double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
107615         -
107616         -    /* The following variables are populated based on the properties of
107617         -    ** index being evaluated. They are then used to determine the expected
107618         -    ** cost and number of rows returned.
107619         -    **
107620         -    **  pc.plan.nEq: 
107621         -    **    Number of equality terms that can be implemented using the index.
107622         -    **    In other words, the number of initial fields in the index that
107623         -    **    are used in == or IN or NOT NULL constraints of the WHERE clause.
107624         -    **
107625         -    **  nInMul:  
107626         -    **    The "in-multiplier". This is an estimate of how many seek operations 
107627         -    **    SQLite must perform on the index in question. For example, if the 
107628         -    **    WHERE clause is:
107629         -    **
107630         -    **      WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
107631         -    **
107632         -    **    SQLite must perform 9 lookups on an index on (a, b), so nInMul is 
107633         -    **    set to 9. Given the same schema and either of the following WHERE 
107634         -    **    clauses:
107635         -    **
107636         -    **      WHERE a =  1
107637         -    **      WHERE a >= 2
107638         -    **
107639         -    **    nInMul is set to 1.
107640         -    **
107641         -    **    If there exists a WHERE term of the form "x IN (SELECT ...)", then 
107642         -    **    the sub-select is assumed to return 25 rows for the purposes of 
107643         -    **    determining nInMul.
107644         -    **
107645         -    **  bInEst:  
107646         -    **    Set to true if there was at least one "x IN (SELECT ...)" term used 
107647         -    **    in determining the value of nInMul.  Note that the RHS of the
107648         -    **    IN operator must be a SELECT, not a value list, for this variable
107649         -    **    to be true.
107650         -    **
107651         -    **  rangeDiv:
107652         -    **    An estimate of a divisor by which to reduce the search space due
107653         -    **    to inequality constraints.  In the absence of sqlite_stat3 ANALYZE
107654         -    **    data, a single inequality reduces the search space to 1/4rd its
107655         -    **    original size (rangeDiv==4).  Two inequalities reduce the search
107656         -    **    space to 1/16th of its original size (rangeDiv==16).
107657         -    **
107658         -    **  bSort:   
107659         -    **    Boolean. True if there is an ORDER BY clause that will require an 
107660         -    **    external sort (i.e. scanning the index being evaluated will not 
107661         -    **    correctly order records).
107662         -    **
107663         -    **  bDist:
107664         -    **    Boolean. True if there is a DISTINCT clause that will require an 
107665         -    **    external btree.
107666         -    **
107667         -    **  bLookup: 
107668         -    **    Boolean. True if a table lookup is required for each index entry
107669         -    **    visited.  In other words, true if this is not a covering index.
107670         -    **    This is always false for the rowid primary key index of a table.
107671         -    **    For other indexes, it is true unless all the columns of the table
107672         -    **    used by the SELECT statement are present in the index (such an
107673         -    **    index is sometimes described as a covering index).
107674         -    **    For example, given the index on (a, b), the second of the following 
107675         -    **    two queries requires table b-tree lookups in order to find the value
107676         -    **    of column c, but the first does not because columns a and b are
107677         -    **    both available in the index.
107678         -    **
107679         -    **             SELECT a, b    FROM tbl WHERE a = 1;
107680         -    **             SELECT a, b, c FROM tbl WHERE a = 1;
107681         -    */
107682         -    int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
107683         -    int nInMul = 1;               /* Number of distinct equalities to lookup */
107684         -    double rangeDiv = (double)1;  /* Estimated reduction in search space */
107685         -    int nBound = 0;               /* Number of range constraints seen */
107686         -    char bSort = bSortInit;       /* True if external sort required */
107687         -    char bDist = bDistInit;       /* True if index cannot help with DISTINCT */
107688         -    char bLookup = 0;             /* True if not a covering index */
107689         -    WhereTerm *pTerm;             /* A single term of the WHERE clause */
107690         -#ifdef SQLITE_ENABLE_STAT3
107691         -    WhereTerm *pFirstTerm = 0;    /* First term matching the index */
107692         -#endif
107693         -
107694         -    WHERETRACE((
107695         -      "   %s(%s):\n",
107696         -      pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk")
107697         -    ));
107698         -    memset(&pc, 0, sizeof(pc));
107699         -    pc.plan.nOBSat = nPriorSat;
107700         -
107701         -    /* Determine the values of pc.plan.nEq and nInMul */
107702         -    for(pc.plan.nEq=0; pc.plan.nEq<pProbe->nColumn; pc.plan.nEq++){
107703         -      int j = pProbe->aiColumn[pc.plan.nEq];
107704         -      pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
107705         -      if( pTerm==0 ) break;
107706         -      pc.plan.wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
107707         -      testcase( pTerm->pWC!=pWC );
107708         -      if( pTerm->eOperator & WO_IN ){
107709         -        Expr *pExpr = pTerm->pExpr;
107710         -        pc.plan.wsFlags |= WHERE_COLUMN_IN;
107711         -        if( ExprHasProperty(pExpr, EP_xIsSelect) ){
107712         -          /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
107713         -          nInMul *= 25;
107714         -          bInEst = 1;
107715         -        }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
107716         -          /* "x IN (value, value, ...)" */
107717         -          nInMul *= pExpr->x.pList->nExpr;
107718         -        }
107719         -      }else if( pTerm->eOperator & WO_ISNULL ){
107720         -        pc.plan.wsFlags |= WHERE_COLUMN_NULL;
107721         -      }
107722         -#ifdef SQLITE_ENABLE_STAT3
107723         -      if( pc.plan.nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
107724         -#endif
107725         -      pc.used |= pTerm->prereqRight;
107726         -    }
107727         - 
107728         -    /* If the index being considered is UNIQUE, and there is an equality 
107729         -    ** constraint for all columns in the index, then this search will find
107730         -    ** at most a single row. In this case set the WHERE_UNIQUE flag to 
107731         -    ** indicate this to the caller.
107732         -    **
107733         -    ** Otherwise, if the search may find more than one row, test to see if
107734         -    ** there is a range constraint on indexed column (pc.plan.nEq+1) that
107735         -    ** can be optimized using the index. 
107736         -    */
107737         -    if( pc.plan.nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
107738         -      testcase( pc.plan.wsFlags & WHERE_COLUMN_IN );
107739         -      testcase( pc.plan.wsFlags & WHERE_COLUMN_NULL );
107740         -      if( (pc.plan.wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
107741         -        pc.plan.wsFlags |= WHERE_UNIQUE;
107742         -        if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
107743         -          pc.plan.wsFlags |= WHERE_ALL_UNIQUE;
107744         -        }
107745         -      }
107746         -    }else if( pProbe->bUnordered==0 ){
107747         -      int j;
107748         -      j = (pc.plan.nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[pc.plan.nEq]);
107749         -      if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
107750         -        WhereTerm *pTop, *pBtm;
107751         -        pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
107752         -        pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
107753         -        whereRangeScanEst(pParse, pProbe, pc.plan.nEq, pBtm, pTop, &rangeDiv);
107754         -        if( pTop ){
107755         -          nBound = 1;
107756         -          pc.plan.wsFlags |= WHERE_TOP_LIMIT;
107757         -          pc.used |= pTop->prereqRight;
107758         -          testcase( pTop->pWC!=pWC );
107759         -        }
107760         -        if( pBtm ){
107761         -          nBound++;
107762         -          pc.plan.wsFlags |= WHERE_BTM_LIMIT;
107763         -          pc.used |= pBtm->prereqRight;
107764         -          testcase( pBtm->pWC!=pWC );
107765         -        }
107766         -        pc.plan.wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
107767         -      }
107768         -    }
107769         -
107770         -    /* If there is an ORDER BY clause and the index being considered will
107771         -    ** naturally scan rows in the required order, set the appropriate flags
107772         -    ** in pc.plan.wsFlags. Otherwise, if there is an ORDER BY clause but
107773         -    ** the index will scan rows in a different order, set the bSort
107774         -    ** variable.  */
107775         -    if( bSort && (pSrc->jointype & JT_LEFT)==0 ){
107776         -      int bRev = 2;
107777         -      int bObUnique = 0;
107778         -      WHERETRACE(("      --> before isSortIndex: nPriorSat=%d\n",nPriorSat));
107779         -      pc.plan.nOBSat = isSortingIndex(p, pProbe, iCur, &bRev, &bObUnique);
107780         -      WHERETRACE(("      --> after  isSortIndex: bRev=%d bObU=%d nOBSat=%d\n",
107781         -                  bRev, bObUnique, pc.plan.nOBSat));
107782         -      if( nPriorSat<pc.plan.nOBSat || (pc.plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
107783         -        pc.plan.wsFlags |= WHERE_ORDERED;
107784         -        if( bObUnique ) pc.plan.wsFlags |= WHERE_OB_UNIQUE;
107785         -      }
107786         -      if( nOrderBy==pc.plan.nOBSat ){
107787         -        bSort = 0;
107788         -        pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE;
107789         -      }
107790         -      if( bRev & 1 ) pc.plan.wsFlags |= WHERE_REVERSE;
107791         -    }
107792         -
107793         -    /* If there is a DISTINCT qualifier and this index will scan rows in
107794         -    ** order of the DISTINCT expressions, clear bDist and set the appropriate
107795         -    ** flags in pc.plan.wsFlags. */
107796         -    if( bDist
107797         -     && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, pc.plan.nEq)
107798         -     && (pc.plan.wsFlags & WHERE_COLUMN_IN)==0
107799         -    ){
107800         -      bDist = 0;
107801         -      pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
107802         -    }
107803         -
107804         -    /* If currently calculating the cost of using an index (not the IPK
107805         -    ** index), determine if all required column data may be obtained without 
107806         -    ** using the main table (i.e. if the index is a covering
107807         -    ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
107808         -    ** pc.plan.wsFlags. Otherwise, set the bLookup variable to true.  */
107809         -    if( pIdx ){
107810         -      Bitmask m = pSrc->colUsed;
107811         -      int j;
107812         -      for(j=0; j<pIdx->nColumn; j++){
107813         -        int x = pIdx->aiColumn[j];
107814         -        if( x<BMS-1 ){
107815         -          m &= ~(((Bitmask)1)<<x);
107816         -        }
107817         -      }
107818         -      if( m==0 ){
107819         -        pc.plan.wsFlags |= WHERE_IDX_ONLY;
107820         -      }else{
107821         -        bLookup = 1;
107822         -      }
107823         -    }
107824         -
107825         -    /*
107826         -    ** Estimate the number of rows of output.  For an "x IN (SELECT...)"
107827         -    ** constraint, do not let the estimate exceed half the rows in the table.
107828         -    */
107829         -    pc.plan.nRow = (double)(aiRowEst[pc.plan.nEq] * nInMul);
107830         -    if( bInEst && pc.plan.nRow*2>aiRowEst[0] ){
107831         -      pc.plan.nRow = aiRowEst[0]/2;
107832         -      nInMul = (int)(pc.plan.nRow / aiRowEst[pc.plan.nEq]);
107833         -    }
107834         -
107835         -#ifdef SQLITE_ENABLE_STAT3
107836         -    /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
107837         -    ** and we do not think that values of x are unique and if histogram
107838         -    ** data is available for column x, then it might be possible
107839         -    ** to get a better estimate on the number of rows based on
107840         -    ** VALUE and how common that value is according to the histogram.
107841         -    */
107842         -    if( pc.plan.nRow>(double)1 && pc.plan.nEq==1
107843         -     && pFirstTerm!=0 && aiRowEst[1]>1 ){
107844         -      assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
107845         -      if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
107846         -        testcase( pFirstTerm->eOperator & WO_EQ );
107847         -        testcase( pFirstTerm->eOperator & WO_EQUIV );
107848         -        testcase( pFirstTerm->eOperator & WO_ISNULL );
107849         -        whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight,
107850         -                          &pc.plan.nRow);
107851         -      }else if( bInEst==0 ){
107852         -        assert( pFirstTerm->eOperator & WO_IN );
107853         -        whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList,
107854         -                       &pc.plan.nRow);
107855         -      }
107856         -    }
107857         -#endif /* SQLITE_ENABLE_STAT3 */
107858         -
107859         -    /* Adjust the number of output rows and downward to reflect rows
107860         -    ** that are excluded by range constraints.
107861         -    */
107862         -    pc.plan.nRow = pc.plan.nRow/rangeDiv;
107863         -    if( pc.plan.nRow<1 ) pc.plan.nRow = 1;
107864         -
107865         -    /* Experiments run on real SQLite databases show that the time needed
107866         -    ** to do a binary search to locate a row in a table or index is roughly
107867         -    ** log10(N) times the time to move from one row to the next row within
107868         -    ** a table or index.  The actual times can vary, with the size of
107869         -    ** records being an important factor.  Both moves and searches are
107870         -    ** slower with larger records, presumably because fewer records fit
107871         -    ** on one page and hence more pages have to be fetched.
107872         -    **
107873         -    ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
107874         -    ** not give us data on the relative sizes of table and index records.
107875         -    ** So this computation assumes table records are about twice as big
107876         -    ** as index records
107877         -    */
107878         -    if( (pc.plan.wsFlags&~(WHERE_REVERSE|WHERE_ORDERED|WHERE_OB_UNIQUE))
107879         -                                                              ==WHERE_IDX_ONLY
107880         -     && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
107881         -     && sqlite3GlobalConfig.bUseCis
107882         -     && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
107883         -    ){
107884         -      /* This index is not useful for indexing, but it is a covering index.
107885         -      ** A full-scan of the index might be a little faster than a full-scan
107886         -      ** of the table, so give this case a cost slightly less than a table
107887         -      ** scan. */
107888         -      pc.rCost = aiRowEst[0]*3 + pProbe->nColumn;
107889         -      pc.plan.wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
107890         -    }else if( (pc.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
107891         -      /* The cost of a full table scan is a number of move operations equal
107892         -      ** to the number of rows in the table.
107893         -      **
107894         -      ** We add an additional 4x penalty to full table scans.  This causes
107895         -      ** the cost function to err on the side of choosing an index over
107896         -      ** choosing a full scan.  This 4x full-scan penalty is an arguable
107897         -      ** decision and one which we expect to revisit in the future.  But
107898         -      ** it seems to be working well enough at the moment.
107899         -      */
107900         -      pc.rCost = aiRowEst[0]*4;
107901         -      pc.plan.wsFlags &= ~WHERE_IDX_ONLY;
107902         -      if( pIdx ){
107903         -        pc.plan.wsFlags &= ~WHERE_ORDERED;
107904         -        pc.plan.nOBSat = nPriorSat;
107905         -      }
107906         -    }else{
107907         -      log10N = estLog(aiRowEst[0]);
107908         -      pc.rCost = pc.plan.nRow;
107909         -      if( pIdx ){
107910         -        if( bLookup ){
107911         -          /* For an index lookup followed by a table lookup:
107912         -          **    nInMul index searches to find the start of each index range
107913         -          **  + nRow steps through the index
107914         -          **  + nRow table searches to lookup the table entry using the rowid
107915         -          */
107916         -          pc.rCost += (nInMul + pc.plan.nRow)*log10N;
107917         -        }else{
107918         -          /* For a covering index:
107919         -          **     nInMul index searches to find the initial entry 
107920         -          **   + nRow steps through the index
107921         -          */
107922         -          pc.rCost += nInMul*log10N;
107923         -        }
107924         -      }else{
107925         -        /* For a rowid primary key lookup:
107926         -        **    nInMult table searches to find the initial entry for each range
107927         -        **  + nRow steps through the table
107928         -        */
107929         -        pc.rCost += nInMul*log10N;
107930         -      }
107931         -    }
107932         -
107933         -    /* Add in the estimated cost of sorting the result.  Actual experimental
107934         -    ** measurements of sorting performance in SQLite show that sorting time
107935         -    ** adds C*N*log10(N) to the cost, where N is the number of rows to be 
107936         -    ** sorted and C is a factor between 1.95 and 4.3.  We will split the
107937         -    ** difference and select C of 3.0.
107938         -    */
107939         -    if( bSort ){
107940         -      double m = estLog(pc.plan.nRow*(nOrderBy - pc.plan.nOBSat)/nOrderBy);
107941         -      m *= (double)(pc.plan.nOBSat ? 2 : 3);
107942         -      pc.rCost += pc.plan.nRow*m;
107943         -    }
107944         -    if( bDist ){
107945         -      pc.rCost += pc.plan.nRow*estLog(pc.plan.nRow)*3;
107946         -    }
107947         -
107948         -    /**** Cost of using this index has now been computed ****/
107949         -
107950         -    /* If there are additional constraints on this table that cannot
107951         -    ** be used with the current index, but which might lower the number
107952         -    ** of output rows, adjust the nRow value accordingly.  This only 
107953         -    ** matters if the current index is the least costly, so do not bother
107954         -    ** with this step if we already know this index will not be chosen.
107955         -    ** Also, never reduce the output row count below 2 using this step.
107956         -    **
107957         -    ** It is critical that the notValid mask be used here instead of
107958         -    ** the notReady mask.  When computing an "optimal" index, the notReady
107959         -    ** mask will only have one bit set - the bit for the current table.
107960         -    ** The notValid mask, on the other hand, always has all bits set for
107961         -    ** tables that are not in outer loops.  If notReady is used here instead
107962         -    ** of notValid, then a optimal index that depends on inner joins loops
107963         -    ** might be selected even when there exists an optimal index that has
107964         -    ** no such dependency.
107965         -    */
107966         -    if( pc.plan.nRow>2 && pc.rCost<=p->cost.rCost ){
107967         -      int k;                       /* Loop counter */
107968         -      int nSkipEq = pc.plan.nEq;   /* Number of == constraints to skip */
107969         -      int nSkipRange = nBound;     /* Number of < constraints to skip */
107970         -      Bitmask thisTab;             /* Bitmap for pSrc */
107971         -
107972         -      thisTab = getMask(pWC->pMaskSet, iCur);
107973         -      for(pTerm=pWC->a, k=pWC->nTerm; pc.plan.nRow>2 && k; k--, pTerm++){
107974         -        if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
107975         -        if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
107976         -        if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
107977         -          if( nSkipEq ){
107978         -            /* Ignore the first pc.plan.nEq equality matches since the index
107979         -            ** has already accounted for these */
107980         -            nSkipEq--;
107981         -          }else{
107982         -            /* Assume each additional equality match reduces the result
107983         -            ** set size by a factor of 10 */
107984         -            pc.plan.nRow /= 10;
107985         -          }
107986         -        }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
107987         -          if( nSkipRange ){
107988         -            /* Ignore the first nSkipRange range constraints since the index
107989         -            ** has already accounted for these */
107990         -            nSkipRange--;
107991         -          }else{
107992         -            /* Assume each additional range constraint reduces the result
107993         -            ** set size by a factor of 3.  Indexed range constraints reduce
107994         -            ** the search space by a larger factor: 4.  We make indexed range
107995         -            ** more selective intentionally because of the subjective 
107996         -            ** observation that indexed range constraints really are more
107997         -            ** selective in practice, on average. */
107998         -            pc.plan.nRow /= 3;
107999         -          }
108000         -        }else if( (pTerm->eOperator & WO_NOOP)==0 ){
108001         -          /* Any other expression lowers the output row count by half */
108002         -          pc.plan.nRow /= 2;
108003         -        }
108004         -      }
108005         -      if( pc.plan.nRow<2 ) pc.plan.nRow = 2;
108006         -    }
108007         -
108008         -
108009         -    WHERETRACE((
108010         -      "      nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
108011         -      "      notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
108012         -      "      used=0x%llx nOBSat=%d\n",
108013         -      pc.plan.nEq, nInMul, (int)rangeDiv, bSort, bLookup, pc.plan.wsFlags,
108014         -      p->notReady, log10N, pc.plan.nRow, pc.rCost, pc.used,
108015         -      pc.plan.nOBSat
108016         -    ));
108017         -
108018         -    /* If this index is the best we have seen so far, then record this
108019         -    ** index and its cost in the p->cost structure.
108020         -    */
108021         -    if( (!pIdx || pc.plan.wsFlags) && compareCost(&pc, &p->cost) ){
108022         -      p->cost = pc;
108023         -      p->cost.plan.wsFlags &= wsFlagMask;
108024         -      p->cost.plan.u.pIdx = pIdx;
108025         -    }
108026         -
108027         -    /* If there was an INDEXED BY clause, then only that one index is
108028         -    ** considered. */
108029         -    if( pSrc->pIndex ) break;
108030         -
108031         -    /* Reset masks for the next index in the loop */
108032         -    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
108033         -    eqTermMask = idxEqTermMask;
108034         -  }
108035         -
108036         -  /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
108037         -  ** is set, then reverse the order that the index will be scanned
108038         -  ** in. This is used for application testing, to help find cases
108039         -  ** where application behavior depends on the (undefined) order that
108040         -  ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
108041         -  if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
108042         -    p->cost.plan.wsFlags |= WHERE_REVERSE;
108043         -  }
108044         -
108045         -  assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERED)==0 );
108046         -  assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
108047         -  assert( pSrc->pIndex==0 
108048         -       || p->cost.plan.u.pIdx==0 
108049         -       || p->cost.plan.u.pIdx==pSrc->pIndex 
108050         -  );
108051         -
108052         -  WHERETRACE(("   best index is %s cost=%.1f\n",
108053         -         p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk",
108054         -         p->cost.rCost));
108055         -  
108056         -  bestOrClauseIndex(p);
108057         -  bestAutomaticIndex(p);
108058         -  p->cost.plan.wsFlags |= eqTermMask;
108059         -}
108060         -
108061         -/*
108062         -** Find the query plan for accessing table pSrc->pTab. Write the
108063         -** best query plan and its cost into the WhereCost object supplied 
108064         -** as the last parameter. This function may calculate the cost of
108065         -** both real and virtual table scans.
108066         -**
108067         -** This function does not take ORDER BY or DISTINCT into account.  Nor
108068         -** does it remember the virtual table query plan.  All it does is compute
108069         -** the cost while determining if an OR optimization is applicable.  The
108070         -** details will be reconsidered later if the optimization is found to be
108071         -** applicable.
108072         -*/
108073         -static void bestIndex(WhereBestIdx *p){
108074         -#ifndef SQLITE_OMIT_VIRTUALTABLE
108075         -  if( IsVirtual(p->pSrc->pTab) ){
108076         -    sqlite3_index_info *pIdxInfo = 0;
108077         -    p->ppIdxInfo = &pIdxInfo;
108078         -    bestVirtualIndex(p);
108079         -    assert( pIdxInfo!=0 || p->pParse->db->mallocFailed );
108080         -    if( pIdxInfo && pIdxInfo->needToFreeIdxStr ){
108081         -      sqlite3_free(pIdxInfo->idxStr);
108082         -    }
108083         -    sqlite3DbFree(p->pParse->db, pIdxInfo);
108084         -  }else
108085         -#endif
108086         -  {
108087         -    bestBtreeIndex(p);
108088         -  }
108089         -}
       107016  +    WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst));
       107017  +  }
       107018  +  return rc;
       107019  +}
       107020  +#endif /* defined(SQLITE_ENABLE_STAT3) */
108090 107021   
108091 107022   /*
108092 107023   ** Disable a term in the WHERE clause.  Except, do not disable the term
108093 107024   ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
108094 107025   ** or USING clause of that join.
108095 107026   **
108096 107027   ** Consider the term t2.z='ok' in the following queries:
................................................................................
108181 107112   ** this routine sets up a loop that will iterate over all values of X.
108182 107113   */
108183 107114   static int codeEqualityTerm(
108184 107115     Parse *pParse,      /* The parsing context */
108185 107116     WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
108186 107117     WhereLevel *pLevel, /* The level of the FROM clause we are working on */
108187 107118     int iEq,            /* Index of the equality term within this level */
       107119  +  int bRev,           /* True for reverse-order IN operations */
108188 107120     int iTarget         /* Attempt to leave results in this register */
108189 107121   ){
108190 107122     Expr *pX = pTerm->pExpr;
108191 107123     Vdbe *v = pParse->pVdbe;
108192 107124     int iReg;                  /* Register holding results */
108193 107125   
108194 107126     assert( iTarget>0 );
................................................................................
108198 107130       iReg = iTarget;
108199 107131       sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
108200 107132   #ifndef SQLITE_OMIT_SUBQUERY
108201 107133     }else{
108202 107134       int eType;
108203 107135       int iTab;
108204 107136       struct InLoop *pIn;
108205         -    u8 bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
       107137  +    WhereLoop *pLoop = pLevel->pWLoop;
108206 107138   
108207         -    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 
108208         -      && pLevel->plan.u.pIdx->aSortOrder[iEq]
       107139  +    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
       107140  +      && pLoop->u.btree.pIndex!=0
       107141  +      && pLoop->u.btree.pIndex->aSortOrder[iEq]
108209 107142       ){
108210 107143         testcase( iEq==0 );
108211         -      testcase( iEq==pLevel->plan.u.pIdx->nColumn-1 );
108212         -      testcase( iEq>0 && iEq+1<pLevel->plan.u.pIdx->nColumn );
108213 107144         testcase( bRev );
108214 107145         bRev = !bRev;
108215 107146       }
108216 107147       assert( pX->op==TK_IN );
108217 107148       iReg = iTarget;
108218 107149       eType = sqlite3FindInIndex(pParse, pX, 0);
108219 107150       if( eType==IN_INDEX_INDEX_DESC ){
108220 107151         testcase( bRev );
108221 107152         bRev = !bRev;
108222 107153       }
108223 107154       iTab = pX->iTable;
108224 107155       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
108225         -    assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
       107156  +    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
       107157  +    pLoop->wsFlags |= WHERE_IN_ABLE;
108226 107158       if( pLevel->u.in.nIn==0 ){
108227 107159         pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
108228 107160       }
108229 107161       pLevel->u.in.nIn++;
108230 107162       pLevel->u.in.aInLoop =
108231 107163          sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
108232 107164                                 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
................................................................................
108290 107222   ** string in this example would be set to SQLITE_AFF_NONE.
108291 107223   */
108292 107224   static int codeAllEqualityTerms(
108293 107225     Parse *pParse,        /* Parsing context */
108294 107226     WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
108295 107227     WhereClause *pWC,     /* The WHERE clause */
108296 107228     Bitmask notReady,     /* Which parts of FROM have not yet been coded */
       107229  +  int bRev,             /* Reverse the order of IN operators */
108297 107230     int nExtraReg,        /* Number of extra registers to allocate */
108298 107231     char **pzAff          /* OUT: Set to point to affinity string */
108299 107232   ){
108300         -  int nEq = pLevel->plan.nEq;   /* The number of == or IN constraints to code */
       107233  +  int nEq;                      /* The number of == or IN constraints to code */
108301 107234     Vdbe *v = pParse->pVdbe;      /* The vm under construction */
108302 107235     Index *pIdx;                  /* The index being used for this loop */
108303         -  int iCur = pLevel->iTabCur;   /* The cursor of the table */
108304 107236     WhereTerm *pTerm;             /* A single constraint term */
       107237  +  WhereLoop *pLoop;             /* The WhereLoop object */
108305 107238     int j;                        /* Loop counter */
108306 107239     int regBase;                  /* Base register */
108307 107240     int nReg;                     /* Number of registers to allocate */
108308 107241     char *zAff;                   /* Affinity string to return */
108309 107242   
108310 107243     /* This module is only called on query plans that use an index. */
108311         -  assert( pLevel->plan.wsFlags & WHERE_INDEXED );
108312         -  pIdx = pLevel->plan.u.pIdx;
       107244  +  pLoop = pLevel->pWLoop;
       107245  +  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
       107246  +  nEq = pLoop->u.btree.nEq;
       107247  +  pIdx = pLoop->u.btree.pIndex;
       107248  +  assert( pIdx!=0 );
108313 107249   
108314 107250     /* Figure out how many memory cells we will need then allocate them.
108315 107251     */
108316 107252     regBase = pParse->nMem + 1;
108317         -  nReg = pLevel->plan.nEq + nExtraReg;
       107253  +  nReg = pLoop->u.btree.nEq + nExtraReg;
108318 107254     pParse->nMem += nReg;
108319 107255   
108320 107256     zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
108321 107257     if( !zAff ){
108322 107258       pParse->db->mallocFailed = 1;
108323 107259     }
108324 107260   
108325 107261     /* Evaluate the equality constraints
108326 107262     */
108327 107263     assert( pIdx->nColumn>=nEq );
108328 107264     for(j=0; j<nEq; j++){
108329 107265       int r1;
108330         -    int k = pIdx->aiColumn[j];
108331         -    pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
108332         -    if( pTerm==0 ) break;
       107266  +    pTerm = pLoop->aLTerm[j];
       107267  +    assert( pTerm!=0 );
108333 107268       /* The following true for indices with redundant columns. 
108334 107269       ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
108335 107270       testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
108336 107271       testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
108337         -    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, regBase+j);
       107272  +    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
108338 107273       if( r1!=regBase+j ){
108339 107274         if( nReg==1 ){
108340 107275           sqlite3ReleaseTempReg(pParse, regBase);
108341 107276           regBase = r1;
108342 107277         }else{
108343 107278           sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
108344 107279         }
................................................................................
108398 107333   **
108399 107334   **   "a=? AND b>?"
108400 107335   **
108401 107336   ** The returned pointer points to memory obtained from sqlite3DbMalloc().
108402 107337   ** It is the responsibility of the caller to free the buffer when it is
108403 107338   ** no longer required.
108404 107339   */
108405         -static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){
108406         -  WherePlan *pPlan = &pLevel->plan;
108407         -  Index *pIndex = pPlan->u.pIdx;
108408         -  int nEq = pPlan->nEq;
       107340  +static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){
       107341  +  Index *pIndex = pLoop->u.btree.pIndex;
       107342  +  int nEq = pLoop->u.btree.nEq;
108409 107343     int i, j;
108410 107344     Column *aCol = pTab->aCol;
108411 107345     int *aiColumn = pIndex->aiColumn;
108412 107346     StrAccum txt;
108413 107347   
108414         -  if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
       107348  +  if( pIndex==0 ) return 0;
       107349  +  if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
108415 107350       return 0;
108416 107351     }
108417 107352     sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
108418 107353     txt.db = db;
108419 107354     sqlite3StrAccumAppend(&txt, " (", 2);
108420 107355     for(i=0; i<nEq; i++){
108421 107356       explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
108422 107357     }
108423 107358   
108424 107359     j = i;
108425         -  if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
       107360  +  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
108426 107361       char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
108427 107362       explainAppendTerm(&txt, i++, z, ">");
108428 107363     }
108429         -  if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
       107364  +  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
108430 107365       char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
108431 107366       explainAppendTerm(&txt, i, z, "<");
108432 107367     }
108433 107368     sqlite3StrAccumAppend(&txt, ")", 1);
108434 107369     return sqlite3StrAccumFinish(&txt);
108435 107370   }
108436 107371   
................................................................................
108445 107380     SrcList *pTabList,              /* Table list this loop refers to */
108446 107381     WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
108447 107382     int iLevel,                     /* Value for "level" column of output */
108448 107383     int iFrom,                      /* Value for "from" column of output */
108449 107384     u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
108450 107385   ){
108451 107386     if( pParse->explain==2 ){
108452         -    u32 flags = pLevel->plan.wsFlags;
108453 107387       struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
108454 107388       Vdbe *v = pParse->pVdbe;      /* VM being constructed */
108455 107389       sqlite3 *db = pParse->db;     /* Database handle */
108456 107390       char *zMsg;                   /* Text to add to EQP output */
108457         -    sqlite3_int64 nRow;           /* Expected number of rows visited by scan */
108458 107391       int iId = pParse->iSelectId;  /* Select id (left-most output column) */
108459 107392       int isSearch;                 /* True for a SEARCH. False for SCAN. */
       107393  +    WhereLoop *pLoop;             /* The controlling WhereLoop object */
       107394  +    u32 flags;                    /* Flags that describe this loop */
108460 107395   
       107396  +    pLoop = pLevel->pWLoop;
       107397  +    flags = pLoop->wsFlags;
108461 107398       if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
108462 107399   
108463         -    isSearch = (pLevel->plan.nEq>0)
108464         -             || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
108465         -             || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
       107400  +    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
       107401  +            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
       107402  +            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
108466 107403   
108467 107404       zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
108468 107405       if( pItem->pSelect ){
108469 107406         zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
108470 107407       }else{
108471 107408         zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
108472 107409       }
108473 107410   
108474 107411       if( pItem->zAlias ){
108475 107412         zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
108476 107413       }
108477         -    if( (flags & WHERE_INDEXED)!=0 ){
108478         -      char *zWhere = explainIndexRange(db, pLevel, pItem->pTab);
       107414  +    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0
       107415  +     && pLoop->u.btree.pIndex!=0
       107416  +    ){
       107417  +      char *zWhere = explainIndexRange(db, pLoop, pItem->pTab);
108479 107418         zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, 
108480 107419             ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""),
108481 107420             ((flags & WHERE_IDX_ONLY)?"COVERING ":""),
108482 107421             ((flags & WHERE_TEMP_INDEX)?"":" "),
108483         -          ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName),
       107422  +          ((flags & WHERE_TEMP_INDEX)?"": pLoop->u.btree.pIndex->zName),
108484 107423             zWhere
108485 107424         );
108486 107425         sqlite3DbFree(db, zWhere);
108487         -    }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
       107426  +    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
108488 107427         zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
108489 107428   
108490         -      if( flags&WHERE_ROWID_EQ ){
       107429  +      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
108491 107430           zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
108492 107431         }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
108493 107432           zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
108494 107433         }else if( flags&WHERE_BTM_LIMIT ){
108495 107434           zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
108496 107435         }else if( flags&WHERE_TOP_LIMIT ){
108497 107436           zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
108498 107437         }
108499 107438       }
108500 107439   #ifndef SQLITE_OMIT_VIRTUALTABLE
108501 107440       else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
108502         -      sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
108503 107441         zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
108504         -                  pVtabIdx->idxNum, pVtabIdx->idxStr);
       107442  +                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
108505 107443       }
108506 107444   #endif
108507         -    if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){
108508         -      testcase( wctrlFlags & WHERE_ORDERBY_MIN );
108509         -      nRow = 1;
108510         -    }else{
108511         -      nRow = (sqlite3_int64)pLevel->plan.nRow;
108512         -    }
108513         -    zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow);
       107445  +    zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg);
108514 107446       sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
108515 107447     }
108516 107448   }
108517 107449   #else
108518 107450   # define explainOneScan(u,v,w,x,y,z)
108519 107451   #endif /* SQLITE_OMIT_EXPLAIN */
108520 107452   
................................................................................
108522 107454   /*
108523 107455   ** Generate code for the start of the iLevel-th loop in the WHERE clause
108524 107456   ** implementation described by pWInfo.
108525 107457   */
108526 107458   static Bitmask codeOneLoopStart(
108527 107459     WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
108528 107460     int iLevel,          /* Which level of pWInfo->a[] should be coded */
108529         -  u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
108530 107461     Bitmask notReady     /* Which tables are currently available */
108531 107462   ){
108532 107463     int j, k;            /* Loop counters */
108533 107464     int iCur;            /* The VDBE cursor for the table */
108534 107465     int addrNxt;         /* Where to jump to continue with the next IN case */
108535 107466     int omitTable;       /* True if we use the index only */
108536 107467     int bRev;            /* True if we need to scan in reverse order */
108537 107468     WhereLevel *pLevel;  /* The where level to be coded */
       107469  +  WhereLoop *pLoop;    /* The WhereLoop object being coded */
108538 107470     WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
108539 107471     WhereTerm *pTerm;               /* A WHERE clause term */
108540 107472     Parse *pParse;                  /* Parsing context */
108541 107473     Vdbe *v;                        /* The prepared stmt under constructions */
108542 107474     struct SrcList_item *pTabItem;  /* FROM clause term being coded */
108543 107475     int addrBrk;                    /* Jump here to break out of the loop */
108544 107476     int addrCont;                   /* Jump here to continue with next cycle */
108545 107477     int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
108546 107478     int iReleaseReg = 0;      /* Temp register to free before returning */
108547 107479     Bitmask newNotReady;      /* Return value */
108548 107480   
108549 107481     pParse = pWInfo->pParse;
108550 107482     v = pParse->pVdbe;
108551         -  pWC = pWInfo->pWC;
       107483  +  pWC = &pWInfo->sWC;
108552 107484     pLevel = &pWInfo->a[iLevel];
       107485  +  pLoop = pLevel->pWLoop;
108553 107486     pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
108554 107487     iCur = pTabItem->iCursor;
108555         -  bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
108556         -  omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0 
108557         -           && (wctrlFlags & WHERE_FORCE_TABLE)==0;
       107488  +  bRev = (pWInfo->revMask>>iLevel)&1;
       107489  +  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
       107490  +           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
108558 107491     VdbeNoopComment((v, "Begin Join Loop %d", iLevel));
108559 107492   
108560 107493     /* Create labels for the "break" and "continue" instructions
108561 107494     ** for the current loop.  Jump to addrBrk to break out of a loop.
108562 107495     ** Jump to cont to go immediately to the next iteration of the
108563 107496     ** loop.
108564 107497     **
................................................................................
108587 107520       pLevel->p2 =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
108588 107521       VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
108589 107522       sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk);
108590 107523       pLevel->op = OP_Goto;
108591 107524     }else
108592 107525   
108593 107526   #ifndef SQLITE_OMIT_VIRTUALTABLE
108594         -  if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
108595         -    /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
       107527  +  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
       107528  +    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
108596 107529       **          to access the data.
108597 107530       */
108598 107531       int iReg;   /* P3 Value for OP_VFilter */
108599 107532       int addrNotFound;
108600         -    sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
108601         -    int nConstraint = pVtabIdx->nConstraint;
108602         -    struct sqlite3_index_constraint_usage *aUsage =
108603         -                                                pVtabIdx->aConstraintUsage;
108604         -    const struct sqlite3_index_constraint *aConstraint =
108605         -                                                pVtabIdx->aConstraint;
       107533  +    int nConstraint = pLoop->nLTerm;
108606 107534   
108607 107535       sqlite3ExprCachePush(pParse);
108608 107536       iReg = sqlite3GetTempRange(pParse, nConstraint+2);
108609 107537       addrNotFound = pLevel->addrBrk;
108610         -    for(j=1; j<=nConstraint; j++){
108611         -      for(k=0; k<nConstraint; k++){
108612         -        if( aUsage[k].argvIndex==j ){
108613         -          int iTarget = iReg+j+1;
108614         -          pTerm = &pWC->a[aConstraint[k].iTermOffset];
108615         -          if( pTerm->eOperator & WO_IN ){
108616         -            codeEqualityTerm(pParse, pTerm, pLevel, k, iTarget);
108617         -            addrNotFound = pLevel->addrNxt;
108618         -          }else{
108619         -            sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
108620         -          }
108621         -          break;
108622         -        }
108623         -      }
108624         -      if( k==nConstraint ) break;
108625         -    }
108626         -    sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
108627         -    sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
108628         -    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, pVtabIdx->idxStr,
108629         -                      pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
108630         -    pVtabIdx->needToFreeIdxStr = 0;
108631 107538       for(j=0; j<nConstraint; j++){
108632         -      if( aUsage[j].omit ){
108633         -        int iTerm = aConstraint[j].iTermOffset;
108634         -        disableTerm(pLevel, &pWC->a[iTerm]);
       107539  +      int iTarget = iReg+j+2;
       107540  +      pTerm = pLoop->aLTerm[j];
       107541  +      if( pTerm==0 ) continue;
       107542  +      if( pTerm->eOperator & WO_IN ){
       107543  +        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
       107544  +        addrNotFound = pLevel->addrNxt;
       107545  +      }else{
       107546  +        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
       107547  +      }
       107548  +    }
       107549  +    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
       107550  +    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
       107551  +    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
       107552  +                      pLoop->u.vtab.idxStr,
       107553  +                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
       107554  +    pLoop->u.vtab.needFree = 0;
       107555  +    for(j=0; j<nConstraint && j<16; j++){
       107556  +      if( (pLoop->u.vtab.omitMask>>j)&1 ){
       107557  +        disableTerm(pLevel, pLoop->aLTerm[j]);
108635 107558         }
108636 107559       }
108637 107560       pLevel->op = OP_VNext;
108638 107561       pLevel->p1 = iCur;
108639 107562       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
108640 107563       sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
108641 107564       sqlite3ExprCachePop(pParse, 1);
108642 107565     }else
108643 107566   #endif /* SQLITE_OMIT_VIRTUALTABLE */
108644 107567   
108645         -  if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
108646         -    /* Case 1:  We can directly reference a single row using an
       107568  +  if( (pLoop->wsFlags & WHERE_IPK)!=0
       107569  +   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
       107570  +  ){
       107571  +    /* Case 2:  We can directly reference a single row using an
108647 107572       **          equality comparison against the ROWID field.  Or
108648 107573       **          we reference multiple rows using a "rowid IN (...)"
108649 107574       **          construct.
108650 107575       */
       107576  +    assert( pLoop->u.btree.nEq==1 );
108651 107577       iReleaseReg = sqlite3GetTempReg(pParse);
108652         -    pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
       107578  +    pTerm = pLoop->aLTerm[0];
108653 107579       assert( pTerm!=0 );
108654 107580       assert( pTerm->pExpr!=0 );
108655 107581       assert( omitTable==0 );
108656 107582       testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
108657         -    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, iReleaseReg);
       107583  +    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
108658 107584       addrNxt = pLevel->addrNxt;
108659 107585       sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
108660 107586       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
108661 107587       sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
108662 107588       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
108663 107589       VdbeComment((v, "pk"));
108664 107590       pLevel->op = OP_Noop;
108665         -  }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
108666         -    /* Case 2:  We have an inequality comparison against the ROWID field.
       107591  +  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
       107592  +         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
       107593  +  ){
       107594  +    /* Case 3:  We have an inequality comparison against the ROWID field.
108667 107595       */
108668 107596       int testOp = OP_Noop;
108669 107597       int start;
108670 107598       int memEndValue = 0;
108671 107599       WhereTerm *pStart, *pEnd;
108672 107600   
108673 107601       assert( omitTable==0 );
108674         -    pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
108675         -    pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
       107602  +    j = 0;
       107603  +    pStart = pEnd = 0;
       107604  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
       107605  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
108676 107606       if( bRev ){
108677 107607         pTerm = pStart;
108678 107608         pStart = pEnd;
108679 107609         pEnd = pTerm;
108680 107610       }
108681 107611       if( pStart ){
108682 107612         Expr *pX;             /* The expression that defines the start bound */
................................................................................
108735 107665       if( testOp!=OP_Noop ){
108736 107666         iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
108737 107667         sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
108738 107668         sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
108739 107669         sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
108740 107670         sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
108741 107671       }
108742         -  }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
108743         -    /* Case 3: A scan using an index.
       107672  +  }else if( pLoop->wsFlags & WHERE_INDEXED ){
       107673  +    /* Case 4: A scan using an index.
108744 107674       **
108745 107675       **         The WHERE clause may contain zero or more equality 
108746 107676       **         terms ("==" or "IN" operators) that refer to the N
108747 107677       **         left-most columns of the index. It may also contain
108748 107678       **         inequality constraints (>, <, >= or <=) on the indexed
108749 107679       **         column that immediately follows the N equalities. Only 
108750 107680       **         the right-most column can be an inequality - the rest must
................................................................................
108782 107712         OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
108783 107713       };
108784 107714       static const u8 aEndOp[] = {
108785 107715         OP_Noop,             /* 0: (!end_constraints) */
108786 107716         OP_IdxGE,            /* 1: (end_constraints && !bRev) */
108787 107717         OP_IdxLT             /* 2: (end_constraints && bRev) */
108788 107718       };
108789         -    int nEq = pLevel->plan.nEq;  /* Number of == or IN terms */
108790         -    int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
       107719  +    int nEq = pLoop->u.btree.nEq;  /* Number of == or IN terms */
       107720  +    int isMinQuery = 0;            /* If this is an optimized SELECT min(x).. */
108791 107721       int regBase;                 /* Base register holding constraint values */
108792 107722       int r1;                      /* Temp register */
108793 107723       WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
108794 107724       WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
108795 107725       int startEq;                 /* True if range start uses ==, >= or <= */
108796 107726       int endEq;                   /* True if range end uses ==, >= or <= */
108797 107727       int start_constraints;       /* Start of range is constrained */
................................................................................
108799 107729       Index *pIdx;                 /* The index we will be using */
108800 107730       int iIdxCur;                 /* The VDBE cursor for the index */
108801 107731       int nExtraReg = 0;           /* Number of extra registers needed */
108802 107732       int op;                      /* Instruction opcode */
108803 107733       char *zStartAff;             /* Affinity for start of range constraint */
108804 107734       char *zEndAff;               /* Affinity for end of range constraint */
108805 107735   
108806         -    pIdx = pLevel->plan.u.pIdx;
       107736  +    pIdx = pLoop->u.btree.pIndex;
108807 107737       iIdxCur = pLevel->iIdxCur;
108808 107738       k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]);
108809 107739   
108810 107740       /* If this loop satisfies a sort order (pOrderBy) request that 
108811 107741       ** was passed to this function to implement a "SELECT min(x) ..." 
108812 107742       ** query, then the caller will only allow the loop to run for
108813 107743       ** a single iteration. This means that the first row returned
108814 107744       ** should not have a NULL value stored in 'x'. If column 'x' is
108815 107745       ** the first one after the nEq equality constraints in the index,
108816 107746       ** this requires some special handling.
108817 107747       */
108818         -    if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
108819         -     && (pLevel->plan.wsFlags&WHERE_ORDERED)
       107748  +    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
       107749  +     && (pWInfo->bOBSat!=0)
108820 107750        && (pIdx->nColumn>nEq)
108821 107751       ){
108822 107752         /* assert( pOrderBy->nExpr==1 ); */
108823 107753         /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
108824 107754         isMinQuery = 1;
108825 107755         nExtraReg = 1;
108826 107756       }
108827 107757   
108828 107758       /* Find any inequality constraint terms for the start and end 
108829 107759       ** of the range. 
108830 107760       */
108831         -    if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
108832         -      pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
       107761  +    j = nEq;
       107762  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
       107763  +      pRangeStart = pLoop->aLTerm[j++];
108833 107764         nExtraReg = 1;
108834 107765       }
108835         -    if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
108836         -      pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
       107766  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
       107767  +      pRangeEnd = pLoop->aLTerm[j++];
108837 107768         nExtraReg = 1;
108838 107769       }
108839 107770   
108840 107771       /* Generate code to evaluate all constraint terms using == or IN
108841 107772       ** and store the values of those terms in an array of registers
108842 107773       ** starting at regBase.
108843 107774       */
108844 107775       regBase = codeAllEqualityTerms(
108845         -        pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
       107776  +        pParse, pLevel, pWC, notReady, bRev, nExtraReg, &zStartAff
108846 107777       );
108847 107778       zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
108848 107779       addrNxt = pLevel->addrNxt;
108849 107780   
108850 107781       /* If we are doing a reverse order scan on an ascending index, or
108851 107782       ** a forward order scan on a descending index, interchange the 
108852 107783       ** start and end terms (pRangeStart and pRangeEnd).
................................................................................
108946 107877       }
108947 107878   
108948 107879       /* If there are inequality constraints, check that the value
108949 107880       ** of the table column that the inequality contrains is not NULL.
108950 107881       ** If it is, jump to the next iteration of the loop.
108951 107882       */
108952 107883       r1 = sqlite3GetTempReg(pParse);
108953         -    testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
108954         -    testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
108955         -    if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
       107884  +    testcase( pLoop->wsFlags & WHERE_BTM_LIMIT );
       107885  +    testcase( pLoop->wsFlags & WHERE_TOP_LIMIT );
       107886  +    if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
108956 107887         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
108957 107888         sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
108958 107889       }
108959 107890       sqlite3ReleaseTempReg(pParse, r1);
108960 107891   
108961 107892       /* Seek the table cursor, if required */
108962 107893       disableTerm(pLevel, pRangeStart);
................................................................................
108967 107898         sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
108968 107899         sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
108969 107900       }
108970 107901   
108971 107902       /* Record the instruction used to terminate the loop. Disable 
108972 107903       ** WHERE clause terms made redundant by the index range scan.
108973 107904       */
108974         -    if( pLevel->plan.wsFlags & WHERE_UNIQUE ){
       107905  +    if( pLoop->wsFlags & WHERE_ONEROW ){
108975 107906         pLevel->op = OP_Noop;
108976 107907       }else if( bRev ){
108977 107908         pLevel->op = OP_Prev;
108978 107909       }else{
108979 107910         pLevel->op = OP_Next;
108980 107911       }
108981 107912       pLevel->p1 = iIdxCur;
108982         -    if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){
       107913  +    if( (pLoop->wsFlags & (WHERE_COLUMN_EQ | WHERE_COLUMN_RANGE | 
       107914  +                          WHERE_COLUMN_NULL | WHERE_COLUMN_IN))==0 ){
108983 107915         pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
108984 107916       }else{
108985 107917         assert( pLevel->p5==0 );
108986 107918       }
108987 107919     }else
108988 107920   
108989 107921   #ifndef SQLITE_OMIT_OR_OPTIMIZATION
108990         -  if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
108991         -    /* Case 4:  Two or more separately indexed terms connected by OR
       107922  +  if( pLoop->wsFlags & WHERE_MULTI_OR ){
       107923  +    /* Case 5:  Two or more separately indexed terms connected by OR
108992 107924       **
108993 107925       ** Example:
108994 107926       **
108995 107927       **   CREATE TABLE t1(a,b,c,d);
108996 107928       **   CREATE INDEX i1 ON t1(a);
108997 107929       **   CREATE INDEX i2 ON t1(b);
108998 107930       **   CREATE INDEX i3 ON t1(c);
................................................................................
109037 107969       int regRowid = 0;                         /* Register holding rowid */
109038 107970       int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
109039 107971       int iRetInit;                             /* Address of regReturn init */
109040 107972       int untestedTerms = 0;             /* Some terms not completely tested */
109041 107973       int ii;                            /* Loop counter */
109042 107974       Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
109043 107975      
109044         -    pTerm = pLevel->plan.u.pTerm;
       107976  +    pTerm = pLoop->aLTerm[0];
109045 107977       assert( pTerm!=0 );
109046 107978       assert( pTerm->eOperator & WO_OR );
109047 107979       assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
109048 107980       pOrWc = &pTerm->u.pOrInfo->wc;
109049 107981       pLevel->op = OP_Return;
109050 107982       pLevel->p1 = regReturn;
109051 107983   
................................................................................
109078 108010       ** immediately following the OP_Return at the bottom of the loop. This
109079 108011       ** is required in a few obscure LEFT JOIN cases where control jumps
109080 108012       ** over the top of the loop into the body of it. In this case the 
109081 108013       ** correct response for the end-of-loop code (the OP_Return) is to 
109082 108014       ** fall through to the next instruction, just as an OP_Next does if
109083 108015       ** called on an uninitialized cursor.
109084 108016       */
109085         -    if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
       108017  +    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
109086 108018         regRowset = ++pParse->nMem;
109087 108019         regRowid = ++pParse->nMem;
109088 108020         sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
109089 108021       }
109090 108022       iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
109091 108023   
109092 108024       /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
................................................................................
109129 108061           }
109130 108062           /* Loop through table entries that match term pOrTerm. */
109131 108063           pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
109132 108064                           WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
109133 108065                           WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
109134 108066           assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed );
109135 108067           if( pSubWInfo ){
109136         -          WhereLevel *pLvl;
       108068  +          WhereLoop *pSubLoop;
109137 108069             explainOneScan(
109138 108070                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
109139 108071             );
109140         -          if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
       108072  +          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
109141 108073               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
109142 108074               int r;
109143 108075               r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
109144 108076                                            regRowid, 0);
109145 108077               sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
109146 108078                                    sqlite3VdbeCurrentAddr(v)+2, r, iSet);
109147 108079             }
................................................................................
109162 108094             ** If the call to sqlite3WhereBegin() above resulted in a scan that
109163 108095             ** uses an index, and this is either the first OR-connected term
109164 108096             ** processed or the index is the same as that used by all previous
109165 108097             ** terms, set pCov to the candidate covering index. Otherwise, set 
109166 108098             ** pCov to NULL to indicate that no candidate covering index will 
109167 108099             ** be available.
109168 108100             */
109169         -          pLvl = &pSubWInfo->a[0];
109170         -          if( (pLvl->plan.wsFlags & WHERE_INDEXED)!=0
109171         -           && (pLvl->plan.wsFlags & WHERE_TEMP_INDEX)==0
109172         -           && (ii==0 || pLvl->plan.u.pIdx==pCov)
       108101  +          pSubLoop = pSubWInfo->a[0].pWLoop;
       108102  +          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
       108103  +           && (pSubLoop->wsFlags & WHERE_TEMP_INDEX)==0
       108104  +           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
109173 108105             ){
109174         -            assert( pLvl->iIdxCur==iCovCur );
109175         -            pCov = pLvl->plan.u.pIdx;
       108106  +            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
       108107  +            pCov = pSubLoop->u.btree.pIndex;
109176 108108             }else{
109177 108109               pCov = 0;
109178 108110             }
109179 108111   
109180 108112             /* Finish the loop through table entries that match term pOrTerm. */
109181 108113             sqlite3WhereEnd(pSubWInfo);
109182 108114           }
................................................................................
109194 108126   
109195 108127       if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
109196 108128       if( !untestedTerms ) disableTerm(pLevel, pTerm);
109197 108129     }else
109198 108130   #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
109199 108131   
109200 108132     {
109201         -    /* Case 5:  There is no usable index.  We must do a complete
       108133  +    /* Case 6:  There is no usable index.  We must do a complete
109202 108134       **          scan of the entire table.
109203 108135       */
109204 108136       static const u8 aStep[] = { OP_Next, OP_Prev };
109205 108137       static const u8 aStart[] = { OP_Rewind, OP_Last };
109206 108138       assert( bRev==0 || bRev==1 );
109207         -    assert( omitTable==0 );
109208 108139       pLevel->op = aStep[bRev];
109209 108140       pLevel->p1 = iCur;
109210 108141       pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
109211 108142       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
109212 108143     }
109213         -  newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur);
       108144  +  newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
109214 108145   
109215 108146     /* Insert code to test every subexpression that can be completely
109216 108147     ** computed using the current set of tables.
109217 108148     **
109218 108149     ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
109219 108150     ** the use of indices become tests that are evaluated against each row of
109220 108151     ** the relevant input tables.
................................................................................
109288 108219       }
109289 108220     }
109290 108221     sqlite3ReleaseTempReg(pParse, iReleaseReg);
109291 108222   
109292 108223     return newNotReady;
109293 108224   }
109294 108225   
109295         -#if defined(SQLITE_TEST)
109296         -/*
109297         -** The following variable holds a text description of query plan generated
109298         -** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
109299         -** overwrites the previous.  This information is used for testing and
109300         -** analysis only.
109301         -*/
109302         -SQLITE_API char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
109303         -static int nQPlan = 0;              /* Next free slow in _query_plan[] */
109304         -
109305         -#endif /* SQLITE_TEST */
109306         -
       108226  +#ifdef WHERETRACE_ENABLED
       108227  +/*
       108228  +** Print a WhereLoop object for debugging purposes
       108229  +*/
       108230  +static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){
       108231  +  int nb = 1+(pTabList->nSrc+7)/8;
       108232  +  struct SrcList_item *pItem = pTabList->a + p->iTab;
       108233  +  Table *pTab = pItem->pTab;
       108234  +  sqlite3DebugPrintf("%c %2d.%0*llx.%0*llx", p->cId,
       108235  +                     p->iTab, nb, p->maskSelf, nb, p->prereq);
       108236  +  sqlite3DebugPrintf(" %8s",
       108237  +                     pItem->zAlias ? pItem->zAlias : pTab->zName);
       108238  +  if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
       108239  +    if( p->u.btree.pIndex ){
       108240  +      const char *zName = p->u.btree.pIndex->zName;
       108241  +      if( zName==0 ) zName = "ipk";
       108242  +      if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
       108243  +        int i = sqlite3Strlen30(zName) - 1;
       108244  +        while( zName[i]!='_' ) i--;
       108245  +        zName += i;
       108246  +      }
       108247  +      sqlite3DebugPrintf(".%-12s %2d", zName, p->u.btree.nEq);
       108248  +    }else{
       108249  +      sqlite3DebugPrintf("%16s","");
       108250  +    }
       108251  +  }else{
       108252  +    char *z;
       108253  +    if( p->u.vtab.idxStr ){
       108254  +      z = sqlite3_mprintf("(%d,\"%s\",%x)",
       108255  +                p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
       108256  +    }else{
       108257  +      z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
       108258  +    }
       108259  +    sqlite3DebugPrintf(" %-15s", z);
       108260  +    sqlite3_free(z);
       108261  +  }
       108262  +  sqlite3DebugPrintf(" fg %05x N %d", p->wsFlags, p->nLTerm);
       108263  +  sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
       108264  +}
       108265  +#endif
       108266  +
       108267  +/*
       108268  +** Convert bulk memory into a valid WhereLoop that can be passed
       108269  +** to whereLoopClear harmlessly.
       108270  +*/
       108271  +static void whereLoopInit(WhereLoop *p){
       108272  +  p->aLTerm = p->aLTermSpace;
       108273  +  p->nLTerm = 0;
       108274  +  p->nLSlot = ArraySize(p->aLTermSpace);
       108275  +  p->wsFlags = 0;
       108276  +}
       108277  +
       108278  +/*
       108279  +** Clear the WhereLoop.u union.  Leave WhereLoop.pLTerm intact.
       108280  +*/
       108281  +static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
       108282  +  if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_TEMP_INDEX) ){
       108283  +    if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
       108284  +      sqlite3_free(p->u.vtab.idxStr);
       108285  +      p->u.vtab.needFree = 0;
       108286  +      p->u.vtab.idxStr = 0;
       108287  +    }else if( (p->wsFlags & WHERE_TEMP_INDEX)!=0 && p->u.btree.pIndex!=0 ){
       108288  +      sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
       108289  +      sqlite3DbFree(db, p->u.btree.pIndex);
       108290  +      p->u.btree.pIndex = 0;
       108291  +    }
       108292  +  }
       108293  +}
       108294  +
       108295  +/*
       108296  +** Deallocate internal memory used by a WhereLoop object
       108297  +*/
       108298  +static void whereLoopClear(sqlite3 *db, WhereLoop *p){
       108299  +  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
       108300  +  whereLoopClearUnion(db, p);
       108301  +  whereLoopInit(p);
       108302  +}
       108303  +
       108304  +/*
       108305  +** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
       108306  +*/
       108307  +static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
       108308  +  WhereTerm **paNew;
       108309  +  if( p->nLSlot>=n ) return SQLITE_OK;
       108310  +  n = (n+7)&~7;
       108311  +  paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
       108312  +  if( paNew==0 ) return SQLITE_NOMEM;
       108313  +  memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
       108314  +  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
       108315  +  p->aLTerm = paNew;
       108316  +  p->nLSlot = n;
       108317  +  return SQLITE_OK;
       108318  +}
       108319  +
       108320  +/*
       108321  +** Transfer content from the second pLoop into the first.
       108322  +*/
       108323  +static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
       108324  +  if( whereLoopResize(db, pTo, pFrom->nLTerm) ) return SQLITE_NOMEM;
       108325  +  whereLoopClearUnion(db, pTo);
       108326  +  memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
       108327  +  memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
       108328  +  if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
       108329  +    pFrom->u.vtab.needFree = 0;
       108330  +  }else if( (pFrom->wsFlags & WHERE_TEMP_INDEX)!=0 ){
       108331  +    pFrom->u.btree.pIndex = 0;
       108332  +  }
       108333  +  return SQLITE_OK;
       108334  +}
       108335  +
       108336  +/*
       108337  +** Delete a WhereLoop object
       108338  +*/
       108339  +static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
       108340  +  whereLoopClear(db, p);
       108341  +  sqlite3DbFree(db, p);
       108342  +}
109307 108343   
109308 108344   /*
109309 108345   ** Free a WhereInfo structure
109310 108346   */
109311 108347   static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
109312 108348     if( ALWAYS(pWInfo) ){
109313         -    int i;
109314         -    for(i=0; i<pWInfo->nLevel; i++){
109315         -      sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
109316         -      if( pInfo ){
109317         -        /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
109318         -        if( pInfo->needToFreeIdxStr ){
109319         -          sqlite3_free(pInfo->idxStr);
109320         -        }
109321         -        sqlite3DbFree(db, pInfo);
109322         -      }
109323         -      if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){
109324         -        Index *pIdx = pWInfo->a[i].plan.u.pIdx;
109325         -        if( pIdx ){
109326         -          sqlite3DbFree(db, pIdx->zColAff);
109327         -          sqlite3DbFree(db, pIdx);
109328         -        }
109329         -      }
       108349  +    whereClauseClear(&pWInfo->sWC);
       108350  +    while( pWInfo->pLoops ){
       108351  +      WhereLoop *p = pWInfo->pLoops;
       108352  +      pWInfo->pLoops = p->pNextLoop;
       108353  +      whereLoopDelete(db, p);
109330 108354       }
109331         -    whereClauseClear(pWInfo->pWC);
109332 108355       sqlite3DbFree(db, pWInfo);
109333 108356     }
109334 108357   }
109335 108358   
       108359  +/*
       108360  +** Insert or replace a WhereLoop entry using the template supplied.
       108361  +**
       108362  +** An existing WhereLoop entry might be overwritten if the new template
       108363  +** is better and has fewer dependencies.  Or the template will be ignored
       108364  +** and no insert will occur if an existing WhereLoop is faster and has
       108365  +** fewer dependencies than the template.  Otherwise a new WhereLoop is
       108366  +** added based on the template.
       108367  +**
       108368  +** If pBuilder->pBest is not NULL then we only care about the very
       108369  +** best template and that template should be stored in pBuilder->pBest.
       108370  +** If pBuilder->pBest is NULL then a list of the best templates are stored
       108371  +** in pBuilder->pWInfo->pLoops.
       108372  +**
       108373  +** When accumulating multiple loops (when pBuilder->pBest is NULL) we
       108374  +** still might overwrite similar loops with the new template if the
       108375  +** template is better.  Loops may be overwritten if the following 
       108376  +** conditions are met:
       108377  +**
       108378  +**    (1)  They have the same iTab.
       108379  +**    (2)  They have the same iSortIdx.
       108380  +**    (3)  The template has same or fewer dependencies than the current loop
       108381  +**    (4)  The template has the same or lower cost than the current loop
       108382  +**    (5)  The template uses more terms of the same index but has no additional
       108383  +**         dependencies          
       108384  +*/
       108385  +static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
       108386  +  WhereLoop **ppPrev, *p, *pNext = 0;
       108387  +  WhereInfo *pWInfo = pBuilder->pWInfo;
       108388  +  sqlite3 *db = pWInfo->pParse->db;
       108389  +
       108390  +  /* If pBuilder->pBest is defined, then only keep track of the single
       108391  +  ** best WhereLoop.  pBuilder->pBest->maskSelf==0 indicates that no
       108392  +  ** prior WhereLoops have been evaluated and that the current pTemplate
       108393  +  ** is therefore the first and hence the best and should be retained.
       108394  +  */
       108395  +  if( (p = pBuilder->pBest)!=0 ){
       108396  +    if( p->maskSelf!=0 ){
       108397  +      WhereCost rCost = whereCostAdd(p->rRun,p->rSetup);
       108398  +      WhereCost rTemplate = whereCostAdd(pTemplate->rRun,pTemplate->rSetup);
       108399  +      if( rCost < rTemplate ){
       108400  +        goto whereLoopInsert_noop;
       108401  +      }
       108402  +      if( rCost == rTemplate && p->prereq <= pTemplate->prereq ){
       108403  +        goto whereLoopInsert_noop;
       108404  +      }
       108405  +    }
       108406  +#if WHERETRACE_ENABLED
       108407  +    if( sqlite3WhereTrace & 0x8 ){
       108408  +      sqlite3DebugPrintf(p->maskSelf==0 ? "ins-init: " : "ins-best: ");
       108409  +      whereLoopPrint(pTemplate, pWInfo->pTabList);
       108410  +    }
       108411  +#endif
       108412  +    whereLoopXfer(db, p, pTemplate);
       108413  +    return SQLITE_OK;
       108414  +  }
       108415  +
       108416  +  /* Search for an existing WhereLoop to overwrite, or which takes
       108417  +  ** priority over pTemplate.
       108418  +  */
       108419  +  for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){
       108420  +    if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ) continue;
       108421  +    if( (p->prereq & pTemplate->prereq)==p->prereq
       108422  +     && p->rSetup<=pTemplate->rSetup
       108423  +     && p->rRun<=pTemplate->rRun
       108424  +    ){
       108425  +      /* p is equal or better than pTemplate */
       108426  +      if( p->nLTerm<pTemplate->nLTerm
       108427  +       && (p->wsFlags & WHERE_INDEXED)!=0
       108428  +       && (pTemplate->wsFlags & WHERE_INDEXED)!=0
       108429  +       && p->u.btree.pIndex==pTemplate->u.btree.pIndex
       108430  +       && p->prereq==pTemplate->prereq
       108431  +      ){
       108432  +        /* Overwrite an existing WhereLoop with an similar one that uses
       108433  +        ** more terms of the index */
       108434  +        pNext = p->pNextLoop;
       108435  +        break;
       108436  +      }else if( p->nOut>pTemplate->nOut
       108437  +       && p->rSetup==pTemplate->rSetup
       108438  +       && p->rRun==pTemplate->rRun
       108439  +      ){
       108440  +        /* Overwrite an existing WhereLoop with the same cost but more
       108441  +        ** outputs */
       108442  +        pNext = p->pNextLoop;
       108443  +        break;
       108444  +      }else{
       108445  +        /* pTemplate is not helpful.
       108446  +        ** Return without changing or adding anything */
       108447  +        goto whereLoopInsert_noop;
       108448  +      }
       108449  +    }
       108450  +    if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
       108451  +     && p->rSetup>=pTemplate->rSetup
       108452  +     && p->rRun>=pTemplate->rRun
       108453  +    ){
       108454  +      /* Overwrite an existing WhereLoop with a better one */
       108455  +      pNext = p->pNextLoop;
       108456  +      break;
       108457  +    }
       108458  +  }
       108459  +
       108460  +  /* If we reach this point it means that either p[] should be overwritten
       108461  +  ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
       108462  +  ** WhereLoop and insert it.
       108463  +  */
       108464  +#if WHERETRACE_ENABLED
       108465  +  if( sqlite3WhereTrace & 0x8 ){
       108466  +    if( p!=0 ){
       108467  +      sqlite3DebugPrintf("ins-del:  ");
       108468  +      whereLoopPrint(p, pWInfo->pTabList);
       108469  +    }
       108470  +    sqlite3DebugPrintf("ins-new:  ");
       108471  +    whereLoopPrint(pTemplate, pWInfo->pTabList);
       108472  +  }
       108473  +#endif
       108474  +  if( p==0 ){
       108475  +    p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
       108476  +    if( p==0 ) return SQLITE_NOMEM;
       108477  +    whereLoopInit(p);
       108478  +  }
       108479  +  whereLoopXfer(db, p, pTemplate);
       108480  +  p->pNextLoop = pNext;
       108481  +  *ppPrev = p;
       108482  +  if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
       108483  +    Index *pIndex = p->u.btree.pIndex;
       108484  +    if( pIndex && pIndex->tnum==0 ){
       108485  +      p->u.btree.pIndex = 0;
       108486  +    }
       108487  +  }
       108488  +  return SQLITE_OK;
       108489  +
       108490  +  /* Jump here if the insert is a no-op */
       108491  +whereLoopInsert_noop:
       108492  +#if WHERETRACE_ENABLED
       108493  +  if( sqlite3WhereTrace & 0x8 ){
       108494  +    sqlite3DebugPrintf(pBuilder->pBest ? "ins-skip: " : "ins-noop: ");
       108495  +    whereLoopPrint(pTemplate, pWInfo->pTabList);
       108496  +  }
       108497  +#endif
       108498  +  return SQLITE_OK;  
       108499  +}
       108500  +
       108501  +/*
       108502  +** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex.
       108503  +** Try to match one more.
       108504  +**
       108505  +** If pProbe->tnum==0, that means pIndex is a fake index used for the
       108506  +** INTEGER PRIMARY KEY.
       108507  +*/
       108508  +static int whereLoopAddBtreeIndex(
       108509  +  WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
       108510  +  struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
       108511  +  Index *pProbe,                  /* An index on pSrc */
       108512  +  WhereCost nInMul                /* log(Number of iterations due to IN) */
       108513  +){
       108514  +  WhereInfo *pWInfo = pBuilder->pWInfo;  /* WHERE analyse context */
       108515  +  Parse *pParse = pWInfo->pParse;        /* Parsing context */
       108516  +  sqlite3 *db = pParse->db;       /* Database connection malloc context */
       108517  +  WhereLoop *pNew;                /* Template WhereLoop under construction */
       108518  +  WhereTerm *pTerm;               /* A WhereTerm under consideration */
       108519  +  int opMask;                     /* Valid operators for constraints */
       108520  +  WhereScan scan;                 /* Iterator for WHERE terms */
       108521  +  Bitmask saved_prereq;           /* Original value of pNew->prereq */
       108522  +  u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
       108523  +  int saved_nEq;                  /* Original value of pNew->u.btree.nEq */
       108524  +  u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
       108525  +  WhereCost saved_nOut;           /* Original value of pNew->nOut */
       108526  +  int iCol;                       /* Index of the column in the table */
       108527  +  int rc = SQLITE_OK;             /* Return code */
       108528  +  WhereCost nRowEst;              /* Estimated index selectivity */
       108529  +  WhereCost rLogSize;             /* Logarithm of table size */
       108530  +  WhereTerm *pTop, *pBtm;         /* Top and bottom range constraints */
       108531  +
       108532  +  pNew = pBuilder->pNew;
       108533  +  if( db->mallocFailed ) return SQLITE_NOMEM;
       108534  +
       108535  +  assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
       108536  +  assert( pNew->u.btree.nEq<=pProbe->nColumn );
       108537  +  assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
       108538  +  if( pNew->wsFlags & WHERE_BTM_LIMIT ){
       108539  +    opMask = WO_LT|WO_LE;
       108540  +  }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
       108541  +    opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
       108542  +  }else{
       108543  +    opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
       108544  +  }
       108545  +  if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
       108546  +
       108547  +  if( pNew->u.btree.nEq < pProbe->nColumn ){
       108548  +    iCol = pProbe->aiColumn[pNew->u.btree.nEq];
       108549  +    nRowEst = whereCostFromInt(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
       108550  +  }else{
       108551  +    iCol = -1;
       108552  +    nRowEst = 0;
       108553  +  }
       108554  +  pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
       108555  +                        opMask, pProbe);
       108556  +  saved_nEq = pNew->u.btree.nEq;
       108557  +  saved_nLTerm = pNew->nLTerm;
       108558  +  saved_wsFlags = pNew->wsFlags;
       108559  +  saved_prereq = pNew->prereq;
       108560  +  saved_nOut = pNew->nOut;
       108561  +  pNew->rSetup = 0;
       108562  +  rLogSize = estLog(whereCostFromInt(pProbe->aiRowEst[0]));
       108563  +  for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
       108564  +    int nIn = 0;
       108565  +    if( pTerm->prereqRight & pNew->maskSelf ) continue;
       108566  +    pNew->wsFlags = saved_wsFlags;
       108567  +    pNew->u.btree.nEq = saved_nEq;
       108568  +    pNew->nLTerm = saved_nLTerm;
       108569  +    if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
       108570  +    pNew->aLTerm[pNew->nLTerm++] = pTerm;
       108571  +    pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
       108572  +    pNew->rRun = rLogSize;
       108573  +    if( pTerm->eOperator & WO_IN ){
       108574  +      Expr *pExpr = pTerm->pExpr;
       108575  +      pNew->wsFlags |= WHERE_COLUMN_IN;
       108576  +      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
       108577  +        /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
       108578  +        nIn = 46;  assert( 46==whereCostFromInt(25) );
       108579  +      }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
       108580  +        /* "x IN (value, value, ...)" */
       108581  +        nIn = whereCostFromInt(pExpr->x.pList->nExpr);
       108582  +      }
       108583  +      pNew->rRun += nIn;
       108584  +      pNew->u.btree.nEq++;
       108585  +      pNew->nOut = nRowEst + nInMul + nIn;
       108586  +    }else if( pTerm->eOperator & (WO_EQ) ){
       108587  +      assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0
       108588  +                  || nInMul==0 );
       108589  +      pNew->wsFlags |= WHERE_COLUMN_EQ;
       108590  +      if( iCol<0  
       108591  +       || (pProbe->onError!=OE_None && nInMul==0
       108592  +           && pNew->u.btree.nEq==pProbe->nColumn-1)
       108593  +      ){
       108594  +        testcase( pNew->wsFlags & WHERE_COLUMN_IN );
       108595  +        pNew->wsFlags |= WHERE_ONEROW;
       108596  +      }
       108597  +      pNew->u.btree.nEq++;
       108598  +      pNew->nOut = nRowEst + nInMul;
       108599  +    }else if( pTerm->eOperator & (WO_ISNULL) ){
       108600  +      pNew->wsFlags |= WHERE_COLUMN_NULL;
       108601  +      pNew->u.btree.nEq++;
       108602  +      nIn = 10;  assert( 10==whereCostFromInt(2) );
       108603  +      pNew->nOut = nRowEst + nInMul + nIn;
       108604  +    }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
       108605  +      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
       108606  +      pBtm = pTerm;
       108607  +      pTop = 0;
       108608  +    }else if( pTerm->eOperator & (WO_LT|WO_LE) ){
       108609  +      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
       108610  +      pTop = pTerm;
       108611  +      pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
       108612  +                     pNew->aLTerm[pNew->nLTerm-2] : 0;
       108613  +    }
       108614  +    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
       108615  +      /* Adjust nOut and rRun for STAT3 range values */
       108616  +      WhereCost rDiv;
       108617  +      whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq,
       108618  +                        pBtm, pTop, &rDiv);
       108619  +      pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10;
       108620  +    }
       108621  +#ifdef SQLITE_ENABLE_STAT3
       108622  +    if( pNew->u.btree.nEq==1 && pProbe->nSample ){
       108623  +      tRowcnt nOut = 0;
       108624  +      if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
       108625  +        rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut);
       108626  +      }else if( (pTerm->eOperator & WO_IN)
       108627  +             &&  !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)  ){
       108628  +        rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut);
       108629  +      }
       108630  +      pNew->nOut = whereCostFromInt(nOut);
       108631  +    }
       108632  +#endif
       108633  +    if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
       108634  +      /* Each row involves a step of the index, then a binary search of
       108635  +      ** the main table */
       108636  +      pNew->rRun =  whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10);
       108637  +    }
       108638  +    /* Step cost for each output row */
       108639  +    pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut);
       108640  +    /* TBD: Adjust nOut for additional constraints */
       108641  +    rc = whereLoopInsert(pBuilder, pNew);
       108642  +    if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
       108643  +     && pNew->u.btree.nEq<=pProbe->nColumn
       108644  +     && pProbe->zName!=0
       108645  +    ){
       108646  +      whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
       108647  +    }
       108648  +  }
       108649  +  pNew->prereq = saved_prereq;
       108650  +  pNew->u.btree.nEq = saved_nEq;
       108651  +  pNew->wsFlags = saved_wsFlags;
       108652  +  pNew->nOut = saved_nOut;
       108653  +  pNew->nLTerm = saved_nLTerm;
       108654  +  return rc;
       108655  +}
       108656  +
       108657  +/*
       108658  +** Return True if it is possible that pIndex might be useful in
       108659  +** implementing the ORDER BY clause in pBuilder.
       108660  +**
       108661  +** Return False if pBuilder does not contain an ORDER BY clause or
       108662  +** if there is no way for pIndex to be useful in implementing that
       108663  +** ORDER BY clause.
       108664  +*/
       108665  +static int indexMightHelpWithOrderBy(
       108666  +  WhereLoopBuilder *pBuilder,
       108667  +  Index *pIndex,
       108668  +  int iCursor
       108669  +){
       108670  +  ExprList *pOB;
       108671  +  int iCol;
       108672  +  int ii;
       108673  +
       108674  +  if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
       108675  +  iCol = pIndex->aiColumn[0];
       108676  +  for(ii=0; ii<pOB->nExpr; ii++){
       108677  +    Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
       108678  +    if( pExpr->op!=TK_COLUMN ) return 0;
       108679  +    if( pExpr->iTable==iCursor ){
       108680  +      if( pExpr->iColumn==iCol ) return 1;
       108681  +      return 0;
       108682  +    }
       108683  +  }
       108684  +  return 0;
       108685  +}
       108686  +
       108687  +/*
       108688  +** Return a bitmask where 1s indicate that the corresponding column of
       108689  +** the table is used by an index.  Only the first 63 columns are considered.
       108690  +*/
       108691  +static Bitmask columnsInIndex(Index *pIdx){
       108692  +  Bitmask m = 0;
       108693  +  int j;
       108694  +  for(j=pIdx->nColumn-1; j>=0; j--){
       108695  +    int x = pIdx->aiColumn[j];
       108696  +    if( x<BMS-1 ) m |= MASKBIT(x);
       108697  +  }
       108698  +  return m;
       108699  +}
       108700  +
       108701  +
       108702  +/*
       108703  +** Add all WhereLoop objects a single table of the join were the table
       108704  +** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
       108705  +** a b-tree table, not a virtual table.
       108706  +*/
       108707  +static int whereLoopAddBtree(
       108708  +  WhereLoopBuilder *pBuilder, /* WHERE clause information */
       108709  +  Bitmask mExtra              /* Extra prerequesites for using this table */
       108710  +){
       108711  +  WhereInfo *pWInfo;          /* WHERE analysis context */
       108712  +  Index *pProbe;              /* An index we are evaluating */
       108713  +  Index sPk;                  /* A fake index object for the primary key */
       108714  +  tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
       108715  +  int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
       108716  +  SrcList *pTabList;          /* The FROM clause */
       108717  +  struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
       108718  +  WhereLoop *pNew;            /* Template WhereLoop object */
       108719  +  int rc = SQLITE_OK;         /* Return code */
       108720  +  int iSortIdx = 1;           /* Index number */
       108721  +  int b;                      /* A boolean value */
       108722  +  WhereCost rSize;            /* number of rows in the table */
       108723  +  WhereCost rLogSize;         /* Logarithm of the number of rows in the table */
       108724  +  
       108725  +  pNew = pBuilder->pNew;
       108726  +  pWInfo = pBuilder->pWInfo;
       108727  +  pTabList = pWInfo->pTabList;
       108728  +  pSrc = pTabList->a + pNew->iTab;
       108729  +  assert( !IsVirtual(pSrc->pTab) );
       108730  +
       108731  +  if( pSrc->pIndex ){
       108732  +    /* An INDEXED BY clause specifies a particular index to use */
       108733  +    pProbe = pSrc->pIndex;
       108734  +  }else{
       108735  +    /* There is no INDEXED BY clause.  Create a fake Index object in local
       108736  +    ** variable sPk to represent the rowid primary key index.  Make this
       108737  +    ** fake index the first in a chain of Index objects with all of the real
       108738  +    ** indices to follow */
       108739  +    Index *pFirst;                  /* First of real indices on the table */
       108740  +    memset(&sPk, 0, sizeof(Index));
       108741  +    sPk.nColumn = 1;
       108742  +    sPk.aiColumn = &aiColumnPk;
       108743  +    sPk.aiRowEst = aiRowEstPk;
       108744  +    sPk.onError = OE_Replace;
       108745  +    sPk.pTable = pSrc->pTab;
       108746  +    aiRowEstPk[0] = pSrc->pTab->nRowEst;
       108747  +    aiRowEstPk[1] = 1;
       108748  +    pFirst = pSrc->pTab->pIndex;
       108749  +    if( pSrc->notIndexed==0 ){
       108750  +      /* The real indices of the table are only considered if the
       108751  +      ** NOT INDEXED qualifier is omitted from the FROM clause */
       108752  +      sPk.pNext = pFirst;
       108753  +    }
       108754  +    pProbe = &sPk;
       108755  +  }
       108756  +  rSize = whereCostFromInt(pSrc->pTab->nRowEst);
       108757  +  rLogSize = estLog(rSize);
       108758  +
       108759  +  /* Automatic indexes */
       108760  +  if( !pBuilder->pBest
       108761  +   && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
       108762  +   && pSrc->pIndex==0
       108763  +   && !pSrc->viaCoroutine
       108764  +   && !pSrc->notIndexed
       108765  +   && !pSrc->isCorrelated
       108766  +  ){
       108767  +    /* Generate auto-index WhereLoops */
       108768  +    WhereClause *pWC = pBuilder->pWC;
       108769  +    WhereTerm *pTerm;
       108770  +    WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
       108771  +    for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
       108772  +      if( pTerm->prereqRight & pNew->maskSelf ) continue;
       108773  +      if( termCanDriveIndex(pTerm, pSrc, 0) ){
       108774  +        pNew->u.btree.nEq = 1;
       108775  +        pNew->u.btree.pIndex = 0;
       108776  +        pNew->nLTerm = 1;
       108777  +        pNew->aLTerm[0] = pTerm;
       108778  +        assert( 43==whereCostFromInt(20) );
       108779  +        pNew->rSetup = 43 + rLogSize + rSize;
       108780  +        pNew->nOut = 33;  assert( 33==whereCostFromInt(10) );
       108781  +        pNew->rRun = whereCostAdd(rLogSize,pNew->nOut);
       108782  +        pNew->wsFlags = WHERE_TEMP_INDEX;
       108783  +        pNew->prereq = mExtra | pTerm->prereqRight;
       108784  +        rc = whereLoopInsert(pBuilder, pNew);
       108785  +      }
       108786  +    }
       108787  +  }
       108788  +
       108789  +  /* Loop over all indices
       108790  +  */
       108791  +  for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
       108792  +    pNew->u.btree.nEq = 0;
       108793  +    pNew->nLTerm = 0;
       108794  +    pNew->iSortIdx = 0;
       108795  +    pNew->rSetup = 0;
       108796  +    pNew->prereq = mExtra;
       108797  +    pNew->u.btree.pIndex = pProbe;
       108798  +    b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
       108799  +    if( pProbe->tnum<=0 ){
       108800  +      /* Integer primary key index */
       108801  +      pNew->wsFlags = WHERE_IPK;
       108802  +
       108803  +      /* Full table scan */
       108804  +      pNew->iSortIdx = b ? iSortIdx : 0;
       108805  +      pNew->nOut = rSize;
       108806  +      pNew->rRun = whereCostAdd(rSize,rLogSize) + 16 - b;
       108807  +      rc = whereLoopInsert(pBuilder, pNew);
       108808  +      if( rc ) break;
       108809  +    }else{
       108810  +      Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe);
       108811  +      pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
       108812  +
       108813  +      /* Full scan via index */
       108814  +      if( (m==0 || b)
       108815  +       && pProbe->bUnordered==0
       108816  +       && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
       108817  +       && sqlite3GlobalConfig.bUseCis
       108818  +       && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
       108819  +      ){
       108820  +        pNew->iSortIdx = b ? iSortIdx : 0;
       108821  +        pNew->nOut = rSize;
       108822  +        pNew->rRun = whereCostAdd(rSize,rLogSize);
       108823  +        pNew->rRun += ((m!=0) ? rLogSize : 10) - b;
       108824  +        rc = whereLoopInsert(pBuilder, pNew);
       108825  +        if( rc ) break;
       108826  +      }
       108827  +    }
       108828  +    rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
       108829  +
       108830  +    /* If there was an INDEXED BY clause, then only that one index is
       108831  +    ** considered. */
       108832  +    if( pSrc->pIndex ) break;
       108833  +  }
       108834  +  return rc;
       108835  +}
       108836  +
       108837  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       108838  +/*
       108839  +** Add all WhereLoop objects for a table of the join identified by
       108840  +** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
       108841  +*/
       108842  +static int whereLoopAddVirtual(
       108843  +  WhereLoopBuilder *pBuilder,  /* WHERE clause information */
       108844  +  Bitmask mExtra               /* Extra prerequesites for using this table */
       108845  +){
       108846  +  WhereInfo *pWInfo;           /* WHERE analysis context */
       108847  +  Parse *pParse;               /* The parsing context */
       108848  +  WhereClause *pWC;            /* The WHERE clause */
       108849  +  struct SrcList_item *pSrc;   /* The FROM clause term to search */
       108850  +  Table *pTab;
       108851  +  sqlite3 *db;
       108852  +  sqlite3_index_info *pIdxInfo;
       108853  +  struct sqlite3_index_constraint *pIdxCons;
       108854  +  struct sqlite3_index_constraint_usage *pUsage;
       108855  +  WhereTerm *pTerm;
       108856  +  int i, j;
       108857  +  int iTerm, mxTerm;
       108858  +  int nConstraint;
       108859  +  int seenIn = 0;              /* True if an IN operator is seen */
       108860  +  int seenVar = 0;             /* True if a non-constant constraint is seen */
       108861  +  int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
       108862  +  WhereLoop *pNew;
       108863  +  int rc = SQLITE_OK;
       108864  +
       108865  +  pWInfo = pBuilder->pWInfo;
       108866  +  pParse = pWInfo->pParse;
       108867  +  db = pParse->db;
       108868  +  pWC = pBuilder->pWC;
       108869  +  pNew = pBuilder->pNew;
       108870  +  pSrc = &pWInfo->pTabList->a[pNew->iTab];
       108871  +  pTab = pSrc->pTab;
       108872  +  assert( IsVirtual(pTab) );
       108873  +  pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
       108874  +  if( pIdxInfo==0 ) return SQLITE_NOMEM;
       108875  +  pNew->prereq = 0;
       108876  +  pNew->rSetup = 0;
       108877  +  pNew->wsFlags = WHERE_VIRTUALTABLE;
       108878  +  pNew->nLTerm = 0;
       108879  +  pNew->u.vtab.needFree = 0;
       108880  +  pUsage = pIdxInfo->aConstraintUsage;
       108881  +  nConstraint = pIdxInfo->nConstraint;
       108882  +  if( whereLoopResize(db, pNew, nConstraint) ) return SQLITE_NOMEM;
       108883  +
       108884  +  for(iPhase=0; iPhase<=3; iPhase++){
       108885  +    if( !seenIn && (iPhase&1)!=0 ){
       108886  +      iPhase++;
       108887  +      if( iPhase>3 ) break;
       108888  +    }
       108889  +    if( !seenVar && iPhase>1 ) break;
       108890  +    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
       108891  +    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
       108892  +      j = pIdxCons->iTermOffset;
       108893  +      pTerm = &pWC->a[j];
       108894  +      switch( iPhase ){
       108895  +        case 0:    /* Constants without IN operator */
       108896  +          pIdxCons->usable = 0;
       108897  +          if( (pTerm->eOperator & WO_IN)!=0 ){
       108898  +            seenIn = 1;
       108899  +          }else if( pTerm->prereqRight!=0 ){
       108900  +            seenVar = 1;
       108901  +          }else{
       108902  +            pIdxCons->usable = 1;
       108903  +          }
       108904  +          break;
       108905  +        case 1:    /* Constants with IN operators */
       108906  +          assert( seenIn );
       108907  +          pIdxCons->usable = (pTerm->prereqRight==0);
       108908  +          break;
       108909  +        case 2:    /* Variables without IN */
       108910  +          assert( seenVar );
       108911  +          pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
       108912  +          break;
       108913  +        default:   /* Variables with IN */
       108914  +          assert( seenVar && seenIn );
       108915  +          pIdxCons->usable = 1;
       108916  +          break;
       108917  +      }
       108918  +    }
       108919  +    memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
       108920  +    if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
       108921  +    pIdxInfo->idxStr = 0;
       108922  +    pIdxInfo->idxNum = 0;
       108923  +    pIdxInfo->needToFreeIdxStr = 0;
       108924  +    pIdxInfo->orderByConsumed = 0;
       108925  +    pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
       108926  +    rc = vtabBestIndex(pParse, pTab, pIdxInfo);
       108927  +    if( rc ) goto whereLoopAddVtab_exit;
       108928  +    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
       108929  +    pNew->prereq = 0;
       108930  +    mxTerm = -1;
       108931  +    assert( pNew->nLSlot>=nConstraint );
       108932  +    for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
       108933  +    pNew->u.vtab.omitMask = 0;
       108934  +    for(i=0; i<nConstraint; i++, pIdxCons++){
       108935  +      if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
       108936  +        j = pIdxCons->iTermOffset;
       108937  +        if( iTerm>=nConstraint
       108938  +         || j<0
       108939  +         || j>=pWC->nTerm
       108940  +         || pNew->aLTerm[iTerm]!=0
       108941  +        ){
       108942  +          rc = SQLITE_ERROR;
       108943  +          sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
       108944  +          goto whereLoopAddVtab_exit;
       108945  +        }
       108946  +        pTerm = &pWC->a[j];
       108947  +        pNew->prereq |= pTerm->prereqRight;
       108948  +        assert( iTerm<pNew->nLSlot );
       108949  +        pNew->aLTerm[iTerm] = pTerm;
       108950  +        if( iTerm>mxTerm ) mxTerm = iTerm;
       108951  +        if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
       108952  +        if( (pTerm->eOperator & WO_IN)!=0 ){
       108953  +          if( pUsage[i].omit==0 ){
       108954  +            /* Do not attempt to use an IN constraint if the virtual table
       108955  +            ** says that the equivalent EQ constraint cannot be safely omitted.
       108956  +            ** If we do attempt to use such a constraint, some rows might be
       108957  +            ** repeated in the output. */
       108958  +            break;
       108959  +          }
       108960  +          /* A virtual table that is constrained by an IN clause may not
       108961  +          ** consume the ORDER BY clause because (1) the order of IN terms
       108962  +          ** is not necessarily related to the order of output terms and
       108963  +          ** (2) Multiple outputs from a single IN value will not merge
       108964  +          ** together.  */
       108965  +          pIdxInfo->orderByConsumed = 0;
       108966  +        }
       108967  +      }
       108968  +    }
       108969  +    if( i>=nConstraint ){
       108970  +      pNew->nLTerm = mxTerm+1;
       108971  +      assert( pNew->nLTerm<=pNew->nLSlot );
       108972  +      pNew->u.vtab.idxNum = pIdxInfo->idxNum;
       108973  +      pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
       108974  +      pIdxInfo->needToFreeIdxStr = 0;
       108975  +      pNew->u.vtab.idxStr = pIdxInfo->idxStr;
       108976  +      pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
       108977  +                                     && pIdxInfo->orderByConsumed);
       108978  +      pNew->rSetup = 0;
       108979  +      pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost);
       108980  +      pNew->nOut = 46;  assert( 46 == whereCostFromInt(25) );
       108981  +      whereLoopInsert(pBuilder, pNew);
       108982  +      if( pNew->u.vtab.needFree ){
       108983  +        sqlite3_free(pNew->u.vtab.idxStr);
       108984  +        pNew->u.vtab.needFree = 0;
       108985  +      }
       108986  +    }
       108987  +  }  
       108988  +
       108989  +whereLoopAddVtab_exit:
       108990  +  if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
       108991  +  sqlite3DbFree(db, pIdxInfo);
       108992  +  return rc;
       108993  +}
       108994  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
       108995  +
       108996  +/*
       108997  +** Add WhereLoop entries to handle OR terms.  This works for either
       108998  +** btrees or virtual tables.
       108999  +*/
       109000  +static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
       109001  +  WhereInfo *pWInfo = pBuilder->pWInfo;
       109002  +  WhereClause *pWC;
       109003  +  WhereLoop *pNew;
       109004  +  WhereTerm *pTerm, *pWCEnd;
       109005  +  int rc = SQLITE_OK;
       109006  +  int iCur;
       109007  +  WhereClause tempWC;
       109008  +  WhereLoopBuilder sSubBuild;
       109009  +  WhereLoop sBest;
       109010  +  struct SrcList_item *pItem;
       109011  +  
       109012  +  pWC = pBuilder->pWC;
       109013  +  if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
       109014  +  pWCEnd = pWC->a + pWC->nTerm;
       109015  +  pNew = pBuilder->pNew;
       109016  +
       109017  +  for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
       109018  +    if( (pTerm->eOperator & WO_OR)!=0
       109019  +     && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 
       109020  +    ){
       109021  +      WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
       109022  +      WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
       109023  +      WhereTerm *pOrTerm;
       109024  +      WhereCost rTotal = 0;
       109025  +      WhereCost nRow = 0;
       109026  +      Bitmask prereq = mExtra;
       109027  +    
       109028  +      whereLoopInit(&sBest);
       109029  +      pItem = pWInfo->pTabList->a + pNew->iTab;
       109030  +      iCur = pItem->iCursor;
       109031  +      sSubBuild = *pBuilder;
       109032  +      sSubBuild.pOrderBy = 0;
       109033  +      sSubBuild.pBest = &sBest;
       109034  +
       109035  +      for(pOrTerm=pOrWC->a; rc==SQLITE_OK && pOrTerm<pOrWCEnd; pOrTerm++){
       109036  +        if( (pOrTerm->eOperator & WO_AND)!=0 ){
       109037  +          sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
       109038  +        }else if( pOrTerm->leftCursor==iCur ){
       109039  +          tempWC.pWInfo = pWC->pWInfo;
       109040  +          tempWC.pOuter = pWC;
       109041  +          tempWC.op = TK_AND;
       109042  +          tempWC.nTerm = 1;
       109043  +          tempWC.a = pOrTerm;
       109044  +          sSubBuild.pWC = &tempWC;
       109045  +        }else{
       109046  +          continue;
       109047  +        }
       109048  +        sBest.maskSelf = 0;
       109049  +        sBest.rSetup = 0;
       109050  +        sBest.rRun = 0;
       109051  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       109052  +        if( IsVirtual(pItem->pTab) ){
       109053  +          rc = whereLoopAddVirtual(&sSubBuild, mExtra);
       109054  +        }else
       109055  +#endif
       109056  +        {
       109057  +          rc = whereLoopAddBtree(&sSubBuild, mExtra);
       109058  +        }
       109059  +        if( sBest.maskSelf==0 ) break;
       109060  +        assert( sBest.rSetup==0 );
       109061  +        rTotal = whereCostAdd(rTotal, sBest.rRun);
       109062  +        nRow = whereCostAdd(nRow, sBest.nOut);
       109063  +        prereq |= sBest.prereq;
       109064  +      }
       109065  +      assert( pNew->nLSlot>=1 );
       109066  +      if( sBest.maskSelf ){
       109067  +        pNew->nLTerm = 1;
       109068  +        pNew->aLTerm[0] = pTerm;
       109069  +        pNew->wsFlags = WHERE_MULTI_OR;
       109070  +        pNew->rSetup = 0;
       109071  +        pNew->rRun = rTotal;
       109072  +        pNew->nOut = nRow;
       109073  +        pNew->prereq = prereq;
       109074  +        memset(&pNew->u, 0, sizeof(pNew->u));
       109075  +        rc = whereLoopInsert(pBuilder, pNew);
       109076  +      }
       109077  +      whereLoopClear(pWInfo->pParse->db, &sBest);
       109078  +    }
       109079  +  }
       109080  +  return rc;
       109081  +}
       109082  +
       109083  +/*
       109084  +** Add all WhereLoop objects for all tables 
       109085  +*/
       109086  +static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
       109087  +  WhereInfo *pWInfo = pBuilder->pWInfo;
       109088  +  Bitmask mExtra = 0;
       109089  +  Bitmask mPrior = 0;
       109090  +  int iTab;
       109091  +  SrcList *pTabList = pWInfo->pTabList;
       109092  +  struct SrcList_item *pItem;
       109093  +  sqlite3 *db = pWInfo->pParse->db;
       109094  +  int nTabList = pWInfo->nLevel;
       109095  +  int rc = SQLITE_OK;
       109096  +  u8 priorJoinType = 0;
       109097  +  WhereLoop *pNew;
       109098  +
       109099  +  /* Loop over the tables in the join, from left to right */
       109100  +  pNew = pBuilder->pNew;
       109101  +  whereLoopInit(pNew);
       109102  +  for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
       109103  +    pNew->iTab = iTab;
       109104  +    pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
       109105  +    if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
       109106  +      mExtra = mPrior;
       109107  +    }
       109108  +    priorJoinType = pItem->jointype;
       109109  +    if( IsVirtual(pItem->pTab) ){
       109110  +      rc = whereLoopAddVirtual(pBuilder, mExtra);
       109111  +    }else{
       109112  +      rc = whereLoopAddBtree(pBuilder, mExtra);
       109113  +    }
       109114  +    if( rc==SQLITE_OK ){
       109115  +      rc = whereLoopAddOr(pBuilder, mExtra);
       109116  +    }
       109117  +    mPrior |= pNew->maskSelf;
       109118  +    if( rc || db->mallocFailed ) break;
       109119  +  }
       109120  +  whereLoopClear(db, pNew);
       109121  +  return rc;
       109122  +}
       109123  +
       109124  +/*
       109125  +** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
       109126  +** parameters) to see if it outputs rows in the requested ORDER BY
       109127  +** (or GROUP BY) without requiring a separate source operation.  Return:
       109128  +** 
       109129  +**    0:  ORDER BY is not satisfied.  Sorting required
       109130  +**    1:  ORDER BY is satisfied.      Omit sorting
       109131  +**   -1:  Unknown at this time
       109132  +**
       109133  +*/
       109134  +static int wherePathSatisfiesOrderBy(
       109135  +  WhereInfo *pWInfo,    /* The WHERE clause */
       109136  +  ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
       109137  +  WherePath *pPath,     /* The WherePath to check */
       109138  +  u16 wctrlFlags,       /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
       109139  +  u16 nLoop,            /* Number of entries in pPath->aLoop[] */
       109140  +  u8 isLastLoop,        /* True if pLast is the inner-most loop */
       109141  +  WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
       109142  +  Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
       109143  +){
       109144  +  u8 revSet;            /* True if rev is known */
       109145  +  u8 rev;               /* Composite sort order */
       109146  +  u8 revIdx;            /* Index sort order */
       109147  +  u8 isOrderDistinct;   /* All prior WhereLoops are order-distinct */
       109148  +  u8 distinctColumns;   /* True if the loop has UNIQUE NOT NULL columns */
       109149  +  u8 isMatch;           /* iColumn matches a term of the ORDER BY clause */
       109150  +  u16 nColumn;          /* Number of columns in pIndex */
       109151  +  u16 nOrderBy;         /* Number terms in the ORDER BY clause */
       109152  +  int iLoop;            /* Index of WhereLoop in pPath being processed */
       109153  +  int i, j;             /* Loop counters */
       109154  +  int iCur;             /* Cursor number for current WhereLoop */
       109155  +  int iColumn;          /* A column number within table iCur */
       109156  +  WhereLoop *pLoop;     /* Current WhereLoop being processed. */
       109157  +  WhereTerm *pTerm;     /* A single term of the WHERE clause */
       109158  +  Expr *pOBExpr;        /* An expression from the ORDER BY clause */
       109159  +  CollSeq *pColl;       /* COLLATE function from an ORDER BY clause term */
       109160  +  Index *pIndex;        /* The index associated with pLoop */
       109161  +  sqlite3 *db = pWInfo->pParse->db;  /* Database connection */
       109162  +  Bitmask obSat = 0;    /* Mask of ORDER BY terms satisfied so far */
       109163  +  Bitmask obDone;       /* Mask of all ORDER BY terms */
       109164  +  Bitmask orderDistinctMask;  /* Mask of all well-ordered loops */
       109165  +  
       109166  +
       109167  +  /*
       109168  +  ** We say the WhereLoop is "one-row" if it generates no more than one
       109169  +  ** row of output.  A WhereLoop is one-row if all of the following are true:
       109170  +  **  (a) All index columns match with WHERE_COLUMN_EQ.
       109171  +  **  (b) The index is unique
       109172  +  ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
       109173  +  ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
       109174  +  **
       109175  +  ** We say the WhereLoop is "order-distinct" if the set of columns from
       109176  +  ** that WhereLoop that are in the ORDER BY clause are different for every
       109177  +  ** row of the WhereLoop.  Every one-row WhereLoop is automatically
       109178  +  ** order-distinct.   A WhereLoop that has no columns in the ORDER BY clause
       109179  +  ** is not order-distinct. To be order-distinct is not quite the same as being
       109180  +  ** UNIQUE since a UNIQUE column or index can have multiple rows that 
       109181  +  ** are NULL and NULL values are equivalent for the purpose of order-distinct.
       109182  +  ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
       109183  +  **
       109184  +  ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
       109185  +  ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
       109186  +  ** automatically order-distinct.
       109187  +  */
       109188  +
       109189  +  assert( pOrderBy!=0 );
       109190  +
       109191  +  /* Sortability of virtual tables is determined by the xBestIndex method
       109192  +  ** of the virtual table itself */
       109193  +  if( pLast->wsFlags & WHERE_VIRTUALTABLE ){
       109194  +    testcase( nLoop>0 );  /* True when outer loops are one-row and match 
       109195  +                          ** no ORDER BY terms */
       109196  +    return pLast->u.vtab.isOrdered;
       109197  +  }
       109198  +  if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
       109199  +
       109200  +  nOrderBy = pOrderBy->nExpr;
       109201  +  if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
       109202  +  isOrderDistinct = 1;
       109203  +  obDone = MASKBIT(nOrderBy)-1;
       109204  +  orderDistinctMask = 0;
       109205  +  for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
       109206  +    pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
       109207  +    assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
       109208  +    iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
       109209  +    if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
       109210  +      if( pLoop->wsFlags & WHERE_IPK ){
       109211  +        pIndex = 0;
       109212  +        nColumn = 0;
       109213  +      }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
       109214  +        return 0;
       109215  +      }else{
       109216  +        nColumn = pIndex->nColumn;
       109217  +        isOrderDistinct = pIndex->onError!=OE_None;
       109218  +      }
       109219  +
       109220  +      /* For every term of the index that is constrained by == or IS NULL,
       109221  +      ** mark off corresponding ORDER BY terms wherever they occur
       109222  +      ** in the ORDER BY clause.
       109223  +      */
       109224  +      for(i=0; i<pLoop->u.btree.nEq; i++){
       109225  +        pTerm = pLoop->aLTerm[i];
       109226  +        if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))==0 ) continue;
       109227  +        iColumn = pTerm->u.leftColumn;
       109228  +        for(j=0; j<nOrderBy; j++){
       109229  +          if( MASKBIT(j) & obSat ) continue;
       109230  +          pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[j].pExpr);
       109231  +          if( pOBExpr->op!=TK_COLUMN ) continue;
       109232  +          if( pOBExpr->iTable!=iCur ) continue;
       109233  +          if( pOBExpr->iColumn!=iColumn ) continue;
       109234  +          if( iColumn>=0 ){
       109235  +            pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[j].pExpr);
       109236  +            if( !pColl ) pColl = db->pDfltColl;
       109237  +            if( sqlite3StrICmp(pColl->zName, pIndex->azColl[i])!=0 ) continue;
       109238  +          }
       109239  +          obSat |= MASKBIT(j);
       109240  +        }
       109241  +        if( obSat==obDone ) return 1;
       109242  +      }
       109243  +
       109244  +      /* Loop through all columns of the index and deal with the ones
       109245  +      ** that are not constrained by == or IN.
       109246  +      */
       109247  +      rev = revSet = 0;
       109248  +      distinctColumns = 0;
       109249  +      for(j=0; j<=nColumn; j++){
       109250  +        u8 bOnce;   /* True to run the ORDER BY search loop */
       109251  +
       109252  +        /* Skip over == and IS NULL terms */
       109253  +        if( j<pLoop->u.btree.nEq
       109254  +         && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
       109255  +        ){
       109256  +          if( i & WO_ISNULL ) isOrderDistinct = 0;
       109257  +          continue;  
       109258  +        }
       109259  +
       109260  +        /* Get the column number in the table (iColumn) and sort order
       109261  +        ** (revIdx) for the j-th column of the index.
       109262  +        */
       109263  +        if( j<nColumn ){
       109264  +          /* Normal index columns */
       109265  +          iColumn = pIndex->aiColumn[j];
       109266  +          revIdx = pIndex->aSortOrder[j];
       109267  +          if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
       109268  +        }else{
       109269  +          /* The ROWID column at the end */
       109270  +          iColumn = -1;
       109271  +          revIdx = 0;
       109272  +        }
       109273  +
       109274  +        /* An unconstrained column that might be NULL means that this
       109275  +        ** WhereLoop is not well-ordered 
       109276  +        */
       109277  +        if( isOrderDistinct
       109278  +         && iColumn>=0
       109279  +         && j>=pLoop->u.btree.nEq
       109280  +         && pIndex->pTable->aCol[iColumn].notNull==0
       109281  +        ){
       109282  +          isOrderDistinct = 0;
       109283  +        }
       109284  +
       109285  +        /* Find the ORDER BY term that corresponds to the j-th column
       109286  +        ** of the index and and mark that ORDER BY term off 
       109287  +        */
       109288  +        bOnce = 1;
       109289  +        isMatch = 0;
       109290  +        for(i=0; bOnce && i<nOrderBy; i++){
       109291  +          if( MASKBIT(i) & obSat ) continue;
       109292  +          pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
       109293  +          if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
       109294  +          if( pOBExpr->op!=TK_COLUMN ) continue;
       109295  +          if( pOBExpr->iTable!=iCur ) continue;
       109296  +          if( pOBExpr->iColumn!=iColumn ) continue;
       109297  +          if( iColumn>=0 ){
       109298  +            pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
       109299  +            if( !pColl ) pColl = db->pDfltColl;
       109300  +            if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
       109301  +          }
       109302  +          isMatch = 1;
       109303  +          break;
       109304  +        }
       109305  +        if( isMatch ){
       109306  +          if( iColumn<0 ) distinctColumns = 1;
       109307  +          obSat |= MASKBIT(i);
       109308  +          if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
       109309  +            /* Make sure the sort order is compatible in an ORDER BY clause.
       109310  +            ** Sort order is irrelevant for a GROUP BY clause. */
       109311  +            if( revSet ){
       109312  +              if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0;
       109313  +            }else{
       109314  +              rev = revIdx ^ pOrderBy->a[i].sortOrder;
       109315  +              if( rev ) *pRevMask |= MASKBIT(iLoop);
       109316  +              revSet = 1;
       109317  +            }
       109318  +          }
       109319  +        }else{
       109320  +          /* No match found */
       109321  +          if( j==0 || j<nColumn ) isOrderDistinct = 0;
       109322  +          break;
       109323  +        }
       109324  +      } /* end Loop over all index columns */
       109325  +      if( distinctColumns ) isOrderDistinct = 1;
       109326  +    } /* end-if not one-row */
       109327  +
       109328  +    /* Mark off any other ORDER BY terms that reference pLoop */
       109329  +    if( isOrderDistinct ){
       109330  +      orderDistinctMask |= pLoop->maskSelf;
       109331  +      for(i=0; i<nOrderBy; i++){
       109332  +        Expr *p;
       109333  +        if( MASKBIT(i) & obSat ) continue;
       109334  +        p = pOrderBy->a[i].pExpr;
       109335  +        if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){
       109336  +          obSat |= MASKBIT(i);
       109337  +        }
       109338  +      }
       109339  +    }
       109340  +  }
       109341  +  if( obSat==obDone ) return 1;
       109342  +  if( !isOrderDistinct ) return 0;
       109343  +  if( isLastLoop ) return 1;
       109344  +  return -1;
       109345  +}
       109346  +
       109347  +#ifdef WHERETRACE_ENABLED
       109348  +/* For debugging use only: */
       109349  +static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
       109350  +  static char zName[65];
       109351  +  int i;
       109352  +  for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
       109353  +  if( pLast ) zName[i++] = pLast->cId;
       109354  +  zName[i] = 0;
       109355  +  return zName;
       109356  +}
       109357  +#endif
       109358  +
       109359  +
       109360  +/*
       109361  +** Given the list of WhereLoop objects on pWInfo->pLoops, this routine
       109362  +** attempts to find the lowest cost path that visits each WhereLoop
       109363  +** once.  This path is then loaded into the pWInfo->a[].pWLoop fields.
       109364  +**
       109365  +** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
       109366  +** error occurs.
       109367  +*/
       109368  +static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){
       109369  +  int mxChoice;             /* Maximum number of simultaneous paths tracked */
       109370  +  int nLoop;                /* Number of terms in the join */
       109371  +  sqlite3 *db;              /* The database connection */
       109372  +  int iLoop;                /* Loop counter over the terms of the join */
       109373  +  int ii, jj;               /* Loop counters */
       109374  +  WhereCost rCost;             /* Cost of a path */
       109375  +  WhereCost mxCost;            /* Maximum cost of a set of paths */
       109376  +  WhereCost rSortCost;         /* Cost to do a sort */
       109377  +  int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
       109378  +  WherePath *aFrom;         /* All nFrom paths at the previous level */
       109379  +  WherePath *aTo;           /* The nTo best paths at the current level */
       109380  +  WherePath *pFrom;         /* An element of aFrom[] that we are working on */
       109381  +  WherePath *pTo;           /* An element of aTo[] that we are working on */
       109382  +  WhereLoop *pWLoop;        /* One of the WhereLoop objects */
       109383  +  WhereLoop **pX;           /* Used to divy up the pSpace memory */
       109384  +  char *pSpace;             /* Temporary memory used by this routine */
       109385  +
       109386  +  db = pWInfo->pParse->db;
       109387  +  nLoop = pWInfo->nLevel;
       109388  +  mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10);
       109389  +  assert( nLoop<=pWInfo->pTabList->nSrc );
       109390  +  WHERETRACE(0x002, ("---- begin solver\n"));
       109391  +
       109392  +  /* Allocate and initialize space for aTo and aFrom */
       109393  +  ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
       109394  +  pSpace = sqlite3DbMallocRaw(db, ii);
       109395  +  if( pSpace==0 ) return SQLITE_NOMEM;
       109396  +  aTo = (WherePath*)pSpace;
       109397  +  aFrom = aTo+mxChoice;
       109398  +  memset(aFrom, 0, sizeof(aFrom[0]));
       109399  +  pX = (WhereLoop**)(aFrom+mxChoice);
       109400  +  for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
       109401  +    pFrom->aLoop = pX;
       109402  +  }
       109403  +
       109404  +  /* Seed the search with a single WherePath containing zero WhereLoops */
       109405  +  aFrom[0].nRow = pWInfo->pParse->nQueryLoop;
       109406  +  nFrom = 1;
       109407  +
       109408  +  /* Precompute the cost of sorting the final result set, if the caller
       109409  +  ** to sqlite3WhereBegin() was concerned about sorting */
       109410  +  rSortCost = 0;
       109411  +  if( pWInfo->pOrderBy==0 || nRowEst==0 ){
       109412  +    aFrom[0].isOrderedValid = 1;
       109413  +  }else{
       109414  +    /* Compute an estimate on the cost to sort the entire result set */
       109415  +    rSortCost = nRowEst + estLog(nRowEst);
       109416  +    WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
       109417  +  }
       109418  +
       109419  +  /* Compute successively longer WherePaths using the previous generation
       109420  +  ** of WherePaths as the basis for the next.  Keep track of the mxChoice
       109421  +  ** best paths at each generation */
       109422  +  for(iLoop=0; iLoop<nLoop; iLoop++){
       109423  +    nTo = 0;
       109424  +    for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
       109425  +      for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
       109426  +        Bitmask maskNew;
       109427  +        Bitmask revMask = 0;
       109428  +        u8 isOrderedValid = pFrom->isOrderedValid;
       109429  +        u8 isOrdered = pFrom->isOrdered;
       109430  +        if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
       109431  +        if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
       109432  +        /* At this point, pWLoop is a candidate to be the next loop. 
       109433  +        ** Compute its cost */
       109434  +        rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
       109435  +        rCost = whereCostAdd(rCost, pFrom->rCost);
       109436  +        maskNew = pFrom->maskLoop | pWLoop->maskSelf;
       109437  +        if( !isOrderedValid ){
       109438  +          switch( wherePathSatisfiesOrderBy(pWInfo,
       109439  +                       pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
       109440  +                       iLoop, iLoop==nLoop-1, pWLoop, &revMask) ){
       109441  +            case 1:  /* Yes.  pFrom+pWLoop does satisfy the ORDER BY clause */
       109442  +              isOrdered = 1;
       109443  +              isOrderedValid = 1;
       109444  +              break;
       109445  +            case 0:  /* No.  pFrom+pWLoop will require a separate sort */
       109446  +              isOrdered = 0;
       109447  +              isOrderedValid = 1;
       109448  +              rCost = whereCostAdd(rCost, rSortCost);
       109449  +              break;
       109450  +            default: /* Cannot tell yet.  Try again on the next iteration */
       109451  +              break;
       109452  +          }
       109453  +        }else{
       109454  +          revMask = pFrom->revLoop;
       109455  +        }
       109456  +        /* Check to see if pWLoop should be added to the mxChoice best so far */
       109457  +        for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
       109458  +          if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){
       109459  +            break;
       109460  +          }
       109461  +        }
       109462  +        if( jj>=nTo ){
       109463  +          if( nTo>=mxChoice && rCost>=mxCost ){
       109464  +#ifdef WHERETRACE_ENABLED
       109465  +            if( sqlite3WhereTrace&0x4 ){
       109466  +              sqlite3DebugPrintf("Skip   %s cost=%3d order=%c\n",
       109467  +                  wherePathName(pFrom, iLoop, pWLoop), rCost,
       109468  +                  isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
       109469  +            }
       109470  +#endif
       109471  +            continue;
       109472  +          }
       109473  +          /* Add a new Path to the aTo[] set */
       109474  +          if( nTo<mxChoice ){
       109475  +            /* Increase the size of the aTo set by one */
       109476  +            jj = nTo++;
       109477  +          }else{
       109478  +            /* New path replaces the prior worst to keep count below mxChoice */
       109479  +            for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); }
       109480  +          }
       109481  +          pTo = &aTo[jj];
       109482  +#ifdef WHERETRACE_ENABLED
       109483  +          if( sqlite3WhereTrace&0x4 ){
       109484  +            sqlite3DebugPrintf("New    %s cost=%-3d order=%c\n",
       109485  +                wherePathName(pFrom, iLoop, pWLoop), rCost,
       109486  +                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
       109487  +          }
       109488  +#endif
       109489  +        }else{
       109490  +          if( pTo->rCost<=rCost ){
       109491  +#ifdef WHERETRACE_ENABLED
       109492  +            if( sqlite3WhereTrace&0x4 ){
       109493  +              sqlite3DebugPrintf(
       109494  +                  "Skip   %s cost=%-3d order=%c",
       109495  +                  wherePathName(pFrom, iLoop, pWLoop), rCost,
       109496  +                  isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
       109497  +              sqlite3DebugPrintf("   vs %s cost=%-3d order=%c\n",
       109498  +                  wherePathName(pTo, iLoop+1, 0), pTo->rCost,
       109499  +                  pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
       109500  +            }
       109501  +#endif
       109502  +            continue;
       109503  +          }
       109504  +          /* A new and better score for a previously created equivalent path */
       109505  +#ifdef WHERETRACE_ENABLED
       109506  +          if( sqlite3WhereTrace&0x4 ){
       109507  +            sqlite3DebugPrintf(
       109508  +                "Update %s cost=%-3d order=%c",
       109509  +                wherePathName(pFrom, iLoop, pWLoop), rCost,
       109510  +                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
       109511  +            sqlite3DebugPrintf("  was %s cost=%-3d order=%c\n",
       109512  +                wherePathName(pTo, iLoop+1, 0), pTo->rCost,
       109513  +                pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
       109514  +          }
       109515  +#endif
       109516  +        }
       109517  +        /* pWLoop is a winner.  Add it to the set of best so far */
       109518  +        pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
       109519  +        pTo->revLoop = revMask;
       109520  +        pTo->nRow = pFrom->nRow + pWLoop->nOut;
       109521  +        pTo->rCost = rCost;
       109522  +        pTo->isOrderedValid = isOrderedValid;
       109523  +        pTo->isOrdered = isOrdered;
       109524  +        memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
       109525  +        pTo->aLoop[iLoop] = pWLoop;
       109526  +        if( nTo>=mxChoice ){
       109527  +          mxCost = aTo[0].rCost;
       109528  +          for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
       109529  +            if( pTo->rCost>mxCost ) mxCost = pTo->rCost;
       109530  +          }
       109531  +        }
       109532  +      }
       109533  +    }
       109534  +
       109535  +#ifdef WHERETRACE_ENABLED
       109536  +    if( sqlite3WhereTrace>=2 ){
       109537  +      sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
       109538  +      for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
       109539  +        sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
       109540  +           wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
       109541  +           pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
       109542  +        if( pTo->isOrderedValid && pTo->isOrdered ){
       109543  +          sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
       109544  +        }else{
       109545  +          sqlite3DebugPrintf("\n");
       109546  +        }
       109547  +      }
       109548  +    }
       109549  +#endif
       109550  +
       109551  +    /* Swap the roles of aFrom and aTo for the next generation */
       109552  +    pFrom = aTo;
       109553  +    aTo = aFrom;
       109554  +    aFrom = pFrom;
       109555  +    nFrom = nTo;
       109556  +  }
       109557  +
       109558  +  if( nFrom==0 ){
       109559  +    sqlite3ErrorMsg(pWInfo->pParse, "no query solution");
       109560  +    sqlite3DbFree(db, pSpace);
       109561  +    return SQLITE_ERROR;
       109562  +  }
       109563  +  
       109564  +  /* Find the lowest cost path.  pFrom will be left pointing to that path */
       109565  +  pFrom = aFrom;
       109566  +  for(ii=1; ii<nFrom; ii++){
       109567  +    if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
       109568  +  }
       109569  +  assert( pWInfo->nLevel==nLoop );
       109570  +  /* Load the lowest cost path into pWInfo */
       109571  +  for(iLoop=0; iLoop<nLoop; iLoop++){
       109572  +    WhereLevel *pLevel = pWInfo->a + iLoop;
       109573  +    pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
       109574  +    pLevel->iFrom = pWLoop->iTab;
       109575  +    pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
       109576  +  }
       109577  +  if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 
       109578  +   && pWInfo->pDistinct
       109579  +   && nRowEst
       109580  +  ){
       109581  +    Bitmask notUsed;
       109582  +    int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinct, pFrom,
       109583  +                 WHERE_DISTINCTBY, nLoop-1, 1, pFrom->aLoop[nLoop-1], &notUsed);
       109584  +    if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
       109585  +  }
       109586  +  if( pFrom->isOrdered ){
       109587  +    if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
       109588  +      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
       109589  +    }else{
       109590  +      pWInfo->bOBSat = 1;
       109591  +      pWInfo->revMask = pFrom->revLoop;
       109592  +    }
       109593  +  }
       109594  +  pWInfo->nRowOut = pFrom->nRow;
       109595  +
       109596  +  /* Free temporary memory and return success */
       109597  +  sqlite3DbFree(db, pSpace);
       109598  +  return SQLITE_OK;
       109599  +}
       109600  +
       109601  +/*
       109602  +** Most queries use only a single table (they are not joins) and have
       109603  +** simple == constraints against indexed fields.  This routine attempts
       109604  +** to plan those simple cases using much less ceremony than the
       109605  +** general-purpose query planner, and thereby yield faster sqlite3_prepare()
       109606  +** times for the common case.
       109607  +**
       109608  +** Return non-zero on success, if this query can be handled by this
       109609  +** no-frills query planner.  Return zero if this query needs the 
       109610  +** general-purpose query planner.
       109611  +*/
       109612  +static int whereShortCut(WhereLoopBuilder *pBuilder){
       109613  +  WhereInfo *pWInfo;
       109614  +  struct SrcList_item *pItem;
       109615  +  WhereClause *pWC;
       109616  +  WhereTerm *pTerm;
       109617  +  WhereLoop *pLoop;
       109618  +  int iCur;
       109619  +  int j;
       109620  +  Table *pTab;
       109621  +  Index *pIdx;
       109622  +  
       109623  +  pWInfo = pBuilder->pWInfo;
       109624  +  if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
       109625  +  assert( pWInfo->pTabList->nSrc>=1 );
       109626  +  pItem = pWInfo->pTabList->a;
       109627  +  pTab = pItem->pTab;
       109628  +  if( IsVirtual(pTab) ) return 0;
       109629  +  if( pItem->zIndex ) return 0;
       109630  +  iCur = pItem->iCursor;
       109631  +  pWC = &pWInfo->sWC;
       109632  +  pLoop = pBuilder->pNew;
       109633  +  pLoop->wsFlags = 0;
       109634  +  pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
       109635  +  if( pTerm ){
       109636  +    pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
       109637  +    pLoop->aLTerm[0] = pTerm;
       109638  +    pLoop->nLTerm = 1;
       109639  +    pLoop->u.btree.nEq = 1;
       109640  +    pLoop->rRun = 33;  /* 33 == whereCostFromInt(10) */
       109641  +  }else{
       109642  +    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       109643  +      if( pIdx->onError==OE_None ) continue;
       109644  +      for(j=0; j<pIdx->nColumn; j++){
       109645  +        pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
       109646  +        if( pTerm==0 ) break;
       109647  +        whereLoopResize(pWInfo->pParse->db, pLoop, j);
       109648  +        pLoop->aLTerm[j] = pTerm;
       109649  +      }
       109650  +      if( j!=pIdx->nColumn ) continue;
       109651  +      pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
       109652  +      if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
       109653  +        pLoop->wsFlags |= WHERE_IDX_ONLY;
       109654  +      }
       109655  +      pLoop->nLTerm = j;
       109656  +      pLoop->u.btree.nEq = j;
       109657  +      pLoop->u.btree.pIndex = pIdx;
       109658  +      pLoop->rRun = 39;  /* 39 == whereCostFromInt(15) */
       109659  +      break;
       109660  +    }
       109661  +  }
       109662  +  if( pLoop->wsFlags ){
       109663  +    pLoop->nOut = (WhereCost)1;
       109664  +    pWInfo->a[0].pWLoop = pLoop;
       109665  +    pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
       109666  +    pWInfo->a[0].iTabCur = iCur;
       109667  +    pWInfo->nRowOut = 1;
       109668  +    if( pWInfo->pOrderBy ) pWInfo->bOBSat =  1;
       109669  +    if( pWInfo->pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
       109670  +#ifdef SQLITE_DEBUG
       109671  +    pLoop->cId = '0';
       109672  +#endif
       109673  +    return 1;
       109674  +  }
       109675  +  return 0;
       109676  +}
109336 109677   
109337 109678   /*
109338 109679   ** Generate the beginning of the loop used for WHERE clause processing.
109339 109680   ** The return value is a pointer to an opaque structure that contains
109340 109681   ** information needed to terminate the loop.  Later, the calling routine
109341 109682   ** should invoke sqlite3WhereEnd() with the return value of this function
109342 109683   ** in order to complete the WHERE clause processing.
................................................................................
109408 109749   **    end
109409 109750   **
109410 109751   ** ORDER BY CLAUSE PROCESSING
109411 109752   **
109412 109753   ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
109413 109754   ** if there is one.  If there is no ORDER BY clause or if this routine
109414 109755   ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
109415         -**
109416         -** If an index can be used so that the natural output order of the table
109417         -** scan is correct for the ORDER BY clause, then that index is used and
109418         -** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr.  This
109419         -** is an optimization that prevents an unnecessary sort of the result set
109420         -** if an index appropriate for the ORDER BY clause already exists.
109421         -**
109422         -** If the where clause loops cannot be arranged to provide the correct
109423         -** output order, then WhereInfo.nOBSat is 0.
109424 109756   */
109425 109757   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
109426 109758     Parse *pParse,        /* The parser context */
109427 109759     SrcList *pTabList,    /* A list of all tables to be scanned */
109428 109760     Expr *pWhere,         /* The WHERE clause */
109429 109761     ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
109430 109762     ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
................................................................................
109432 109764     int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
109433 109765   ){
109434 109766     int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
109435 109767     int nTabList;              /* Number of elements in pTabList */
109436 109768     WhereInfo *pWInfo;         /* Will become the return value of this function */
109437 109769     Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
109438 109770     Bitmask notReady;          /* Cursors that are not yet positioned */
109439         -  WhereBestIdx sWBI;         /* Best index search context */
       109771  +  WhereLoopBuilder sWLB;     /* The WhereLoop builder */
109440 109772     WhereMaskSet *pMaskSet;    /* The expression mask set */
109441 109773     WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
109442         -  int iFrom;                 /* First unused FROM clause element */
109443         -  int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
109444 109774     int ii;                    /* Loop counter */
109445 109775     sqlite3 *db;               /* Database connection */
       109776  +  int rc;                    /* Return code */
109446 109777   
109447 109778   
109448 109779     /* Variable initialization */
109449         -  memset(&sWBI, 0, sizeof(sWBI));
109450         -  sWBI.pParse = pParse;
       109780  +  memset(&sWLB, 0, sizeof(sWLB));
       109781  +  sWLB.pOrderBy = pOrderBy;
109451 109782   
109452 109783     /* The number of tables in the FROM clause is limited by the number of
109453 109784     ** bits in a Bitmask 
109454 109785     */
109455 109786     testcase( pTabList->nSrc==BMS );
109456 109787     if( pTabList->nSrc>BMS ){
109457 109788       sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
................................................................................
109470 109801     ** struct, the contents of WhereInfo.a[], the WhereClause structure
109471 109802     ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
109472 109803     ** field (type Bitmask) it must be aligned on an 8-byte boundary on
109473 109804     ** some architectures. Hence the ROUND8() below.
109474 109805     */
109475 109806     db = pParse->db;
109476 109807     nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
109477         -  pWInfo = sqlite3DbMallocZero(db, 
109478         -      nByteWInfo + 
109479         -      sizeof(WhereClause) +
109480         -      sizeof(WhereMaskSet)
109481         -  );
       109808  +  pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop));
109482 109809     if( db->mallocFailed ){
109483 109810       sqlite3DbFree(db, pWInfo);
109484 109811       pWInfo = 0;
109485 109812       goto whereBeginError;
109486 109813     }
109487 109814     pWInfo->nLevel = nTabList;
109488 109815     pWInfo->pParse = pParse;
109489 109816     pWInfo->pTabList = pTabList;
       109817  +  pWInfo->pOrderBy = pOrderBy;
       109818  +  pWInfo->pDistinct = pDistinct;
109490 109819     pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
109491         -  pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
109492 109820     pWInfo->wctrlFlags = wctrlFlags;
109493 109821     pWInfo->savedNQueryLoop = pParse->nQueryLoop;
109494         -  pMaskSet = (WhereMaskSet*)&sWBI.pWC[1];
109495         -  sWBI.aLevel = pWInfo->a;
       109822  +  pMaskSet = &pWInfo->sMaskSet;
       109823  +  sWLB.pWInfo = pWInfo;
       109824  +  sWLB.pWC = &pWInfo->sWC;
       109825  +  sWLB.pNew = (WhereLoop*)&pWInfo->a[nTabList];
       109826  +  whereLoopInit(sWLB.pNew);
       109827  +#ifdef SQLITE_DEBUG
       109828  +  sWLB.pNew->cId = '*';
       109829  +#endif
109496 109830   
109497 109831     /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
109498 109832     ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
109499 109833     if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
109500 109834   
109501 109835     /* Split the WHERE clause into separate subexpressions where each
109502 109836     ** subexpression is separated by an AND operator.
109503 109837     */
109504 109838     initMaskSet(pMaskSet);
109505         -  whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags);
       109839  +  whereClauseInit(&pWInfo->sWC, pWInfo);
109506 109840     sqlite3ExprCodeConstants(pParse, pWhere);
109507         -  whereSplit(sWBI.pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
       109841  +  whereSplit(&pWInfo->sWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
109508 109842       
109509 109843     /* Special case: a WHERE clause that is constant.  Evaluate the
109510 109844     ** expression and either jump over all of the code or fall thru.
109511 109845     */
109512 109846     if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
109513 109847       sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
109514 109848       pWhere = 0;
109515 109849     }
       109850  +
       109851  +  /* Special case: No FROM clause
       109852  +  */
       109853  +  if( nTabList==0 ){
       109854  +    if( pOrderBy ) pWInfo->bOBSat = 1;
       109855  +    if( pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
       109856  +  }
109516 109857   
109517 109858     /* Assign a bit from the bitmask to every term in the FROM clause.
109518 109859     **
109519 109860     ** When assigning bitmask values to FROM clause cursors, it must be
109520 109861     ** the case that if X is the bitmask for the N-th FROM clause term then
109521 109862     ** the bitmask for all FROM clause terms to the left of the N-th term
109522 109863     ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
................................................................................
109545 109886   #endif
109546 109887   
109547 109888     /* Analyze all of the subexpressions.  Note that exprAnalyze() might
109548 109889     ** add new virtual terms onto the end of the WHERE clause.  We do not
109549 109890     ** want to analyze these virtual terms, so start analyzing at the end
109550 109891     ** and work forward so that the added virtual terms are never processed.
109551 109892     */
109552         -  exprAnalyzeAll(pTabList, sWBI.pWC);
       109893  +  exprAnalyzeAll(pTabList, &pWInfo->sWC);
109553 109894     if( db->mallocFailed ){
109554 109895       goto whereBeginError;
109555 109896     }
       109897  +
       109898  +  /* If the ORDER BY (or GROUP BY) clause contains references to general
       109899  +  ** expressions, then we won't be able to satisfy it using indices, so
       109900  +  ** go ahead and disable it now.
       109901  +  */
       109902  +  if( pOrderBy && pDistinct ){
       109903  +    for(ii=0; ii<pOrderBy->nExpr; ii++){
       109904  +      Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr);
       109905  +      if( pExpr->op!=TK_COLUMN ){
       109906  +        pWInfo->pOrderBy = pOrderBy = 0;
       109907  +        break;
       109908  +      }else if( pExpr->iColumn<0 ){
       109909  +        break;
       109910  +      }
       109911  +    }
       109912  +  }
109556 109913   
109557 109914     /* Check if the DISTINCT qualifier, if there is one, is redundant. 
109558 109915     ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
109559 109916     ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
109560 109917     */
109561         -  if( pDistinct && isDistinctRedundant(pParse, pTabList, sWBI.pWC, pDistinct) ){
109562         -    pDistinct = 0;
109563         -    pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
109564         -  }
109565         -
109566         -  /* Chose the best index to use for each table in the FROM clause.
109567         -  **
109568         -  ** This loop fills in the following fields:
109569         -  **
109570         -  **   pWInfo->a[].pIdx      The index to use for this level of the loop.
109571         -  **   pWInfo->a[].wsFlags   WHERE_xxx flags associated with pIdx
109572         -  **   pWInfo->a[].nEq       The number of == and IN constraints
109573         -  **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
109574         -  **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
109575         -  **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
109576         -  **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
109577         -  **
109578         -  ** This loop also figures out the nesting order of tables in the FROM
109579         -  ** clause.
109580         -  */
109581         -  sWBI.notValid = ~(Bitmask)0;
109582         -  sWBI.pOrderBy = pOrderBy;
109583         -  sWBI.n = nTabList;
109584         -  sWBI.pDistinct = pDistinct;
109585         -  andFlags = ~0;
109586         -  WHERETRACE(("*** Optimizer Start ***\n"));
109587         -  for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
109588         -    WhereCost bestPlan;         /* Most efficient plan seen so far */
109589         -    Index *pIdx;                /* Index for FROM table at pTabItem */
109590         -    int j;                      /* For looping over FROM tables */
109591         -    int bestJ = -1;             /* The value of j */
109592         -    Bitmask m;                  /* Bitmask value for j or bestJ */
109593         -    int isOptimal;              /* Iterator for optimal/non-optimal search */
109594         -    int ckOptimal;              /* Do the optimal scan check */
109595         -    int nUnconstrained;         /* Number tables without INDEXED BY */
109596         -    Bitmask notIndexed;         /* Mask of tables that cannot use an index */
109597         -
109598         -    memset(&bestPlan, 0, sizeof(bestPlan));
109599         -    bestPlan.rCost = SQLITE_BIG_DBL;
109600         -    WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
109601         -
109602         -    /* Loop through the remaining entries in the FROM clause to find the
109603         -    ** next nested loop. The loop tests all FROM clause entries
109604         -    ** either once or twice. 
109605         -    **
109606         -    ** The first test is always performed if there are two or more entries
109607         -    ** remaining and never performed if there is only one FROM clause entry
109608         -    ** to choose from.  The first test looks for an "optimal" scan.  In
109609         -    ** this context an optimal scan is one that uses the same strategy
109610         -    ** for the given FROM clause entry as would be selected if the entry
109611         -    ** were used as the innermost nested loop.  In other words, a table
109612         -    ** is chosen such that the cost of running that table cannot be reduced
109613         -    ** by waiting for other tables to run first.  This "optimal" test works
109614         -    ** by first assuming that the FROM clause is on the inner loop and finding
109615         -    ** its query plan, then checking to see if that query plan uses any
109616         -    ** other FROM clause terms that are sWBI.notValid.  If no notValid terms
109617         -    ** are used then the "optimal" query plan works.
109618         -    **
109619         -    ** Note that the WhereCost.nRow parameter for an optimal scan might
109620         -    ** not be as small as it would be if the table really were the innermost
109621         -    ** join.  The nRow value can be reduced by WHERE clause constraints
109622         -    ** that do not use indices.  But this nRow reduction only happens if the
109623         -    ** table really is the innermost join.  
109624         -    **
109625         -    ** The second loop iteration is only performed if no optimal scan
109626         -    ** strategies were found by the first iteration. This second iteration
109627         -    ** is used to search for the lowest cost scan overall.
109628         -    **
109629         -    ** Without the optimal scan step (the first iteration) a suboptimal
109630         -    ** plan might be chosen for queries like this:
109631         -    **   
109632         -    **   CREATE TABLE t1(a, b); 
109633         -    **   CREATE TABLE t2(c, d);
109634         -    **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
109635         -    **
109636         -    ** The best strategy is to iterate through table t1 first. However it
109637         -    ** is not possible to determine this with a simple greedy algorithm.
109638         -    ** Since the cost of a linear scan through table t2 is the same 
109639         -    ** as the cost of a linear scan through table t1, a simple greedy 
109640         -    ** algorithm may choose to use t2 for the outer loop, which is a much
109641         -    ** costlier approach.
109642         -    */
109643         -    nUnconstrained = 0;
109644         -    notIndexed = 0;
109645         -
109646         -    /* The optimal scan check only occurs if there are two or more tables
109647         -    ** available to be reordered */
109648         -    if( iFrom==nTabList-1 ){
109649         -      ckOptimal = 0;  /* Common case of just one table in the FROM clause */
109650         -    }else{
109651         -      ckOptimal = -1;
109652         -      for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
109653         -        m = getMask(pMaskSet, sWBI.pSrc->iCursor);
109654         -        if( (m & sWBI.notValid)==0 ){
109655         -          if( j==iFrom ) iFrom++;
109656         -          continue;
109657         -        }
109658         -        if( j>iFrom && (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0 ) break;
109659         -        if( ++ckOptimal ) break;
109660         -        if( (sWBI.pSrc->jointype & JT_LEFT)!=0 ) break;
109661         -      }
109662         -    }
109663         -    assert( ckOptimal==0 || ckOptimal==1 );
109664         -
109665         -    for(isOptimal=ckOptimal; isOptimal>=0 && bestJ<0; isOptimal--){
109666         -      for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
109667         -        if( j>iFrom && (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0 ){
109668         -          /* This break and one like it in the ckOptimal computation loop
109669         -          ** above prevent table reordering across LEFT and CROSS JOINs.
109670         -          ** The LEFT JOIN case is necessary for correctness.  The prohibition
109671         -          ** against reordering across a CROSS JOIN is an SQLite feature that
109672         -          ** allows the developer to control table reordering */
109673         -          break;
109674         -        }
109675         -        m = getMask(pMaskSet, sWBI.pSrc->iCursor);
109676         -        if( (m & sWBI.notValid)==0 ){
109677         -          assert( j>iFrom );
109678         -          continue;
109679         -        }
109680         -        sWBI.notReady = (isOptimal ? m : sWBI.notValid);
109681         -        if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
109682         -  
109683         -        WHERETRACE(("   === trying table %d (%s) with isOptimal=%d ===\n",
109684         -                    j, sWBI.pSrc->pTab->zName, isOptimal));
109685         -        assert( sWBI.pSrc->pTab );
109686         -#ifndef SQLITE_OMIT_VIRTUALTABLE
109687         -        if( IsVirtual(sWBI.pSrc->pTab) ){
109688         -          sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
109689         -          bestVirtualIndex(&sWBI);
109690         -        }else 
109691         -#endif
109692         -        {
109693         -          bestBtreeIndex(&sWBI);
109694         -        }
109695         -        assert( isOptimal || (sWBI.cost.used&sWBI.notValid)==0 );
109696         -
109697         -        /* If an INDEXED BY clause is present, then the plan must use that
109698         -        ** index if it uses any index at all */
109699         -        assert( sWBI.pSrc->pIndex==0 
109700         -                  || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
109701         -                  || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
109702         -
109703         -        if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
109704         -          notIndexed |= m;
109705         -        }
109706         -        if( isOptimal ){
109707         -          pWInfo->a[j].rOptCost = sWBI.cost.rCost;
109708         -        }else if( ckOptimal ){
109709         -          /* If two or more tables have nearly the same outer loop cost, but
109710         -          ** very different inner loop (optimal) cost, we want to choose
109711         -          ** for the outer loop that table which benefits the least from
109712         -          ** being in the inner loop.  The following code scales the 
109713         -          ** outer loop cost estimate to accomplish that. */
109714         -          WHERETRACE(("   scaling cost from %.1f to %.1f\n",
109715         -                      sWBI.cost.rCost,
109716         -                      sWBI.cost.rCost/pWInfo->a[j].rOptCost));
109717         -          sWBI.cost.rCost /= pWInfo->a[j].rOptCost;
109718         -        }
109719         -
109720         -        /* Conditions under which this table becomes the best so far:
109721         -        **
109722         -        **   (1) The table must not depend on other tables that have not
109723         -        **       yet run.  (In other words, it must not depend on tables
109724         -        **       in inner loops.)
109725         -        **
109726         -        **   (2) (This rule was removed on 2012-11-09.  The scaling of the
109727         -        **       cost using the optimal scan cost made this rule obsolete.)
109728         -        **
109729         -        **   (3) All tables have an INDEXED BY clause or this table lacks an
109730         -        **       INDEXED BY clause or this table uses the specific
109731         -        **       index specified by its INDEXED BY clause.  This rule ensures
109732         -        **       that a best-so-far is always selected even if an impossible
109733         -        **       combination of INDEXED BY clauses are given.  The error
109734         -        **       will be detected and relayed back to the application later.
109735         -        **       The NEVER() comes about because rule (2) above prevents
109736         -        **       An indexable full-table-scan from reaching rule (3).
109737         -        **
109738         -        **   (4) The plan cost must be lower than prior plans, where "cost"
109739         -        **       is defined by the compareCost() function above. 
109740         -        */
109741         -        if( (sWBI.cost.used&sWBI.notValid)==0                    /* (1) */
109742         -            && (nUnconstrained==0 || sWBI.pSrc->pIndex==0        /* (3) */
109743         -                || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
109744         -            && (bestJ<0 || compareCost(&sWBI.cost, &bestPlan))   /* (4) */
109745         -        ){
109746         -          WHERETRACE(("   === table %d (%s) is best so far\n"
109747         -                      "       cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=%08x\n",
109748         -                      j, sWBI.pSrc->pTab->zName,
109749         -                      sWBI.cost.rCost, sWBI.cost.plan.nRow,
109750         -                      sWBI.cost.plan.nOBSat, sWBI.cost.plan.wsFlags));
109751         -          bestPlan = sWBI.cost;
109752         -          bestJ = j;
109753         -        }
109754         -
109755         -        /* In a join like "w JOIN x LEFT JOIN y JOIN z"  make sure that
109756         -        ** table y (and not table z) is always the next inner loop inside
109757         -        ** of table x. */
109758         -        if( (sWBI.pSrc->jointype & JT_LEFT)!=0 ) break;
109759         -      }
109760         -    }
109761         -    assert( bestJ>=0 );
109762         -    assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
109763         -    assert( bestJ==iFrom || (pTabList->a[iFrom].jointype & JT_LEFT)==0 );
109764         -    testcase( bestJ>iFrom && (pTabList->a[iFrom].jointype & JT_CROSS)!=0 );
109765         -    testcase( bestJ>iFrom && bestJ<nTabList-1
109766         -                          && (pTabList->a[bestJ+1].jointype & JT_LEFT)!=0 );
109767         -    WHERETRACE(("*** Optimizer selects table %d (%s) for loop %d with:\n"
109768         -                "    cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=0x%08x\n",
109769         -                bestJ, pTabList->a[bestJ].pTab->zName,
109770         -                pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
109771         -                bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
109772         -    if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
109773         -      assert( pWInfo->eDistinct==0 );
109774         -      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
109775         -    }
109776         -    andFlags &= bestPlan.plan.wsFlags;
109777         -    pLevel->plan = bestPlan.plan;
109778         -    pLevel->iTabCur = pTabList->a[bestJ].iCursor;
109779         -    testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
109780         -    testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
109781         -    if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
109782         -      if( (wctrlFlags & WHERE_ONETABLE_ONLY) 
109783         -       && (bestPlan.plan.wsFlags & WHERE_TEMP_INDEX)==0 
109784         -      ){
109785         -        pLevel->iIdxCur = iIdxCur;
109786         -      }else{
109787         -        pLevel->iIdxCur = pParse->nTab++;
109788         -      }
109789         -    }else{
109790         -      pLevel->iIdxCur = -1;
109791         -    }
109792         -    sWBI.notValid &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
109793         -    pLevel->iFrom = (u8)bestJ;
109794         -    if( bestPlan.plan.nRow>=(double)1 ){
109795         -      pParse->nQueryLoop *= bestPlan.plan.nRow;
109796         -    }
109797         -
109798         -    /* Check that if the table scanned by this loop iteration had an
109799         -    ** INDEXED BY clause attached to it, that the named index is being
109800         -    ** used for the scan. If not, then query compilation has failed.
109801         -    ** Return an error.
109802         -    */
109803         -    pIdx = pTabList->a[bestJ].pIndex;
109804         -    if( pIdx ){
109805         -      if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
109806         -        sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
109807         -        goto whereBeginError;
109808         -      }else{
109809         -        /* If an INDEXED BY clause is used, the bestIndex() function is
109810         -        ** guaranteed to find the index specified in the INDEXED BY clause
109811         -        ** if it find an index at all. */
109812         -        assert( bestPlan.plan.u.pIdx==pIdx );
109813         -      }
109814         -    }
109815         -  }
109816         -  WHERETRACE(("*** Optimizer Finished ***\n"));
       109918  +  if( pDistinct ){
       109919  +    if( isDistinctRedundant(pParse,pTabList,&pWInfo->sWC,pDistinct) ){
       109920  +      pDistinct = 0;
       109921  +      pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
       109922  +    }else if( pOrderBy==0 ){
       109923  +      pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
       109924  +      pWInfo->pOrderBy = pDistinct;
       109925  +    }
       109926  +  }
       109927  +
       109928  +  /* Construct the WhereLoop objects */
       109929  +  WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
       109930  +  if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
       109931  +    rc = whereLoopAddAll(&sWLB);
       109932  +    if( rc ) goto whereBeginError;
       109933  +  
       109934  +    /* Display all of the WhereLoop objects if wheretrace is enabled */
       109935  +#ifdef WHERETRACE_ENABLED
       109936  +    if( sqlite3WhereTrace ){
       109937  +      WhereLoop *p;
       109938  +      int i = 0;
       109939  +      static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
       109940  +                                       "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
       109941  +      for(p=pWInfo->pLoops; p; p=p->pNextLoop){
       109942  +        p->cId = zLabel[(i++)%sizeof(zLabel)];
       109943  +        whereLoopPrint(p, pTabList);
       109944  +      }
       109945  +    }
       109946  +#endif
       109947  +  
       109948  +    wherePathSolver(pWInfo, 0);
       109949  +    if( db->mallocFailed ) goto whereBeginError;
       109950  +    if( pWInfo->pOrderBy ){
       109951  +       wherePathSolver(pWInfo, pWInfo->nRowOut);
       109952  +       if( db->mallocFailed ) goto whereBeginError;
       109953  +    }
       109954  +  }
       109955  +  if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
       109956  +     pWInfo->revMask = (Bitmask)(-1);
       109957  +  }
109817 109958     if( pParse->nErr || db->mallocFailed ){
109818 109959       goto whereBeginError;
109819 109960     }
109820         -  if( nTabList ){
109821         -    pLevel--;
109822         -    pWInfo->nOBSat = pLevel->plan.nOBSat;
109823         -  }else{
109824         -    pWInfo->nOBSat = 0;
109825         -  }
109826         -
109827         -  /* If the total query only selects a single row, then the ORDER BY
109828         -  ** clause is irrelevant.
109829         -  */
109830         -  if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
109831         -    assert( nTabList==0 || (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 );
109832         -    pWInfo->nOBSat = pOrderBy->nExpr;
109833         -  }
       109961  +#ifdef WHERETRACE_ENABLED
       109962  +  if( sqlite3WhereTrace ){
       109963  +    int ii;
       109964  +    sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
       109965  +    if( pWInfo->bOBSat ){
       109966  +      sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask);
       109967  +    }
       109968  +    switch( pWInfo->eDistinct ){
       109969  +      case WHERE_DISTINCT_UNIQUE: {
       109970  +        sqlite3DebugPrintf("  DISTINCT=unique");
       109971  +        break;
       109972  +      }
       109973  +      case WHERE_DISTINCT_ORDERED: {
       109974  +        sqlite3DebugPrintf("  DISTINCT=ordered");
       109975  +        break;
       109976  +      }
       109977  +      case WHERE_DISTINCT_UNORDERED: {
       109978  +        sqlite3DebugPrintf("  DISTINCT=unordered");
       109979  +        break;
       109980  +      }
       109981  +    }
       109982  +    sqlite3DebugPrintf("\n");
       109983  +    for(ii=0; ii<nTabList; ii++){
       109984  +      whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList);
       109985  +    }
       109986  +  }
       109987  +#endif
       109988  +  WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
       109989  +  pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
109834 109990   
109835 109991     /* If the caller is an UPDATE or DELETE statement that is requesting
109836 109992     ** to use a one-pass algorithm, determine if this is appropriate.
109837 109993     ** The one-pass algorithm only works if the WHERE clause constraints
109838 109994     ** the statement to update a single row.
109839 109995     */
109840 109996     assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
109841         -  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
       109997  +  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 
       109998  +   && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
109842 109999       pWInfo->okOnePass = 1;
109843         -    pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
       110000  +    pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
109844 110001     }
109845 110002   
109846 110003     /* Open all tables in the pTabList and any indices selected for
109847 110004     ** searching those tables.
109848 110005     */
109849 110006     sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
109850 110007     notReady = ~(Bitmask)0;
109851         -  pWInfo->nRowOut = (double)1;
       110008  +  pWInfo->nRowOut = (WhereCost)1;
109852 110009     for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
109853 110010       Table *pTab;     /* Table to open */
109854 110011       int iDb;         /* Index of database containing table/index */
109855 110012       struct SrcList_item *pTabItem;
       110013  +    WhereLoop *pLoop;
109856 110014   
109857 110015       pTabItem = &pTabList->a[pLevel->iFrom];
109858 110016       pTab = pTabItem->pTab;
109859         -    pWInfo->nRowOut *= pLevel->plan.nRow;
109860 110017       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       110018  +    pLoop = pLevel->pWLoop;
109861 110019       if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
109862 110020         /* Do nothing */
109863 110021       }else
109864 110022   #ifndef SQLITE_OMIT_VIRTUALTABLE
109865         -    if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
       110023  +    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
109866 110024         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
109867 110025         int iCur = pTabItem->iCursor;
109868 110026         sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
109869 110027       }else if( IsVirtual(pTab) ){
109870 110028         /* noop */
109871 110029       }else
109872 110030   #endif
109873         -    if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
       110031  +    if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
109874 110032            && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
109875 110033         int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
109876 110034         sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
109877 110035         testcase( pTab->nCol==BMS-1 );
109878 110036         testcase( pTab->nCol==BMS );
109879 110037         if( !pWInfo->okOnePass && pTab->nCol<BMS ){
109880 110038           Bitmask b = pTabItem->colUsed;
................................................................................
109884 110042                               SQLITE_INT_TO_PTR(n), P4_INT32);
109885 110043           assert( n<=pTab->nCol );
109886 110044         }
109887 110045       }else{
109888 110046         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
109889 110047       }
109890 110048   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
109891         -    if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
109892         -      constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel);
       110049  +    if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){
       110050  +      constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel);
109893 110051       }else
109894 110052   #endif
109895         -    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
109896         -      Index *pIx = pLevel->plan.u.pIdx;
       110053  +    if( pLoop->wsFlags & WHERE_INDEXED ){
       110054  +      Index *pIx = pLoop->u.btree.pIndex;
109897 110055         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
109898         -      int iIndexCur = pLevel->iIdxCur;
       110056  +      /* FIXME:  As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */
       110057  +      int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++;
109899 110058         assert( pIx->pSchema==pTab->pSchema );
109900 110059         assert( iIndexCur>=0 );
109901 110060         sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
109902 110061                           (char*)pKey, P4_KEYINFO_HANDOFF);
109903 110062         VdbeComment((v, "%s", pIx->zName));
109904 110063       }
109905 110064       sqlite3CodeVerifySchema(pParse, iDb);
109906         -    notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor);
       110065  +    notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
109907 110066     }
109908 110067     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
109909 110068     if( db->mallocFailed ) goto whereBeginError;
109910 110069   
109911 110070     /* Generate the code to do the search.  Each iteration of the for
109912 110071     ** loop below generates code for a single nested loop of the VM
109913 110072     ** program.
109914 110073     */
109915 110074     notReady = ~(Bitmask)0;
109916 110075     for(ii=0; ii<nTabList; ii++){
109917 110076       pLevel = &pWInfo->a[ii];
109918 110077       explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
109919         -    notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
       110078  +    notReady = codeOneLoopStart(pWInfo, ii, notReady);
109920 110079       pWInfo->iContinue = pLevel->addrCont;
109921 110080     }
109922 110081   
109923         -#ifdef SQLITE_TEST  /* For testing and debugging use only */
109924         -  /* Record in the query plan information about the current table
109925         -  ** and the index used to access it (if any).  If the table itself
109926         -  ** is not used, its name is just '{}'.  If no index is used
109927         -  ** the index is listed as "{}".  If the primary key is used the
109928         -  ** index name is '*'.
109929         -  */
109930         -  for(ii=0; ii<nTabList; ii++){
109931         -    char *z;
109932         -    int n;
109933         -    int w;
109934         -    struct SrcList_item *pTabItem;
109935         -
109936         -    pLevel = &pWInfo->a[ii];
109937         -    w = pLevel->plan.wsFlags;
109938         -    pTabItem = &pTabList->a[pLevel->iFrom];
109939         -    z = pTabItem->zAlias;
109940         -    if( z==0 ) z = pTabItem->pTab->zName;
109941         -    n = sqlite3Strlen30(z);
109942         -    if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
109943         -      if( (w & WHERE_IDX_ONLY)!=0 && (w & WHERE_COVER_SCAN)==0 ){
109944         -        memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
109945         -        nQPlan += 2;
109946         -      }else{
109947         -        memcpy(&sqlite3_query_plan[nQPlan], z, n);
109948         -        nQPlan += n;
109949         -      }
109950         -      sqlite3_query_plan[nQPlan++] = ' ';
109951         -    }
109952         -    testcase( w & WHERE_ROWID_EQ );
109953         -    testcase( w & WHERE_ROWID_RANGE );
109954         -    if( w & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
109955         -      memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
109956         -      nQPlan += 2;
109957         -    }else if( (w & WHERE_INDEXED)!=0 && (w & WHERE_COVER_SCAN)==0 ){
109958         -      n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
109959         -      if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
109960         -        memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
109961         -        nQPlan += n;
109962         -        sqlite3_query_plan[nQPlan++] = ' ';
109963         -      }
109964         -    }else{
109965         -      memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
109966         -      nQPlan += 3;
109967         -    }
109968         -  }
109969         -  while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
109970         -    sqlite3_query_plan[--nQPlan] = 0;
109971         -  }
109972         -  sqlite3_query_plan[nQPlan] = 0;
109973         -  nQPlan = 0;
109974         -#endif /* SQLITE_TEST // Testing and debugging use only */
109975         -
109976         -  /* Record the continuation address in the WhereInfo structure.  Then
109977         -  ** clean up and return.
109978         -  */
       110082  +  /* Done. */
109979 110083     return pWInfo;
109980 110084   
109981 110085     /* Jump here if malloc fails */
109982 110086   whereBeginError:
109983 110087     if( pWInfo ){
109984 110088       pParse->nQueryLoop = pWInfo->savedNQueryLoop;
109985 110089       whereInfoFree(db, pWInfo);
................................................................................
109992 110096   ** sqlite3WhereBegin() for additional information.
109993 110097   */
109994 110098   SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
109995 110099     Parse *pParse = pWInfo->pParse;
109996 110100     Vdbe *v = pParse->pVdbe;
109997 110101     int i;
109998 110102     WhereLevel *pLevel;
       110103  +  WhereLoop *pLoop;
109999 110104     SrcList *pTabList = pWInfo->pTabList;
110000 110105     sqlite3 *db = pParse->db;
110001 110106   
110002 110107     /* Generate loop termination code.
110003 110108     */
110004 110109     sqlite3ExprCacheClear(pParse);
110005 110110     for(i=pWInfo->nLevel-1; i>=0; i--){
110006 110111       pLevel = &pWInfo->a[i];
       110112  +    pLoop = pLevel->pWLoop;
110007 110113       sqlite3VdbeResolveLabel(v, pLevel->addrCont);
110008 110114       if( pLevel->op!=OP_Noop ){
110009 110115         sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
110010 110116         sqlite3VdbeChangeP5(v, pLevel->p5);
110011 110117       }
110012         -    if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
       110118  +    if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
110013 110119         struct InLoop *pIn;
110014 110120         int j;
110015 110121         sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
110016 110122         for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
110017 110123           sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
110018 110124           sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
110019 110125           sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
................................................................................
110020 110126         }
110021 110127         sqlite3DbFree(db, pLevel->u.in.aInLoop);
110022 110128       }
110023 110129       sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
110024 110130       if( pLevel->iLeftJoin ){
110025 110131         int addr;
110026 110132         addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
110027         -      assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
110028         -           || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 );
110029         -      if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
       110133  +      assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
       110134  +           || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
       110135  +      if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
110030 110136           sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
110031 110137         }
110032         -      if( pLevel->iIdxCur>=0 ){
       110138  +      if( pLoop->wsFlags & WHERE_INDEXED ){
110033 110139           sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
110034 110140         }
110035 110141         if( pLevel->op==OP_Return ){
110036 110142           sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
110037 110143         }else{
110038 110144           sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
110039 110145         }
................................................................................
110050 110156     */
110051 110157     assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
110052 110158     for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
110053 110159       Index *pIdx = 0;
110054 110160       struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
110055 110161       Table *pTab = pTabItem->pTab;
110056 110162       assert( pTab!=0 );
       110163  +    pLoop = pLevel->pWLoop;
110057 110164       if( (pTab->tabFlags & TF_Ephemeral)==0
110058 110165        && pTab->pSelect==0
110059 110166        && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
110060 110167       ){
110061         -      int ws = pLevel->plan.wsFlags;
       110168  +      int ws = pLoop->wsFlags;
110062 110169         if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
110063 110170           sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
110064 110171         }
110065         -      if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
       110172  +      if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_TEMP_INDEX))==0 ){
110066 110173           sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
110067 110174         }
110068 110175       }
110069 110176   
110070 110177       /* If this scan uses an index, make code substitutions to read data
110071 110178       ** from the index in preference to the table. Sometimes, this means
110072 110179       ** the table need never be read from. This is a performance boost,
................................................................................
110076 110183       ** 
110077 110184       ** Calls to the code generator in between sqlite3WhereBegin and
110078 110185       ** sqlite3WhereEnd will have created code that references the table
110079 110186       ** directly.  This loop scans all that code looking for opcodes
110080 110187       ** that reference the table and converts them into opcodes that
110081 110188       ** reference the index.
110082 110189       */
110083         -    if( pLevel->plan.wsFlags & WHERE_INDEXED ){
110084         -      pIdx = pLevel->plan.u.pIdx;
110085         -    }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
       110190  +    if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
       110191  +      pIdx = pLoop->u.btree.pIndex;
       110192  +    }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
110086 110193         pIdx = pLevel->u.pCovidx;
110087 110194       }
110088         -    if( pIdx && !db->mallocFailed){
       110195  +    if( pIdx && !db->mallocFailed ){
110089 110196         int k, j, last;
110090 110197         VdbeOp *pOp;
110091 110198   
110092 110199         pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
110093 110200         last = sqlite3VdbeCurrentAddr(v);
110094 110201         for(k=pWInfo->iTop; k<last; k++, pOp++){
110095 110202           if( pOp->p1!=pLevel->iTabCur ) continue;
................................................................................
110097 110204             for(j=0; j<pIdx->nColumn; j++){
110098 110205               if( pOp->p2==pIdx->aiColumn[j] ){
110099 110206                 pOp->p2 = j;
110100 110207                 pOp->p1 = pLevel->iIdxCur;
110101 110208                 break;
110102 110209               }
110103 110210             }
110104         -          assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
110105         -               || j<pIdx->nColumn );
       110211  +          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn );
110106 110212           }else if( pOp->opcode==OP_Rowid ){
110107 110213             pOp->p1 = pLevel->iIdxCur;
110108 110214             pOp->opcode = OP_IdxRowid;
110109 110215           }
110110 110216         }
110111 110217       }
110112 110218     }
................................................................................
115478 115584     }
115479 115585     return rc;
115480 115586   }
115481 115587   
115482 115588   /*
115483 115589   ** Another built-in collating sequence: NOCASE. 
115484 115590   **
115485         -** This collating sequence is intended to be used for "case independant
       115591  +** This collating sequence is intended to be used for "case independent
115486 115592   ** comparison". SQLite's knowledge of upper and lower case equivalents
115487 115593   ** extends only to the 26 characters used in the English language.
115488 115594   **
115489 115595   ** At the moment there is only a UTF-8 implementation.
115490 115596   */
115491 115597   static int nocaseCollatingFunc(
115492 115598     void *NotUsed,
................................................................................
115801 115907     for(i=0; i<db->nDb; i++){
115802 115908       Btree *p = db->aDb[i].pBt;
115803 115909       if( p ){
115804 115910         if( sqlite3BtreeIsInTrans(p) ){
115805 115911           inTrans = 1;
115806 115912         }
115807 115913         sqlite3BtreeRollback(p, tripCode);
115808         -      db->aDb[i].inTrans = 0;
115809 115914       }
115810 115915     }
115811 115916     sqlite3VtabRollback(db);
115812 115917     sqlite3EndBenignMalloc();
115813 115918   
115814 115919     if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
115815 115920       sqlite3ExpirePreparedStatements(db);
................................................................................
117568 117673   #endif
117569 117674   
117570 117675   /*
117571 117676   ** Test to see whether or not the database connection is in autocommit
117572 117677   ** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
117573 117678   ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
117574 117679   ** by the next COMMIT or ROLLBACK.
117575         -**
117576         -******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
117577 117680   */
117578 117681   SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
117579 117682     return db->autoCommit;
117580 117683   }
117581 117684   
117582 117685   /*
117583 117686   ** The following routines are subtitutes for constants SQLITE_CORRUPT,
................................................................................
119057 119160   */
119058 119161   #define fts3HashCount(H)  ((H)->count)
119059 119162   
119060 119163   #endif /* _FTS3_HASH_H_ */
119061 119164   
119062 119165   /************** End of fts3_hash.h *******************************************/
119063 119166   /************** Continuing where we left off in fts3Int.h ********************/
       119167  +
       119168  +/*
       119169  +** This constant determines the maximum depth of an FTS expression tree
       119170  +** that the library will create and use. FTS uses recursion to perform 
       119171  +** various operations on the query tree, so the disadvantage of a large
       119172  +** limit is that it may allow very large queries to use large amounts
       119173  +** of stack space (perhaps causing a stack overflow).
       119174  +*/
       119175  +#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
       119176  +# define SQLITE_FTS3_MAX_EXPR_DEPTH 12
       119177  +#endif
       119178  +
119064 119179   
119065 119180   /*
119066 119181   ** This constant controls how often segments are merged. Once there are
119067 119182   ** FTS3_MERGE_COUNT segments of level N, they are merged into a single
119068 119183   ** segment of level N+1.
119069 119184   */
119070 119185   #define FTS3_MERGE_COUNT 16
................................................................................
120715 120830     int iLangidCons = -1;           /* Index of langid=x constraint, if present */
120716 120831   
120717 120832     /* By default use a full table scan. This is an expensive option,
120718 120833     ** so search through the constraints to see if a more efficient 
120719 120834     ** strategy is possible.
120720 120835     */
120721 120836     pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
120722         -  pInfo->estimatedCost = 500000;
       120837  +  pInfo->estimatedCost = 5000000;
120723 120838     for(i=0; i<pInfo->nConstraint; i++){
120724 120839       struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
120725 120840       if( pCons->usable==0 ) continue;
120726 120841   
120727 120842       /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
120728 120843       if( iCons<0 
120729 120844        && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ 
................................................................................
122276 122391           p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr, 
122277 122392           &p->base.zErrMsg
122278 122393       );
122279 122394       if( rc!=SQLITE_OK ){
122280 122395         return rc;
122281 122396       }
122282 122397   
122283         -    rc = sqlite3Fts3ReadLock(p);
122284         -    if( rc!=SQLITE_OK ) return rc;
122285         -
122286 122398       rc = fts3EvalStart(pCsr);
122287         -
122288 122399       sqlite3Fts3SegmentsClose(p);
122289 122400       if( rc!=SQLITE_OK ) return rc;
122290 122401       pCsr->pNextId = pCsr->aDoclist;
122291 122402       pCsr->iPrevId = 0;
122292 122403     }
122293 122404   
122294 122405     /* Compile a SELECT statement for this cursor. For a full-table-scan, the
................................................................................
126135 126246     int bFts4,                          /* True to allow FTS4-only syntax */
126136 126247     int nCol,                           /* Number of entries in azCol[] */
126137 126248     int iDefaultCol,                    /* Default column to query */
126138 126249     const char *z, int n,               /* Text of MATCH query */
126139 126250     Fts3Expr **ppExpr,                  /* OUT: Parsed query structure */
126140 126251     char **pzErr                        /* OUT: Error message (sqlite3_malloc) */
126141 126252   ){
126142         -  static const int MAX_EXPR_DEPTH = 12;
126143 126253     int rc = fts3ExprParseUnbalanced(
126144 126254         pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
126145 126255     );
126146 126256     
126147 126257     /* Rebalance the expression. And check that its depth does not exceed
126148         -  ** MAX_EXPR_DEPTH.  */
       126258  +  ** SQLITE_FTS3_MAX_EXPR_DEPTH.  */
126149 126259     if( rc==SQLITE_OK && *ppExpr ){
126150         -    rc = fts3ExprBalance(ppExpr, MAX_EXPR_DEPTH);
       126260  +    rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
126151 126261       if( rc==SQLITE_OK ){
126152         -      rc = fts3ExprCheckDepth(*ppExpr, MAX_EXPR_DEPTH);
       126262  +      rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
126153 126263       }
126154 126264     }
126155 126265   
126156 126266     if( rc!=SQLITE_OK ){
126157 126267       sqlite3Fts3ExprFree(*ppExpr);
126158 126268       *ppExpr = 0;
126159 126269       if( rc==SQLITE_TOOBIG ){
126160 126270         *pzErr = sqlite3_mprintf(
126161         -          "FTS expression tree is too large (maximum depth %d)", MAX_EXPR_DEPTH
       126271  +          "FTS expression tree is too large (maximum depth %d)", 
       126272  +          SQLITE_FTS3_MAX_EXPR_DEPTH
126162 126273         );
126163 126274         rc = SQLITE_ERROR;
126164 126275       }else if( rc==SQLITE_ERROR ){
126165 126276         *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z);
126166 126277       }
126167 126278     }
126168 126279   
................................................................................
129116 129227       rc = sqlite3_reset(pStmt);
129117 129228     }
129118 129229     *pRC = rc;
129119 129230   }
129120 129231   
129121 129232   
129122 129233   /*
129123         -** This function ensures that the caller has obtained a shared-cache
129124         -** table-lock on the %_content table. This is required before reading
129125         -** data from the fts3 table. If this lock is not acquired first, then
129126         -** the caller may end up holding read-locks on the %_segments and %_segdir
129127         -** tables, but no read-lock on the %_content table. If this happens 
129128         -** a second connection will be able to write to the fts3 table, but
129129         -** attempting to commit those writes might return SQLITE_LOCKED or
129130         -** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain 
129131         -** write-locks on the %_segments and %_segdir ** tables). 
129132         -**
129133         -** We try to avoid this because if FTS3 returns any error when committing
129134         -** a transaction, the whole transaction will be rolled back. And this is
129135         -** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can
129136         -** still happen if the user reads data directly from the %_segments or
129137         -** %_segdir tables instead of going through FTS3 though.
129138         -**
129139         -** This reasoning does not apply to a content=xxx table.
129140         -*/
129141         -SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){
129142         -  int rc;                         /* Return code */
129143         -  sqlite3_stmt *pStmt;            /* Statement used to obtain lock */
129144         -
129145         -  if( p->zContentTbl==0 ){
129146         -    rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
       129234  +** This function ensures that the caller has obtained an exclusive 
       129235  +** shared-cache table-lock on the %_segdir table. This is required before 
       129236  +** writing data to the fts3 table. If this lock is not acquired first, then
       129237  +** the caller may end up attempting to take this lock as part of committing
       129238  +** a transaction, causing SQLite to return SQLITE_LOCKED or 
       129239  +** LOCKED_SHAREDCACHEto a COMMIT command.
       129240  +**
       129241  +** It is best to avoid this because if FTS3 returns any error when 
       129242  +** committing a transaction, the whole transaction will be rolled back. 
       129243  +** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. 
       129244  +** It can still happen if the user locks the underlying tables directly 
       129245  +** instead of accessing them via FTS.
       129246  +*/
       129247  +static int fts3Writelock(Fts3Table *p){
       129248  +  int rc = SQLITE_OK;
       129249  +  
       129250  +  if( p->nPendingData==0 ){
       129251  +    sqlite3_stmt *pStmt;
       129252  +    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
129147 129253       if( rc==SQLITE_OK ){
129148 129254         sqlite3_bind_null(pStmt, 1);
129149 129255         sqlite3_step(pStmt);
129150 129256         rc = sqlite3_reset(pStmt);
129151 129257       }
129152         -  }else{
129153         -    rc = SQLITE_OK;
129154 129258     }
129155 129259   
129156 129260     return rc;
129157 129261   }
129158 129262   
129159 129263   /*
129160 129264   ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
................................................................................
133924 134028     if( aSzDel==0 ){
133925 134029       rc = SQLITE_NOMEM;
133926 134030       goto update_out;
133927 134031     }
133928 134032     aSzIns = &aSzDel[p->nColumn+1];
133929 134033     memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
133930 134034   
       134035  +  rc = fts3Writelock(p);
       134036  +  if( rc!=SQLITE_OK ) goto update_out;
       134037  +
133931 134038     /* If this is an INSERT operation, or an UPDATE that modifies the rowid
133932 134039     ** value, then this operation requires constraint handling.
133933 134040     **
133934 134041     ** If the on-conflict mode is REPLACE, this means that the existing row
133935 134042     ** should be deleted from the database before inserting the new row. Or,
133936 134043     ** if the on-conflict mode is other than REPLACE, then this method must
133937 134044     ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
................................................................................
136057 136164       0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
136058 136165       0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
136059 136166       0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
136060 136167       0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
136061 136168       0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
136062 136169       0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
136063 136170       0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
136064         -    0x037FFC02, 0x03E3FC01, 0x03EC7801, 0x03ECA401, 0x03EEC810,
136065         -    0x03F4F802, 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023,
136066         -    0x03F95013, 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807,
136067         -    0x03FCEC06, 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405,
136068         -    0x04040003, 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E,
136069         -    0x040E7C01, 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01,
136070         -    0x04280403, 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01,
136071         -    0x04294009, 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016,
136072         -    0x04420003, 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004,
136073         -    0x04460003, 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004,
136074         -    0x05BD442E, 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5,
136075         -    0x07480046, 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01,
136076         -    0x075C5401, 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401,
136077         -    0x075EA401, 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064,
136078         -    0x07C2800F, 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F,
136079         -    0x07C4C03C, 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009,
136080         -    0x07C94002, 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014,
136081         -    0x07CE8025, 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001,
136082         -    0x07D108B6, 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018,
136083         -    0x07D7EC46, 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401,
136084         -    0x38008060, 0x380400F0, 0x3C000001, 0x3FFFF401, 0x40000001,
136085         -    0x43FFF401,
       136171  +    0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
       136172  +    0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
       136173  +    0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
       136174  +    0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
       136175  +    0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
       136176  +    0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
       136177  +    0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
       136178  +    0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
       136179  +    0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
       136180  +    0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
       136181  +    0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
       136182  +    0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
       136183  +    0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
       136184  +    0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
       136185  +    0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
       136186  +    0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
       136187  +    0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
       136188  +    0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
       136189  +    0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
       136190  +    0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
       136191  +    0x380400F0,
136086 136192     };
136087 136193     static const unsigned int aAscii[4] = {
136088 136194       0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
136089 136195     };
136090 136196   
136091 136197     if( c<128 ){
136092 136198       return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
................................................................................
139711 139817   **
139712 139818   **   * An implementation of the SQL regexp() function (and hence REGEXP
139713 139819   **     operator) using the ICU uregex_XX() APIs.
139714 139820   **
139715 139821   **   * Implementations of the SQL scalar upper() and lower() functions
139716 139822   **     for case mapping.
139717 139823   **
139718         -**   * Integration of ICU and SQLite collation seqences.
       139824  +**   * Integration of ICU and SQLite collation sequences.
139719 139825   **
139720 139826   **   * An implementation of the LIKE operator that uses ICU to 
139721 139827   **     provide case-independent matching.
139722 139828   */
139723 139829   
139724 139830   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
139725 139831   
................................................................................
140450 140556     *ppModule = &icuTokenizerModule;
140451 140557   }
140452 140558   
140453 140559   #endif /* defined(SQLITE_ENABLE_ICU) */
140454 140560   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
140455 140561   
140456 140562   /************** End of fts3_icu.c ********************************************/
       140563  +/*
       140564  +** 2013-06-12
       140565  +**
       140566  +** The author disclaims copyright to this source code.  In place of
       140567  +** a legal notice, here is a blessing:
       140568  +**
       140569  +**    May you do good and not evil.
       140570  +**    May you find forgiveness for yourself and forgive others.
       140571  +**    May you share freely, never taking more than you give.
       140572  +**
       140573  +*************************************************************************
       140574  +**
       140575  +** A shim that sits between the SQLite virtual table interface and
       140576  +** managed memory of .NET
       140577  +*/
       140578  +#include "sqlite3ext.h"
       140579  +SQLITE_EXTENSION_INIT1
       140580  +#include <assert.h>
       140581  +#include <string.h>
       140582  +
       140583  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       140584  +
       140585  +/* Forward references */
       140586  +typedef struct vtshim_aux vtshim_aux;
       140587  +typedef struct vtshim_vtab vtshim_vtab;
       140588  +typedef struct vtshim_cursor vtshim_cursor;
       140589  +
       140590  +
       140591  +/* The vtshim_aux argument is the auxiliary parameter that is passed
       140592  +** into sqlite3_create_module_v2().
       140593  +*/
       140594  +struct vtshim_aux {
       140595  +  void *pChildAux;              /* pAux for child virtual tables */
       140596  +  void (*xChildDestroy)(void*); /* Destructor for pChildAux */
       140597  +  const sqlite3_module *pMod;   /* Methods for child virtual tables */
       140598  +  sqlite3 *db;                  /* The database to which we are attached */
       140599  +  char *zName;                  /* Name of the module */
       140600  +  int bDisposed;                /* True if disposed */
       140601  +  vtshim_vtab *pAllVtab;        /* List of all vtshim_vtab objects */
       140602  +  sqlite3_module sSelf;         /* Methods used by this shim */
       140603  +};
       140604  +
       140605  +/* A vtshim virtual table object */
       140606  +struct vtshim_vtab {
       140607  +  sqlite3_vtab base;       /* Base class - must be first */
       140608  +  sqlite3_vtab *pChild;    /* Child virtual table */
       140609  +  vtshim_aux *pAux;        /* Pointer to vtshim_aux object */
       140610  +  vtshim_cursor *pAllCur;  /* List of all cursors */
       140611  +  vtshim_vtab **ppPrev;    /* Previous on list */
       140612  +  vtshim_vtab *pNext;      /* Next on list */
       140613  +};
       140614  +
       140615  +/* A vtshim cursor object */
       140616  +struct vtshim_cursor {
       140617  +  sqlite3_vtab_cursor base;    /* Base class - must be first */
       140618  +  sqlite3_vtab_cursor *pChild; /* Cursor generated by the managed subclass */
       140619  +  vtshim_cursor **ppPrev;      /* Previous on list of all cursors */
       140620  +  vtshim_cursor *pNext;        /* Next on list of all cursors */
       140621  +};
       140622  +
       140623  +/* Methods for the vtshim module */
       140624  +static int vtshimCreate(
       140625  +  sqlite3 *db,
       140626  +  void *pPAux,
       140627  +  int argc, const char *const*argv,
       140628  +  sqlite3_vtab **ppVtab,
       140629  +  char **pzErr
       140630  +){
       140631  +  vtshim_aux *pAux = (vtshim_aux*)pPAux;
       140632  +  vtshim_vtab *pNew;
       140633  +  int rc; 
       140634  +
       140635  +  assert( db==pAux->db );
       140636  +  pNew = sqlite3_malloc( sizeof(*pNew) );
       140637  +  *ppVtab = (sqlite3_vtab*)pNew;
       140638  +  if( pNew==0 ) return SQLITE_NOMEM;
       140639  +  memset(pNew, 0, sizeof(*pNew));
       140640  +  rc = pAux->pMod->xCreate(db, pAux->pChildAux, argc, argv,
       140641  +                           &pNew->pChild, pzErr);
       140642  +  if( rc ){
       140643  +    sqlite3_free(pNew);
       140644  +    *ppVtab = 0;
       140645  +  }
       140646  +  pNew->ppPrev = &pAux->pAllVtab;
       140647  +  pNew->pNext = pAux->pAllVtab;
       140648  +  if( pAux->pAllVtab ) pAux->pAllVtab->ppPrev = &pNew->pNext;
       140649  +  pAux->pAllVtab = pNew;
       140650  +  return rc;
       140651  +}
       140652  +
       140653  +static int vtshimConnect(
       140654  +  sqlite3 *db,
       140655  +  void *pPAux,
       140656  +  int argc, const char *const*argv,
       140657  +  sqlite3_vtab **ppVtab,
       140658  +  char **pzErr
       140659  +){
       140660  +  vtshim_aux *pAux = (vtshim_aux*)pPAux;
       140661  +  vtshim_vtab *pNew;
       140662  +  int rc; 
       140663  +
       140664  +  assert( db==pAux->db );
       140665  +  pNew = sqlite3_malloc( sizeof(*pNew) );
       140666  +  *ppVtab = (sqlite3_vtab*)pNew;
       140667  +  if( pNew==0 ) return SQLITE_NOMEM;
       140668  +  memset(pNew, 0, sizeof(*pNew));
       140669  +  rc = pAux->pMod->xConnect(db, pAux->pChildAux, argc, argv,
       140670  +                            &pNew->pChild, pzErr);
       140671  +  if( rc ){
       140672  +    sqlite3_free(pNew);
       140673  +    *ppVtab = 0;
       140674  +  }
       140675  +  pNew->ppPrev = &pAux->pAllVtab;
       140676  +  pNew->pNext = pAux->pAllVtab;
       140677  +  if( pAux->pAllVtab ) pAux->pAllVtab->ppPrev = &pNew->pNext;
       140678  +  pAux->pAllVtab = pNew;
       140679  +  return rc;
       140680  +}
       140681  +
       140682  +static int vtshimBestIndex(
       140683  +  sqlite3_vtab *pBase,
       140684  +  sqlite3_index_info *pIdxInfo
       140685  +){
       140686  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140687  +  vtshim_aux *pAux = pVtab->pAux;
       140688  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140689  +  return pAux->pMod->xBestIndex(pVtab->pChild, pIdxInfo);
       140690  +}
       140691  +
       140692  +
       140693  +static int vtshimDisconnect(sqlite3_vtab *pBase){
       140694  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140695  +  vtshim_aux *pAux = pVtab->pAux;
       140696  +  int rc = SQLITE_OK;
       140697  +  if( !pAux->bDisposed ){
       140698  +    rc = pAux->pMod->xDisconnect(pVtab->pChild);
       140699  +  }
       140700  +  if( pVtab->pNext ) pVtab->pNext->ppPrev = pVtab->ppPrev;
       140701  +  *pVtab->ppPrev = pVtab->pNext;
       140702  +  sqlite3_free(pVtab);
       140703  +  return rc;
       140704  +}
       140705  +
       140706  +static int vtshimDestroy(sqlite3_vtab *pBase){
       140707  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140708  +  vtshim_aux *pAux = pVtab->pAux;
       140709  +  int rc = SQLITE_OK;
       140710  +  if( !pAux->bDisposed ){
       140711  +    rc = pAux->pMod->xDestroy(pVtab->pChild);
       140712  +  }
       140713  +  if( pVtab->pNext ) pVtab->pNext->ppPrev = pVtab->ppPrev;
       140714  +  *pVtab->ppPrev = pVtab->pNext;
       140715  +  sqlite3_free(pVtab);
       140716  +  return rc;
       140717  +}
       140718  +
       140719  +static int vtshimOpen(sqlite3_vtab *pBase, sqlite3_vtab_cursor **ppCursor){
       140720  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140721  +  vtshim_aux *pAux = pVtab->pAux;
       140722  +  vtshim_cursor *pCur;
       140723  +  int rc;
       140724  +  *ppCursor = 0;
       140725  +  if( pAux->bDisposed )  return SQLITE_ERROR;
       140726  +  pCur = sqlite3_malloc( sizeof(*pCur) );
       140727  +  if( pCur==0 ) return SQLITE_NOMEM;
       140728  +  memset(pCur, 0, sizeof(*pCur));
       140729  +  rc = pAux->pMod->xOpen(pVtab->pChild, &pCur->pChild);
       140730  +  if( rc ){
       140731  +    sqlite3_free(pCur);
       140732  +    return rc;
       140733  +  }
       140734  +  pCur->pChild->pVtab = pVtab->pChild;
       140735  +  *ppCursor = &pCur->base;
       140736  +  pCur->ppPrev = &pVtab->pAllCur;
       140737  +  if( pVtab->pAllCur ) pVtab->pAllCur->ppPrev = &pCur->pNext;
       140738  +  pCur->pNext = pVtab->pAllCur;
       140739  +  pVtab->pAllCur = pCur;
       140740  +  return SQLITE_OK;
       140741  +}
       140742  +
       140743  +static int vtshimClose(sqlite3_vtab_cursor *pX){
       140744  +  vtshim_cursor *pCur = (vtshim_cursor*)pX;
       140745  +  vtshim_vtab *pVtab = (vtshim_vtab*)pCur->base.pVtab;
       140746  +  vtshim_aux *pAux = pVtab->pAux;
       140747  +  int rc = SQLITE_OK;
       140748  +  if( !pAux->bDisposed ){
       140749  +    rc = pAux->pMod->xClose(pCur->pChild);
       140750  +  }
       140751  +  if( pCur->pNext ) pCur->pNext->ppPrev = pCur->ppPrev;
       140752  +  *pCur->ppPrev = pCur->pNext;
       140753  +  sqlite3_free(pCur);
       140754  +  return rc;
       140755  +}
       140756  +
       140757  +static int vtshimFilter(
       140758  +  sqlite3_vtab_cursor *pX,
       140759  +  int idxNum, const char *idxStr,
       140760  +  int argc, sqlite3_value **argv
       140761  +){
       140762  +  vtshim_cursor *pCur = (vtshim_cursor*)pX;
       140763  +  vtshim_vtab *pVtab = (vtshim_vtab*)pCur->base.pVtab;
       140764  +  vtshim_aux *pAux = pVtab->pAux;
       140765  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140766  +  return pAux->pMod->xFilter(pCur->pChild, idxNum, idxStr, argc, argv);
       140767  +}
       140768  +
       140769  +static int vtshimNext(sqlite3_vtab_cursor *pX){
       140770  +  vtshim_cursor *pCur = (vtshim_cursor*)pX;
       140771  +  vtshim_vtab *pVtab = (vtshim_vtab*)pCur->base.pVtab;
       140772  +  vtshim_aux *pAux = pVtab->pAux;
       140773  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140774  +  return pAux->pMod->xNext(pCur->pChild);
       140775  +}
       140776  +
       140777  +static int vtshimEof(sqlite3_vtab_cursor *pX){
       140778  +  vtshim_cursor *pCur = (vtshim_cursor*)pX;
       140779  +  vtshim_vtab *pVtab = (vtshim_vtab*)pCur->base.pVtab;
       140780  +  vtshim_aux *pAux = pVtab->pAux;
       140781  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140782  +  return pAux->pMod->xEof(pCur->pChild);
       140783  +}
       140784  +
       140785  +static int vtshimColumn(sqlite3_vtab_cursor *pX, sqlite3_context *ctx, int i){
       140786  +  vtshim_cursor *pCur = (vtshim_cursor*)pX;
       140787  +  vtshim_vtab *pVtab = (vtshim_vtab*)pCur->base.pVtab;
       140788  +  vtshim_aux *pAux = pVtab->pAux;
       140789  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140790  +  return pAux->pMod->xColumn(pCur->pChild, ctx, i);
       140791  +}
       140792  +
       140793  +static int vtshimRowid(sqlite3_vtab_cursor *pX, sqlite3_int64 *pRowid){
       140794  +  vtshim_cursor *pCur = (vtshim_cursor*)pX;
       140795  +  vtshim_vtab *pVtab = (vtshim_vtab*)pCur->base.pVtab;
       140796  +  vtshim_aux *pAux = pVtab->pAux;
       140797  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140798  +  return pAux->pMod->xRowid(pCur->pChild, pRowid);
       140799  +}
       140800  +
       140801  +static int vtshimUpdate(
       140802  +  sqlite3_vtab *pBase,
       140803  +  int argc,
       140804  +  sqlite3_value **argv,
       140805  +  sqlite3_int64 *pRowid
       140806  +){
       140807  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140808  +  vtshim_aux *pAux = pVtab->pAux;
       140809  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140810  +  return pAux->pMod->xUpdate(pVtab->pChild, argc, argv, pRowid);
       140811  +}
       140812  +
       140813  +static int vtshimBegin(sqlite3_vtab *pBase){
       140814  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140815  +  vtshim_aux *pAux = pVtab->pAux;
       140816  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140817  +  return pAux->pMod->xBegin(pVtab->pChild);
       140818  +}
       140819  +
       140820  +static int vtshimSync(sqlite3_vtab *pBase){
       140821  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140822  +  vtshim_aux *pAux = pVtab->pAux;
       140823  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140824  +  return pAux->pMod->xSync(pVtab->pChild);
       140825  +}
       140826  +
       140827  +static int vtshimCommit(sqlite3_vtab *pBase){
       140828  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140829  +  vtshim_aux *pAux = pVtab->pAux;
       140830  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140831  +  return pAux->pMod->xCommit(pVtab->pChild);
       140832  +}
       140833  +
       140834  +static int vtshimRollback(sqlite3_vtab *pBase){
       140835  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140836  +  vtshim_aux *pAux = pVtab->pAux;
       140837  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140838  +  return pAux->pMod->xRollback(pVtab->pChild);
       140839  +}
       140840  +
       140841  +static int vtshimFindFunction(
       140842  +  sqlite3_vtab *pBase,
       140843  +  int nArg,
       140844  +  const char *zName,
       140845  +  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
       140846  +  void **ppArg
       140847  +){
       140848  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140849  +  vtshim_aux *pAux = pVtab->pAux;
       140850  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140851  +  return pAux->pMod->xFindFunction(pVtab->pChild, nArg, zName, pxFunc, ppArg);
       140852  +}
       140853  +
       140854  +static int vtshimRename(sqlite3_vtab *pBase, const char *zNewName){
       140855  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140856  +  vtshim_aux *pAux = pVtab->pAux;
       140857  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140858  +  return pAux->pMod->xRename(pVtab->pChild, zNewName);
       140859  +}
       140860  +
       140861  +static int vtshimSavepoint(sqlite3_vtab *pBase, int n){
       140862  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140863  +  vtshim_aux *pAux = pVtab->pAux;
       140864  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140865  +  return pAux->pMod->xSavepoint(pVtab->pChild, n);
       140866  +}
       140867  +
       140868  +static int vtshimRelease(sqlite3_vtab *pBase, int n){
       140869  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140870  +  vtshim_aux *pAux = pVtab->pAux;
       140871  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140872  +  return pAux->pMod->xRelease(pVtab->pChild, n);
       140873  +}
       140874  +
       140875  +static int vtshimRollbackTo(sqlite3_vtab *pBase, int n){
       140876  +  vtshim_vtab *pVtab = (vtshim_vtab*)pBase;
       140877  +  vtshim_aux *pAux = pVtab->pAux;
       140878  +  if( pAux->bDisposed ) return SQLITE_ERROR;
       140879  +  return pAux->pMod->xRollbackTo(pVtab->pChild, n);
       140880  +}
       140881  +
       140882  +/* The destructor function for a disposible module */
       140883  +static void vtshimAuxDestructor(void *pXAux){
       140884  +  vtshim_aux *pAux = (vtshim_aux*)pXAux;
       140885  +  assert( pAux->pAllVtab==0 );
       140886  +  if( !pAux->bDisposed && pAux->xChildDestroy ){
       140887  +    pAux->xChildDestroy(pAux->pChildAux);
       140888  +  }
       140889  +  sqlite3_free(pAux);
       140890  +}
       140891  +
       140892  +#ifdef _WIN32
       140893  +__declspec(dllexport)
       140894  +#endif
       140895  +void *sqlite3_create_disposable_module(
       140896  +  sqlite3 *db,               /* SQLite connection to register module with */
       140897  +  const char *zName,         /* Name of the module */
       140898  +  const sqlite3_module *p,   /* Methods for the module */
       140899  +  void *pClientData,         /* Client data for xCreate/xConnect */
       140900  +  void(*xDestroy)(void*)     /* Module destructor function */
       140901  +){
       140902  +  vtshim_aux *pAux;
       140903  +  int rc;
       140904  +  pAux = sqlite3_malloc( sizeof(*pAux) );
       140905  +  if( pAux==0 ){
       140906  +    if( xDestroy ) xDestroy(pClientData);
       140907  +    return 0;
       140908  +  }
       140909  +  pAux->pChildAux = pClientData;
       140910  +  pAux->xChildDestroy = xDestroy;
       140911  +  pAux->pMod = p;
       140912  +  pAux->db = db;
       140913  +  sqlite3_mutex_enter(db->mutex);
       140914  +  pAux->zName = sqlite3DbStrDup(db, zName);
       140915  +  sqlite3_mutex_leave(db->mutex);
       140916  +  pAux->bDisposed = 0;
       140917  +  pAux->pAllVtab = 0;
       140918  +  pAux->sSelf.iVersion = p->iVersion<=2 ? p->iVersion : 1;
       140919  +  pAux->sSelf.xCreate = p->xCreate ? vtshimCreate : 0;
       140920  +  pAux->sSelf.xConnect = p->xConnect ? vtshimConnect : 0;
       140921  +  pAux->sSelf.xBestIndex = p->xBestIndex ? vtshimBestIndex : 0;
       140922  +  pAux->sSelf.xDisconnect = p->xDisconnect ? vtshimDisconnect : 0;
       140923  +  pAux->sSelf.xDestroy = p->xDestroy ? vtshimDestroy : 0;
       140924  +  pAux->sSelf.xOpen = p->xOpen ? vtshimOpen : 0;
       140925  +  pAux->sSelf.xClose = p->xClose ? vtshimClose : 0;
       140926  +  pAux->sSelf.xFilter = p->xFilter ? vtshimFilter : 0;
       140927  +  pAux->sSelf.xNext = p->xNext ? vtshimNext : 0;
       140928  +  pAux->sSelf.xEof = p->xEof ? vtshimEof : 0;
       140929  +  pAux->sSelf.xColumn = p->xColumn ? vtshimColumn : 0;
       140930  +  pAux->sSelf.xRowid = p->xRowid ? vtshimRowid : 0;
       140931  +  pAux->sSelf.xUpdate = p->xUpdate ? vtshimUpdate : 0;
       140932  +  pAux->sSelf.xBegin = p->xBegin ? vtshimBegin : 0;
       140933  +  pAux->sSelf.xSync = p->xSync ? vtshimSync : 0;
       140934  +  pAux->sSelf.xCommit = p->xCommit ? vtshimCommit : 0;
       140935  +  pAux->sSelf.xRollback = p->xRollback ? vtshimRollback : 0;
       140936  +  pAux->sSelf.xFindFunction = p->xFindFunction ? vtshimFindFunction : 0;
       140937  +  pAux->sSelf.xRename = p->xRename ? vtshimRename : 0;
       140938  +  if( p->iVersion>=1 ){
       140939  +    pAux->sSelf.xSavepoint = p->xSavepoint ? vtshimSavepoint : 0;
       140940  +    pAux->sSelf.xRelease = p->xRelease ? vtshimRelease : 0;
       140941  +    pAux->sSelf.xRollbackTo = p->xRollbackTo ? vtshimRollbackTo : 0;
       140942  +  }else{
       140943  +    pAux->sSelf.xSavepoint = 0;
       140944  +    pAux->sSelf.xRelease = 0;
       140945  +    pAux->sSelf.xRollbackTo = 0;
       140946  +  }
       140947  +  rc = sqlite3_create_module_v2(db, zName, &pAux->sSelf,
       140948  +                                pAux, vtshimAuxDestructor);
       140949  +  return rc==SQLITE_OK ? (void*)pAux : 0;
       140950  +}
       140951  +
       140952  +#ifdef _WIN32
       140953  +__declspec(dllexport)
       140954  +#endif
       140955  +void sqlite3_dispose_module(void *pX){
       140956  +  vtshim_aux *pAux = (vtshim_aux*)pX;
       140957  +  if( !pAux->bDisposed ){
       140958  +    vtshim_vtab *pVtab;
       140959  +    vtshim_cursor *pCur;
       140960  +    for(pVtab=pAux->pAllVtab; pVtab; pVtab=pVtab->pNext){
       140961  +      for(pCur=pVtab->pAllCur; pCur; pCur=pCur->pNext){
       140962  +        pAux->pMod->xClose(pCur->pChild);
       140963  +      }
       140964  +      pAux->pMod->xDisconnect(pVtab->pChild);
       140965  +    }
       140966  +    sqlite3DbFree(pAux->db, pAux->zName);
       140967  +    pAux->zName = 0;
       140968  +    pAux->bDisposed = 1;
       140969  +    if( pAux->xChildDestroy ) pAux->xChildDestroy(pAux->pChildAux);
       140970  +  }
       140971  +}
       140972  +
       140973  +
       140974  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
       140975  +
       140976  +#ifdef _WIN32
       140977  +__declspec(dllexport)
       140978  +#endif
       140979  +int sqlite3_vtshim_init(
       140980  +  sqlite3 *db, 
       140981  +  char **pzErrMsg, 
       140982  +  const sqlite3_api_routines *pApi
       140983  +){
       140984  +  SQLITE_EXTENSION_INIT2(pApi);
       140985  +  return SQLITE_OK;
       140986  +}

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

   105    105   **
   106    106   ** See also: [sqlite3_libversion()],
   107    107   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   108    108   ** [sqlite_version()] and [sqlite_source_id()].
   109    109   */
   110    110   #define SQLITE_VERSION        "3.7.17"
   111    111   #define SQLITE_VERSION_NUMBER 3007017
   112         -#define SQLITE_SOURCE_ID      "2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668"
          112  +#define SQLITE_SOURCE_ID      "2013-06-13 00:32:29 10bba8d0821159a45c6a0d6c3cef897cb4d4e9a6"
   113    113   
   114    114   /*
   115    115   ** CAPI3REF: Run-Time Library Version Numbers
   116    116   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   117    117   **
   118    118   ** These interfaces provide the same information as the [SQLITE_VERSION],
   119    119   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  4514   4514   ** The code to implement this API is not available in the public release
  4515   4515   ** of SQLite.
  4516   4516   */
  4517   4517   SQLITE_API int sqlite3_key(
  4518   4518     sqlite3 *db,                   /* Database to be rekeyed */
  4519   4519     const void *pKey, int nKey     /* The key */
  4520   4520   );
         4521  +SQLITE_API int sqlite3_key_v2(
         4522  +  sqlite3 *db,                   /* Database to be rekeyed */
         4523  +  const char *zDbName,           /* Name of the database */
         4524  +  const void *pKey, int nKey     /* The key */
         4525  +);
  4521   4526   
  4522   4527   /*
  4523   4528   ** Change the key on an open database.  If the current database is not
  4524   4529   ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
  4525   4530   ** database is decrypted.
  4526   4531   **
  4527   4532   ** The code to implement this API is not available in the public release
  4528   4533   ** of SQLite.
  4529   4534   */
  4530   4535   SQLITE_API int sqlite3_rekey(
  4531   4536     sqlite3 *db,                   /* Database to be rekeyed */
  4532   4537     const void *pKey, int nKey     /* The new key */
  4533   4538   );
         4539  +SQLITE_API int sqlite3_rekey_v2(
         4540  +  sqlite3 *db,                   /* Database to be rekeyed */
         4541  +  const char *zDbName,           /* Name of the database */
         4542  +  const void *pKey, int nKey     /* The new key */
         4543  +);
  4534   4544   
  4535   4545   /*
  4536   4546   ** Specify the activation key for a SEE database.  Unless 
  4537   4547   ** activated, none of the SEE routines will work.
  4538   4548   */
  4539   4549   SQLITE_API void sqlite3_activate_see(
  4540   4550     const char *zPassPhrase        /* Activation phrase */

Changes to System.Data.SQLite/SQLite3.cs.

    79     79       protected bool _ownHandle;
    80     80   
    81     81       /// <summary>
    82     82       /// The user-defined functions registered on this connection
    83     83       /// </summary>
    84     84       protected SQLiteFunction[] _functionsArray;
    85     85   
           86  +    /// <summary>
           87  +    /// The modules created using this connection.
           88  +    /// </summary>
           89  +    protected Dictionary<string, SQLiteModuleBase> _modules;
           90  +
    86     91       ///////////////////////////////////////////////////////////////////////////////////////////////
    87     92   
    88     93       internal SQLite3(SQLiteDateFormats fmt, DateTimeKind kind, string fmtString, bool ownHandle)
    89     94         : base(fmt, kind, fmtString)
    90     95       {
    91     96           _ownHandle = ownHandle;
    92     97       }
................................................................................
   117    122                   //    // dispose managed resources here...
   118    123                   //    ////////////////////////////////////
   119    124                   //}
   120    125   
   121    126                   //////////////////////////////////////
   122    127                   // release unmanaged resources here...
   123    128                   //////////////////////////////////////
          129  +
          130  +                if (_modules != null)
          131  +                {
          132  +                    foreach (KeyValuePair<string, SQLiteModuleBase> pair in _modules)
          133  +                    {
          134  +                        SQLiteModuleBase module = pair.Value;
          135  +
          136  +                        if (module == null)
          137  +                            continue;
          138  +
          139  +                        module.Dispose();
          140  +                    }
          141  +                }
   124    142   
   125    143                   Close(false); /* Disposing, cannot throw. */
   126    144   
   127    145                   disposed = true;
   128    146               }
   129    147           }
   130    148           finally
................................................................................
  1500   1518       }
  1501   1519   
  1502   1520       internal override void ReturnText(IntPtr context, string value)
  1503   1521       {
  1504   1522         byte[] b = ToUTF8(value);
  1505   1523         UnsafeNativeMethods.sqlite3_result_text(context, ToUTF8(value), b.Length - 1, (IntPtr)(-1));
  1506   1524       }
         1525  +
         1526  +    internal override void CreateModule(SQLiteModuleBase module)
         1527  +    {
         1528  +        if (module == null)
         1529  +            throw new ArgumentNullException("module");
         1530  +
         1531  +        SetLoadExtension(true);
         1532  +        LoadExtension(UnsafeNativeMethods.SQLITE_DLL, "sqlite3_vtshim_init");
         1533  +
         1534  +        IntPtr pName = IntPtr.Zero;
         1535  +
         1536  +        try
         1537  +        {
         1538  +            pName = SQLiteMarshal.Utf8IntPtrFromString(module.Name);
         1539  +            UnsafeNativeMethods.sqlite3_module nativeModule = module.CreateNativeModule();
         1540  +
         1541  +            if (UnsafeNativeMethods.sqlite3_create_disposable_module(
         1542  +                    _sql, pName, ref nativeModule, IntPtr.Zero, null) != IntPtr.Zero)
         1543  +            {
         1544  +                if (_modules == null)
         1545  +                    _modules = new Dictionary<string, SQLiteModuleBase>();
         1546  +
         1547  +                _modules.Add(module.Name, module);
         1548  +            }
         1549  +            else
         1550  +            {
         1551  +                throw new SQLiteException(SQLiteErrorCode.Error, GetLastError());
         1552  +            }
         1553  +        }
         1554  +        finally
         1555  +        {
         1556  +            if (pName != IntPtr.Zero)
         1557  +            {
         1558  +                SQLiteMarshal.Free(pName);
         1559  +                pName = IntPtr.Zero;
         1560  +            }
         1561  +        }
         1562  +    }
         1563  +
         1564  +    internal override void DisposeModule(SQLiteModuleBase module)
         1565  +    {
         1566  +        if (module == null)
         1567  +            throw new ArgumentNullException("module");
         1568  +
         1569  +        UnsafeNativeMethods.sqlite3_module nativeModule = module.GetNativeModule();
         1570  +
         1571  +        if (nativeModule.iVersion == 0)
         1572  +            throw new ArgumentException("native module is not initialized");
         1573  +
         1574  +        UnsafeNativeMethods.sqlite3_dispose_module(ref nativeModule);
         1575  +    }
  1507   1576   
  1508   1577       internal override IntPtr AggregateContext(IntPtr context)
  1509   1578       {
  1510   1579         return UnsafeNativeMethods.sqlite3_aggregate_context(context, 1);
  1511   1580       }
  1512   1581   
  1513   1582       /// <summary>

Changes to System.Data.SQLite/SQLiteBase.cs.

   207    207       internal abstract void ReturnDouble(IntPtr context, double value);
   208    208       internal abstract void ReturnError(IntPtr context, string value);
   209    209       internal abstract void ReturnInt32(IntPtr context, Int32 value);
   210    210       internal abstract void ReturnInt64(IntPtr context, Int64 value);
   211    211       internal abstract void ReturnNull(IntPtr context);
   212    212       internal abstract void ReturnText(IntPtr context, string value);
   213    213   
          214  +    /// <summary>
          215  +    /// Calls the native SQLite core library in order to create a disposable
          216  +    /// module containing the implementation of a virtual table.
          217  +    /// </summary>
          218  +    /// <param name="module">
          219  +    /// The module object to be used when creating the native disposable module.
          220  +    /// </param>
          221  +    internal abstract void CreateModule(SQLiteModuleBase module);
          222  +
          223  +    /// <summary>
          224  +    /// Calls the native SQLite core library in order to cleanup the resources
          225  +    /// associated with a module containing the implementation of a virtual table.
          226  +    /// </summary>
          227  +    /// <param name="module">
          228  +    /// The module object previously passed to the <see cref="CreateModule" />
          229  +    /// method.
          230  +    /// </param>
          231  +    internal abstract void DisposeModule(SQLiteModuleBase module);
          232  +
   214    233       /// <summary>
   215    234       /// Calls the native SQLite core library in order to declare a virtual table
   216    235       /// in response to a call into the xCreate or xConnect virtual table methods.
   217    236       /// </summary>
   218    237       /// <param name="module">
   219    238       /// The virtual table module that is to be responsible for the virtual table
   220    239       /// being declared.

Changes to System.Data.SQLite/SQLiteConnection.cs.

  1716   1716   
  1717   1717           if (_sql == null)
  1718   1718               throw new InvalidOperationException(
  1719   1719                   "Database connection not valid for loading extensions.");
  1720   1720   
  1721   1721           _sql.LoadExtension(fileName, procName);
  1722   1722       }
         1723  +
         1724  +    /// <summary>
         1725  +    /// Creates a disposable module containing the implementation of a virtual
         1726  +    /// table.
         1727  +    /// </summary>
         1728  +    /// <param name="module">
         1729  +    /// The module object to be used when creating the disposable module.
         1730  +    /// </param>
         1731  +    public void CreateModule(
         1732  +        SQLiteModuleBase module
         1733  +        )
         1734  +    {
         1735  +        CheckDisposed();
         1736  +
         1737  +        if (_sql == null)
         1738  +            throw new InvalidOperationException(
         1739  +                "Database connection not valid for creating modules.");
         1740  +
         1741  +        _sql.CreateModule(module);
         1742  +    }
  1723   1743   
  1724   1744       /// <summary>
  1725   1745       /// Parses a string containing a sequence of zero or more hexadecimal
  1726   1746       /// encoded byte values and returns the resulting byte array.  The
  1727   1747       /// "0x" prefix is not allowed on the input string.
  1728   1748       /// </summary>
  1729   1749       /// <param name="text">

Changes to System.Data.SQLite/SQLiteModuleBase.cs.

   662    662   
   663    663           SQLiteErrorCode xNext(
   664    664               IntPtr pCursor
   665    665               );
   666    666   
   667    667           ///////////////////////////////////////////////////////////////////////
   668    668   
   669         -        bool xEof(
          669  +        int xEof(
   670    670               IntPtr pCursor
   671    671               );
   672    672   
   673    673           ///////////////////////////////////////////////////////////////////////
   674    674   
   675    675           SQLiteErrorCode xColumn(
   676    676               IntPtr pCursor,
................................................................................
   716    716   
   717    717           SQLiteErrorCode xRollback(
   718    718               IntPtr pVtab
   719    719               );
   720    720   
   721    721           ///////////////////////////////////////////////////////////////////////
   722    722   
   723         -        bool xFindFunction(
          723  +        int xFindFunction(
   724    724               IntPtr pVtab,
   725    725               int nArg,
   726    726               IntPtr zName,
   727    727               ref SQLiteCallback callback,
   728    728               ref IntPtr pClientData
   729    729               );
   730    730   
................................................................................
   760    760   
   761    761       ///////////////////////////////////////////////////////////////////////////
   762    762   
   763    763       #region ISQLiteManagedModule Interface
   764    764       public interface ISQLiteManagedModule
   765    765       {
   766    766           bool Declared { get; }
          767  +        string Name { get; }
   767    768   
   768    769           ///////////////////////////////////////////////////////////////////////
   769    770   
   770    771           SQLiteErrorCode Create(
   771    772               SQLiteConnection connection, /* in */
   772    773               IntPtr pClientData,          /* in */
   773    774               string[] argv,               /* in */
................................................................................
  1486   1487   
  1487   1488       ///////////////////////////////////////////////////////////////////////////
  1488   1489   
  1489   1490       #region SQLiteModuleBase Class
  1490   1491       public abstract class SQLiteModuleBase :
  1491   1492               ISQLiteManagedModule, ISQLiteNativeModule,  IDisposable
  1492   1493       {
         1494  +        #region Private Data
         1495  +        private UnsafeNativeMethods.sqlite3_module nativeModule;
         1496  +        #endregion
         1497  +
         1498  +        ///////////////////////////////////////////////////////////////////////
         1499  +
  1493   1500           #region Internal Methods
         1501  +        internal UnsafeNativeMethods.sqlite3_module GetNativeModule()
         1502  +        {
         1503  +            return nativeModule;
         1504  +        }
         1505  +
         1506  +        ///////////////////////////////////////////////////////////////////////
         1507  +
  1494   1508           internal UnsafeNativeMethods.sqlite3_module CreateNativeModule()
  1495   1509           {
  1496         -            UnsafeNativeMethods.sqlite3_module module =
  1497         -                new UnsafeNativeMethods.sqlite3_module();
  1498         -
  1499         -            module.iVersion = 2;
  1500         -            module.xCreate = new UnsafeNativeMethods.xCreate(xCreate);
  1501         -            module.xConnect = new UnsafeNativeMethods.xConnect(xConnect);
  1502         -            module.xBestIndex = new UnsafeNativeMethods.xBestIndex(xBestIndex);
  1503         -            module.xDisconnect = new UnsafeNativeMethods.xDisconnect(xDisconnect);
  1504         -            module.xDestroy = new UnsafeNativeMethods.xDestroy(xDestroy);
  1505         -            module.xOpen = new UnsafeNativeMethods.xOpen(xOpen);
  1506         -            module.xClose = new UnsafeNativeMethods.xClose(xClose);
  1507         -            module.xFilter = new UnsafeNativeMethods.xFilter(xFilter);
  1508         -            module.xNext = new UnsafeNativeMethods.xNext(xNext);
  1509         -            module.xEof = new UnsafeNativeMethods.xEof(xEof);
  1510         -            module.xColumn = new UnsafeNativeMethods.xColumn(xColumn);
  1511         -            module.xRowId = new UnsafeNativeMethods.xRowId(xRowId);
  1512         -            module.xUpdate = new UnsafeNativeMethods.xUpdate(xUpdate);
  1513         -            module.xBegin = new UnsafeNativeMethods.xBegin(xBegin);
  1514         -            module.xSync = new UnsafeNativeMethods.xSync(xSync);
  1515         -            module.xCommit = new UnsafeNativeMethods.xCommit(xCommit);
  1516         -            module.xRollback = new UnsafeNativeMethods.xRollback(xRollback);
  1517         -            module.xFindFunction = new UnsafeNativeMethods.xFindFunction(xFindFunction);
  1518         -            module.xRename = new UnsafeNativeMethods.xRename(xRename);
  1519         -            module.xSavepoint = new UnsafeNativeMethods.xSavepoint(xSavepoint);
  1520         -            module.xRelease = new UnsafeNativeMethods.xRelease(xRelease);
  1521         -            module.xRollbackTo = new UnsafeNativeMethods.xRollbackTo(xRollbackTo);
  1522         -
  1523         -            return module;
         1510  +            if (nativeModule.iVersion != 0)
         1511  +                return nativeModule;
         1512  +
         1513  +            nativeModule = new UnsafeNativeMethods.sqlite3_module();
         1514  +            nativeModule.iVersion = 2;
         1515  +            nativeModule.xCreate = new UnsafeNativeMethods.xCreate(xCreate);
         1516  +            nativeModule.xConnect = new UnsafeNativeMethods.xConnect(xConnect);
         1517  +            nativeModule.xBestIndex = new UnsafeNativeMethods.xBestIndex(xBestIndex);
         1518  +            nativeModule.xDisconnect = new UnsafeNativeMethods.xDisconnect(xDisconnect);
         1519  +            nativeModule.xDestroy = new UnsafeNativeMethods.xDestroy(xDestroy);
         1520  +            nativeModule.xOpen = new UnsafeNativeMethods.xOpen(xOpen);
         1521  +            nativeModule.xClose = new UnsafeNativeMethods.xClose(xClose);
         1522  +            nativeModule.xFilter = new UnsafeNativeMethods.xFilter(xFilter);
         1523  +            nativeModule.xNext = new UnsafeNativeMethods.xNext(xNext);
         1524  +            nativeModule.xEof = new UnsafeNativeMethods.xEof(xEof);
         1525  +            nativeModule.xColumn = new UnsafeNativeMethods.xColumn(xColumn);
         1526  +            nativeModule.xRowId = new UnsafeNativeMethods.xRowId(xRowId);
         1527  +            nativeModule.xUpdate = new UnsafeNativeMethods.xUpdate(xUpdate);
         1528  +            nativeModule.xBegin = new UnsafeNativeMethods.xBegin(xBegin);
         1529  +            nativeModule.xSync = new UnsafeNativeMethods.xSync(xSync);
         1530  +            nativeModule.xCommit = new UnsafeNativeMethods.xCommit(xCommit);
         1531  +            nativeModule.xRollback = new UnsafeNativeMethods.xRollback(xRollback);
         1532  +            nativeModule.xFindFunction = new UnsafeNativeMethods.xFindFunction(xFindFunction);
         1533  +            nativeModule.xRename = new UnsafeNativeMethods.xRename(xRename);
         1534  +            nativeModule.xSavepoint = new UnsafeNativeMethods.xSavepoint(xSavepoint);
         1535  +            nativeModule.xRelease = new UnsafeNativeMethods.xRelease(xRelease);
         1536  +            nativeModule.xRollbackTo = new UnsafeNativeMethods.xRollbackTo(xRollbackTo);
         1537  +
         1538  +            return nativeModule;
  1524   1539           }
  1525   1540           #endregion
  1526   1541   
  1527   1542           ///////////////////////////////////////////////////////////////////////
  1528   1543   
  1529   1544           #region Public Constructors
  1530         -        public SQLiteModuleBase()
         1545  +        public SQLiteModuleBase(string name)
  1531   1546           {
  1532         -            // do nothing.
         1547  +            this.name = name;
  1533   1548           }
  1534   1549           #endregion
  1535   1550   
  1536   1551           ///////////////////////////////////////////////////////////////////////
  1537   1552   
  1538   1553           #region Protected Members
  1539   1554           protected virtual IntPtr AllocateTable()
................................................................................
  1985   2000               }
  1986   2001   
  1987   2002               return SQLiteErrorCode.Error;
  1988   2003           }
  1989   2004   
  1990   2005           ///////////////////////////////////////////////////////////////////////
  1991   2006   
  1992         -        public bool xEof(
         2007  +        public int xEof(
  1993   2008               IntPtr pCursor
  1994   2009               )
  1995   2010           {
  1996   2011               IntPtr pVtab = IntPtr.Zero;
  1997   2012   
  1998   2013               try
  1999   2014               {
  2000   2015                   pVtab = GetTableFromCursor(pCursor);
  2001   2016   
  2002   2017                   SQLiteVirtualTableCursor cursor = MarshalCursorFromIntPtr(
  2003   2018                       pCursor);
  2004   2019   
  2005         -                return Eof(cursor);
         2020  +                return Eof(cursor) ? 1 : 0;
  2006   2021               }
  2007   2022               catch (Exception e) /* NOTE: Must catch ALL. */
  2008   2023               {
  2009   2024                   SetTableError(pVtab, e.ToString());
  2010   2025               }
  2011   2026   
  2012         -            return true;
         2027  +            return 1;
  2013   2028           }
  2014   2029   
  2015   2030           ///////////////////////////////////////////////////////////////////////
  2016   2031   
  2017   2032           public SQLiteErrorCode xColumn(
  2018   2033               IntPtr pCursor,
  2019   2034               IntPtr pContext,
................................................................................
  2161   2176               }
  2162   2177   
  2163   2178               return SQLiteErrorCode.Error;
  2164   2179           }
  2165   2180   
  2166   2181           ///////////////////////////////////////////////////////////////////////
  2167   2182   
  2168         -        public bool xFindFunction(
         2183  +        public int xFindFunction(
  2169   2184               IntPtr pVtab,
  2170   2185               int nArg,
  2171   2186               IntPtr zName,
  2172   2187               ref SQLiteCallback callback,
  2173   2188               ref IntPtr pClientData
  2174   2189               )
  2175   2190           {
................................................................................
  2180   2195                   if (FindFunction(
  2181   2196                           nArg, SQLiteMarshal.StringFromUtf8IntPtr(zName),
  2182   2197                           ref function, ref pClientData))
  2183   2198                   {
  2184   2199                       if (function != null)
  2185   2200                       {
  2186   2201                           callback = function.ScalarCallback;
  2187         -                        return true;
         2202  +                        return 1;
  2188   2203                       }
  2189   2204                       else
  2190   2205                       {
  2191   2206                           SetTableError(pVtab, "no function was created");
  2192   2207                       }
  2193   2208                   }
  2194   2209               }
  2195   2210               catch (Exception e) /* NOTE: Must catch ALL. */
  2196   2211               {
  2197   2212                   SetTableError(pVtab, e.ToString());
  2198   2213               }
  2199   2214   
  2200         -            return false;
         2215  +            return 0;
  2201   2216           }
  2202   2217   
  2203   2218           ///////////////////////////////////////////////////////////////////////
  2204   2219   
  2205   2220           public SQLiteErrorCode xRename(
  2206   2221               IntPtr pVtab,
  2207   2222               IntPtr zNew
................................................................................
  2282   2297           #region ISQLiteManagedModule Members
  2283   2298           private bool declared;
  2284   2299           public bool Declared
  2285   2300           {
  2286   2301               get { return declared; }
  2287   2302               internal set { declared = value; }
  2288   2303           }
         2304  +
         2305  +        ///////////////////////////////////////////////////////////////////////
         2306  +
         2307  +        private string name;
         2308  +        public string Name
         2309  +        {
         2310  +            get { return name; }
         2311  +        }
  2289   2312   
  2290   2313           ///////////////////////////////////////////////////////////////////////
  2291   2314   
  2292   2315           public abstract SQLiteErrorCode Create(
  2293   2316               SQLiteConnection connection,
  2294   2317               IntPtr pClientData,
  2295   2318               string[] argv,

Changes to System.Data.SQLite/SQLiteModuleNoop.cs.

     5      5    * Released to the public domain, use at your own risk!
     6      6    ********************************************************/
     7      7   
     8      8   namespace System.Data.SQLite
     9      9   {
    10     10       public class SQLiteModuleNoop : SQLiteModuleBase
    11     11       {
           12  +        #region Public Constructors
           13  +        public SQLiteModuleNoop(string name)
           14  +            : base(name)
           15  +        {
           16  +            // do nothing.
           17  +        }
           18  +        #endregion
           19  +
           20  +        ///////////////////////////////////////////////////////////////////////
           21  +
           22  +        #region ISQLiteManagedModule Members
           23  +        public override SQLiteErrorCode Create(
           24  +            SQLiteConnection connection,
           25  +            IntPtr pClientData,
           26  +            string[] argv,
           27  +            ref string error
           28  +            )
           29  +        {
           30  +            CheckDisposed();
           31  +
           32  +            return SQLiteErrorCode.Ok;
           33  +        }
           34  +
           35  +        ///////////////////////////////////////////////////////////////////////
           36  +
           37  +        public override SQLiteErrorCode Connect(
           38  +            SQLiteConnection connection,
           39  +            IntPtr pClientData,
           40  +            string[] argv,
           41  +            ref string error
           42  +            )
           43  +        {
           44  +            CheckDisposed();
           45  +
           46  +            return SQLiteErrorCode.Ok;
           47  +        }
           48  +
           49  +        ///////////////////////////////////////////////////////////////////////
           50  +
           51  +        public override SQLiteErrorCode BestIndex(
           52  +            SQLiteIndex index
           53  +            )
           54  +        {
           55  +            CheckDisposed();
           56  +
           57  +            return SQLiteErrorCode.Ok;
           58  +        }
           59  +
           60  +        ///////////////////////////////////////////////////////////////////////
           61  +
           62  +        public override SQLiteErrorCode Disconnect()
           63  +        {
           64  +            CheckDisposed();
           65  +
           66  +            return SQLiteErrorCode.Ok;
           67  +        }
           68  +
           69  +        ///////////////////////////////////////////////////////////////////////
           70  +
           71  +        public override SQLiteErrorCode Destroy()
           72  +        {
           73  +            CheckDisposed();
           74  +
           75  +            return SQLiteErrorCode.Ok;
           76  +        }
           77  +
           78  +        ///////////////////////////////////////////////////////////////////////
           79  +
           80  +        public override SQLiteErrorCode Open(
           81  +            ref SQLiteVirtualTableCursor cursor
           82  +            )
           83  +        {
           84  +            CheckDisposed();
           85  +
           86  +            return SQLiteErrorCode.Ok;
           87  +        }
           88  +
           89  +        ///////////////////////////////////////////////////////////////////////
           90  +
           91  +        public override SQLiteErrorCode Close(
           92  +            SQLiteVirtualTableCursor cursor
           93  +            )
           94  +        {
           95  +            CheckDisposed();
           96  +
           97  +            return SQLiteErrorCode.Ok;
           98  +        }
           99  +
          100  +        ///////////////////////////////////////////////////////////////////////
          101  +
          102  +        public override SQLiteErrorCode Filter(
          103  +            SQLiteVirtualTableCursor cursor,
          104  +            int idxNum,
          105  +            string idxStr,
          106  +            SQLiteValue[] argv
          107  +            )
          108  +        {
          109  +            CheckDisposed();
          110  +
          111  +            return SQLiteErrorCode.Ok;
          112  +        }
          113  +
          114  +        ///////////////////////////////////////////////////////////////////////
          115  +
          116  +        public override SQLiteErrorCode Next(
          117  +            SQLiteVirtualTableCursor cursor
          118  +            )
          119  +        {
          120  +            CheckDisposed();
          121  +
          122  +            return SQLiteErrorCode.Ok;
          123  +        }
          124  +
          125  +        ///////////////////////////////////////////////////////////////////////
          126  +
          127  +        public override bool Eof(
          128  +            SQLiteVirtualTableCursor cursor
          129  +            )
          130  +        {
          131  +            CheckDisposed();
          132  +
          133  +            return true;
          134  +        }
          135  +
          136  +        ///////////////////////////////////////////////////////////////////////
          137  +
          138  +        public override SQLiteErrorCode Column(
          139  +            SQLiteVirtualTableCursor cursor,
          140  +            SQLiteContext context,
          141  +            int index
          142  +            )
          143  +        {
          144  +            CheckDisposed();
          145  +
          146  +            return SQLiteErrorCode.Ok;
          147  +        }
          148  +
          149  +        ///////////////////////////////////////////////////////////////////////
          150  +
          151  +        public override SQLiteErrorCode RowId(
          152  +            SQLiteVirtualTableCursor cursor,
          153  +            ref long rowId
          154  +            )
          155  +        {
          156  +            CheckDisposed();
          157  +
          158  +            return SQLiteErrorCode.Ok;
          159  +        }
          160  +
          161  +        ///////////////////////////////////////////////////////////////////////
          162  +
          163  +        public override SQLiteErrorCode Update(
          164  +            SQLiteValue[] values,
          165  +            ref long rowId
          166  +            )
          167  +        {
          168  +            CheckDisposed();
          169  +
          170  +            return SQLiteErrorCode.Ok;
          171  +        }
          172  +
          173  +        ///////////////////////////////////////////////////////////////////////
          174  +
          175  +        public override SQLiteErrorCode Begin()
          176  +        {
          177  +            CheckDisposed();
          178  +
          179  +            return SQLiteErrorCode.Ok;
          180  +        }
          181  +
          182  +        ///////////////////////////////////////////////////////////////////////
          183  +
          184  +        public override SQLiteErrorCode Sync()
          185  +        {
          186  +            CheckDisposed();
          187  +
          188  +            return SQLiteErrorCode.Ok;
          189  +        }
          190  +
          191  +        ///////////////////////////////////////////////////////////////////////
          192  +
          193  +        public override SQLiteErrorCode Commit()
          194  +        {
          195  +            CheckDisposed();
          196  +
          197  +            return SQLiteErrorCode.Ok;
          198  +        }
          199  +
          200  +        ///////////////////////////////////////////////////////////////////////
          201  +
          202  +        public override SQLiteErrorCode Rollback()
          203  +        {
          204  +            CheckDisposed();
          205  +
          206  +            return SQLiteErrorCode.Ok;
          207  +        }
          208  +
          209  +        ///////////////////////////////////////////////////////////////////////
          210  +
          211  +        public override bool FindFunction(
          212  +            int nArg,
          213  +            string zName,
          214  +            ref SQLiteFunction function,
          215  +            ref IntPtr pClientData
          216  +            )
          217  +        {
          218  +            CheckDisposed();
          219  +
          220  +            return false;
          221  +        }
          222  +
          223  +        ///////////////////////////////////////////////////////////////////////
          224  +
          225  +        public override SQLiteErrorCode Rename(
          226  +            string zNew
          227  +            )
          228  +        {
          229  +            CheckDisposed();
          230  +
          231  +            return SQLiteErrorCode.Ok;
          232  +        }
          233  +
          234  +        ///////////////////////////////////////////////////////////////////////
          235  +
          236  +        public override SQLiteErrorCode Savepoint(
          237  +            int iSavepoint
          238  +            )
          239  +        {
          240  +            CheckDisposed();
          241  +
          242  +            return SQLiteErrorCode.Ok;
          243  +        }
          244  +
          245  +        ///////////////////////////////////////////////////////////////////////
          246  +
          247  +        public override SQLiteErrorCode Release(
          248  +            int iSavepoint
          249  +            )
          250  +        {
          251  +            CheckDisposed();
          252  +
          253  +            return SQLiteErrorCode.Ok;
          254  +        }
          255  +
          256  +        ///////////////////////////////////////////////////////////////////////
          257  +
          258  +        public override SQLiteErrorCode RollbackTo(
          259  +            int iSavepoint
          260  +            )
          261  +        {
          262  +            CheckDisposed();
          263  +
          264  +            return SQLiteErrorCode.Ok;
          265  +        }
          266  +        #endregion
          267  +
          268  +        ///////////////////////////////////////////////////////////////////////
          269  +
    12    270           #region IDisposable "Pattern" Members
    13    271           private bool disposed;
    14    272           private void CheckDisposed() /* throw */
    15    273           {
    16    274   #if THROW_ON_DISPOSED
    17    275               if (disposed)
    18    276                   throw new ObjectDisposedException(typeof(SQLiteModuleNoop).Name);
................................................................................
    42    300                   }
    43    301               }
    44    302               finally
    45    303               {
    46    304                   base.Dispose(disposing);
    47    305               }
    48    306           }
    49         -        #endregion
    50         -
    51         -        ///////////////////////////////////////////////////////////////////////
    52         -
    53         -        #region ISQLiteManagedModule Members
    54         -        public override SQLiteErrorCode Create(
    55         -            SQLiteConnection connection,
    56         -            IntPtr pClientData,
    57         -            string[] argv,
    58         -            ref string error
    59         -            )
    60         -        {
    61         -            CheckDisposed();
    62         -            throw new NotImplementedException();
    63         -        }
    64         -
    65         -        ///////////////////////////////////////////////////////////////////////
    66         -
    67         -        public override SQLiteErrorCode Connect(
    68         -            SQLiteConnection connection,
    69         -            IntPtr pClientData,
    70         -            string[] argv,
    71         -            ref string error
    72         -            )
    73         -        {
    74         -            CheckDisposed();
    75         -            throw new NotImplementedException();
    76         -        }
    77         -
    78         -        ///////////////////////////////////////////////////////////////////////
    79         -
    80         -        public override SQLiteErrorCode BestIndex(
    81         -            SQLiteIndex index
    82         -            )
    83         -        {
    84         -            CheckDisposed();
    85         -            throw new NotImplementedException();
    86         -        }
    87         -
    88         -        ///////////////////////////////////////////////////////////////////////
    89         -
    90         -        public override SQLiteErrorCode Disconnect()
    91         -        {
    92         -            CheckDisposed();
    93         -            throw new NotImplementedException();
    94         -        }
    95         -
    96         -        ///////////////////////////////////////////////////////////////////////
    97         -
    98         -        public override SQLiteErrorCode Destroy()
    99         -        {
   100         -            CheckDisposed();
   101         -            throw new NotImplementedException();
   102         -        }
   103         -
   104         -        ///////////////////////////////////////////////////////////////////////
   105         -
   106         -        public override SQLiteErrorCode Open(
   107         -            ref SQLiteVirtualTableCursor cursor
   108         -            )
   109         -        {
   110         -            CheckDisposed();
   111         -            throw new NotImplementedException();
   112         -        }
   113         -
   114         -        ///////////////////////////////////////////////////////////////////////
   115         -
   116         -        public override SQLiteErrorCode Close(
   117         -            SQLiteVirtualTableCursor cursor
   118         -            )
   119         -        {
   120         -            CheckDisposed();
   121         -            throw new NotImplementedException();
   122         -        }
   123         -
   124         -        ///////////////////////////////////////////////////////////////////////
   125         -
   126         -        public override SQLiteErrorCode Filter(
   127         -            SQLiteVirtualTableCursor cursor,
   128         -            int idxNum,
   129         -            string idxStr,
   130         -            SQLiteValue[] argv
   131         -            )
   132         -        {
   133         -            CheckDisposed();
   134         -            throw new NotImplementedException();
   135         -        }
   136         -
   137         -        ///////////////////////////////////////////////////////////////////////
   138         -
   139         -        public override SQLiteErrorCode Next(
   140         -            SQLiteVirtualTableCursor cursor
   141         -            )
   142         -        {
   143         -            CheckDisposed();
   144         -            throw new NotImplementedException();
   145         -        }
   146         -
   147         -        ///////////////////////////////////////////////////////////////////////
   148         -
   149         -        public override bool Eof(
   150         -            SQLiteVirtualTableCursor cursor
   151         -            )
   152         -        {
   153         -            CheckDisposed();
   154         -            throw new NotImplementedException();
   155         -        }
   156         -
   157         -        ///////////////////////////////////////////////////////////////////////
   158         -
   159         -        public override SQLiteErrorCode Column(
   160         -            SQLiteVirtualTableCursor cursor,
   161         -            SQLiteContext context,
   162         -            int index
   163         -            )
   164         -        {
   165         -            CheckDisposed();
   166         -            throw new NotImplementedException();
   167         -        }
   168         -
   169         -        ///////////////////////////////////////////////////////////////////////
   170         -
   171         -        public override SQLiteErrorCode RowId(
   172         -            SQLiteVirtualTableCursor cursor,
   173         -            ref long rowId
   174         -            )
   175         -        {
   176         -            CheckDisposed();
   177         -            throw new NotImplementedException();
   178         -        }
   179         -
   180         -        ///////////////////////////////////////////////////////////////////////
   181         -
   182         -        public override SQLiteErrorCode Update(
   183         -            SQLiteValue[] values,
   184         -            ref long rowId
   185         -            )
   186         -        {
   187         -            CheckDisposed();
   188         -            throw new NotImplementedException();
   189         -        }
   190         -
   191         -        ///////////////////////////////////////////////////////////////////////
   192         -
   193         -        public override SQLiteErrorCode Begin()
   194         -        {
   195         -            CheckDisposed();
   196         -            throw new NotImplementedException();
   197         -        }
   198         -
   199         -        ///////////////////////////////////////////////////////////////////////
   200         -
   201         -        public override SQLiteErrorCode Sync()
   202         -        {
   203         -            CheckDisposed();
   204         -            throw new NotImplementedException();
   205         -        }
   206         -
   207         -        ///////////////////////////////////////////////////////////////////////
   208         -
   209         -        public override SQLiteErrorCode Commit()
   210         -        {
   211         -            CheckDisposed();
   212         -            throw new NotImplementedException();
   213         -        }
   214         -
   215         -        ///////////////////////////////////////////////////////////////////////
   216         -
   217         -        public override SQLiteErrorCode Rollback()
   218         -        {
   219         -            CheckDisposed();
   220         -            throw new NotImplementedException();
   221         -        }
   222         -
   223         -        ///////////////////////////////////////////////////////////////////////
   224         -
   225         -        public override bool FindFunction(
   226         -            int nArg,
   227         -            string zName,
   228         -            ref SQLiteFunction function,
   229         -            ref IntPtr pClientData
   230         -            )
   231         -        {
   232         -            CheckDisposed();
   233         -            throw new NotImplementedException();
   234         -        }
   235         -
   236         -        ///////////////////////////////////////////////////////////////////////
   237         -
   238         -        public override SQLiteErrorCode Rename(
   239         -            string zNew
   240         -            )
   241         -        {
   242         -            CheckDisposed();
   243         -            throw new NotImplementedException();
   244         -        }
   245         -
   246         -        ///////////////////////////////////////////////////////////////////////
   247         -
   248         -        public override SQLiteErrorCode Savepoint(
   249         -            int iSavepoint
   250         -            )
   251         -        {
   252         -            CheckDisposed();
   253         -            throw new NotImplementedException();
   254         -        }
   255         -
   256         -        ///////////////////////////////////////////////////////////////////////
   257         -
   258         -        public override SQLiteErrorCode Release(
   259         -            int iSavepoint
   260         -            )
   261         -        {
   262         -            CheckDisposed();
   263         -            throw new NotImplementedException();
   264         -        }
   265         -
   266         -        ///////////////////////////////////////////////////////////////////////
   267         -
   268         -        public override SQLiteErrorCode RollbackTo(
   269         -            int iSavepoint
   270         -            )
   271         -        {
   272         -            CheckDisposed();
   273         -            throw new NotImplementedException();
   274         -        }
   275    307           #endregion
   276    308       }
   277    309   }

Changes to System.Data.SQLite/UnsafeNativeMethods.cs.

   628    628       //       parameters and handle column values of types Int64 and Double).
   629    629       //
   630    630       internal const string SQLITE_DLL = "SQLite.Interop.087.dll";
   631    631   #elif SQLITE_STANDARD
   632    632       //
   633    633       // NOTE: Otherwise, if the standard SQLite library is enabled, use it.
   634    634       //
   635         -    private const string SQLITE_DLL = "sqlite3";
          635  +    internal const string SQLITE_DLL = "sqlite3";
   636    636   #elif USE_INTEROP_DLL
   637         -    //
          637  +      //
   638    638       // NOTE: Otherwise, if the native SQLite interop assembly is enabled,
   639    639       //       use it.
   640    640       //
   641         -    private const string SQLITE_DLL = "SQLite.Interop.dll";
          641  +    internal const string SQLITE_DLL = "SQLite.Interop.dll";
   642    642   #else
   643    643       //
   644    644       // NOTE: Finally, assume that the mixed-mode assembly is being used.
   645    645       //
   646         -    private const string SQLITE_DLL = "System.Data.SQLite.dll";
          646  +    internal const string SQLITE_DLL = "System.Data.SQLite.dll";
   647    647   #endif
   648    648   
   649    649       // This section uses interop calls that also fetch text length to optimize conversion.
   650    650       // When using the standard dll, we can replace these calls with normal sqlite calls and
   651    651       // do unoptimized conversions instead afterwards
   652    652       #region interop added textlength calls
   653    653   
................................................................................
  1594   1594       [DllImport(SQLITE_DLL)]
  1595   1595   #endif
  1596   1596       internal static extern int sqlite3_backup_pagecount(IntPtr backup);
  1597   1597   
  1598   1598   #if !PLATFORM_COMPACTFRAMEWORK
  1599   1599       [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
  1600   1600   #else
  1601         -    [DllImport(SQLITE_DLL)]
  1602         -#endif
  1603         -    internal static extern SQLiteErrorCode sqlite3_create_module_v2(IntPtr db, IntPtr name, IntPtr pModule, IntPtr pClientData, xDestroyModule xDestroy);
  1604         -
  1605         -#if !PLATFORM_COMPACTFRAMEWORK
  1606         -    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
  1607         -#else
  1608   1601       [DllImport(SQLITE_DLL)]
  1609   1602   #endif
  1610   1603       internal static extern SQLiteErrorCode sqlite3_declare_vtab(IntPtr db, IntPtr zSQL);
  1611   1604   
  1612   1605   #if !PLATFORM_COMPACTFRAMEWORK
  1613   1606       [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
  1614   1607   #else
  1615   1608       [DllImport(SQLITE_DLL)]
  1616   1609   #endif
  1617   1610       internal static extern IntPtr sqlite3_mprintf(IntPtr format, __arglist);
  1618   1611       #endregion
         1612  +
         1613  +    // SQLite API calls that are provided by "well-known" extensions that may be statically
         1614  +    // linked with the SQLite core native library currently in use.
         1615  +    #region extension sqlite api calls
         1616  +#if !PLATFORM_COMPACTFRAMEWORK
         1617  +    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
         1618  +#else
         1619  +    [DllImport(SQLITE_DLL)]
         1620  +#endif
         1621  +    internal static extern IntPtr sqlite3_create_disposable_module(IntPtr db, IntPtr name, ref sqlite3_module module, IntPtr pClientData, xDestroyModule xDestroy);
         1622  +
         1623  +#if !PLATFORM_COMPACTFRAMEWORK
         1624  +    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
         1625  +#else
         1626  +    [DllImport(SQLITE_DLL)]
         1627  +#endif
         1628  +    internal static extern void sqlite3_dispose_module(ref sqlite3_module pModule);
         1629  +    #endregion
  1619   1630   
  1620   1631       ///////////////////////////////////////////////////////////////////////////
  1621   1632   
  1622   1633       #region Native Delegates
  1623   1634   #if !PLATFORM_COMPACTFRAMEWORK
  1624   1635       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1625   1636   #endif
................................................................................
  1718   1729       );
  1719   1730   
  1720   1731       ///////////////////////////////////////////////////////////////////////////
  1721   1732   
  1722   1733   #if !PLATFORM_COMPACTFRAMEWORK
  1723   1734       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1724   1735   #endif
  1725         -    [return: MarshalAs(UnmanagedType.I4)]
  1726         -    public delegate bool xEof(
         1736  +    public delegate int xEof(
  1727   1737           IntPtr pCursor
  1728   1738       );
  1729   1739   
  1730   1740       ///////////////////////////////////////////////////////////////////////////
  1731   1741   
  1732   1742   #if !PLATFORM_COMPACTFRAMEWORK
  1733   1743       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
................................................................................
  1797   1807       );
  1798   1808   
  1799   1809       ///////////////////////////////////////////////////////////////////////////
  1800   1810   
  1801   1811   #if !PLATFORM_COMPACTFRAMEWORK
  1802   1812       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1803   1813   #endif
  1804         -    [return: MarshalAs(UnmanagedType.I4)]
  1805         -    public delegate bool xFindFunction(
         1814  +    public delegate int xFindFunction(
  1806   1815           IntPtr pVtab,
  1807   1816           int nArg,
  1808   1817           IntPtr zName,
  1809   1818           ref SQLiteCallback callback,
  1810   1819           ref IntPtr pUserData
  1811   1820       );
  1812   1821   

Added Tests/vtab.eagle.

            1  +###############################################################################
            2  +#
            3  +# vtab.eagle --
            4  +#
            5  +# Written by Joe Mistachkin.
            6  +# Released to the public domain, use at your own risk!
            7  +#
            8  +###############################################################################
            9  +
           10  +package require Eagle
           11  +package require Eagle.Library
           12  +package require Eagle.Test
           13  +
           14  +runTestPrologue
           15  +
           16  +###############################################################################
           17  +
           18  +package require System.Data.SQLite.Test
           19  +runSQLiteTestPrologue
           20  +
           21  +###############################################################################
           22  +
           23  +runTest {test vtab-1.1 {virtual table support} -setup {
           24  +  setupDb [set fileName vtab-1.1.db]
           25  +} -body {
           26  +  set id [object invoke Interpreter.GetActive NextId]
           27  +  set dataSource [file join [getDatabaseDirectory] $fileName]
           28  +
           29  +  set sql { \
           30  +    CREATE VIRTUAL TABLE t${id} USING vt${id}; \
           31  +  }
           32  +
           33  +  unset -nocomplain results errors
           34  +
           35  +  set code [compileCSharpWith [subst {
           36  +    using System.Data.SQLite;
           37  +
           38  +    namespace _Dynamic${id}
           39  +    {
           40  +      public sealed class SQLiteModuleTest${id} : SQLiteModuleNoop
           41  +      {
           42  +        public SQLiteModuleTest${id}(string name)
           43  +          : base(name)
           44  +        {
           45  +          // do nothing.
           46  +        }
           47  +      }
           48  +
           49  +      /////////////////////////////////////////////////////////////////////////
           50  +
           51  +      public static class Test${id}
           52  +      {
           53  +        public static void Main()
           54  +        {
           55  +          using (SQLiteConnection connection = new SQLiteConnection(
           56  +              "Data Source=${dataSource};"))
           57  +          {
           58  +            connection.Open();
           59  +            connection.CreateModule(new SQLiteModuleTest${id}("vt${id}"));
           60  +
           61  +            using (SQLiteCommand command = connection.CreateCommand())
           62  +            {
           63  +              command.CommandText = "[subst ${sql}]";
           64  +              command.ExecuteNonQuery();
           65  +            }
           66  +
           67  +            connection.Close();
           68  +          }
           69  +        }
           70  +      }
           71  +    }
           72  +  }] true true true results errors System.Data.SQLite.dll]
           73  +
           74  +  list $code $results \
           75  +      [expr {[info exists errors] ? $errors : ""}] \
           76  +      [expr {$code eq "Ok" ? [catch {
           77  +        object invoke _Dynamic${id}.Test${id} Main
           78  +      } result] : [set result ""]}] $result
           79  +} -cleanup {
           80  +  cleanupDb $fileName
           81  +
           82  +  unset -nocomplain result code results errors sql dataSource id db fileName
           83  +} -constraints [fixConstraints {eagle monoBug28 command.sql compile.DATA SQLite System.Data.SQLite}] -match regexp -result {^Ok System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 \{\}$}}
           84  +
           85  +###############################################################################
           86  +
           87  +runSQLiteTestEpilogue
           88  +runTestEpilogue