| | @@ -352,11 +352,11 @@ |
| 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. |
| | @@ -431,24 +431,16 @@ |
| 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 | */ |
| | @@ -678,11 +670,11 @@ |
| 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-15 18:34:17 00231fb0127960d700de3549e34e82f8ec1b5819" |
| 675 | +#define SQLITE_SOURCE_ID "2013-06-19 18:01:44 d97898e8e3990ae8c1882c9102b57692d8810730" |
| 684 | 676 | |
| 685 | 677 | /* |
| 686 | 678 | ** CAPI3REF: Run-Time Library Version Numbers |
| 687 | 679 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 688 | 680 | ** |
| | @@ -5087,10 +5079,15 @@ |
| 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. |
| | @@ -5100,10 +5097,15 @@ |
| 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 | */ |
| | @@ -8151,10 +8153,16 @@ |
| 8151 | 8153 | */ |
| 8152 | 8154 | #ifndef offsetof |
| 8153 | 8155 | #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) |
| 8154 | 8156 | #endif |
| 8155 | 8157 | |
| 8158 | +/* |
| 8159 | +** Macros to compute minimum and maximum of two numbers. |
| 8160 | +*/ |
| 8161 | +#define MIN(A,B) ((A)<(B)?(A):(B)) |
| 8162 | +#define MAX(A,B) ((A)>(B)?(A):(B)) |
| 8163 | + |
| 8156 | 8164 | /* |
| 8157 | 8165 | ** Check to see if this machine uses EBCDIC. (Yes, believe it or |
| 8158 | 8166 | ** not, there are still machines out there that use EBCDIC.) |
| 8159 | 8167 | */ |
| 8160 | 8168 | #if 'A' == '\301' |
| | @@ -8476,13 +8484,11 @@ |
| 8476 | 8484 | typedef struct TriggerStep TriggerStep; |
| 8477 | 8485 | typedef struct UnpackedRecord UnpackedRecord; |
| 8478 | 8486 | typedef struct VTable VTable; |
| 8479 | 8487 | typedef struct VtabCtx VtabCtx; |
| 8480 | 8488 | typedef struct Walker Walker; |
| 8481 | | -typedef struct WherePlan WherePlan; |
| 8482 | 8489 | typedef struct WhereInfo WhereInfo; |
| 8483 | | -typedef struct WhereLevel WhereLevel; |
| 8484 | 8490 | |
| 8485 | 8491 | /* |
| 8486 | 8492 | ** Defer sourcing vdbe.h and btree.h until after the "u8" and |
| 8487 | 8493 | ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque |
| 8488 | 8494 | ** pointer types (i.e. FuncDef) defined above. |
| | @@ -9915,11 +9921,10 @@ |
| 9915 | 9921 | ** databases may be attached. |
| 9916 | 9922 | */ |
| 9917 | 9923 | struct Db { |
| 9918 | 9924 | char *zName; /* Name of this database */ |
| 9919 | 9925 | Btree *pBt; /* The B*Tree structure for this database file */ |
| 9920 | | - u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */ |
| 9921 | 9926 | u8 safety_level; /* How aggressive at syncing data to disk */ |
| 9922 | 9927 | Schema *pSchema; /* Pointer to database schema (possibly shared) */ |
| 9923 | 9928 | }; |
| 9924 | 9929 | |
| 9925 | 9930 | /* |
| | @@ -10713,10 +10718,11 @@ |
| 10713 | 10718 | int tnum; /* DB Page containing root of this index */ |
| 10714 | 10719 | u16 nColumn; /* Number of columns in table used by this index */ |
| 10715 | 10720 | u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 10716 | 10721 | unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ |
| 10717 | 10722 | unsigned bUnordered:1; /* Use this index for == or IN queries only */ |
| 10723 | + unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ |
| 10718 | 10724 | #ifdef SQLITE_ENABLE_STAT3 |
| 10719 | 10725 | int nSample; /* Number of elements in aSample[] */ |
| 10720 | 10726 | tRowcnt avgEq; /* Average nEq value for key values not in aSample */ |
| 10721 | 10727 | IndexSample *aSample; /* Samples of the left-most key */ |
| 10722 | 10728 | #endif |
| | @@ -11058,10 +11064,15 @@ |
| 11058 | 11064 | /* |
| 11059 | 11065 | ** The number of bits in a Bitmask. "BMS" means "BitMask Size". |
| 11060 | 11066 | */ |
| 11061 | 11067 | #define BMS ((int)(sizeof(Bitmask)*8)) |
| 11062 | 11068 | |
| 11069 | +/* |
| 11070 | +** A bit in a Bitmask |
| 11071 | +*/ |
| 11072 | +#define MASKBIT(n) (((Bitmask)1)<<(n)) |
| 11073 | + |
| 11063 | 11074 | /* |
| 11064 | 11075 | ** The following structure describes the FROM clause of a SELECT statement. |
| 11065 | 11076 | ** Each table or subquery in the FROM clause is a separate element of |
| 11066 | 11077 | ** the SrcList.a[] array. |
| 11067 | 11078 | ** |
| | @@ -11078,12 +11089,12 @@ |
| 11078 | 11089 | ** |
| 11079 | 11090 | ** In the colUsed field, the high-order bit (bit 63) is set if the table |
| 11080 | 11091 | ** contains more than 63 columns and the 64-th or later column is used. |
| 11081 | 11092 | */ |
| 11082 | 11093 | struct SrcList { |
| 11083 | | - i16 nSrc; /* Number of tables or subqueries in the FROM clause */ |
| 11084 | | - i16 nAlloc; /* Number of entries allocated in a[] below */ |
| 11094 | + u8 nSrc; /* Number of tables or subqueries in the FROM clause */ |
| 11095 | + u8 nAlloc; /* Number of entries allocated in a[] below */ |
| 11085 | 11096 | struct SrcList_item { |
| 11086 | 11097 | Schema *pSchema; /* Schema to which this item is fixed */ |
| 11087 | 11098 | char *zDatabase; /* Name of database holding this table */ |
| 11088 | 11099 | char *zName; /* Name of the table */ |
| 11089 | 11100 | char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ |
| | @@ -11117,83 +11128,10 @@ |
| 11117 | 11128 | #define JT_RIGHT 0x0010 /* Right outer join */ |
| 11118 | 11129 | #define JT_OUTER 0x0020 /* The "OUTER" keyword is present */ |
| 11119 | 11130 | #define JT_ERROR 0x0040 /* unknown or unsupported join type */ |
| 11120 | 11131 | |
| 11121 | 11132 | |
| 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 | 11133 | /* |
| 11196 | 11134 | ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin() |
| 11197 | 11135 | ** and the WhereInfo.wctrlFlags member. |
| 11198 | 11136 | */ |
| 11199 | 11137 | #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */ |
| | @@ -11203,37 +11141,15 @@ |
| 11203 | 11141 | #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ |
| 11204 | 11142 | #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ |
| 11205 | 11143 | #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ |
| 11206 | 11144 | #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ |
| 11207 | 11145 | #define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */ |
| 11208 | | - |
| 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. |
| 11215 | | -*/ |
| 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 */ |
| 11146 | +#define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */ |
| 11147 | +#define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */ |
| 11148 | + |
| 11149 | +/* Allowed return values from sqlite3WhereIsDistinct() |
| 11150 | +*/ |
| 11235 | 11151 | #define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */ |
| 11236 | 11152 | #define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */ |
| 11237 | 11153 | #define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */ |
| 11238 | 11154 | #define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */ |
| 11239 | 11155 | |
| | @@ -11303,11 +11219,11 @@ |
| 11303 | 11219 | ExprList *pEList; /* The fields of the result */ |
| 11304 | 11220 | u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ |
| 11305 | 11221 | u16 selFlags; /* Various SF_* values */ |
| 11306 | 11222 | int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ |
| 11307 | 11223 | int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */ |
| 11308 | | - double nSelectRow; /* Estimated number of result rows */ |
| 11224 | + u64 nSelectRow; /* Estimated number of result rows */ |
| 11309 | 11225 | SrcList *pSrc; /* The FROM clause */ |
| 11310 | 11226 | Expr *pWhere; /* The WHERE clause */ |
| 11311 | 11227 | ExprList *pGroupBy; /* The GROUP BY clause */ |
| 11312 | 11228 | Expr *pHaving; /* The HAVING clause */ |
| 11313 | 11229 | ExprList *pOrderBy; /* The ORDER BY clause */ |
| | @@ -11487,11 +11403,11 @@ |
| 11487 | 11403 | AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ |
| 11488 | 11404 | |
| 11489 | 11405 | /* Information used while coding trigger programs. */ |
| 11490 | 11406 | Parse *pToplevel; /* Parse structure for main program (or NULL) */ |
| 11491 | 11407 | Table *pTriggerTab; /* Table triggers are being coded for */ |
| 11492 | | - double nQueryLoop; /* Estimated number of iterations of a query */ |
| 11408 | + u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ |
| 11493 | 11409 | u32 oldmask; /* Mask of old.* columns referenced */ |
| 11494 | 11410 | u32 newmask; /* Mask of new.* columns referenced */ |
| 11495 | 11411 | u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ |
| 11496 | 11412 | u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ |
| 11497 | 11413 | u8 disableTriggers; /* True to disable triggers */ |
| | @@ -12057,10 +11973,16 @@ |
| 12057 | 11973 | #endif |
| 12058 | 11974 | SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*); |
| 12059 | 11975 | SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int); |
| 12060 | 11976 | SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); |
| 12061 | 11977 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*); |
| 11978 | +SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*); |
| 11979 | +SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*); |
| 11980 | +SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*); |
| 11981 | +SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*); |
| 11982 | +SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*); |
| 11983 | +SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*); |
| 12062 | 11984 | SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8); |
| 12063 | 11985 | SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int); |
| 12064 | 11986 | SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int); |
| 12065 | 11987 | SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int); |
| 12066 | 11988 | SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*); |
| | @@ -19960,17 +19882,11 @@ |
| 19960 | 19882 | if( flag_plussign ) prefix = '+'; |
| 19961 | 19883 | else if( flag_blanksign ) prefix = ' '; |
| 19962 | 19884 | else prefix = 0; |
| 19963 | 19885 | } |
| 19964 | 19886 | 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 | 19887 | for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} |
| 19971 | | -#endif |
| 19972 | 19888 | if( xtype==etFLOAT ) realvalue += rounder; |
| 19973 | 19889 | /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 19974 | 19890 | exp = 0; |
| 19975 | 19891 | if( sqlite3IsNaN((double)realvalue) ){ |
| 19976 | 19892 | bufpt = "NaN"; |
| | @@ -26866,19 +26782,23 @@ |
| 26866 | 26782 | } |
| 26867 | 26783 | return SQLITE_OK; |
| 26868 | 26784 | } |
| 26869 | 26785 | case SQLITE_FCNTL_MMAP_SIZE: { |
| 26870 | 26786 | i64 newLimit = *(i64*)pArg; |
| 26787 | + int rc = SQLITE_OK; |
| 26871 | 26788 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 26872 | 26789 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 26873 | 26790 | } |
| 26874 | 26791 | *(i64*)pArg = pFile->mmapSizeMax; |
| 26875 | | - if( newLimit>=0 ){ |
| 26792 | + if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
| 26876 | 26793 | pFile->mmapSizeMax = newLimit; |
| 26877 | | - if( newLimit<pFile->mmapSize ) pFile->mmapSize = newLimit; |
| 26794 | + if( pFile->mmapSize>0 ){ |
| 26795 | + unixUnmapfile(pFile); |
| 26796 | + rc = unixMapfile(pFile, -1); |
| 26797 | + } |
| 26878 | 26798 | } |
| 26879 | | - return SQLITE_OK; |
| 26799 | + return rc; |
| 26880 | 26800 | } |
| 26881 | 26801 | #ifdef SQLITE_DEBUG |
| 26882 | 26802 | /* The pager calls this method to signal that it has done |
| 26883 | 26803 | ** a rollback and that the database is therefore unchanged and |
| 26884 | 26804 | ** it hence it is OK for the transaction change counter to be |
| | @@ -28244,11 +28164,11 @@ |
| 28244 | 28164 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
| 28245 | 28165 | pNew->h = h; |
| 28246 | 28166 | pNew->pVfs = pVfs; |
| 28247 | 28167 | pNew->zPath = zFilename; |
| 28248 | 28168 | pNew->ctrlFlags = (u8)ctrlFlags; |
| 28249 | | - pNew->mmapSizeMax = sqlite3GlobalConfig.mxMmap; |
| 28169 | + pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
| 28250 | 28170 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 28251 | 28171 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 28252 | 28172 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
| 28253 | 28173 | } |
| 28254 | 28174 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
| | @@ -30799,17 +30719,10 @@ |
| 30799 | 30719 | ** This file mapping API is common to both Win32 and WinRT. |
| 30800 | 30720 | */ |
| 30801 | 30721 | WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
| 30802 | 30722 | #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */ |
| 30803 | 30723 | |
| 30804 | | -/* |
| 30805 | | -** Macro to find the minimum of two numeric values. |
| 30806 | | -*/ |
| 30807 | | -#ifndef MIN |
| 30808 | | -# define MIN(x,y) ((x)<(y)?(x):(y)) |
| 30809 | | -#endif |
| 30810 | | - |
| 30811 | 30724 | /* |
| 30812 | 30725 | ** Some Microsoft compilers lack this definition. |
| 30813 | 30726 | */ |
| 30814 | 30727 | #ifndef INVALID_FILE_ATTRIBUTES |
| 30815 | 30728 | # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
| | @@ -33531,10 +33444,13 @@ |
| 33531 | 33444 | } |
| 33532 | 33445 | } |
| 33533 | 33446 | |
| 33534 | 33447 | /* Forward declaration */ |
| 33535 | 33448 | static int getTempname(int nBuf, char *zBuf); |
| 33449 | +#if SQLITE_MAX_MMAP_SIZE>0 |
| 33450 | +static int winMapfile(winFile*, sqlite3_int64); |
| 33451 | +#endif |
| 33536 | 33452 | |
| 33537 | 33453 | /* |
| 33538 | 33454 | ** Control and query of the open file handle. |
| 33539 | 33455 | */ |
| 33540 | 33456 | static int winFileControl(sqlite3_file *id, int op, void *pArg){ |
| | @@ -33614,17 +33530,24 @@ |
| 33614 | 33530 | return SQLITE_OK; |
| 33615 | 33531 | } |
| 33616 | 33532 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 33617 | 33533 | case SQLITE_FCNTL_MMAP_SIZE: { |
| 33618 | 33534 | i64 newLimit = *(i64*)pArg; |
| 33535 | + int rc = SQLITE_OK; |
| 33619 | 33536 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 33620 | 33537 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 33621 | 33538 | } |
| 33622 | 33539 | *(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; |
| 33540 | + if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
| 33541 | + pFile->mmapSizeMax = newLimit; |
| 33542 | + if( pFile->mmapSize>0 ){ |
| 33543 | + (void)winUnmapfile(pFile); |
| 33544 | + rc = winMapfile(pFile, -1); |
| 33545 | + } |
| 33546 | + } |
| 33547 | + OSTRACE(("FCNTL file=%p, rc=%d\n", pFile->h, rc)); |
| 33548 | + return rc; |
| 33626 | 33549 | } |
| 33627 | 33550 | #endif |
| 33628 | 33551 | } |
| 33629 | 33552 | OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h)); |
| 33630 | 33553 | return SQLITE_NOTFOUND; |
| | @@ -33652,19 +33575,19 @@ |
| 33652 | 33575 | winFile *p = (winFile*)id; |
| 33653 | 33576 | return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | |
| 33654 | 33577 | ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0); |
| 33655 | 33578 | } |
| 33656 | 33579 | |
| 33657 | | -#ifndef SQLITE_OMIT_WAL |
| 33658 | | - |
| 33659 | 33580 | /* |
| 33660 | 33581 | ** Windows will only let you create file view mappings |
| 33661 | 33582 | ** on allocation size granularity boundaries. |
| 33662 | 33583 | ** During sqlite3_os_init() we do a GetSystemInfo() |
| 33663 | 33584 | ** to get the granularity size. |
| 33664 | 33585 | */ |
| 33665 | 33586 | SYSTEM_INFO winSysInfo; |
| 33587 | + |
| 33588 | +#ifndef SQLITE_OMIT_WAL |
| 33666 | 33589 | |
| 33667 | 33590 | /* |
| 33668 | 33591 | ** Helper functions to obtain and relinquish the global mutex. The |
| 33669 | 33592 | ** global mutex is used to protect the winLockInfo objects used by |
| 33670 | 33593 | ** this file, all of which may be shared by multiple threads. |
| | @@ -34961,11 +34884,11 @@ |
| 34961 | 34884 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 34962 | 34885 | pFile->hMap = NULL; |
| 34963 | 34886 | pFile->pMapRegion = 0; |
| 34964 | 34887 | pFile->mmapSize = 0; |
| 34965 | 34888 | pFile->mmapSizeActual = 0; |
| 34966 | | - pFile->mmapSizeMax = sqlite3GlobalConfig.mxMmap; |
| 34889 | + pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
| 34967 | 34890 | #endif |
| 34968 | 34891 | |
| 34969 | 34892 | OpenCounter(+1); |
| 34970 | 34893 | return rc; |
| 34971 | 34894 | } |
| | @@ -37220,11 +37143,11 @@ |
| 37220 | 37143 | PCache1 *pCache; /* The newly created page cache */ |
| 37221 | 37144 | PGroup *pGroup; /* The group the new page cache will belong to */ |
| 37222 | 37145 | int sz; /* Bytes of memory required to allocate the new cache */ |
| 37223 | 37146 | |
| 37224 | 37147 | /* |
| 37225 | | - ** The seperateCache variable is true if each PCache has its own private |
| 37148 | + ** The separateCache variable is true if each PCache has its own private |
| 37226 | 37149 | ** PGroup. In other words, separateCache is true for mode (1) where no |
| 37227 | 37150 | ** mutexing is required. |
| 37228 | 37151 | ** |
| 37229 | 37152 | ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT |
| 37230 | 37153 | ** |
| | @@ -42544,11 +42467,12 @@ |
| 42544 | 42467 | /* Before the first write, give the VFS a hint of what the final |
| 42545 | 42468 | ** file size will be. |
| 42546 | 42469 | */ |
| 42547 | 42470 | assert( rc!=SQLITE_OK || isOpen(pPager->fd) ); |
| 42548 | 42471 | if( rc==SQLITE_OK |
| 42549 | | - && (pList->pDirty ? pPager->dbSize : pList->pgno+1)>pPager->dbHintSize |
| 42472 | + && pPager->dbHintSize<pPager->dbSize |
| 42473 | + && (pList->pDirty || pList->pgno>pPager->dbHintSize) |
| 42550 | 42474 | ){ |
| 42551 | 42475 | sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize; |
| 42552 | 42476 | sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile); |
| 42553 | 42477 | pPager->dbHintSize = pPager->dbSize; |
| 42554 | 42478 | } |
| | @@ -43509,11 +43433,11 @@ |
| 43509 | 43433 | ** requested page is not already stored in the cache, then no |
| 43510 | 43434 | ** actual disk read occurs. In this case the memory image of the |
| 43511 | 43435 | ** page is initialized to all zeros. |
| 43512 | 43436 | ** |
| 43513 | 43437 | ** If noContent is true, it means that we do not care about the contents |
| 43514 | | -** of the page. This occurs in two seperate scenarios: |
| 43438 | +** of the page. This occurs in two scenarios: |
| 43515 | 43439 | ** |
| 43516 | 43440 | ** a) When reading a free-list leaf page from the database, and |
| 43517 | 43441 | ** |
| 43518 | 43442 | ** b) When a savepoint is being rolled back and we need to load |
| 43519 | 43443 | ** a new page into the cache to be filled with the data read |
| | @@ -44919,11 +44843,31 @@ |
| 44919 | 44843 | pagerReportSize(pPager); |
| 44920 | 44844 | } |
| 44921 | 44845 | SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){ |
| 44922 | 44846 | return pPager->pCodec; |
| 44923 | 44847 | } |
| 44924 | | -#endif |
| 44848 | + |
| 44849 | +/* |
| 44850 | +** This function is called by the wal module when writing page content |
| 44851 | +** into the log file. |
| 44852 | +** |
| 44853 | +** This function returns a pointer to a buffer containing the encrypted |
| 44854 | +** page content. If a malloc fails, this function may return NULL. |
| 44855 | +*/ |
| 44856 | +SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){ |
| 44857 | + void *aData = 0; |
| 44858 | + CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData); |
| 44859 | + return aData; |
| 44860 | +} |
| 44861 | + |
| 44862 | +/* |
| 44863 | +** Return the current pager state |
| 44864 | +*/ |
| 44865 | +SQLITE_PRIVATE int sqlite3PagerState(Pager *pPager){ |
| 44866 | + return pPager->eState; |
| 44867 | +} |
| 44868 | +#endif /* SQLITE_HAS_CODEC */ |
| 44925 | 44869 | |
| 44926 | 44870 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 44927 | 44871 | /* |
| 44928 | 44872 | ** Move the page pPg to location pgno in the file. |
| 44929 | 44873 | ** |
| | @@ -45474,25 +45418,10 @@ |
| 45474 | 45418 | assert( pPager->eState==PAGER_READER ); |
| 45475 | 45419 | return sqlite3WalFramesize(pPager->pWal); |
| 45476 | 45420 | } |
| 45477 | 45421 | #endif |
| 45478 | 45422 | |
| 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 | 45423 | #endif /* SQLITE_OMIT_DISKIO */ |
| 45495 | 45424 | |
| 45496 | 45425 | /************** End of pager.c ***********************************************/ |
| 45497 | 45426 | /************** Begin file wal.c *********************************************/ |
| 45498 | 45427 | /* |
| | @@ -50759,11 +50688,11 @@ |
| 50759 | 50688 | if( rc ) return rc; |
| 50760 | 50689 | top = get2byteNotZero(&data[hdr+5]); |
| 50761 | 50690 | }else if( gap+2<=top ){ |
| 50762 | 50691 | /* Search the freelist looking for a free slot big enough to satisfy |
| 50763 | 50692 | ** the request. The allocation is made from the first free slot in |
| 50764 | | - ** the list that is large enough to accomadate it. |
| 50693 | + ** the list that is large enough to accommodate it. |
| 50765 | 50694 | */ |
| 50766 | 50695 | int pc, addr; |
| 50767 | 50696 | for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ |
| 50768 | 50697 | int size; /* Size of the free slot */ |
| 50769 | 50698 | if( pc>usableSize-4 || pc<addr+4 ){ |
| | @@ -52702,11 +52631,11 @@ |
| 52702 | 52631 | return rc; |
| 52703 | 52632 | } |
| 52704 | 52633 | |
| 52705 | 52634 | /* |
| 52706 | 52635 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
| 52707 | | -** is commited for an auto-vacuum database. |
| 52636 | +** is committed for an auto-vacuum database. |
| 52708 | 52637 | ** |
| 52709 | 52638 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 52710 | 52639 | ** the database file should be truncated to during the commit process. |
| 52711 | 52640 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 52712 | 52641 | ** pages are in use. |
| | @@ -58045,16 +57974,10 @@ |
| 58045 | 57974 | ************************************************************************* |
| 58046 | 57975 | ** This file contains the implementation of the sqlite3_backup_XXX() |
| 58047 | 57976 | ** API functions and the related features. |
| 58048 | 57977 | */ |
| 58049 | 57978 | |
| 58050 | | -/* Macro to find the minimum of two numeric values. |
| 58051 | | -*/ |
| 58052 | | -#ifndef MIN |
| 58053 | | -# define MIN(x,y) ((x)<(y)?(x):(y)) |
| 58054 | | -#endif |
| 58055 | | - |
| 58056 | 57979 | /* |
| 58057 | 57980 | ** Structure allocated for each backup operation. |
| 58058 | 57981 | */ |
| 58059 | 57982 | struct sqlite3_backup { |
| 58060 | 57983 | sqlite3* pDestDb; /* Destination database handle */ |
| | @@ -61942,11 +61865,11 @@ |
| 61942 | 61865 | /* |
| 61943 | 61866 | ** If the Vdbe passed as the first argument opened a statement-transaction, |
| 61944 | 61867 | ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or |
| 61945 | 61868 | ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement |
| 61946 | 61869 | ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the |
| 61947 | | -** statement transaction is commtted. |
| 61870 | +** statement transaction is committed. |
| 61948 | 61871 | ** |
| 61949 | 61872 | ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. |
| 61950 | 61873 | ** Otherwise SQLITE_OK. |
| 61951 | 61874 | */ |
| 61952 | 61875 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
| | @@ -64011,17 +63934,10 @@ |
| 64011 | 63934 | int iType = sqlite3_value_type( columnMem(pStmt,i) ); |
| 64012 | 63935 | columnMallocFailure(pStmt); |
| 64013 | 63936 | return iType; |
| 64014 | 63937 | } |
| 64015 | 63938 | |
| 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 | 63939 | /* |
| 64024 | 63940 | ** Convert the N-th element of pStmt->pColName[] into a string using |
| 64025 | 63941 | ** xFunc() then return that string. If N is out of range, return 0. |
| 64026 | 63942 | ** |
| 64027 | 63943 | ** There are up to 5 names for each column. useType determines which |
| | @@ -64643,18 +64559,18 @@ |
| 64643 | 64559 | pVar = &utf8; |
| 64644 | 64560 | } |
| 64645 | 64561 | #endif |
| 64646 | 64562 | nOut = pVar->n; |
| 64647 | 64563 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 64648 | | - if( n>SQLITE_TRACE_SIZE_LIMIT ){ |
| 64564 | + if( nOut>SQLITE_TRACE_SIZE_LIMIT ){ |
| 64649 | 64565 | nOut = SQLITE_TRACE_SIZE_LIMIT; |
| 64650 | | - while( nOut<pVar->n && (pVar->z[n]&0xc0)==0x80 ){ n++; } |
| 64566 | + while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; } |
| 64651 | 64567 | } |
| 64652 | 64568 | #endif |
| 64653 | 64569 | sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z); |
| 64654 | 64570 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 64655 | | - if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-n); |
| 64571 | + if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut); |
| 64656 | 64572 | #endif |
| 64657 | 64573 | #ifndef SQLITE_OMIT_UTF16 |
| 64658 | 64574 | if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8); |
| 64659 | 64575 | #endif |
| 64660 | 64576 | }else if( pVar->flags & MEM_Zero ){ |
| | @@ -64670,11 +64586,11 @@ |
| 64670 | 64586 | for(i=0; i<nOut; i++){ |
| 64671 | 64587 | sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff); |
| 64672 | 64588 | } |
| 64673 | 64589 | sqlite3StrAccumAppend(&out, "'", 1); |
| 64674 | 64590 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 64675 | | - if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-n); |
| 64591 | + if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut); |
| 64676 | 64592 | #endif |
| 64677 | 64593 | } |
| 64678 | 64594 | } |
| 64679 | 64595 | } |
| 64680 | 64596 | return sqlite3StrAccumFinish(&out); |
| | @@ -68269,12 +68185,12 @@ |
| 68269 | 68185 | ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is |
| 68270 | 68186 | ** obtained on the database file when a write-transaction is started. No |
| 68271 | 68187 | ** other process can start another write transaction while this transaction is |
| 68272 | 68188 | ** underway. Starting a write transaction also creates a rollback journal. A |
| 68273 | 68189 | ** 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. |
| 68190 | +** database. If P2 is greater than or equal to 2 then an EXCLUSIVE lock is |
| 68191 | +** also obtained on the file. |
| 68276 | 68192 | ** |
| 68277 | 68193 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 68278 | 68194 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 68279 | 68195 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 68280 | 68196 | ** More specifically, a statement transaction is opened iff the database |
| | @@ -72224,11 +72140,11 @@ |
| 72224 | 72140 | ** |
| 72225 | 72141 | ** For the purposes of this comparison, EOF is considered greater than any |
| 72226 | 72142 | ** other key value. If the keys are equal (only possible with two EOF |
| 72227 | 72143 | ** values), it doesn't matter which index is stored. |
| 72228 | 72144 | ** |
| 72229 | | -** The (N/4) elements of aTree[] that preceed the final (N/2) described |
| 72145 | +** The (N/4) elements of aTree[] that precede the final (N/2) described |
| 72230 | 72146 | ** above contains the index of the smallest of each block of 4 iterators. |
| 72231 | 72147 | ** And so on. So that aTree[1] contains the index of the iterator that |
| 72232 | 72148 | ** currently points to the smallest key value. aTree[0] is unused. |
| 72233 | 72149 | ** |
| 72234 | 72150 | ** Example: |
| | @@ -73499,16 +73415,10 @@ |
| 73499 | 73415 | ** a power-of-two allocation. This mimimizes wasted space in power-of-two |
| 73500 | 73416 | ** memory allocators. |
| 73501 | 73417 | */ |
| 73502 | 73418 | #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*))) |
| 73503 | 73419 | |
| 73504 | | -/* Macro to find the minimum of two numeric values. |
| 73505 | | -*/ |
| 73506 | | -#ifndef MIN |
| 73507 | | -# define MIN(x,y) ((x)<(y)?(x):(y)) |
| 73508 | | -#endif |
| 73509 | | - |
| 73510 | 73420 | /* |
| 73511 | 73421 | ** The rollback journal is composed of a linked list of these structures. |
| 73512 | 73422 | */ |
| 73513 | 73423 | struct FileChunk { |
| 73514 | 73424 | FileChunk *pNext; /* Next chunk in the journal */ |
| | @@ -75045,12 +74955,12 @@ |
| 75045 | 74955 | ** |
| 75046 | 74956 | ** Minor point: If this is the case, then the expression will be |
| 75047 | 74957 | ** re-evaluated for each reference to it. |
| 75048 | 74958 | */ |
| 75049 | 74959 | sNC.pEList = p->pEList; |
| 75050 | | - if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; |
| 75051 | 74960 | sNC.ncFlags |= NC_AsMaybe; |
| 74961 | + if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; |
| 75052 | 74962 | if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort; |
| 75053 | 74963 | sNC.ncFlags &= ~NC_AsMaybe; |
| 75054 | 74964 | |
| 75055 | 74965 | /* The ORDER BY and GROUP BY clauses may not refer to terms in |
| 75056 | 74966 | ** outer queries |
| | @@ -76817,19 +76727,19 @@ |
| 76817 | 76727 | |
| 76818 | 76728 | if( eType==0 ){ |
| 76819 | 76729 | /* Could not found an existing table or index to use as the RHS b-tree. |
| 76820 | 76730 | ** We will have to generate an ephemeral table to do the job. |
| 76821 | 76731 | */ |
| 76822 | | - double savedNQueryLoop = pParse->nQueryLoop; |
| 76732 | + u32 savedNQueryLoop = pParse->nQueryLoop; |
| 76823 | 76733 | int rMayHaveNull = 0; |
| 76824 | 76734 | eType = IN_INDEX_EPH; |
| 76825 | 76735 | if( prNotFound ){ |
| 76826 | 76736 | *prNotFound = rMayHaveNull = ++pParse->nMem; |
| 76827 | 76737 | sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound); |
| 76828 | 76738 | }else{ |
| 76829 | | - testcase( pParse->nQueryLoop>(double)1 ); |
| 76830 | | - pParse->nQueryLoop = (double)1; |
| 76739 | + testcase( pParse->nQueryLoop>0 ); |
| 76740 | + pParse->nQueryLoop = 0; |
| 76831 | 76741 | if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){ |
| 76832 | 76742 | eType = IN_INDEX_ROWID; |
| 76833 | 76743 | } |
| 76834 | 76744 | } |
| 76835 | 76745 | sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); |
| | @@ -76867,11 +76777,11 @@ |
| 76867 | 76777 | ** the register given by rMayHaveNull to NULL. Calling routines will take |
| 76868 | 76778 | ** care of changing this register value to non-NULL if the RHS is NULL-free. |
| 76869 | 76779 | ** |
| 76870 | 76780 | ** If rMayHaveNull is zero, that means that the subquery is being used |
| 76871 | 76781 | ** for membership testing only. There is no need to initialize any |
| 76872 | | -** registers to indicate the presense or absence of NULLs on the RHS. |
| 76782 | +** registers to indicate the presence or absence of NULLs on the RHS. |
| 76873 | 76783 | ** |
| 76874 | 76784 | ** For a SELECT or EXISTS operator, return the register that holds the |
| 76875 | 76785 | ** result. For IN operators or if an error occurs, the return value is 0. |
| 76876 | 76786 | */ |
| 76877 | 76787 | #ifndef SQLITE_OMIT_SUBQUERY |
| | @@ -80267,11 +80177,11 @@ |
| 80267 | 80177 | ** |
| 80268 | 80178 | ** Additional tables might be added in future releases of SQLite. |
| 80269 | 80179 | ** The sqlite_stat2 table is not created or used unless the SQLite version |
| 80270 | 80180 | ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled |
| 80271 | 80181 | ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. |
| 80272 | | -** The sqlite_stat2 table is superceded by sqlite_stat3, which is only |
| 80182 | +** The sqlite_stat2 table is superseded by sqlite_stat3, which is only |
| 80273 | 80183 | ** created and used by SQLite versions 3.7.9 and later and with |
| 80274 | 80184 | ** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3 |
| 80275 | 80185 | ** is a superset of sqlite_stat2. |
| 80276 | 80186 | ** |
| 80277 | 80187 | ** Format of sqlite_stat1: |
| | @@ -83455,10 +83365,11 @@ |
| 83455 | 83365 | zColl = sqlite3NameFromToken(db, pToken); |
| 83456 | 83366 | if( !zColl ) return; |
| 83457 | 83367 | |
| 83458 | 83368 | if( sqlite3LocateCollSeq(pParse, zColl) ){ |
| 83459 | 83369 | Index *pIdx; |
| 83370 | + sqlite3DbFree(db, p->aCol[i].zColl); |
| 83460 | 83371 | p->aCol[i].zColl = zColl; |
| 83461 | 83372 | |
| 83462 | 83373 | /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
| 83463 | 83374 | ** then an index may have been created on this column before the |
| 83464 | 83375 | ** collation type was added. Correct this if it is the case. |
| | @@ -84874,10 +84785,11 @@ |
| 84874 | 84785 | zExtra = (char *)(&pIndex->zName[nName+1]); |
| 84875 | 84786 | memcpy(pIndex->zName, zName, nName+1); |
| 84876 | 84787 | pIndex->pTable = pTab; |
| 84877 | 84788 | pIndex->nColumn = pList->nExpr; |
| 84878 | 84789 | pIndex->onError = (u8)onError; |
| 84790 | + pIndex->uniqNotNull = onError==OE_Abort; |
| 84879 | 84791 | pIndex->autoIndex = (u8)(pName==0); |
| 84880 | 84792 | pIndex->pSchema = db->aDb[iDb].pSchema; |
| 84881 | 84793 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 84882 | 84794 | |
| 84883 | 84795 | /* Check to see if we should honor DESC requests on index columns |
| | @@ -84932,10 +84844,11 @@ |
| 84932 | 84844 | goto exit_create_index; |
| 84933 | 84845 | } |
| 84934 | 84846 | pIndex->azColl[i] = zColl; |
| 84935 | 84847 | requestedSortOrder = pListItem->sortOrder & sortOrderMask; |
| 84936 | 84848 | pIndex->aSortOrder[i] = (u8)requestedSortOrder; |
| 84849 | + if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0; |
| 84937 | 84850 | } |
| 84938 | 84851 | sqlite3DefaultRowEst(pIndex); |
| 84939 | 84852 | |
| 84940 | 84853 | if( pTab==pParse->pNewTable ){ |
| 84941 | 84854 | /* This routine has been called to create an automatic index as a |
| | @@ -85363,19 +85276,19 @@ |
| 85363 | 85276 | assert( db->mallocFailed ); |
| 85364 | 85277 | return pSrc; |
| 85365 | 85278 | } |
| 85366 | 85279 | pSrc = pNew; |
| 85367 | 85280 | nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; |
| 85368 | | - pSrc->nAlloc = (u16)nGot; |
| 85281 | + pSrc->nAlloc = (u8)nGot; |
| 85369 | 85282 | } |
| 85370 | 85283 | |
| 85371 | 85284 | /* Move existing slots that come after the newly inserted slots |
| 85372 | 85285 | ** out of the way */ |
| 85373 | 85286 | for(i=pSrc->nSrc-1; i>=iStart; i--){ |
| 85374 | 85287 | pSrc->a[i+nExtra] = pSrc->a[i]; |
| 85375 | 85288 | } |
| 85376 | | - pSrc->nSrc += (i16)nExtra; |
| 85289 | + pSrc->nSrc += (i8)nExtra; |
| 85377 | 85290 | |
| 85378 | 85291 | /* Zero the newly allocated slots */ |
| 85379 | 85292 | memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); |
| 85380 | 85293 | for(i=iStart; i<iStart+nExtra; i++){ |
| 85381 | 85294 | pSrc->a[i].iCursor = -1; |
| | @@ -87378,11 +87291,11 @@ |
| 87378 | 87291 | ** of x. If x is text, then we actually count UTF-8 characters. |
| 87379 | 87292 | ** If x is a blob, then we count bytes. |
| 87380 | 87293 | ** |
| 87381 | 87294 | ** If p1 is negative, then we begin abs(p1) from the end of x[]. |
| 87382 | 87295 | ** |
| 87383 | | -** If p2 is negative, return the p2 characters preceeding p1. |
| 87296 | +** If p2 is negative, return the p2 characters preceding p1. |
| 87384 | 87297 | */ |
| 87385 | 87298 | static void substrFunc( |
| 87386 | 87299 | sqlite3_context *context, |
| 87387 | 87300 | int argc, |
| 87388 | 87301 | sqlite3_value **argv |
| | @@ -88037,14 +87950,10 @@ |
| 88037 | 87950 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 88038 | 87951 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 88039 | 87952 | }; |
| 88040 | 87953 | |
| 88041 | 87954 | /* |
| 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 | 87955 | ** Implementation of the QUOTE() function. This function takes a single |
| 88047 | 87956 | ** argument. If the argument is numeric, the return value is the same as |
| 88048 | 87957 | ** the argument. If the argument is NULL, the return value is the string |
| 88049 | 87958 | ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 88050 | 87959 | ** single-quote escapes. |
| | @@ -88229,11 +88138,11 @@ |
| 88229 | 88138 | } |
| 88230 | 88139 | |
| 88231 | 88140 | /* |
| 88232 | 88141 | ** The replace() function. Three arguments are all strings: call |
| 88233 | 88142 | ** 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 |
| 88143 | +** from A by replacing every occurrence of B with C. The match |
| 88235 | 88144 | ** must be exact. Collating sequences are not used. |
| 88236 | 88145 | */ |
| 88237 | 88146 | static void replaceFunc( |
| 88238 | 88147 | sqlite3_context *context, |
| 88239 | 88148 | int argc, |
| | @@ -94147,15 +94056,19 @@ |
| 94147 | 94056 | sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz); |
| 94148 | 94057 | } |
| 94149 | 94058 | } |
| 94150 | 94059 | } |
| 94151 | 94060 | sz = -1; |
| 94152 | | - if( sqlite3_file_control(db,zDb,SQLITE_FCNTL_MMAP_SIZE,&sz)==SQLITE_OK ){ |
| 94061 | + rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz); |
| 94153 | 94062 | #if SQLITE_MAX_MMAP_SIZE==0 |
| 94154 | | - sz = 0; |
| 94063 | + sz = 0; |
| 94155 | 94064 | #endif |
| 94065 | + if( rc==SQLITE_OK ){ |
| 94156 | 94066 | returnSingleInt(pParse, "mmap_size", sz); |
| 94067 | + }else if( rc!=SQLITE_NOTFOUND ){ |
| 94068 | + pParse->nErr++; |
| 94069 | + pParse->rc = rc; |
| 94157 | 94070 | } |
| 94158 | 94071 | }else |
| 94159 | 94072 | |
| 94160 | 94073 | /* |
| 94161 | 94074 | ** PRAGMA temp_store |
| | @@ -94682,11 +94595,11 @@ |
| 94682 | 94595 | #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX |
| 94683 | 94596 | # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 |
| 94684 | 94597 | #endif |
| 94685 | 94598 | |
| 94686 | 94599 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
| 94687 | | - /* Pragma "quick_check" is an experimental reduced version of |
| 94600 | + /* Pragma "quick_check" is reduced version of |
| 94688 | 94601 | ** integrity_check designed to detect most database corruption |
| 94689 | 94602 | ** without most of the overhead of a full integrity-check. |
| 94690 | 94603 | */ |
| 94691 | 94604 | if( sqlite3StrICmp(zLeft, "integrity_check")==0 |
| 94692 | 94605 | || sqlite3StrICmp(zLeft, "quick_check")==0 |
| | @@ -95140,14 +95053,14 @@ |
| 95140 | 95053 | }else |
| 95141 | 95054 | #endif |
| 95142 | 95055 | |
| 95143 | 95056 | #ifdef SQLITE_HAS_CODEC |
| 95144 | 95057 | if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){ |
| 95145 | | - sqlite3_key(db, zRight, sqlite3Strlen30(zRight)); |
| 95058 | + sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); |
| 95146 | 95059 | }else |
| 95147 | 95060 | if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){ |
| 95148 | | - sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight)); |
| 95061 | + sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); |
| 95149 | 95062 | }else |
| 95150 | 95063 | if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 || |
| 95151 | 95064 | sqlite3StrICmp(zLeft, "hexrekey")==0) ){ |
| 95152 | 95065 | int i, h1, h2; |
| 95153 | 95066 | char zKey[40]; |
| | @@ -95155,13 +95068,13 @@ |
| 95155 | 95068 | h1 += 9*(1&(h1>>6)); |
| 95156 | 95069 | h2 += 9*(1&(h2>>6)); |
| 95157 | 95070 | zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4); |
| 95158 | 95071 | } |
| 95159 | 95072 | if( (zLeft[3] & 0xf)==0xb ){ |
| 95160 | | - sqlite3_key(db, zKey, i/2); |
| 95073 | + sqlite3_key_v2(db, zDb, zKey, i/2); |
| 95161 | 95074 | }else{ |
| 95162 | | - sqlite3_rekey(db, zKey, i/2); |
| 95075 | + sqlite3_rekey_v2(db, zDb, zKey, i/2); |
| 95163 | 95076 | } |
| 95164 | 95077 | }else |
| 95165 | 95078 | #endif |
| 95166 | 95079 | #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) |
| 95167 | 95080 | if( sqlite3StrICmp(zLeft, "activate_extensions")==0 && zRight ){ |
| | @@ -95792,11 +95705,11 @@ |
| 95792 | 95705 | } |
| 95793 | 95706 | |
| 95794 | 95707 | sqlite3VtabUnlockList(db); |
| 95795 | 95708 | |
| 95796 | 95709 | pParse->db = db; |
| 95797 | | - pParse->nQueryLoop = (double)1; |
| 95710 | + pParse->nQueryLoop = 0; /* Logarithmic, so 0 really means 1 */ |
| 95798 | 95711 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
| 95799 | 95712 | char *zSqlCopy; |
| 95800 | 95713 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 95801 | 95714 | testcase( nBytes==mxLen ); |
| 95802 | 95715 | testcase( nBytes==mxLen+1 ); |
| | @@ -95814,11 +95727,11 @@ |
| 95814 | 95727 | pParse->zTail = &zSql[nBytes]; |
| 95815 | 95728 | } |
| 95816 | 95729 | }else{ |
| 95817 | 95730 | sqlite3RunParser(pParse, zSql, &zErrMsg); |
| 95818 | 95731 | } |
| 95819 | | - assert( 1==(int)pParse->nQueryLoop ); |
| 95732 | + assert( 0==pParse->nQueryLoop ); |
| 95820 | 95733 | |
| 95821 | 95734 | if( db->mallocFailed ){ |
| 95822 | 95735 | pParse->rc = SQLITE_NOMEM; |
| 95823 | 95736 | } |
| 95824 | 95737 | if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; |
| | @@ -96178,11 +96091,11 @@ |
| 96178 | 96091 | sqlite3DbFree(db, p); |
| 96179 | 96092 | } |
| 96180 | 96093 | } |
| 96181 | 96094 | |
| 96182 | 96095 | /* |
| 96183 | | -** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the |
| 96096 | +** Given 1 to 3 identifiers preceding the JOIN keyword, determine the |
| 96184 | 96097 | ** type of join. Return an integer constant that expresses that type |
| 96185 | 96098 | ** in terms of the following bit values: |
| 96186 | 96099 | ** |
| 96187 | 96100 | ** JT_INNER |
| 96188 | 96101 | ** JT_CROSS |
| | @@ -97592,11 +97505,11 @@ |
| 97592 | 97505 | int addr1, n; |
| 97593 | 97506 | if( p->iLimit ) return; |
| 97594 | 97507 | |
| 97595 | 97508 | /* |
| 97596 | 97509 | ** "LIMIT -1" always shows all rows. There is some |
| 97597 | | - ** contraversy about what the correct behavior should be. |
| 97510 | + ** controversy about what the correct behavior should be. |
| 97598 | 97511 | ** The current implementation interprets "LIMIT 0" to mean |
| 97599 | 97512 | ** no rows. |
| 97600 | 97513 | */ |
| 97601 | 97514 | sqlite3ExprCacheClear(pParse); |
| 97602 | 97515 | assert( p->pOffset==0 || p->pLimit!=0 ); |
| | @@ -97607,12 +97520,12 @@ |
| 97607 | 97520 | if( sqlite3ExprIsInteger(p->pLimit, &n) ){ |
| 97608 | 97521 | sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); |
| 97609 | 97522 | VdbeComment((v, "LIMIT counter")); |
| 97610 | 97523 | if( n==0 ){ |
| 97611 | 97524 | sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak); |
| 97612 | | - }else{ |
| 97613 | | - if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n; |
| 97525 | + }else if( n>=0 && p->nSelectRow>(u64)n ){ |
| 97526 | + p->nSelectRow = n; |
| 97614 | 97527 | } |
| 97615 | 97528 | }else{ |
| 97616 | 97529 | sqlite3ExprCode(pParse, p->pLimit, iLimit); |
| 97617 | 97530 | sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); |
| 97618 | 97531 | VdbeComment((v, "LIMIT counter")); |
| | @@ -97802,13 +97715,13 @@ |
| 97802 | 97715 | pDelete = p->pPrior; |
| 97803 | 97716 | p->pPrior = pPrior; |
| 97804 | 97717 | p->nSelectRow += pPrior->nSelectRow; |
| 97805 | 97718 | if( pPrior->pLimit |
| 97806 | 97719 | && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit) |
| 97807 | | - && p->nSelectRow > (double)nLimit |
| 97720 | + && nLimit>0 && p->nSelectRow > (u64)nLimit |
| 97808 | 97721 | ){ |
| 97809 | | - p->nSelectRow = (double)nLimit; |
| 97722 | + p->nSelectRow = nLimit; |
| 97810 | 97723 | } |
| 97811 | 97724 | if( addr ){ |
| 97812 | 97725 | sqlite3VdbeJumpHere(v, addr); |
| 97813 | 97726 | } |
| 97814 | 97727 | break; |
| | @@ -99953,15 +99866,14 @@ |
| 99953 | 99866 | Parse *pParse, /* Parse context */ |
| 99954 | 99867 | Table *pTab, /* Table being queried */ |
| 99955 | 99868 | Index *pIdx /* Index used to optimize scan, or NULL */ |
| 99956 | 99869 | ){ |
| 99957 | 99870 | if( pParse->explain==2 ){ |
| 99958 | | - char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)", |
| 99871 | + char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s", |
| 99959 | 99872 | pTab->zName, |
| 99960 | | - pIdx ? "USING COVERING INDEX " : "", |
| 99961 | | - pIdx ? pIdx->zName : "", |
| 99962 | | - pTab->nRowEst |
| 99873 | + pIdx ? " USING COVERING INDEX " : "", |
| 99874 | + pIdx ? pIdx->zName : "" |
| 99963 | 99875 | ); |
| 99964 | 99876 | sqlite3VdbeAddOp4( |
| 99965 | 99877 | pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC |
| 99966 | 99878 | ); |
| 99967 | 99879 | } |
| | @@ -100115,11 +100027,11 @@ |
| 100115 | 100027 | } |
| 100116 | 100028 | continue; |
| 100117 | 100029 | } |
| 100118 | 100030 | |
| 100119 | 100031 | /* Increment Parse.nHeight by the height of the largest expression |
| 100120 | | - ** tree refered to by this, the parent select. The child select |
| 100032 | + ** tree referred to by this, the parent select. The child select |
| 100121 | 100033 | ** may contain expression trees of at most |
| 100122 | 100034 | ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit |
| 100123 | 100035 | ** more conservative than necessary, but much easier than enforcing |
| 100124 | 100036 | ** an exact limit. |
| 100125 | 100037 | */ |
| | @@ -100308,11 +100220,11 @@ |
| 100308 | 100220 | } |
| 100309 | 100221 | |
| 100310 | 100222 | /* Set the limiter. |
| 100311 | 100223 | */ |
| 100312 | 100224 | iEnd = sqlite3VdbeMakeLabel(v); |
| 100313 | | - p->nSelectRow = (double)LARGEST_INT64; |
| 100225 | + p->nSelectRow = LARGEST_INT64; |
| 100314 | 100226 | computeLimitRegisters(pParse, p, iEnd); |
| 100315 | 100227 | if( p->iLimit==0 && addrSortIndex>=0 ){ |
| 100316 | 100228 | sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen; |
| 100317 | 100229 | p->selFlags |= SF_UseSorter; |
| 100318 | 100230 | } |
| | @@ -100336,13 +100248,17 @@ |
| 100336 | 100248 | ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0); |
| 100337 | 100249 | |
| 100338 | 100250 | /* Begin the database scan. */ |
| 100339 | 100251 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0); |
| 100340 | 100252 | 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; |
| 100253 | + if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){ |
| 100254 | + p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo); |
| 100255 | + } |
| 100256 | + if( sqlite3WhereIsDistinct(pWInfo) ){ |
| 100257 | + sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo); |
| 100258 | + } |
| 100259 | + if( pOrderBy && sqlite3WhereIsOrdered(pWInfo) ) pOrderBy = 0; |
| 100344 | 100260 | |
| 100345 | 100261 | /* If sorting index that was created by a prior OP_OpenEphemeral |
| 100346 | 100262 | ** instruction ended up not being needed, then change the OP_OpenEphemeral |
| 100347 | 100263 | ** into an OP_Noop. |
| 100348 | 100264 | */ |
| | @@ -100351,11 +100267,12 @@ |
| 100351 | 100267 | p->addrOpenEphm[2] = -1; |
| 100352 | 100268 | } |
| 100353 | 100269 | |
| 100354 | 100270 | /* Use the standard inner loop. */ |
| 100355 | 100271 | selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest, |
| 100356 | | - pWInfo->iContinue, pWInfo->iBreak); |
| 100272 | + sqlite3WhereContinueLabel(pWInfo), |
| 100273 | + sqlite3WhereBreakLabel(pWInfo)); |
| 100357 | 100274 | |
| 100358 | 100275 | /* End the database scan loop. |
| 100359 | 100276 | */ |
| 100360 | 100277 | sqlite3WhereEnd(pWInfo); |
| 100361 | 100278 | }else{ |
| | @@ -100384,13 +100301,13 @@ |
| 100384 | 100301 | pItem->iAlias = 0; |
| 100385 | 100302 | } |
| 100386 | 100303 | for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ |
| 100387 | 100304 | pItem->iAlias = 0; |
| 100388 | 100305 | } |
| 100389 | | - if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100; |
| 100306 | + if( p->nSelectRow>100 ) p->nSelectRow = 100; |
| 100390 | 100307 | }else{ |
| 100391 | | - p->nSelectRow = (double)1; |
| 100308 | + p->nSelectRow = 1; |
| 100392 | 100309 | } |
| 100393 | 100310 | |
| 100394 | 100311 | |
| 100395 | 100312 | /* Create a label to jump to when we want to abort the query */ |
| 100396 | 100313 | addrEnd = sqlite3VdbeMakeLabel(v); |
| | @@ -100466,13 +100383,14 @@ |
| 100466 | 100383 | ** This might involve two separate loops with an OP_Sort in between, or |
| 100467 | 100384 | ** it might be a single loop that uses an index to extract information |
| 100468 | 100385 | ** in the right order to begin with. |
| 100469 | 100386 | */ |
| 100470 | 100387 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
| 100471 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0); |
| 100388 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, |
| 100389 | + WHERE_GROUPBY, 0); |
| 100472 | 100390 | if( pWInfo==0 ) goto select_end; |
| 100473 | | - if( pWInfo->nOBSat==pGroupBy->nExpr ){ |
| 100391 | + if( sqlite3WhereIsOrdered(pWInfo) ){ |
| 100474 | 100392 | /* The optimizer is able to deliver rows in group by order so |
| 100475 | 100393 | ** we do not have to sort. The OP_OpenEphemeral table will be |
| 100476 | 100394 | ** cancelled later because we still need to use the pKeyInfo |
| 100477 | 100395 | */ |
| 100478 | 100396 | groupBySort = 0; |
| | @@ -100749,12 +100667,12 @@ |
| 100749 | 100667 | sqlite3ExprListDelete(db, pDel); |
| 100750 | 100668 | goto select_end; |
| 100751 | 100669 | } |
| 100752 | 100670 | updateAccumulator(pParse, &sAggInfo); |
| 100753 | 100671 | assert( pMinMax==0 || pMinMax->nExpr==1 ); |
| 100754 | | - if( pWInfo->nOBSat>0 ){ |
| 100755 | | - sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak); |
| 100672 | + if( sqlite3WhereIsOrdered(pWInfo) ){ |
| 100673 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo)); |
| 100756 | 100674 | VdbeComment((v, "%s() by index", |
| 100757 | 100675 | (flag==WHERE_ORDERBY_MIN?"min":"max"))); |
| 100758 | 100676 | } |
| 100759 | 100677 | sqlite3WhereEnd(pWInfo); |
| 100760 | 100678 | finalizeAggFunctions(pParse, &sAggInfo); |
| | @@ -102109,11 +102027,11 @@ |
| 102109 | 102027 | } |
| 102110 | 102028 | |
| 102111 | 102029 | /* |
| 102112 | 102030 | ** This is called to code the required FOR EACH ROW triggers for an operation |
| 102113 | 102031 | ** 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 |
| 102032 | +** is given by the op parameter. The tr_tm parameter determines whether the |
| 102115 | 102033 | ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then |
| 102116 | 102034 | ** parameter pChanges is passed the list of columns being modified. |
| 102117 | 102035 | ** |
| 102118 | 102036 | ** If there are no triggers that fire at the specified time for the specified |
| 102119 | 102037 | ** operation on pTab, this function is a no-op. |
| | @@ -102560,11 +102478,11 @@ |
| 102560 | 102478 | sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); |
| 102561 | 102479 | pWInfo = sqlite3WhereBegin( |
| 102562 | 102480 | pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0 |
| 102563 | 102481 | ); |
| 102564 | 102482 | if( pWInfo==0 ) goto update_cleanup; |
| 102565 | | - okOnePass = pWInfo->okOnePass; |
| 102483 | + okOnePass = sqlite3WhereOkOnePass(pWInfo); |
| 102566 | 102484 | |
| 102567 | 102485 | /* Remember the rowid of every item to be updated. |
| 102568 | 102486 | */ |
| 102569 | 102487 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid); |
| 102570 | 102488 | if( !okOnePass ){ |
| | @@ -104397,22 +104315,165 @@ |
| 104397 | 104315 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 104398 | 104316 | /***/ int sqlite3WhereTrace = 0; |
| 104399 | 104317 | #endif |
| 104400 | 104318 | #if defined(SQLITE_DEBUG) \ |
| 104401 | 104319 | && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
| 104402 | | -# define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X |
| 104320 | +# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X |
| 104321 | +# define WHERETRACE_ENABLED 1 |
| 104403 | 104322 | #else |
| 104404 | | -# define WHERETRACE(X) |
| 104323 | +# define WHERETRACE(K,X) |
| 104405 | 104324 | #endif |
| 104406 | 104325 | |
| 104407 | 104326 | /* Forward reference |
| 104408 | 104327 | */ |
| 104409 | 104328 | typedef struct WhereClause WhereClause; |
| 104410 | 104329 | typedef struct WhereMaskSet WhereMaskSet; |
| 104411 | 104330 | typedef struct WhereOrInfo WhereOrInfo; |
| 104412 | 104331 | typedef struct WhereAndInfo WhereAndInfo; |
| 104413 | | -typedef struct WhereCost WhereCost; |
| 104332 | +typedef struct WhereLevel WhereLevel; |
| 104333 | +typedef struct WhereLoop WhereLoop; |
| 104334 | +typedef struct WherePath WherePath; |
| 104335 | +typedef struct WhereTerm WhereTerm; |
| 104336 | +typedef struct WhereLoopBuilder WhereLoopBuilder; |
| 104337 | +typedef struct WhereScan WhereScan; |
| 104338 | + |
| 104339 | +/* |
| 104340 | +** Cost X is tracked as 10*log2(X) stored in a 16-bit integer. The |
| 104341 | +** maximum cost for ordinary tables is 64*(2**63) which becomes 6900. |
| 104342 | +** (Virtual tables can return a larger cost, but let's assume they do not.) |
| 104343 | +** So all costs can be stored in a 16-bit unsigned integer without risk |
| 104344 | +** of overflow. |
| 104345 | +** |
| 104346 | +** Costs are estimates, so don't go to the computational trouble to compute |
| 104347 | +** 10*log2(X) exactly. Instead, a close estimate is used. Any value of |
| 104348 | +** X<=1 is stored as 0. X=2 is 10. X=3 is 16. X=1000 is 99. etc. |
| 104349 | +** |
| 104350 | +** The tool/wherecosttest.c source file implements a command-line program |
| 104351 | +** that will convert between WhereCost to integers and do addition and |
| 104352 | +** multiplication on WhereCost values. That command-line program is a |
| 104353 | +** useful utility to have around when working with this module. |
| 104354 | +*/ |
| 104355 | +typedef unsigned short int WhereCost; |
| 104356 | + |
| 104357 | +/* |
| 104358 | +** This object contains information needed to implement a single nested |
| 104359 | +** loop in WHERE clause. |
| 104360 | +** |
| 104361 | +** Contrast this object with WhereLoop. This object describes the |
| 104362 | +** implementation of the loop. WhereLoop describes the algorithm. |
| 104363 | +** This object contains a pointer to the WhereLoop algorithm as one of |
| 104364 | +** its elements. |
| 104365 | +** |
| 104366 | +** The WhereInfo object contains a single instance of this object for |
| 104367 | +** each term in the FROM clause (which is to say, for each of the |
| 104368 | +** nested loops as implemented). The order of WhereLevel objects determines |
| 104369 | +** the loop nested order, with WhereInfo.a[0] being the outer loop and |
| 104370 | +** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. |
| 104371 | +*/ |
| 104372 | +struct WhereLevel { |
| 104373 | + int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 104374 | + int iTabCur; /* The VDBE cursor used to access the table */ |
| 104375 | + int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 104376 | + int addrBrk; /* Jump here to break out of the loop */ |
| 104377 | + int addrNxt; /* Jump here to start the next IN combination */ |
| 104378 | + int addrCont; /* Jump here to continue with the next loop cycle */ |
| 104379 | + int addrFirst; /* First instruction of interior of the loop */ |
| 104380 | + u8 iFrom; /* Which entry in the FROM clause */ |
| 104381 | + u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ |
| 104382 | + int p1, p2; /* Operands of the opcode used to ends the loop */ |
| 104383 | + union { /* Information that depends on pWLoop->wsFlags */ |
| 104384 | + struct { |
| 104385 | + int nIn; /* Number of entries in aInLoop[] */ |
| 104386 | + struct InLoop { |
| 104387 | + int iCur; /* The VDBE cursor used by this IN operator */ |
| 104388 | + int addrInTop; /* Top of the IN loop */ |
| 104389 | + u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 104390 | + } *aInLoop; /* Information about each nested IN operator */ |
| 104391 | + } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 104392 | + Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 104393 | + } u; |
| 104394 | + struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 104395 | +}; |
| 104396 | + |
| 104397 | +/* |
| 104398 | +** Each instance of this object represents an algorithm for evaluating one |
| 104399 | +** term of a join. Every term of the FROM clause will have at least |
| 104400 | +** one corresponding WhereLoop object (unless INDEXED BY constraints |
| 104401 | +** prevent a query solution - which is an error) and many terms of the |
| 104402 | +** FROM clause will have multiple WhereLoop objects, each describing a |
| 104403 | +** potential way of implementing that FROM-clause term, together with |
| 104404 | +** dependencies and cost estimates for using the chosen algorithm. |
| 104405 | +** |
| 104406 | +** Query planning consists of building up a collection of these WhereLoop |
| 104407 | +** objects, then computing a particular sequence of WhereLoop objects, with |
| 104408 | +** one WhereLoop object per FROM clause term, that satisfy all dependencies |
| 104409 | +** and that minimize the overall cost. |
| 104410 | +*/ |
| 104411 | +struct WhereLoop { |
| 104412 | + Bitmask prereq; /* Bitmask of other loops that must run first */ |
| 104413 | + Bitmask maskSelf; /* Bitmask identifying table iTab */ |
| 104414 | +#ifdef SQLITE_DEBUG |
| 104415 | + char cId; /* Symbolic ID of this loop for debugging use */ |
| 104416 | +#endif |
| 104417 | + u8 iTab; /* Position in FROM clause of table for this loop */ |
| 104418 | + u8 iSortIdx; /* Sorting index number. 0==None */ |
| 104419 | + WhereCost rSetup; /* One-time setup cost (ex: create transient index) */ |
| 104420 | + WhereCost rRun; /* Cost of running each loop */ |
| 104421 | + WhereCost nOut; /* Estimated number of output rows */ |
| 104422 | + union { |
| 104423 | + struct { /* Information for internal btree tables */ |
| 104424 | + int nEq; /* Number of equality constraints */ |
| 104425 | + Index *pIndex; /* Index used, or NULL */ |
| 104426 | + } btree; |
| 104427 | + struct { /* Information for virtual tables */ |
| 104428 | + int idxNum; /* Index number */ |
| 104429 | + u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 104430 | + u8 isOrdered; /* True if satisfies ORDER BY */ |
| 104431 | + u16 omitMask; /* Terms that may be omitted */ |
| 104432 | + char *idxStr; /* Index identifier string */ |
| 104433 | + } vtab; |
| 104434 | + } u; |
| 104435 | + u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 104436 | + u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 104437 | + /**** whereLoopXfer() copies fields above ***********************/ |
| 104438 | +# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 104439 | + u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 104440 | + WhereTerm **aLTerm; /* WhereTerms used */ |
| 104441 | + WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 104442 | + WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
| 104443 | +}; |
| 104444 | + |
| 104445 | +/* Forward declaration of methods */ |
| 104446 | +static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 104447 | + |
| 104448 | +/* |
| 104449 | +** Each instance of this object holds a sequence of WhereLoop objects |
| 104450 | +** that implement some or all of a query plan. |
| 104451 | +** |
| 104452 | +** Think of each WhereLoop objects as a node in a graph, which arcs |
| 104453 | +** showing dependences and costs for travelling between nodes. (That is |
| 104454 | +** not a completely accurate description because WhereLoop costs are a |
| 104455 | +** vector, not a scalar, and because dependences are many-to-one, not |
| 104456 | +** one-to-one as are graph nodes. But it is a useful visualization aid.) |
| 104457 | +** Then a WherePath object is a path through the graph that visits some |
| 104458 | +** or all of the WhereLoop objects once. |
| 104459 | +** |
| 104460 | +** The "solver" works by creating the N best WherePath objects of length |
| 104461 | +** 1. Then using those as a basis to compute the N best WherePath objects |
| 104462 | +** of length 2. And so forth until the length of WherePaths equals the |
| 104463 | +** number of nodes in the FROM clause. The best (lowest cost) WherePath |
| 104464 | +** at the end is the choosen query plan. |
| 104465 | +*/ |
| 104466 | +struct WherePath { |
| 104467 | + Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
| 104468 | + Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
| 104469 | + WhereCost nRow; /* Estimated number of rows generated by this path */ |
| 104470 | + WhereCost rCost; /* Total cost of this path */ |
| 104471 | + u8 isOrdered; /* True if this path satisfies ORDER BY */ |
| 104472 | + u8 isOrderedValid; /* True if the isOrdered field is valid */ |
| 104473 | + WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
| 104474 | +}; |
| 104414 | 104475 | |
| 104415 | 104476 | /* |
| 104416 | 104477 | ** The query generator uses an array of instances of this structure to |
| 104417 | 104478 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 104418 | 104479 | ** clause subexpression is separated from the others by AND operators, |
| | @@ -104461,11 +104522,10 @@ |
| 104461 | 104522 | ** |
| 104462 | 104523 | ** The number of terms in a join is limited by the number of bits |
| 104463 | 104524 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 104464 | 104525 | ** is only able to process joins with 64 or fewer tables. |
| 104465 | 104526 | */ |
| 104466 | | -typedef struct WhereTerm WhereTerm; |
| 104467 | 104527 | struct WhereTerm { |
| 104468 | 104528 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
| 104469 | 104529 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 104470 | 104530 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
| 104471 | 104531 | union { |
| | @@ -104495,10 +104555,26 @@ |
| 104495 | 104555 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 104496 | 104556 | #else |
| 104497 | 104557 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 104498 | 104558 | #endif |
| 104499 | 104559 | |
| 104560 | +/* |
| 104561 | +** An instance of the WhereScan object is used as an iterator for locating |
| 104562 | +** terms in the WHERE clause that are useful to the query planner. |
| 104563 | +*/ |
| 104564 | +struct WhereScan { |
| 104565 | + WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 104566 | + WhereClause *pWC; /* WhereClause currently being scanned */ |
| 104567 | + char *zCollName; /* Required collating sequence, if not NULL */ |
| 104568 | + char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 104569 | + unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 104570 | + unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 104571 | + u32 opMask; /* Acceptable operators */ |
| 104572 | + int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 104573 | + int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ |
| 104574 | +}; |
| 104575 | + |
| 104500 | 104576 | /* |
| 104501 | 104577 | ** An instance of the following structure holds all information about a |
| 104502 | 104578 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
| 104503 | 104579 | ** |
| 104504 | 104580 | ** Explanation of pOuter: For a WHERE clause of the form |
| | @@ -104508,15 +104584,13 @@ |
| 104508 | 104584 | ** There are separate WhereClause objects for the whole clause and for |
| 104509 | 104585 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 104510 | 104586 | ** subclauses points to the WhereClause object for the whole clause. |
| 104511 | 104587 | */ |
| 104512 | 104588 | struct WhereClause { |
| 104513 | | - Parse *pParse; /* The parser context */ |
| 104514 | | - WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */ |
| 104589 | + WhereInfo *pWInfo; /* WHERE clause processing context */ |
| 104515 | 104590 | WhereClause *pOuter; /* Outer conjunction */ |
| 104516 | 104591 | u8 op; /* Split operator. TK_AND or TK_OR */ |
| 104517 | | - u16 wctrlFlags; /* Might include WHERE_AND_ONLY */ |
| 104518 | 104592 | int nTerm; /* Number of terms */ |
| 104519 | 104593 | int nSlot; /* Number of entries in a[] */ |
| 104520 | 104594 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
| 104521 | 104595 | #if defined(SQLITE_SMALL_STACK) |
| 104522 | 104596 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| | @@ -104572,23 +104646,59 @@ |
| 104572 | 104646 | int n; /* Number of assigned cursor values */ |
| 104573 | 104647 | int ix[BMS]; /* Cursor assigned to each bit */ |
| 104574 | 104648 | }; |
| 104575 | 104649 | |
| 104576 | 104650 | /* |
| 104577 | | -** A WhereCost object records a lookup strategy and the estimated |
| 104578 | | -** cost of pursuing that strategy. |
| 104651 | +** This object is a convenience wrapper holding all information needed |
| 104652 | +** to construct WhereLoop objects for a particular query. |
| 104579 | 104653 | */ |
| 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 */ |
| 104654 | +struct WhereLoopBuilder { |
| 104655 | + WhereInfo *pWInfo; /* Information about this WHERE */ |
| 104656 | + WhereClause *pWC; /* WHERE clause terms */ |
| 104657 | + ExprList *pOrderBy; /* ORDER BY clause */ |
| 104658 | + WhereLoop *pNew; /* Template WhereLoop */ |
| 104659 | + WhereLoop *pBest; /* If non-NULL, store single best loop here */ |
| 104584 | 104660 | }; |
| 104585 | 104661 | |
| 104586 | 104662 | /* |
| 104587 | | -** Bitmasks for the operators that indices are able to exploit. An |
| 104663 | +** The WHERE clause processing routine has two halves. The |
| 104664 | +** first part does the start of the WHERE loop and the second |
| 104665 | +** half does the tail of the WHERE loop. An instance of |
| 104666 | +** this structure is returned by the first half and passed |
| 104667 | +** into the second half to give some continuity. |
| 104668 | +** |
| 104669 | +** An instance of this object holds the complete state of the query |
| 104670 | +** planner. |
| 104671 | +*/ |
| 104672 | +struct WhereInfo { |
| 104673 | + Parse *pParse; /* Parsing and code generating context */ |
| 104674 | + SrcList *pTabList; /* List of tables in the join */ |
| 104675 | + ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
| 104676 | + ExprList *pDistinct; /* DISTINCT ON values, or NULL */ |
| 104677 | + WhereLoop *pLoops; /* List of all WhereLoop objects */ |
| 104678 | + Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
| 104679 | + WhereCost nRowOut; /* Estimated number of output rows */ |
| 104680 | + u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
| 104681 | + u8 bOBSat; /* ORDER BY satisfied by indices */ |
| 104682 | + u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ |
| 104683 | + u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
| 104684 | + u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ |
| 104685 | + int iTop; /* The very beginning of the WHERE loop */ |
| 104686 | + int iContinue; /* Jump here to continue with next record */ |
| 104687 | + int iBreak; /* Jump here to break out of the loop */ |
| 104688 | + int nLevel; /* Number of nested loop */ |
| 104689 | + int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 104690 | + WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 104691 | + WhereClause sWC; /* Decomposition of the WHERE clause */ |
| 104692 | + WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 104693 | +}; |
| 104694 | + |
| 104695 | +/* |
| 104696 | +** Bitmasks for the operators on WhereTerm objects. These are all |
| 104697 | +** operators that are of interest to the query planner. An |
| 104588 | 104698 | ** OR-ed combination of these values can be used when searching for |
| 104589 | | -** terms in the where clause. |
| 104699 | +** particular WhereTerms within a WhereClause. |
| 104590 | 104700 | */ |
| 104591 | 104701 | #define WO_IN 0x001 |
| 104592 | 104702 | #define WO_EQ 0x002 |
| 104593 | 104703 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 104594 | 104704 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| | @@ -104603,96 +104713,106 @@ |
| 104603 | 104713 | |
| 104604 | 104714 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
| 104605 | 104715 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
| 104606 | 104716 | |
| 104607 | 104717 | /* |
| 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; |
| 104718 | +** These are definitions of bits in the WhereLoop.wsFlags field. |
| 104719 | +** The particular combination of bits in each WhereLoop help to |
| 104720 | +** determine the algorithm that WhereLoop represents. |
| 104721 | +*/ |
| 104722 | +#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR or x IN (...) or x IS NULL */ |
| 104723 | +#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 104724 | +#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 104725 | +#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
| 104726 | +#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
| 104727 | +#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 104728 | +#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 104729 | +#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 104730 | +#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 104731 | +#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 104732 | +#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 104733 | +#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 104734 | +#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
| 104735 | +#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 104736 | +#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 104737 | +#define WHERE_TEMP_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 104738 | + |
| 104739 | + |
| 104740 | +/* Convert a WhereCost value (10 times log2(X)) into its integer value X. |
| 104741 | +** A rough approximation is used. The value returned is not exact. |
| 104742 | +*/ |
| 104743 | +static u64 whereCostToInt(WhereCost x){ |
| 104744 | + u64 n; |
| 104745 | + if( x<10 ) return 1; |
| 104746 | + n = x%10; |
| 104747 | + x /= 10; |
| 104748 | + if( n>=5 ) n -= 2; |
| 104749 | + else if( n>=1 ) n -= 1; |
| 104750 | + if( x>=3 ) return (n+8)<<(x-3); |
| 104751 | + return (n+8)>>(3-x); |
| 104752 | +} |
| 104753 | + |
| 104754 | +/* |
| 104755 | +** Return the estimated number of output rows from a WHERE clause |
| 104756 | +*/ |
| 104757 | +SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 104758 | + return whereCostToInt(pWInfo->nRowOut); |
| 104759 | +} |
| 104760 | + |
| 104761 | +/* |
| 104762 | +** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 104763 | +** WHERE clause returns outputs for DISTINCT processing. |
| 104764 | +*/ |
| 104765 | +SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 104766 | + return pWInfo->eDistinct; |
| 104767 | +} |
| 104768 | + |
| 104769 | +/* |
| 104770 | +** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 104771 | +** Return FALSE if the output needs to be sorted. |
| 104772 | +*/ |
| 104773 | +SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
| 104774 | + return pWInfo->bOBSat!=0; |
| 104775 | +} |
| 104776 | + |
| 104777 | +/* |
| 104778 | +** Return the VDBE address or label to jump to in order to continue |
| 104779 | +** immediately with the next row of a WHERE clause. |
| 104780 | +*/ |
| 104781 | +SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 104782 | + return pWInfo->iContinue; |
| 104783 | +} |
| 104784 | + |
| 104785 | +/* |
| 104786 | +** Return the VDBE address or label to jump to in order to break |
| 104787 | +** out of a WHERE loop. |
| 104788 | +*/ |
| 104789 | +SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 104790 | + return pWInfo->iBreak; |
| 104791 | +} |
| 104792 | + |
| 104793 | +/* |
| 104794 | +** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 104795 | +** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 104796 | +** UPDATE or DELETE might change subsequent WHERE clause results. |
| 104797 | +*/ |
| 104798 | +SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo){ |
| 104799 | + return pWInfo->okOnePass; |
| 104676 | 104800 | } |
| 104677 | 104801 | |
| 104678 | 104802 | /* |
| 104679 | 104803 | ** Initialize a preallocated WhereClause structure. |
| 104680 | 104804 | */ |
| 104681 | 104805 | static void whereClauseInit( |
| 104682 | 104806 | 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 */ |
| 104807 | + WhereInfo *pWInfo /* The WHERE processing context */ |
| 104686 | 104808 | ){ |
| 104687 | | - pWC->pParse = pParse; |
| 104688 | | - pWC->pMaskSet = pMaskSet; |
| 104809 | + pWC->pWInfo = pWInfo; |
| 104689 | 104810 | pWC->pOuter = 0; |
| 104690 | 104811 | pWC->nTerm = 0; |
| 104691 | 104812 | pWC->nSlot = ArraySize(pWC->aStatic); |
| 104692 | 104813 | pWC->a = pWC->aStatic; |
| 104693 | | - pWC->wctrlFlags = wctrlFlags; |
| 104694 | 104814 | } |
| 104695 | 104815 | |
| 104696 | 104816 | /* Forward reference */ |
| 104697 | 104817 | static void whereClauseClear(WhereClause*); |
| 104698 | 104818 | |
| | @@ -104717,11 +104837,11 @@ |
| 104717 | 104837 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 104718 | 104838 | */ |
| 104719 | 104839 | static void whereClauseClear(WhereClause *pWC){ |
| 104720 | 104840 | int i; |
| 104721 | 104841 | WhereTerm *a; |
| 104722 | | - sqlite3 *db = pWC->pParse->db; |
| 104842 | + sqlite3 *db = pWC->pWInfo->pParse->db; |
| 104723 | 104843 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
| 104724 | 104844 | if( a->wtFlags & TERM_DYNAMIC ){ |
| 104725 | 104845 | sqlite3ExprDelete(db, a->pExpr); |
| 104726 | 104846 | } |
| 104727 | 104847 | if( a->wtFlags & TERM_ORINFO ){ |
| | @@ -104758,11 +104878,11 @@ |
| 104758 | 104878 | WhereTerm *pTerm; |
| 104759 | 104879 | int idx; |
| 104760 | 104880 | testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ |
| 104761 | 104881 | if( pWC->nTerm>=pWC->nSlot ){ |
| 104762 | 104882 | WhereTerm *pOld = pWC->a; |
| 104763 | | - sqlite3 *db = pWC->pParse->db; |
| 104883 | + sqlite3 *db = pWC->pWInfo->pParse->db; |
| 104764 | 104884 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
| 104765 | 104885 | if( pWC->a==0 ){ |
| 104766 | 104886 | if( wtFlags & TERM_DYNAMIC ){ |
| 104767 | 104887 | sqlite3ExprDelete(db, p); |
| 104768 | 104888 | } |
| | @@ -104798,12 +104918,12 @@ |
| 104798 | 104918 | ** |
| 104799 | 104919 | ** In the previous sentence and in the diagram, "slot[]" refers to |
| 104800 | 104920 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
| 104801 | 104921 | ** all terms of the WHERE clause. |
| 104802 | 104922 | */ |
| 104803 | | -static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){ |
| 104804 | | - pWC->op = (u8)op; |
| 104923 | +static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 104924 | + pWC->op = op; |
| 104805 | 104925 | if( pExpr==0 ) return; |
| 104806 | 104926 | if( pExpr->op!=op ){ |
| 104807 | 104927 | whereClauseInsert(pWC, pExpr, 0); |
| 104808 | 104928 | }else{ |
| 104809 | 104929 | whereSplit(pWC, pExpr->pLeft, op); |
| | @@ -104810,13 +104930,13 @@ |
| 104810 | 104930 | whereSplit(pWC, pExpr->pRight, op); |
| 104811 | 104931 | } |
| 104812 | 104932 | } |
| 104813 | 104933 | |
| 104814 | 104934 | /* |
| 104815 | | -** Initialize an expression mask set (a WhereMaskSet object) |
| 104935 | +** Initialize a WhereMaskSet object |
| 104816 | 104936 | */ |
| 104817 | | -#define initMaskSet(P) memset(P, 0, sizeof(*P)) |
| 104937 | +#define initMaskSet(P) (P)->n=0 |
| 104818 | 104938 | |
| 104819 | 104939 | /* |
| 104820 | 104940 | ** Return the bitmask for the given cursor number. Return 0 if |
| 104821 | 104941 | ** iCursor is not in the set. |
| 104822 | 104942 | */ |
| | @@ -104823,11 +104943,11 @@ |
| 104823 | 104943 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 104824 | 104944 | int i; |
| 104825 | 104945 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
| 104826 | 104946 | for(i=0; i<pMaskSet->n; i++){ |
| 104827 | 104947 | if( pMaskSet->ix[i]==iCursor ){ |
| 104828 | | - return ((Bitmask)1)<<i; |
| 104948 | + return MASKBIT(i); |
| 104829 | 104949 | } |
| 104830 | 104950 | } |
| 104831 | 104951 | return 0; |
| 104832 | 104952 | } |
| 104833 | 104953 | |
| | @@ -104843,22 +104963,13 @@ |
| 104843 | 104963 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
| 104844 | 104964 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
| 104845 | 104965 | } |
| 104846 | 104966 | |
| 104847 | 104967 | /* |
| 104848 | | -** This routine walks (recursively) an expression tree and generates |
| 104968 | +** These routine walk (recursively) an expression tree and generates |
| 104849 | 104969 | ** a bitmask indicating which tables are used in that expression |
| 104850 | 104970 | ** 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 | 104971 | */ |
| 104861 | 104972 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 104862 | 104973 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 104863 | 104974 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
| 104864 | 104975 | Bitmask mask = 0; |
| | @@ -104908,11 +105019,11 @@ |
| 104908 | 105019 | } |
| 104909 | 105020 | |
| 104910 | 105021 | /* |
| 104911 | 105022 | ** Return TRUE if the given operator is one of the operators that is |
| 104912 | 105023 | ** allowed for an indexable WHERE clause term. The allowed operators are |
| 104913 | | -** "=", "<", ">", "<=", ">=", and "IN". |
| 105024 | +** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
| 104914 | 105025 | ** |
| 104915 | 105026 | ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be |
| 104916 | 105027 | ** of one of the following forms: column = expression column > expression |
| 104917 | 105028 | ** column >= expression column < expression column <= expression |
| 104918 | 105029 | ** expression = column expression > column expression >= column |
| | @@ -104935,14 +105046,13 @@ |
| 104935 | 105046 | /* |
| 104936 | 105047 | ** Commute a comparison operator. Expressions of the form "X op Y" |
| 104937 | 105048 | ** are converted into "Y op X". |
| 104938 | 105049 | ** |
| 104939 | 105050 | ** 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 |
| 105051 | +** collating sequence, then COLLATE operators are adjusted to ensure |
| 105052 | +** that the collating sequence does not change. For example: |
| 105053 | +** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on |
| 104944 | 105054 | ** the left hand side of a comparison overrides any collation sequence |
| 104945 | 105055 | ** attached to the right. For the same reason the EP_Collate flag |
| 104946 | 105056 | ** is not commuted. |
| 104947 | 105057 | */ |
| 104948 | 105058 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
| | @@ -104994,10 +105104,134 @@ |
| 104994 | 105104 | assert( op!=TK_LE || c==WO_LE ); |
| 104995 | 105105 | assert( op!=TK_GT || c==WO_GT ); |
| 104996 | 105106 | assert( op!=TK_GE || c==WO_GE ); |
| 104997 | 105107 | return c; |
| 104998 | 105108 | } |
| 105109 | + |
| 105110 | +/* |
| 105111 | +** Advance to the next WhereTerm that matches according to the criteria |
| 105112 | +** established when the pScan object was initialized by whereScanInit(). |
| 105113 | +** Return NULL if there are no more matching WhereTerms. |
| 105114 | +*/ |
| 105115 | +WhereTerm *whereScanNext(WhereScan *pScan){ |
| 105116 | + int iCur; /* The cursor on the LHS of the term */ |
| 105117 | + int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 105118 | + Expr *pX; /* An expression being tested */ |
| 105119 | + WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 105120 | + WhereTerm *pTerm; /* The term being tested */ |
| 105121 | + int k = pScan->k; /* Where to start scanning */ |
| 105122 | + |
| 105123 | + while( pScan->iEquiv<=pScan->nEquiv ){ |
| 105124 | + iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 105125 | + iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 105126 | + while( (pWC = pScan->pWC)!=0 ){ |
| 105127 | + for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
| 105128 | + if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){ |
| 105129 | + if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 105130 | + && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 105131 | + ){ |
| 105132 | + int j; |
| 105133 | + pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 105134 | + assert( pX->op==TK_COLUMN ); |
| 105135 | + for(j=0; j<pScan->nEquiv; j+=2){ |
| 105136 | + if( pScan->aEquiv[j]==pX->iTable |
| 105137 | + && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 105138 | + break; |
| 105139 | + } |
| 105140 | + } |
| 105141 | + if( j==pScan->nEquiv ){ |
| 105142 | + pScan->aEquiv[j] = pX->iTable; |
| 105143 | + pScan->aEquiv[j+1] = pX->iColumn; |
| 105144 | + pScan->nEquiv += 2; |
| 105145 | + } |
| 105146 | + } |
| 105147 | + if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 105148 | + /* Verify the affinity and collating sequence match */ |
| 105149 | + if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 105150 | + CollSeq *pColl; |
| 105151 | + Parse *pParse = pWC->pWInfo->pParse; |
| 105152 | + pX = pTerm->pExpr; |
| 105153 | + if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 105154 | + continue; |
| 105155 | + } |
| 105156 | + assert(pX->pLeft); |
| 105157 | + pColl = sqlite3BinaryCompareCollSeq(pParse, |
| 105158 | + pX->pLeft, pX->pRight); |
| 105159 | + if( pColl==0 ) pColl = pParse->db->pDfltColl; |
| 105160 | + if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 105161 | + continue; |
| 105162 | + } |
| 105163 | + } |
| 105164 | + if( (pTerm->eOperator & WO_EQ)!=0 |
| 105165 | + && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 105166 | + && pX->iTable==pScan->aEquiv[0] |
| 105167 | + && pX->iColumn==pScan->aEquiv[1] |
| 105168 | + ){ |
| 105169 | + continue; |
| 105170 | + } |
| 105171 | + pScan->k = k+1; |
| 105172 | + return pTerm; |
| 105173 | + } |
| 105174 | + } |
| 105175 | + } |
| 105176 | + pScan->pWC = pScan->pWC->pOuter; |
| 105177 | + k = 0; |
| 105178 | + } |
| 105179 | + pScan->pWC = pScan->pOrigWC; |
| 105180 | + k = 0; |
| 105181 | + pScan->iEquiv += 2; |
| 105182 | + } |
| 105183 | + return 0; |
| 105184 | +} |
| 105185 | + |
| 105186 | +/* |
| 105187 | +** Initialize a WHERE clause scanner object. Return a pointer to the |
| 105188 | +** first match. Return NULL if there are no matches. |
| 105189 | +** |
| 105190 | +** The scanner will be searching the WHERE clause pWC. It will look |
| 105191 | +** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 105192 | +** iCur. The <op> must be one of the operators described by opMask. |
| 105193 | +** |
| 105194 | +** If the search is for X and the WHERE clause contains terms of the |
| 105195 | +** form X=Y then this routine might also return terms of the form |
| 105196 | +** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 105197 | +** but is enough to handle most commonly occurring SQL statements. |
| 105198 | +** |
| 105199 | +** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 105200 | +** index pIdx. |
| 105201 | +*/ |
| 105202 | +WhereTerm *whereScanInit( |
| 105203 | + WhereScan *pScan, /* The WhereScan object being initialized */ |
| 105204 | + WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 105205 | + int iCur, /* Cursor to scan for */ |
| 105206 | + int iColumn, /* Column to scan for */ |
| 105207 | + u32 opMask, /* Operator(s) to scan for */ |
| 105208 | + Index *pIdx /* Must be compatible with this index */ |
| 105209 | +){ |
| 105210 | + int j; |
| 105211 | + |
| 105212 | + /* memset(pScan, 0, sizeof(*pScan)); */ |
| 105213 | + pScan->pOrigWC = pWC; |
| 105214 | + pScan->pWC = pWC; |
| 105215 | + if( pIdx && iColumn>=0 ){ |
| 105216 | + pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 105217 | + for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 105218 | + if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 105219 | + } |
| 105220 | + pScan->zCollName = pIdx->azColl[j]; |
| 105221 | + }else{ |
| 105222 | + pScan->idxaff = 0; |
| 105223 | + pScan->zCollName = 0; |
| 105224 | + } |
| 105225 | + pScan->opMask = opMask; |
| 105226 | + pScan->k = 0; |
| 105227 | + pScan->aEquiv[0] = iCur; |
| 105228 | + pScan->aEquiv[1] = iColumn; |
| 105229 | + pScan->nEquiv = 2; |
| 105230 | + pScan->iEquiv = 2; |
| 105231 | + return whereScanNext(pScan); |
| 105232 | +} |
| 104999 | 105233 | |
| 105000 | 105234 | /* |
| 105001 | 105235 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 105002 | 105236 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 105003 | 105237 | ** the WO_xx operator codes specified by the op parameter. |
| | @@ -105026,98 +105260,32 @@ |
| 105026 | 105260 | int iColumn, /* Column number of LHS */ |
| 105027 | 105261 | Bitmask notReady, /* RHS must not overlap with this mask */ |
| 105028 | 105262 | u32 op, /* Mask of WO_xx values describing operator */ |
| 105029 | 105263 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 105030 | 105264 | ){ |
| 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: |
| 105265 | + WhereTerm *pResult = 0; |
| 105266 | + WhereTerm *p; |
| 105267 | + WhereScan scan; |
| 105268 | + |
| 105269 | + p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 105270 | + while( p ){ |
| 105271 | + if( (p->prereqRight & notReady)==0 ){ |
| 105272 | + if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 105273 | + return p; |
| 105274 | + } |
| 105275 | + if( pResult==0 ) pResult = p; |
| 105276 | + } |
| 105277 | + p = whereScanNext(&scan); |
| 105278 | + } |
| 105109 | 105279 | return pResult; |
| 105110 | 105280 | } |
| 105111 | 105281 | |
| 105112 | 105282 | /* Forward reference */ |
| 105113 | 105283 | static void exprAnalyze(SrcList*, WhereClause*, int); |
| 105114 | 105284 | |
| 105115 | 105285 | /* |
| 105116 | 105286 | ** Call exprAnalyze on all terms in a WHERE clause. |
| 105117 | | -** |
| 105118 | | -** |
| 105119 | 105287 | */ |
| 105120 | 105288 | static void exprAnalyzeAll( |
| 105121 | 105289 | SrcList *pTabList, /* the FROM clause */ |
| 105122 | 105290 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 105123 | 105291 | ){ |
| | @@ -105345,15 +105513,15 @@ |
| 105345 | 105513 | static void exprAnalyzeOrTerm( |
| 105346 | 105514 | SrcList *pSrc, /* the FROM clause */ |
| 105347 | 105515 | WhereClause *pWC, /* the complete WHERE clause */ |
| 105348 | 105516 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 105349 | 105517 | ){ |
| 105350 | | - Parse *pParse = pWC->pParse; /* Parser context */ |
| 105518 | + WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 105519 | + Parse *pParse = pWInfo->pParse; /* Parser context */ |
| 105351 | 105520 | sqlite3 *db = pParse->db; /* Database connection */ |
| 105352 | 105521 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 105353 | 105522 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
| 105354 | | - WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */ |
| 105355 | 105523 | int i; /* Loop counters */ |
| 105356 | 105524 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 105357 | 105525 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 105358 | 105526 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 105359 | 105527 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| | @@ -105368,11 +105536,11 @@ |
| 105368 | 105536 | assert( pExpr->op==TK_OR ); |
| 105369 | 105537 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
| 105370 | 105538 | if( pOrInfo==0 ) return; |
| 105371 | 105539 | pTerm->wtFlags |= TERM_ORINFO; |
| 105372 | 105540 | pOrWc = &pOrInfo->wc; |
| 105373 | | - whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
| 105541 | + whereClauseInit(pOrWc, pWInfo); |
| 105374 | 105542 | whereSplit(pOrWc, pExpr, TK_OR); |
| 105375 | 105543 | exprAnalyzeAll(pSrc, pOrWc); |
| 105376 | 105544 | if( db->mallocFailed ) return; |
| 105377 | 105545 | assert( pOrWc->nTerm>=2 ); |
| 105378 | 105546 | |
| | @@ -105394,20 +105562,20 @@ |
| 105394 | 105562 | Bitmask b = 0; |
| 105395 | 105563 | pOrTerm->u.pAndInfo = pAndInfo; |
| 105396 | 105564 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 105397 | 105565 | pOrTerm->eOperator = WO_AND; |
| 105398 | 105566 | pAndWC = &pAndInfo->wc; |
| 105399 | | - whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
| 105567 | + whereClauseInit(pAndWC, pWC->pWInfo); |
| 105400 | 105568 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 105401 | 105569 | exprAnalyzeAll(pSrc, pAndWC); |
| 105402 | 105570 | pAndWC->pOuter = pWC; |
| 105403 | 105571 | testcase( db->mallocFailed ); |
| 105404 | 105572 | if( !db->mallocFailed ){ |
| 105405 | 105573 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 105406 | 105574 | assert( pAndTerm->pExpr ); |
| 105407 | 105575 | if( allowedOp(pAndTerm->pExpr->op) ){ |
| 105408 | | - b |= getMask(pMaskSet, pAndTerm->leftCursor); |
| 105576 | + b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
| 105409 | 105577 | } |
| 105410 | 105578 | } |
| 105411 | 105579 | } |
| 105412 | 105580 | indexable &= b; |
| 105413 | 105581 | } |
| | @@ -105414,14 +105582,14 @@ |
| 105414 | 105582 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 105415 | 105583 | /* Skip this term for now. We revisit it when we process the |
| 105416 | 105584 | ** corresponding TERM_VIRTUAL term */ |
| 105417 | 105585 | }else{ |
| 105418 | 105586 | Bitmask b; |
| 105419 | | - b = getMask(pMaskSet, pOrTerm->leftCursor); |
| 105587 | + b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
| 105420 | 105588 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 105421 | 105589 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
| 105422 | | - b |= getMask(pMaskSet, pOther->leftCursor); |
| 105590 | + b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
| 105423 | 105591 | } |
| 105424 | 105592 | indexable &= b; |
| 105425 | 105593 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
| 105426 | 105594 | chngToIN = 0; |
| 105427 | 105595 | }else{ |
| | @@ -105479,11 +105647,11 @@ |
| 105479 | 105647 | /* This is the 2-bit case and we are on the second iteration and |
| 105480 | 105648 | ** current term is from the first iteration. So skip this term. */ |
| 105481 | 105649 | assert( j==1 ); |
| 105482 | 105650 | continue; |
| 105483 | 105651 | } |
| 105484 | | - if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){ |
| 105652 | + if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
| 105485 | 105653 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 105486 | 105654 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 105487 | 105655 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 105488 | 105656 | ** and use its inversion. */ |
| 105489 | 105657 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| | @@ -105498,11 +105666,11 @@ |
| 105498 | 105666 | if( i<0 ){ |
| 105499 | 105667 | /* No candidate table+column was found. This can only occur |
| 105500 | 105668 | ** on the second iteration */ |
| 105501 | 105669 | assert( j==1 ); |
| 105502 | 105670 | assert( IsPowerOfTwo(chngToIN) ); |
| 105503 | | - assert( chngToIN==getMask(pMaskSet, iCursor) ); |
| 105671 | + assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
| 105504 | 105672 | break; |
| 105505 | 105673 | } |
| 105506 | 105674 | testcase( j==1 ); |
| 105507 | 105675 | |
| 105508 | 105676 | /* We have found a candidate table and column. Check to see if that |
| | @@ -105547,11 +105715,11 @@ |
| 105547 | 105715 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
| 105548 | 105716 | assert( pOrTerm->eOperator & WO_EQ ); |
| 105549 | 105717 | assert( pOrTerm->leftCursor==iCursor ); |
| 105550 | 105718 | assert( pOrTerm->u.leftColumn==iColumn ); |
| 105551 | 105719 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
| 105552 | | - pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup); |
| 105720 | + pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
| 105553 | 105721 | pLeft = pOrTerm->pExpr->pLeft; |
| 105554 | 105722 | } |
| 105555 | 105723 | assert( pLeft!=0 ); |
| 105556 | 105724 | pDup = sqlite3ExprDup(db, pLeft, 0); |
| 105557 | 105725 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
| | @@ -105596,10 +105764,11 @@ |
| 105596 | 105764 | static void exprAnalyze( |
| 105597 | 105765 | SrcList *pSrc, /* the FROM clause */ |
| 105598 | 105766 | WhereClause *pWC, /* the WHERE clause */ |
| 105599 | 105767 | int idxTerm /* Index of the term to be analyzed */ |
| 105600 | 105768 | ){ |
| 105769 | + WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 105601 | 105770 | WhereTerm *pTerm; /* The term to be analyzed */ |
| 105602 | 105771 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
| 105603 | 105772 | Expr *pExpr; /* The expression to be analyzed */ |
| 105604 | 105773 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 105605 | 105774 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
| | @@ -105606,18 +105775,18 @@ |
| 105606 | 105775 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
| 105607 | 105776 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 105608 | 105777 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 105609 | 105778 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
| 105610 | 105779 | int op; /* Top-level operator. pExpr->op */ |
| 105611 | | - Parse *pParse = pWC->pParse; /* Parsing context */ |
| 105780 | + Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 105612 | 105781 | sqlite3 *db = pParse->db; /* Database connection */ |
| 105613 | 105782 | |
| 105614 | 105783 | if( db->mallocFailed ){ |
| 105615 | 105784 | return; |
| 105616 | 105785 | } |
| 105617 | 105786 | pTerm = &pWC->a[idxTerm]; |
| 105618 | | - pMaskSet = pWC->pMaskSet; |
| 105787 | + pMaskSet = &pWInfo->sMaskSet; |
| 105619 | 105788 | pExpr = pTerm->pExpr; |
| 105620 | 105789 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
| 105621 | 105790 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
| 105622 | 105791 | op = pExpr->op; |
| 105623 | 105792 | if( op==TK_IN ){ |
| | @@ -105891,15 +106060,12 @@ |
| 105891 | 106060 | */ |
| 105892 | 106061 | pTerm->prereqRight |= extraRight; |
| 105893 | 106062 | } |
| 105894 | 106063 | |
| 105895 | 106064 | /* |
| 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. |
| 106065 | +** This function searches pList for a entry that matches the iCol-th column |
| 106066 | +** of index pIdx. |
| 105901 | 106067 | ** |
| 105902 | 106068 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 105903 | 106069 | ** no expression is found, -1 is returned. |
| 105904 | 106070 | */ |
| 105905 | 106071 | static int findIndexCol( |
| | @@ -105925,82 +106091,23 @@ |
| 105925 | 106091 | } |
| 105926 | 106092 | } |
| 105927 | 106093 | |
| 105928 | 106094 | return -1; |
| 105929 | 106095 | } |
| 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 | 106096 | |
| 105991 | 106097 | /* |
| 105992 | 106098 | ** 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. |
| 106099 | +** is redundant. |
| 106100 | +** |
| 106101 | +** A DISTINCT list is redundant if the database contains some subset of |
| 106102 | +** columns that are unique and non-null. |
| 105996 | 106103 | */ |
| 105997 | 106104 | static int isDistinctRedundant( |
| 105998 | | - Parse *pParse, |
| 105999 | | - SrcList *pTabList, |
| 106000 | | - WhereClause *pWC, |
| 106001 | | - ExprList *pDistinct |
| 106105 | + Parse *pParse, /* Parsing context */ |
| 106106 | + SrcList *pTabList, /* The FROM clause */ |
| 106107 | + WhereClause *pWC, /* The WHERE clause */ |
| 106108 | + ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
| 106002 | 106109 | ){ |
| 106003 | 106110 | Table *pTab; |
| 106004 | 106111 | Index *pIdx; |
| 106005 | 106112 | int i; |
| 106006 | 106113 | int iBase; |
| | @@ -106051,35 +106158,90 @@ |
| 106051 | 106158 | } |
| 106052 | 106159 | } |
| 106053 | 106160 | |
| 106054 | 106161 | return 0; |
| 106055 | 106162 | } |
| 106163 | + |
| 106164 | +/* |
| 106165 | +** The (an approximate) sum of two WhereCosts. This computation is |
| 106166 | +** not a simple "+" operator because WhereCost is stored as a logarithmic |
| 106167 | +** value. |
| 106168 | +** |
| 106169 | +*/ |
| 106170 | +static WhereCost whereCostAdd(WhereCost a, WhereCost b){ |
| 106171 | + static const unsigned char x[] = { |
| 106172 | + 10, 10, /* 0,1 */ |
| 106173 | + 9, 9, /* 2,3 */ |
| 106174 | + 8, 8, /* 4,5 */ |
| 106175 | + 7, 7, 7, /* 6,7,8 */ |
| 106176 | + 6, 6, 6, /* 9,10,11 */ |
| 106177 | + 5, 5, 5, /* 12-14 */ |
| 106178 | + 4, 4, 4, 4, /* 15-18 */ |
| 106179 | + 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 106180 | + 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 106181 | + }; |
| 106182 | + if( a>=b ){ |
| 106183 | + if( a>b+49 ) return a; |
| 106184 | + if( a>b+31 ) return a+1; |
| 106185 | + return a+x[a-b]; |
| 106186 | + }else{ |
| 106187 | + if( b>a+49 ) return b; |
| 106188 | + if( b>a+31 ) return b+1; |
| 106189 | + return b+x[b-a]; |
| 106190 | + } |
| 106191 | +} |
| 106056 | 106192 | |
| 106057 | 106193 | /* |
| 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. |
| 106194 | +** Convert an integer into a WhereCost. In other words, compute a |
| 106195 | +** good approximatation for 10*log2(x). |
| 106063 | 106196 | */ |
| 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; |
| 106197 | +static WhereCost whereCost(tRowcnt x){ |
| 106198 | + static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 106199 | + WhereCost y = 40; |
| 106200 | + if( x<8 ){ |
| 106201 | + if( x<2 ) return 0; |
| 106202 | + while( x<8 ){ y -= 10; x <<= 1; } |
| 106203 | + }else{ |
| 106204 | + while( x>255 ){ y += 40; x >>= 4; } |
| 106205 | + while( x>15 ){ y += 10; x >>= 1; } |
| 106206 | + } |
| 106207 | + return a[x&7] + y - 10; |
| 106208 | +} |
| 106209 | + |
| 106210 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 106211 | +/* |
| 106212 | +** Convert a double (as received from xBestIndex of a virtual table) |
| 106213 | +** into a WhereCost. In other words, compute an approximation for |
| 106214 | +** 10*log2(x). |
| 106215 | +*/ |
| 106216 | +static WhereCost whereCostFromDouble(double x){ |
| 106217 | + u64 a; |
| 106218 | + WhereCost e; |
| 106219 | + assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 106220 | + if( x<=1 ) return 0; |
| 106221 | + if( x<=2000000000 ) return whereCost((tRowcnt)x); |
| 106222 | + memcpy(&a, &x, 8); |
| 106223 | + e = (a>>52) - 1022; |
| 106224 | + return e*10; |
| 106225 | +} |
| 106226 | +#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 106227 | + |
| 106228 | +/* |
| 106229 | +** Estimate the logarithm of the input value to base 2. |
| 106230 | +*/ |
| 106231 | +static WhereCost estLog(WhereCost N){ |
| 106232 | + WhereCost x = whereCost(N); |
| 106233 | + return x>33 ? x - 33 : 0; |
| 106072 | 106234 | } |
| 106073 | 106235 | |
| 106074 | 106236 | /* |
| 106075 | 106237 | ** Two routines for printing the content of an sqlite3_index_info |
| 106076 | 106238 | ** structure. Used for testing and debugging only. If neither |
| 106077 | 106239 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 106078 | 106240 | ** are no-ops. |
| 106079 | 106241 | */ |
| 106080 | | -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG) |
| 106242 | +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
| 106081 | 106243 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 106082 | 106244 | int i; |
| 106083 | 106245 | if( !sqlite3WhereTrace ) return; |
| 106084 | 106246 | for(i=0; i<p->nConstraint; i++){ |
| 106085 | 106247 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| | @@ -106113,111 +106275,10 @@ |
| 106113 | 106275 | #else |
| 106114 | 106276 | #define TRACE_IDX_INPUTS(A) |
| 106115 | 106277 | #define TRACE_IDX_OUTPUTS(A) |
| 106116 | 106278 | #endif |
| 106117 | 106279 | |
| 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 | 106280 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 106220 | 106281 | /* |
| 106221 | 106282 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 106222 | 106283 | ** could be used with an index to access pSrc, assuming an appropriate |
| 106223 | 106284 | ** index existed. |
| | @@ -106229,92 +106290,17 @@ |
| 106229 | 106290 | ){ |
| 106230 | 106291 | char aff; |
| 106231 | 106292 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
| 106232 | 106293 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
| 106233 | 106294 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
| 106295 | + if( pTerm->u.leftColumn<0 ) return 0; |
| 106234 | 106296 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 106235 | 106297 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 106236 | 106298 | return 1; |
| 106237 | 106299 | } |
| 106238 | 106300 | #endif |
| 106239 | 106301 | |
| 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 | 106302 | |
| 106317 | 106303 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 106318 | 106304 | /* |
| 106319 | 106305 | ** Generate code to construct the Index object for an automatic index |
| 106320 | 106306 | ** and to set up the WhereLevel object pLevel so that the code generator |
| | @@ -106340,10 +106326,11 @@ |
| 106340 | 106326 | int regRecord; /* Register holding an index record */ |
| 106341 | 106327 | int n; /* Column counter */ |
| 106342 | 106328 | int i; /* Loop counter */ |
| 106343 | 106329 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
| 106344 | 106330 | CollSeq *pColl; /* Collating sequence to on a column */ |
| 106331 | + WhereLoop *pLoop; /* The Loop object */ |
| 106345 | 106332 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 106346 | 106333 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 106347 | 106334 | |
| 106348 | 106335 | /* Generate code to skip over the creation and initialization of the |
| 106349 | 106336 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| | @@ -106354,54 +106341,58 @@ |
| 106354 | 106341 | /* Count the number of columns that will be added to the index |
| 106355 | 106342 | ** and used to match WHERE clause constraints */ |
| 106356 | 106343 | nColumn = 0; |
| 106357 | 106344 | pTable = pSrc->pTab; |
| 106358 | 106345 | pWCEnd = &pWC->a[pWC->nTerm]; |
| 106346 | + pLoop = pLevel->pWLoop; |
| 106359 | 106347 | idxCols = 0; |
| 106360 | 106348 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 106361 | 106349 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 106362 | 106350 | int iCol = pTerm->u.leftColumn; |
| 106363 | | - Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol; |
| 106351 | + Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 106364 | 106352 | testcase( iCol==BMS ); |
| 106365 | 106353 | testcase( iCol==BMS-1 ); |
| 106366 | 106354 | if( (idxCols & cMask)==0 ){ |
| 106367 | | - nColumn++; |
| 106355 | + if( whereLoopResize(pParse->db, pLoop, nColumn+1) ) return; |
| 106356 | + pLoop->aLTerm[nColumn++] = pTerm; |
| 106368 | 106357 | idxCols |= cMask; |
| 106369 | 106358 | } |
| 106370 | 106359 | } |
| 106371 | 106360 | } |
| 106372 | 106361 | assert( nColumn>0 ); |
| 106373 | | - pLevel->plan.nEq = nColumn; |
| 106362 | + pLoop->u.btree.nEq = pLoop->nLTerm = nColumn; |
| 106363 | + pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
| 106364 | + | WHERE_TEMP_INDEX; |
| 106374 | 106365 | |
| 106375 | 106366 | /* Count the number of additional columns needed to create a |
| 106376 | 106367 | ** covering index. A "covering index" is an index that contains all |
| 106377 | 106368 | ** columns that are needed by the query. With a covering index, the |
| 106378 | 106369 | ** original table never needs to be accessed. Automatic indices must |
| 106379 | 106370 | ** be a covering index because the index will not be updated if the |
| 106380 | 106371 | ** original table changes and the index and table cannot both be used |
| 106381 | 106372 | ** if they go out of sync. |
| 106382 | 106373 | */ |
| 106383 | | - extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1))); |
| 106374 | + extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 106384 | 106375 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
| 106385 | 106376 | testcase( pTable->nCol==BMS-1 ); |
| 106386 | 106377 | testcase( pTable->nCol==BMS-2 ); |
| 106387 | 106378 | for(i=0; i<mxBitCol; i++){ |
| 106388 | | - if( extraCols & (((Bitmask)1)<<i) ) nColumn++; |
| 106379 | + if( extraCols & MASKBIT(i) ) nColumn++; |
| 106389 | 106380 | } |
| 106390 | | - if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ |
| 106381 | + if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 106391 | 106382 | nColumn += pTable->nCol - BMS + 1; |
| 106392 | 106383 | } |
| 106393 | | - pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ; |
| 106384 | + pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
| 106394 | 106385 | |
| 106395 | 106386 | /* Construct the Index object to describe this index */ |
| 106396 | 106387 | nByte = sizeof(Index); |
| 106397 | 106388 | nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 106398 | 106389 | nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 106399 | 106390 | nByte += nColumn; /* Index.aSortOrder */ |
| 106400 | 106391 | pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 106401 | 106392 | if( pIdx==0 ) return; |
| 106402 | | - pLevel->plan.u.pIdx = pIdx; |
| 106393 | + pLoop->u.btree.pIndex = pIdx; |
| 106403 | 106394 | pIdx->azColl = (char**)&pIdx[1]; |
| 106404 | 106395 | pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 106405 | 106396 | pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 106406 | 106397 | pIdx->zName = "auto-index"; |
| 106407 | 106398 | pIdx->nColumn = nColumn; |
| | @@ -106409,11 +106400,13 @@ |
| 106409 | 106400 | n = 0; |
| 106410 | 106401 | idxCols = 0; |
| 106411 | 106402 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 106412 | 106403 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 106413 | 106404 | int iCol = pTerm->u.leftColumn; |
| 106414 | | - Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol; |
| 106405 | + Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 106406 | + testcase( iCol==BMS-1 ); |
| 106407 | + testcase( iCol==BMS ); |
| 106415 | 106408 | if( (idxCols & cMask)==0 ){ |
| 106416 | 106409 | Expr *pX = pTerm->pExpr; |
| 106417 | 106410 | idxCols |= cMask; |
| 106418 | 106411 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 106419 | 106412 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| | @@ -106420,22 +106413,22 @@ |
| 106420 | 106413 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
| 106421 | 106414 | n++; |
| 106422 | 106415 | } |
| 106423 | 106416 | } |
| 106424 | 106417 | } |
| 106425 | | - assert( (u32)n==pLevel->plan.nEq ); |
| 106418 | + assert( (u32)n==pLoop->u.btree.nEq ); |
| 106426 | 106419 | |
| 106427 | 106420 | /* Add additional columns needed to make the automatic index into |
| 106428 | 106421 | ** a covering index */ |
| 106429 | 106422 | for(i=0; i<mxBitCol; i++){ |
| 106430 | | - if( extraCols & (((Bitmask)1)<<i) ){ |
| 106423 | + if( extraCols & MASKBIT(i) ){ |
| 106431 | 106424 | pIdx->aiColumn[n] = i; |
| 106432 | 106425 | pIdx->azColl[n] = "BINARY"; |
| 106433 | 106426 | n++; |
| 106434 | 106427 | } |
| 106435 | 106428 | } |
| 106436 | | - if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ |
| 106429 | + if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 106437 | 106430 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 106438 | 106431 | pIdx->aiColumn[n] = i; |
| 106439 | 106432 | pIdx->azColl[n] = "BINARY"; |
| 106440 | 106433 | n++; |
| 106441 | 106434 | } |
| | @@ -106443,10 +106436,11 @@ |
| 106443 | 106436 | assert( n==nColumn ); |
| 106444 | 106437 | |
| 106445 | 106438 | /* Create the automatic index */ |
| 106446 | 106439 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 106447 | 106440 | assert( pLevel->iIdxCur>=0 ); |
| 106441 | + pLevel->iIdxCur = pParse->nTab++; |
| 106448 | 106442 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
| 106449 | 106443 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
| 106450 | 106444 | VdbeComment((v, "for %s", pTable->zName)); |
| 106451 | 106445 | |
| 106452 | 106446 | /* Fill the automatic index with content */ |
| | @@ -106469,26 +106463,25 @@ |
| 106469 | 106463 | /* |
| 106470 | 106464 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 106471 | 106465 | ** responsibility of the caller to eventually release the structure |
| 106472 | 106466 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 106473 | 106467 | */ |
| 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; |
| 106468 | +static sqlite3_index_info *allocateIndexInfo( |
| 106469 | + Parse *pParse, |
| 106470 | + WhereClause *pWC, |
| 106471 | + struct SrcList_item *pSrc, |
| 106472 | + ExprList *pOrderBy |
| 106473 | +){ |
| 106479 | 106474 | int i, j; |
| 106480 | 106475 | int nTerm; |
| 106481 | 106476 | struct sqlite3_index_constraint *pIdxCons; |
| 106482 | 106477 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 106483 | 106478 | struct sqlite3_index_constraint_usage *pUsage; |
| 106484 | 106479 | WhereTerm *pTerm; |
| 106485 | 106480 | int nOrderBy; |
| 106486 | 106481 | sqlite3_index_info *pIdxInfo; |
| 106487 | 106482 | |
| 106488 | | - WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName)); |
| 106489 | | - |
| 106490 | 106483 | /* Count the number of possible WHERE clause constraints referring |
| 106491 | 106484 | ** to this virtual table */ |
| 106492 | 106485 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 106493 | 106486 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 106494 | 106487 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| | @@ -106520,11 +106513,10 @@ |
| 106520 | 106513 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 106521 | 106514 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 106522 | 106515 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 106523 | 106516 | if( pIdxInfo==0 ){ |
| 106524 | 106517 | sqlite3ErrorMsg(pParse, "out of memory"); |
| 106525 | | - /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 106526 | 106518 | return 0; |
| 106527 | 106519 | } |
| 106528 | 106520 | |
| 106529 | 106521 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 106530 | 106522 | ** many fields that are declared "const" to prevent xBestIndex from |
| | @@ -106576,12 +106568,12 @@ |
| 106576 | 106568 | } |
| 106577 | 106569 | |
| 106578 | 106570 | /* |
| 106579 | 106571 | ** The table object reference passed as the second argument to this function |
| 106580 | 106572 | ** 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. |
| 106573 | +** method of the virtual table with the sqlite3_index_info object that |
| 106574 | +** comes in as the 3rd argument to this function. |
| 106583 | 106575 | ** |
| 106584 | 106576 | ** If an error occurs, pParse is populated with an error message and a |
| 106585 | 106577 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 106586 | 106578 | ** part of the sqlite3_index_info structure is left populated. |
| 106587 | 106579 | ** |
| | @@ -106592,11 +106584,10 @@ |
| 106592 | 106584 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
| 106593 | 106585 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
| 106594 | 106586 | int i; |
| 106595 | 106587 | int rc; |
| 106596 | 106588 | |
| 106597 | | - WHERETRACE(("xBestIndex for %s\n", pTab->zName)); |
| 106598 | 106589 | TRACE_IDX_INPUTS(p); |
| 106599 | 106590 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 106600 | 106591 | TRACE_IDX_OUTPUTS(p); |
| 106601 | 106592 | |
| 106602 | 106593 | if( rc!=SQLITE_OK ){ |
| | @@ -106618,211 +106609,12 @@ |
| 106618 | 106609 | } |
| 106619 | 106610 | } |
| 106620 | 106611 | |
| 106621 | 106612 | return pParse->nErr; |
| 106622 | 106613 | } |
| 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 */ |
| 106614 | +#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 106615 | + |
| 106824 | 106616 | |
| 106825 | 106617 | #ifdef SQLITE_ENABLE_STAT3 |
| 106826 | 106618 | /* |
| 106827 | 106619 | ** Estimate the location of a particular key among all keys in an |
| 106828 | 106620 | ** index. Store the results in aStat as follows: |
| | @@ -107060,11 +106852,11 @@ |
| 107060 | 106852 | Parse *pParse, /* Parsing & code generating context */ |
| 107061 | 106853 | Index *p, /* The index containing the range-compared column; "x" */ |
| 107062 | 106854 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 107063 | 106855 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 107064 | 106856 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 107065 | | - double *pRangeDiv /* OUT: Reduce search space by this divisor */ |
| 106857 | + WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */ |
| 107066 | 106858 | ){ |
| 107067 | 106859 | int rc = SQLITE_OK; |
| 107068 | 106860 | |
| 107069 | 106861 | #ifdef SQLITE_ENABLE_STAT3 |
| 107070 | 106862 | |
| | @@ -107098,29 +106890,35 @@ |
| 107098 | 106890 | if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1]; |
| 107099 | 106891 | } |
| 107100 | 106892 | sqlite3ValueFree(pRangeVal); |
| 107101 | 106893 | } |
| 107102 | 106894 | 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); |
| 106895 | + WhereCost iBase = whereCost(p->aiRowEst[0]); |
| 106896 | + if( iUpper>iLower ){ |
| 106897 | + iBase -= whereCost(iUpper - iLower); |
| 107107 | 106898 | } |
| 107108 | | - WHERETRACE(("range scan regions: %u..%u div=%g\n", |
| 107109 | | - (u32)iLower, (u32)iUpper, *pRangeDiv)); |
| 106899 | + *pRangeDiv = iBase; |
| 106900 | + WHERETRACE(0x100, ("range scan regions: %u..%u div=%d\n", |
| 106901 | + (u32)iLower, (u32)iUpper, *pRangeDiv)); |
| 107110 | 106902 | return SQLITE_OK; |
| 107111 | 106903 | } |
| 107112 | 106904 | } |
| 107113 | 106905 | #else |
| 107114 | 106906 | UNUSED_PARAMETER(pParse); |
| 107115 | 106907 | UNUSED_PARAMETER(p); |
| 107116 | 106908 | UNUSED_PARAMETER(nEq); |
| 107117 | 106909 | #endif |
| 107118 | 106910 | assert( pLower || pUpper ); |
| 107119 | | - *pRangeDiv = (double)1; |
| 107120 | | - if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4; |
| 107121 | | - if( pUpper ) *pRangeDiv *= (double)4; |
| 106911 | + *pRangeDiv = 0; |
| 106912 | + /* TUNING: Each inequality constraint reduces the search space 4-fold. |
| 106913 | + ** A BETWEEN operator, therefore, reduces the search space 16-fold */ |
| 106914 | + if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){ |
| 106915 | + *pRangeDiv += 20; assert( 20==whereCost(4) ); |
| 106916 | + } |
| 106917 | + if( pUpper ){ |
| 106918 | + *pRangeDiv += 20; assert( 20==whereCost(4) ); |
| 106919 | + } |
| 107122 | 106920 | return rc; |
| 107123 | 106921 | } |
| 107124 | 106922 | |
| 107125 | 106923 | #ifdef SQLITE_ENABLE_STAT3 |
| 107126 | 106924 | /* |
| | @@ -107142,11 +106940,11 @@ |
| 107142 | 106940 | */ |
| 107143 | 106941 | static int whereEqualScanEst( |
| 107144 | 106942 | Parse *pParse, /* Parsing & code generating context */ |
| 107145 | 106943 | Index *p, /* The index whose left-most column is pTerm */ |
| 107146 | 106944 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 107147 | | - double *pnRow /* Write the revised row estimate here */ |
| 106945 | + tRowcnt *pnRow /* Write the revised row estimate here */ |
| 107148 | 106946 | ){ |
| 107149 | 106947 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
| 107150 | 106948 | u8 aff; /* Column affinity */ |
| 107151 | 106949 | int rc; /* Subfunction return code */ |
| 107152 | 106950 | tRowcnt a[2]; /* Statistics */ |
| | @@ -107161,11 +106959,11 @@ |
| 107161 | 106959 | pRhs = sqlite3ValueNew(pParse->db); |
| 107162 | 106960 | } |
| 107163 | 106961 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
| 107164 | 106962 | rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 107165 | 106963 | if( rc==SQLITE_OK ){ |
| 107166 | | - WHERETRACE(("equality scan regions: %d\n", (int)a[1])); |
| 106964 | + WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1])); |
| 107167 | 106965 | *pnRow = a[1]; |
| 107168 | 106966 | } |
| 107169 | 106967 | whereEqualScanEst_cancel: |
| 107170 | 106968 | sqlite3ValueFree(pRhs); |
| 107171 | 106969 | return rc; |
| | @@ -107191,16 +106989,16 @@ |
| 107191 | 106989 | */ |
| 107192 | 106990 | static int whereInScanEst( |
| 107193 | 106991 | Parse *pParse, /* Parsing & code generating context */ |
| 107194 | 106992 | Index *p, /* The index whose left-most column is pTerm */ |
| 107195 | 106993 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 107196 | | - double *pnRow /* Write the revised row estimate here */ |
| 106994 | + tRowcnt *pnRow /* Write the revised row estimate here */ |
| 107197 | 106995 | ){ |
| 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 */ |
| 106996 | + int rc = SQLITE_OK; /* Subfunction return code */ |
| 106997 | + tRowcnt nEst; /* Number of rows for a single term */ |
| 106998 | + tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 106999 | + int i; /* Loop counter */ |
| 107202 | 107000 | |
| 107203 | 107001 | assert( p->aSample!=0 ); |
| 107204 | 107002 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 107205 | 107003 | nEst = p->aiRowEst[0]; |
| 107206 | 107004 | rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| | @@ -107207,888 +107005,15 @@ |
| 107207 | 107005 | nRowEst += nEst; |
| 107208 | 107006 | } |
| 107209 | 107007 | if( rc==SQLITE_OK ){ |
| 107210 | 107008 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 107211 | 107009 | *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 | | -} |
| 107010 | + WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst)); |
| 107011 | + } |
| 107012 | + return rc; |
| 107013 | +} |
| 107014 | +#endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 108090 | 107015 | |
| 108091 | 107016 | /* |
| 108092 | 107017 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 108093 | 107018 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 108094 | 107019 | ** or USING clause of that join. |
| | @@ -108183,10 +107108,11 @@ |
| 108183 | 107108 | static int codeEqualityTerm( |
| 108184 | 107109 | Parse *pParse, /* The parsing context */ |
| 108185 | 107110 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
| 108186 | 107111 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 108187 | 107112 | int iEq, /* Index of the equality term within this level */ |
| 107113 | + int bRev, /* True for reverse-order IN operations */ |
| 108188 | 107114 | int iTarget /* Attempt to leave results in this register */ |
| 108189 | 107115 | ){ |
| 108190 | 107116 | Expr *pX = pTerm->pExpr; |
| 108191 | 107117 | Vdbe *v = pParse->pVdbe; |
| 108192 | 107118 | int iReg; /* Register holding results */ |
| | @@ -108200,18 +107126,17 @@ |
| 108200 | 107126 | #ifndef SQLITE_OMIT_SUBQUERY |
| 108201 | 107127 | }else{ |
| 108202 | 107128 | int eType; |
| 108203 | 107129 | int iTab; |
| 108204 | 107130 | struct InLoop *pIn; |
| 108205 | | - u8 bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0; |
| 107131 | + WhereLoop *pLoop = pLevel->pWLoop; |
| 108206 | 107132 | |
| 108207 | | - if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 |
| 108208 | | - && pLevel->plan.u.pIdx->aSortOrder[iEq] |
| 107133 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 107134 | + && pLoop->u.btree.pIndex!=0 |
| 107135 | + && pLoop->u.btree.pIndex->aSortOrder[iEq] |
| 108209 | 107136 | ){ |
| 108210 | 107137 | testcase( iEq==0 ); |
| 108211 | | - testcase( iEq==pLevel->plan.u.pIdx->nColumn-1 ); |
| 108212 | | - testcase( iEq>0 && iEq+1<pLevel->plan.u.pIdx->nColumn ); |
| 108213 | 107138 | testcase( bRev ); |
| 108214 | 107139 | bRev = !bRev; |
| 108215 | 107140 | } |
| 108216 | 107141 | assert( pX->op==TK_IN ); |
| 108217 | 107142 | iReg = iTarget; |
| | @@ -108220,11 +107145,12 @@ |
| 108220 | 107145 | testcase( bRev ); |
| 108221 | 107146 | bRev = !bRev; |
| 108222 | 107147 | } |
| 108223 | 107148 | iTab = pX->iTable; |
| 108224 | 107149 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 108225 | | - assert( pLevel->plan.wsFlags & WHERE_IN_ABLE ); |
| 107150 | + assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 107151 | + pLoop->wsFlags |= WHERE_IN_ABLE; |
| 108226 | 107152 | if( pLevel->u.in.nIn==0 ){ |
| 108227 | 107153 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 108228 | 107154 | } |
| 108229 | 107155 | pLevel->u.in.nIn++; |
| 108230 | 107156 | pLevel->u.in.aInLoop = |
| | @@ -108290,33 +107216,35 @@ |
| 108290 | 107216 | ** string in this example would be set to SQLITE_AFF_NONE. |
| 108291 | 107217 | */ |
| 108292 | 107218 | static int codeAllEqualityTerms( |
| 108293 | 107219 | Parse *pParse, /* Parsing context */ |
| 108294 | 107220 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 108295 | | - WhereClause *pWC, /* The WHERE clause */ |
| 108296 | | - Bitmask notReady, /* Which parts of FROM have not yet been coded */ |
| 107221 | + int bRev, /* Reverse the order of IN operators */ |
| 108297 | 107222 | int nExtraReg, /* Number of extra registers to allocate */ |
| 108298 | 107223 | char **pzAff /* OUT: Set to point to affinity string */ |
| 108299 | 107224 | ){ |
| 108300 | | - int nEq = pLevel->plan.nEq; /* The number of == or IN constraints to code */ |
| 107225 | + int nEq; /* The number of == or IN constraints to code */ |
| 108301 | 107226 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 108302 | 107227 | Index *pIdx; /* The index being used for this loop */ |
| 108303 | | - int iCur = pLevel->iTabCur; /* The cursor of the table */ |
| 108304 | 107228 | WhereTerm *pTerm; /* A single constraint term */ |
| 107229 | + WhereLoop *pLoop; /* The WhereLoop object */ |
| 108305 | 107230 | int j; /* Loop counter */ |
| 108306 | 107231 | int regBase; /* Base register */ |
| 108307 | 107232 | int nReg; /* Number of registers to allocate */ |
| 108308 | 107233 | char *zAff; /* Affinity string to return */ |
| 108309 | 107234 | |
| 108310 | 107235 | /* 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; |
| 107236 | + pLoop = pLevel->pWLoop; |
| 107237 | + assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 107238 | + nEq = pLoop->u.btree.nEq; |
| 107239 | + pIdx = pLoop->u.btree.pIndex; |
| 107240 | + assert( pIdx!=0 ); |
| 108313 | 107241 | |
| 108314 | 107242 | /* Figure out how many memory cells we will need then allocate them. |
| 108315 | 107243 | */ |
| 108316 | 107244 | regBase = pParse->nMem + 1; |
| 108317 | | - nReg = pLevel->plan.nEq + nExtraReg; |
| 107245 | + nReg = pLoop->u.btree.nEq + nExtraReg; |
| 108318 | 107246 | pParse->nMem += nReg; |
| 108319 | 107247 | |
| 108320 | 107248 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 108321 | 107249 | if( !zAff ){ |
| 108322 | 107250 | pParse->db->mallocFailed = 1; |
| | @@ -108325,18 +107253,17 @@ |
| 108325 | 107253 | /* Evaluate the equality constraints |
| 108326 | 107254 | */ |
| 108327 | 107255 | assert( pIdx->nColumn>=nEq ); |
| 108328 | 107256 | for(j=0; j<nEq; j++){ |
| 108329 | 107257 | int r1; |
| 108330 | | - int k = pIdx->aiColumn[j]; |
| 108331 | | - pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx); |
| 108332 | | - if( pTerm==0 ) break; |
| 107258 | + pTerm = pLoop->aLTerm[j]; |
| 107259 | + assert( pTerm!=0 ); |
| 108333 | 107260 | /* The following true for indices with redundant columns. |
| 108334 | 107261 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 108335 | 107262 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
| 108336 | 107263 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 108337 | | - r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, regBase+j); |
| 107264 | + r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
| 108338 | 107265 | if( r1!=regBase+j ){ |
| 108339 | 107266 | if( nReg==1 ){ |
| 108340 | 107267 | sqlite3ReleaseTempReg(pParse, regBase); |
| 108341 | 107268 | regBase = r1; |
| 108342 | 107269 | }else{ |
| | @@ -108400,20 +107327,19 @@ |
| 108400 | 107327 | ** |
| 108401 | 107328 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 108402 | 107329 | ** It is the responsibility of the caller to free the buffer when it is |
| 108403 | 107330 | ** no longer required. |
| 108404 | 107331 | */ |
| 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; |
| 107332 | +static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 107333 | + Index *pIndex = pLoop->u.btree.pIndex; |
| 107334 | + int nEq = pLoop->u.btree.nEq; |
| 108409 | 107335 | int i, j; |
| 108410 | 107336 | Column *aCol = pTab->aCol; |
| 108411 | 107337 | int *aiColumn = pIndex->aiColumn; |
| 108412 | 107338 | StrAccum txt; |
| 108413 | 107339 | |
| 108414 | | - if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
| 107340 | + if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
| 108415 | 107341 | return 0; |
| 108416 | 107342 | } |
| 108417 | 107343 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
| 108418 | 107344 | txt.db = db; |
| 108419 | 107345 | sqlite3StrAccumAppend(&txt, " (", 2); |
| | @@ -108420,15 +107346,15 @@ |
| 108420 | 107346 | for(i=0; i<nEq; i++){ |
| 108421 | 107347 | explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "="); |
| 108422 | 107348 | } |
| 108423 | 107349 | |
| 108424 | 107350 | j = i; |
| 108425 | | - if( pPlan->wsFlags&WHERE_BTM_LIMIT ){ |
| 107351 | + if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
| 108426 | 107352 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 108427 | 107353 | explainAppendTerm(&txt, i++, z, ">"); |
| 108428 | 107354 | } |
| 108429 | | - if( pPlan->wsFlags&WHERE_TOP_LIMIT ){ |
| 107355 | + if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
| 108430 | 107356 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 108431 | 107357 | explainAppendTerm(&txt, i, z, "<"); |
| 108432 | 107358 | } |
| 108433 | 107359 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 108434 | 107360 | return sqlite3StrAccumFinish(&txt); |
| | @@ -108447,24 +107373,26 @@ |
| 108447 | 107373 | int iLevel, /* Value for "level" column of output */ |
| 108448 | 107374 | int iFrom, /* Value for "from" column of output */ |
| 108449 | 107375 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 108450 | 107376 | ){ |
| 108451 | 107377 | if( pParse->explain==2 ){ |
| 108452 | | - u32 flags = pLevel->plan.wsFlags; |
| 108453 | 107378 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 108454 | 107379 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 108455 | 107380 | sqlite3 *db = pParse->db; /* Database handle */ |
| 108456 | 107381 | char *zMsg; /* Text to add to EQP output */ |
| 108457 | | - sqlite3_int64 nRow; /* Expected number of rows visited by scan */ |
| 108458 | 107382 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 108459 | 107383 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 107384 | + WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 107385 | + u32 flags; /* Flags that describe this loop */ |
| 108460 | 107386 | |
| 107387 | + pLoop = pLevel->pWLoop; |
| 107388 | + flags = pLoop->wsFlags; |
| 108461 | 107389 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
| 108462 | 107390 | |
| 108463 | | - isSearch = (pLevel->plan.nEq>0) |
| 108464 | | - || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 108465 | | - || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 107391 | + isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 107392 | + || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 107393 | + || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 108466 | 107394 | |
| 108467 | 107395 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
| 108468 | 107396 | if( pItem->pSelect ){ |
| 108469 | 107397 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
| 108470 | 107398 | }else{ |
| | @@ -108472,47 +107400,42 @@ |
| 108472 | 107400 | } |
| 108473 | 107401 | |
| 108474 | 107402 | if( pItem->zAlias ){ |
| 108475 | 107403 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 108476 | 107404 | } |
| 108477 | | - if( (flags & WHERE_INDEXED)!=0 ){ |
| 108478 | | - char *zWhere = explainIndexRange(db, pLevel, pItem->pTab); |
| 107405 | + if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
| 107406 | + && ALWAYS(pLoop->u.btree.pIndex!=0) |
| 107407 | + ){ |
| 107408 | + char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
| 108479 | 107409 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, |
| 108480 | 107410 | ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""), |
| 108481 | 107411 | ((flags & WHERE_IDX_ONLY)?"COVERING ":""), |
| 108482 | 107412 | ((flags & WHERE_TEMP_INDEX)?"":" "), |
| 108483 | | - ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName), |
| 107413 | + ((flags & WHERE_TEMP_INDEX)?"": pLoop->u.btree.pIndex->zName), |
| 108484 | 107414 | zWhere |
| 108485 | 107415 | ); |
| 108486 | 107416 | sqlite3DbFree(db, zWhere); |
| 108487 | | - }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){ |
| 107417 | + }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
| 108488 | 107418 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
| 108489 | 107419 | |
| 108490 | | - if( flags&WHERE_ROWID_EQ ){ |
| 107420 | + if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
| 108491 | 107421 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
| 108492 | 107422 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
| 108493 | 107423 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 108494 | 107424 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 108495 | 107425 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
| 108496 | | - }else if( flags&WHERE_TOP_LIMIT ){ |
| 107426 | + }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){ |
| 108497 | 107427 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 108498 | 107428 | } |
| 108499 | 107429 | } |
| 108500 | 107430 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 108501 | 107431 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
| 108502 | | - sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx; |
| 108503 | 107432 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
| 108504 | | - pVtabIdx->idxNum, pVtabIdx->idxStr); |
| 107433 | + pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
| 108505 | 107434 | } |
| 108506 | 107435 | #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); |
| 107436 | + zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
| 108514 | 107437 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
| 108515 | 107438 | } |
| 108516 | 107439 | } |
| 108517 | 107440 | #else |
| 108518 | 107441 | # define explainOneScan(u,v,w,x,y,z) |
| | @@ -108524,19 +107447,19 @@ |
| 108524 | 107447 | ** implementation described by pWInfo. |
| 108525 | 107448 | */ |
| 108526 | 107449 | static Bitmask codeOneLoopStart( |
| 108527 | 107450 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 108528 | 107451 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 108529 | | - u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 108530 | 107452 | Bitmask notReady /* Which tables are currently available */ |
| 108531 | 107453 | ){ |
| 108532 | 107454 | int j, k; /* Loop counters */ |
| 108533 | 107455 | int iCur; /* The VDBE cursor for the table */ |
| 108534 | 107456 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 108535 | 107457 | int omitTable; /* True if we use the index only */ |
| 108536 | 107458 | int bRev; /* True if we need to scan in reverse order */ |
| 108537 | 107459 | WhereLevel *pLevel; /* The where level to be coded */ |
| 107460 | + WhereLoop *pLoop; /* The WhereLoop object being coded */ |
| 108538 | 107461 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 108539 | 107462 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 108540 | 107463 | Parse *pParse; /* Parsing context */ |
| 108541 | 107464 | Vdbe *v; /* The prepared stmt under constructions */ |
| 108542 | 107465 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
| | @@ -108546,17 +107469,18 @@ |
| 108546 | 107469 | int iReleaseReg = 0; /* Temp register to free before returning */ |
| 108547 | 107470 | Bitmask newNotReady; /* Return value */ |
| 108548 | 107471 | |
| 108549 | 107472 | pParse = pWInfo->pParse; |
| 108550 | 107473 | v = pParse->pVdbe; |
| 108551 | | - pWC = pWInfo->pWC; |
| 107474 | + pWC = &pWInfo->sWC; |
| 108552 | 107475 | pLevel = &pWInfo->a[iLevel]; |
| 107476 | + pLoop = pLevel->pWLoop; |
| 108553 | 107477 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 108554 | 107478 | 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; |
| 107479 | + bRev = (pWInfo->revMask>>iLevel)&1; |
| 107480 | + omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
| 107481 | + && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
| 108558 | 107482 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
| 108559 | 107483 | |
| 108560 | 107484 | /* Create labels for the "break" and "continue" instructions |
| 108561 | 107485 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 108562 | 107486 | ** Jump to cont to go immediately to the next iteration of the |
| | @@ -108589,51 +107513,41 @@ |
| 108589 | 107513 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 108590 | 107514 | pLevel->op = OP_Goto; |
| 108591 | 107515 | }else |
| 108592 | 107516 | |
| 108593 | 107517 | #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 |
| 107518 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 107519 | + /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
| 108596 | 107520 | ** to access the data. |
| 108597 | 107521 | */ |
| 108598 | 107522 | int iReg; /* P3 Value for OP_VFilter */ |
| 108599 | 107523 | 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; |
| 107524 | + int nConstraint = pLoop->nLTerm; |
| 108606 | 107525 | |
| 108607 | 107526 | sqlite3ExprCachePush(pParse); |
| 108608 | 107527 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 108609 | 107528 | 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 | 107529 | for(j=0; j<nConstraint; j++){ |
| 108632 | | - if( aUsage[j].omit ){ |
| 108633 | | - int iTerm = aConstraint[j].iTermOffset; |
| 108634 | | - disableTerm(pLevel, &pWC->a[iTerm]); |
| 107530 | + int iTarget = iReg+j+2; |
| 107531 | + pTerm = pLoop->aLTerm[j]; |
| 107532 | + if( pTerm==0 ) continue; |
| 107533 | + if( pTerm->eOperator & WO_IN ){ |
| 107534 | + codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 107535 | + addrNotFound = pLevel->addrNxt; |
| 107536 | + }else{ |
| 107537 | + sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 107538 | + } |
| 107539 | + } |
| 107540 | + sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| 107541 | + sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
| 107542 | + sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 107543 | + pLoop->u.vtab.idxStr, |
| 107544 | + pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 107545 | + pLoop->u.vtab.needFree = 0; |
| 107546 | + for(j=0; j<nConstraint && j<16; j++){ |
| 107547 | + if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
| 107548 | + disableTerm(pLevel, pLoop->aLTerm[j]); |
| 108635 | 107549 | } |
| 108636 | 107550 | } |
| 108637 | 107551 | pLevel->op = OP_VNext; |
| 108638 | 107552 | pLevel->p1 = iCur; |
| 108639 | 107553 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| | @@ -108640,41 +107554,49 @@ |
| 108640 | 107554 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
| 108641 | 107555 | sqlite3ExprCachePop(pParse, 1); |
| 108642 | 107556 | }else |
| 108643 | 107557 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 108644 | 107558 | |
| 108645 | | - if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){ |
| 108646 | | - /* Case 1: We can directly reference a single row using an |
| 107559 | + if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 107560 | + && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 107561 | + ){ |
| 107562 | + /* Case 2: We can directly reference a single row using an |
| 108647 | 107563 | ** equality comparison against the ROWID field. Or |
| 108648 | 107564 | ** we reference multiple rows using a "rowid IN (...)" |
| 108649 | 107565 | ** construct. |
| 108650 | 107566 | */ |
| 107567 | + assert( pLoop->u.btree.nEq==1 ); |
| 108651 | 107568 | iReleaseReg = sqlite3GetTempReg(pParse); |
| 108652 | | - pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0); |
| 107569 | + pTerm = pLoop->aLTerm[0]; |
| 108653 | 107570 | assert( pTerm!=0 ); |
| 108654 | 107571 | assert( pTerm->pExpr!=0 ); |
| 108655 | 107572 | assert( omitTable==0 ); |
| 108656 | 107573 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 108657 | | - iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, iReleaseReg); |
| 107574 | + iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
| 108658 | 107575 | addrNxt = pLevel->addrNxt; |
| 108659 | 107576 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 108660 | 107577 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
| 108661 | 107578 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
| 108662 | 107579 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 108663 | 107580 | VdbeComment((v, "pk")); |
| 108664 | 107581 | 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. |
| 107582 | + }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 107583 | + && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 107584 | + ){ |
| 107585 | + /* Case 3: We have an inequality comparison against the ROWID field. |
| 108667 | 107586 | */ |
| 108668 | 107587 | int testOp = OP_Noop; |
| 108669 | 107588 | int start; |
| 108670 | 107589 | int memEndValue = 0; |
| 108671 | 107590 | WhereTerm *pStart, *pEnd; |
| 108672 | 107591 | |
| 108673 | 107592 | 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); |
| 107593 | + j = 0; |
| 107594 | + pStart = pEnd = 0; |
| 107595 | + if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 107596 | + if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
| 107597 | + assert( pStart!=0 || pEnd!=0 ); |
| 108676 | 107598 | if( bRev ){ |
| 108677 | 107599 | pTerm = pStart; |
| 108678 | 107600 | pStart = pEnd; |
| 108679 | 107601 | pEnd = pTerm; |
| 108680 | 107602 | } |
| | @@ -108725,24 +107647,20 @@ |
| 108725 | 107647 | } |
| 108726 | 107648 | start = sqlite3VdbeCurrentAddr(v); |
| 108727 | 107649 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 108728 | 107650 | pLevel->p1 = iCur; |
| 108729 | 107651 | pLevel->p2 = start; |
| 108730 | | - if( pStart==0 && pEnd==0 ){ |
| 108731 | | - pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 108732 | | - }else{ |
| 108733 | | - assert( pLevel->p5==0 ); |
| 108734 | | - } |
| 107652 | + assert( pLevel->p5==0 ); |
| 108735 | 107653 | if( testOp!=OP_Noop ){ |
| 108736 | 107654 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 108737 | 107655 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
| 108738 | 107656 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 108739 | 107657 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 108740 | 107658 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
| 108741 | 107659 | } |
| 108742 | | - }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){ |
| 108743 | | - /* Case 3: A scan using an index. |
| 107660 | + }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 107661 | + /* Case 4: A scan using an index. |
| 108744 | 107662 | ** |
| 108745 | 107663 | ** The WHERE clause may contain zero or more equality |
| 108746 | 107664 | ** terms ("==" or "IN" operators) that refer to the N |
| 108747 | 107665 | ** left-most columns of the index. It may also contain |
| 108748 | 107666 | ** inequality constraints (>, <, >= or <=) on the indexed |
| | @@ -108784,12 +107702,12 @@ |
| 108784 | 107702 | static const u8 aEndOp[] = { |
| 108785 | 107703 | OP_Noop, /* 0: (!end_constraints) */ |
| 108786 | 107704 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 108787 | 107705 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 108788 | 107706 | }; |
| 108789 | | - int nEq = pLevel->plan.nEq; /* Number of == or IN terms */ |
| 108790 | | - int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
| 107707 | + int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 107708 | + int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
| 108791 | 107709 | int regBase; /* Base register holding constraint values */ |
| 108792 | 107710 | int r1; /* Temp register */ |
| 108793 | 107711 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 108794 | 107712 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 108795 | 107713 | int startEq; /* True if range start uses ==, >= or <= */ |
| | @@ -108801,24 +107719,23 @@ |
| 108801 | 107719 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 108802 | 107720 | int op; /* Instruction opcode */ |
| 108803 | 107721 | char *zStartAff; /* Affinity for start of range constraint */ |
| 108804 | 107722 | char *zEndAff; /* Affinity for end of range constraint */ |
| 108805 | 107723 | |
| 108806 | | - pIdx = pLevel->plan.u.pIdx; |
| 107724 | + pIdx = pLoop->u.btree.pIndex; |
| 108807 | 107725 | iIdxCur = pLevel->iIdxCur; |
| 108808 | | - k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]); |
| 108809 | 107726 | |
| 108810 | 107727 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 108811 | 107728 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 108812 | 107729 | ** query, then the caller will only allow the loop to run for |
| 108813 | 107730 | ** a single iteration. This means that the first row returned |
| 108814 | 107731 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 108815 | 107732 | ** the first one after the nEq equality constraints in the index, |
| 108816 | 107733 | ** this requires some special handling. |
| 108817 | 107734 | */ |
| 108818 | | - if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 108819 | | - && (pLevel->plan.wsFlags&WHERE_ORDERED) |
| 107735 | + if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 107736 | + && (pWInfo->bOBSat!=0) |
| 108820 | 107737 | && (pIdx->nColumn>nEq) |
| 108821 | 107738 | ){ |
| 108822 | 107739 | /* assert( pOrderBy->nExpr==1 ); */ |
| 108823 | 107740 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 108824 | 107741 | isMinQuery = 1; |
| | @@ -108826,26 +107743,25 @@ |
| 108826 | 107743 | } |
| 108827 | 107744 | |
| 108828 | 107745 | /* Find any inequality constraint terms for the start and end |
| 108829 | 107746 | ** of the range. |
| 108830 | 107747 | */ |
| 108831 | | - if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){ |
| 108832 | | - pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx); |
| 107748 | + j = nEq; |
| 107749 | + if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
| 107750 | + pRangeStart = pLoop->aLTerm[j++]; |
| 108833 | 107751 | nExtraReg = 1; |
| 108834 | 107752 | } |
| 108835 | | - if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){ |
| 108836 | | - pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx); |
| 107753 | + if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
| 107754 | + pRangeEnd = pLoop->aLTerm[j++]; |
| 108837 | 107755 | nExtraReg = 1; |
| 108838 | 107756 | } |
| 108839 | 107757 | |
| 108840 | 107758 | /* Generate code to evaluate all constraint terms using == or IN |
| 108841 | 107759 | ** and store the values of those terms in an array of registers |
| 108842 | 107760 | ** starting at regBase. |
| 108843 | 107761 | */ |
| 108844 | | - regBase = codeAllEqualityTerms( |
| 108845 | | - pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff |
| 108846 | | - ); |
| 107762 | + regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
| 108847 | 107763 | zEndAff = sqlite3DbStrDup(pParse->db, zStartAff); |
| 108848 | 107764 | addrNxt = pLevel->addrNxt; |
| 108849 | 107765 | |
| 108850 | 107766 | /* If we are doing a reverse order scan on an ascending index, or |
| 108851 | 107767 | ** a forward order scan on a descending index, interchange the |
| | @@ -108855,14 +107771,14 @@ |
| 108855 | 107771 | || (bRev && pIdx->nColumn==nEq) |
| 108856 | 107772 | ){ |
| 108857 | 107773 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 108858 | 107774 | } |
| 108859 | 107775 | |
| 108860 | | - testcase( pRangeStart && pRangeStart->eOperator & WO_LE ); |
| 108861 | | - testcase( pRangeStart && pRangeStart->eOperator & WO_GE ); |
| 108862 | | - testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE ); |
| 108863 | | - testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE ); |
| 107776 | + testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 107777 | + testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 107778 | + testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 107779 | + testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
| 108864 | 107780 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 108865 | 107781 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 108866 | 107782 | start_constraints = pRangeStart || nEq>0; |
| 108867 | 107783 | |
| 108868 | 107784 | /* Seek the index cursor to the start of the range. */ |
| | @@ -108948,13 +107864,13 @@ |
| 108948 | 107864 | /* If there are inequality constraints, check that the value |
| 108949 | 107865 | ** of the table column that the inequality contrains is not NULL. |
| 108950 | 107866 | ** If it is, jump to the next iteration of the loop. |
| 108951 | 107867 | */ |
| 108952 | 107868 | 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 ){ |
| 107869 | + testcase( pLoop->wsFlags & WHERE_BTM_LIMIT ); |
| 107870 | + testcase( pLoop->wsFlags & WHERE_TOP_LIMIT ); |
| 107871 | + if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
| 108956 | 107872 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 108957 | 107873 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 108958 | 107874 | } |
| 108959 | 107875 | sqlite3ReleaseTempReg(pParse, r1); |
| 108960 | 107876 | |
| | @@ -108969,28 +107885,28 @@ |
| 108969 | 107885 | } |
| 108970 | 107886 | |
| 108971 | 107887 | /* Record the instruction used to terminate the loop. Disable |
| 108972 | 107888 | ** WHERE clause terms made redundant by the index range scan. |
| 108973 | 107889 | */ |
| 108974 | | - if( pLevel->plan.wsFlags & WHERE_UNIQUE ){ |
| 107890 | + if( pLoop->wsFlags & WHERE_ONEROW ){ |
| 108975 | 107891 | pLevel->op = OP_Noop; |
| 108976 | 107892 | }else if( bRev ){ |
| 108977 | 107893 | pLevel->op = OP_Prev; |
| 108978 | 107894 | }else{ |
| 108979 | 107895 | pLevel->op = OP_Next; |
| 108980 | 107896 | } |
| 108981 | 107897 | pLevel->p1 = iIdxCur; |
| 108982 | | - if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){ |
| 107898 | + if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
| 108983 | 107899 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 108984 | 107900 | }else{ |
| 108985 | 107901 | assert( pLevel->p5==0 ); |
| 108986 | 107902 | } |
| 108987 | 107903 | }else |
| 108988 | 107904 | |
| 108989 | 107905 | #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 |
| 107906 | + if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 107907 | + /* Case 5: Two or more separately indexed terms connected by OR |
| 108992 | 107908 | ** |
| 108993 | 107909 | ** Example: |
| 108994 | 107910 | ** |
| 108995 | 107911 | ** CREATE TABLE t1(a,b,c,d); |
| 108996 | 107912 | ** CREATE INDEX i1 ON t1(a); |
| | @@ -109039,11 +107955,11 @@ |
| 109039 | 107955 | int iRetInit; /* Address of regReturn init */ |
| 109040 | 107956 | int untestedTerms = 0; /* Some terms not completely tested */ |
| 109041 | 107957 | int ii; /* Loop counter */ |
| 109042 | 107958 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
| 109043 | 107959 | |
| 109044 | | - pTerm = pLevel->plan.u.pTerm; |
| 107960 | + pTerm = pLoop->aLTerm[0]; |
| 109045 | 107961 | assert( pTerm!=0 ); |
| 109046 | 107962 | assert( pTerm->eOperator & WO_OR ); |
| 109047 | 107963 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 109048 | 107964 | pOrWc = &pTerm->u.pOrInfo->wc; |
| 109049 | 107965 | pLevel->op = OP_Return; |
| | @@ -109058,11 +107974,11 @@ |
| 109058 | 107974 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 109059 | 107975 | nNotReady = pWInfo->nLevel - iLevel - 1; |
| 109060 | 107976 | pOrTab = sqlite3StackAllocRaw(pParse->db, |
| 109061 | 107977 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 109062 | 107978 | if( pOrTab==0 ) return notReady; |
| 109063 | | - pOrTab->nAlloc = (i16)(nNotReady + 1); |
| 107979 | + pOrTab->nAlloc = (u8)(nNotReady + 1); |
| 109064 | 107980 | pOrTab->nSrc = pOrTab->nAlloc; |
| 109065 | 107981 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 109066 | 107982 | origSrc = pWInfo->pTabList->a; |
| 109067 | 107983 | for(k=1; k<=nNotReady; k++){ |
| 109068 | 107984 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| | @@ -109080,11 +107996,11 @@ |
| 109080 | 107996 | ** over the top of the loop into the body of it. In this case the |
| 109081 | 107997 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 109082 | 107998 | ** fall through to the next instruction, just as an OP_Next does if |
| 109083 | 107999 | ** called on an uninitialized cursor. |
| 109084 | 108000 | */ |
| 109085 | | - if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 108001 | + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 109086 | 108002 | regRowset = ++pParse->nMem; |
| 109087 | 108003 | regRowid = ++pParse->nMem; |
| 109088 | 108004 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 109089 | 108005 | } |
| 109090 | 108006 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| | @@ -109131,15 +108047,15 @@ |
| 109131 | 108047 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 109132 | 108048 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
| 109133 | 108049 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
| 109134 | 108050 | assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed ); |
| 109135 | 108051 | if( pSubWInfo ){ |
| 109136 | | - WhereLevel *pLvl; |
| 108052 | + WhereLoop *pSubLoop; |
| 109137 | 108053 | explainOneScan( |
| 109138 | 108054 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 109139 | 108055 | ); |
| 109140 | | - if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 108056 | + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 109141 | 108057 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 109142 | 108058 | int r; |
| 109143 | 108059 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
| 109144 | 108060 | regRowid, 0); |
| 109145 | 108061 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| | @@ -109164,17 +108080,17 @@ |
| 109164 | 108080 | ** processed or the index is the same as that used by all previous |
| 109165 | 108081 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 109166 | 108082 | ** pCov to NULL to indicate that no candidate covering index will |
| 109167 | 108083 | ** be available. |
| 109168 | 108084 | */ |
| 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) |
| 108085 | + pSubLoop = pSubWInfo->a[0].pWLoop; |
| 108086 | + assert( (pSubLoop->wsFlags & WHERE_TEMP_INDEX)==0 ); |
| 108087 | + if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
| 108088 | + && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
| 109173 | 108089 | ){ |
| 109174 | | - assert( pLvl->iIdxCur==iCovCur ); |
| 109175 | | - pCov = pLvl->plan.u.pIdx; |
| 108090 | + assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
| 108091 | + pCov = pSubLoop->u.btree.pIndex; |
| 109176 | 108092 | }else{ |
| 109177 | 108093 | pCov = 0; |
| 109178 | 108094 | } |
| 109179 | 108095 | |
| 109180 | 108096 | /* Finish the loop through table entries that match term pOrTerm. */ |
| | @@ -109196,23 +108112,22 @@ |
| 109196 | 108112 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 109197 | 108113 | }else |
| 109198 | 108114 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 109199 | 108115 | |
| 109200 | 108116 | { |
| 109201 | | - /* Case 5: There is no usable index. We must do a complete |
| 108117 | + /* Case 6: There is no usable index. We must do a complete |
| 109202 | 108118 | ** scan of the entire table. |
| 109203 | 108119 | */ |
| 109204 | 108120 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 109205 | 108121 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 109206 | 108122 | assert( bRev==0 || bRev==1 ); |
| 109207 | | - assert( omitTable==0 ); |
| 109208 | 108123 | pLevel->op = aStep[bRev]; |
| 109209 | 108124 | pLevel->p1 = iCur; |
| 109210 | 108125 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
| 109211 | 108126 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 109212 | 108127 | } |
| 109213 | | - newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur); |
| 108128 | + newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
| 109214 | 108129 | |
| 109215 | 108130 | /* Insert code to test every subexpression that can be completely |
| 109216 | 108131 | ** computed using the current set of tables. |
| 109217 | 108132 | ** |
| 109218 | 108133 | ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through |
| | @@ -109258,10 +108173,12 @@ |
| 109258 | 108173 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 109259 | 108174 | assert( (pTerm->prereqRight & newNotReady)!=0 ); |
| 109260 | 108175 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 109261 | 108176 | if( pAlt==0 ) continue; |
| 109262 | 108177 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
| 108178 | + testcase( pAlt->eOperator & WO_EQ ); |
| 108179 | + testcase( pAlt->eOperator & WO_IN ); |
| 109263 | 108180 | VdbeNoopComment((v, "begin transitive constraint")); |
| 109264 | 108181 | sEq = *pAlt->pExpr; |
| 109265 | 108182 | sEq.pLeft = pE->pLeft; |
| 109266 | 108183 | sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL); |
| 109267 | 108184 | } |
| | @@ -109290,51 +108207,1562 @@ |
| 109290 | 108207 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
| 109291 | 108208 | |
| 109292 | 108209 | return newNotReady; |
| 109293 | 108210 | } |
| 109294 | 108211 | |
| 109295 | | -#if defined(SQLITE_TEST) |
| 108212 | +#ifdef WHERETRACE_ENABLED |
| 109296 | 108213 | /* |
| 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. |
| 108214 | +** Print a WhereLoop object for debugging purposes |
| 109301 | 108215 | */ |
| 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[] */ |
| 108216 | +static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){ |
| 108217 | + int nb = 1+(pTabList->nSrc+7)/8; |
| 108218 | + struct SrcList_item *pItem = pTabList->a + p->iTab; |
| 108219 | + Table *pTab = pItem->pTab; |
| 108220 | + sqlite3DebugPrintf("%c %2d.%0*llx.%0*llx", p->cId, |
| 108221 | + p->iTab, nb, p->maskSelf, nb, p->prereq); |
| 108222 | + sqlite3DebugPrintf(" %8s", |
| 108223 | + pItem->zAlias ? pItem->zAlias : pTab->zName); |
| 108224 | + if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 108225 | + if( p->u.btree.pIndex ){ |
| 108226 | + const char *zName = p->u.btree.pIndex->zName; |
| 108227 | + if( zName==0 ) zName = "ipk"; |
| 108228 | + if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 108229 | + int i = sqlite3Strlen30(zName) - 1; |
| 108230 | + while( zName[i]!='_' ) i--; |
| 108231 | + zName += i; |
| 108232 | + } |
| 108233 | + sqlite3DebugPrintf(".%-12s %2d", zName, p->u.btree.nEq); |
| 108234 | + }else{ |
| 108235 | + sqlite3DebugPrintf("%16s",""); |
| 108236 | + } |
| 108237 | + }else{ |
| 108238 | + char *z; |
| 108239 | + if( p->u.vtab.idxStr ){ |
| 108240 | + z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 108241 | + p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
| 108242 | + }else{ |
| 108243 | + z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
| 108244 | + } |
| 108245 | + sqlite3DebugPrintf(" %-15s", z); |
| 108246 | + sqlite3_free(z); |
| 108247 | + } |
| 108248 | + sqlite3DebugPrintf(" fg %05x N %d", p->wsFlags, p->nLTerm); |
| 108249 | + sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 108250 | +} |
| 108251 | +#endif |
| 109304 | 108252 | |
| 109305 | | -#endif /* SQLITE_TEST */ |
| 108253 | +/* |
| 108254 | +** Convert bulk memory into a valid WhereLoop that can be passed |
| 108255 | +** to whereLoopClear harmlessly. |
| 108256 | +*/ |
| 108257 | +static void whereLoopInit(WhereLoop *p){ |
| 108258 | + p->aLTerm = p->aLTermSpace; |
| 108259 | + p->nLTerm = 0; |
| 108260 | + p->nLSlot = ArraySize(p->aLTermSpace); |
| 108261 | + p->wsFlags = 0; |
| 108262 | +} |
| 109306 | 108263 | |
| 108264 | +/* |
| 108265 | +** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 108266 | +*/ |
| 108267 | +static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
| 108268 | + if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_TEMP_INDEX) ){ |
| 108269 | + if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 108270 | + sqlite3_free(p->u.vtab.idxStr); |
| 108271 | + p->u.vtab.needFree = 0; |
| 108272 | + p->u.vtab.idxStr = 0; |
| 108273 | + }else if( (p->wsFlags & WHERE_TEMP_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
| 108274 | + sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 108275 | + sqlite3DbFree(db, p->u.btree.pIndex); |
| 108276 | + p->u.btree.pIndex = 0; |
| 108277 | + } |
| 108278 | + } |
| 108279 | +} |
| 108280 | + |
| 108281 | +/* |
| 108282 | +** Deallocate internal memory used by a WhereLoop object |
| 108283 | +*/ |
| 108284 | +static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 108285 | + if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 108286 | + whereLoopClearUnion(db, p); |
| 108287 | + whereLoopInit(p); |
| 108288 | +} |
| 108289 | + |
| 108290 | +/* |
| 108291 | +** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 108292 | +*/ |
| 108293 | +static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 108294 | + WhereTerm **paNew; |
| 108295 | + if( p->nLSlot>=n ) return SQLITE_OK; |
| 108296 | + n = (n+7)&~7; |
| 108297 | + paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 108298 | + if( paNew==0 ) return SQLITE_NOMEM; |
| 108299 | + memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 108300 | + if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 108301 | + p->aLTerm = paNew; |
| 108302 | + p->nLSlot = n; |
| 108303 | + return SQLITE_OK; |
| 108304 | +} |
| 108305 | + |
| 108306 | +/* |
| 108307 | +** Transfer content from the second pLoop into the first. |
| 108308 | +*/ |
| 108309 | +static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
| 108310 | + if( whereLoopResize(db, pTo, pFrom->nLTerm) ) return SQLITE_NOMEM; |
| 108311 | + whereLoopClearUnion(db, pTo); |
| 108312 | + memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 108313 | + memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
| 108314 | + if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 108315 | + pFrom->u.vtab.needFree = 0; |
| 108316 | + }else if( (pFrom->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 108317 | + pFrom->u.btree.pIndex = 0; |
| 108318 | + } |
| 108319 | + return SQLITE_OK; |
| 108320 | +} |
| 108321 | + |
| 108322 | +/* |
| 108323 | +** Delete a WhereLoop object |
| 108324 | +*/ |
| 108325 | +static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
| 108326 | + whereLoopClear(db, p); |
| 108327 | + sqlite3DbFree(db, p); |
| 108328 | +} |
| 109307 | 108329 | |
| 109308 | 108330 | /* |
| 109309 | 108331 | ** Free a WhereInfo structure |
| 109310 | 108332 | */ |
| 109311 | 108333 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
| 109312 | 108334 | 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 | | - } |
| 109330 | | - } |
| 109331 | | - whereClauseClear(pWInfo->pWC); |
| 108335 | + whereClauseClear(&pWInfo->sWC); |
| 108336 | + while( pWInfo->pLoops ){ |
| 108337 | + WhereLoop *p = pWInfo->pLoops; |
| 108338 | + pWInfo->pLoops = p->pNextLoop; |
| 108339 | + whereLoopDelete(db, p); |
| 108340 | + } |
| 109332 | 108341 | sqlite3DbFree(db, pWInfo); |
| 109333 | 108342 | } |
| 109334 | 108343 | } |
| 109335 | 108344 | |
| 108345 | +/* |
| 108346 | +** Insert or replace a WhereLoop entry using the template supplied. |
| 108347 | +** |
| 108348 | +** An existing WhereLoop entry might be overwritten if the new template |
| 108349 | +** is better and has fewer dependencies. Or the template will be ignored |
| 108350 | +** and no insert will occur if an existing WhereLoop is faster and has |
| 108351 | +** fewer dependencies than the template. Otherwise a new WhereLoop is |
| 108352 | +** added based on the template. |
| 108353 | +** |
| 108354 | +** If pBuilder->pBest is not NULL then we only care about the very |
| 108355 | +** best template and that template should be stored in pBuilder->pBest. |
| 108356 | +** If pBuilder->pBest is NULL then a list of the best templates are stored |
| 108357 | +** in pBuilder->pWInfo->pLoops. |
| 108358 | +** |
| 108359 | +** When accumulating multiple loops (when pBuilder->pBest is NULL) we |
| 108360 | +** still might overwrite similar loops with the new template if the |
| 108361 | +** template is better. Loops may be overwritten if the following |
| 108362 | +** conditions are met: |
| 108363 | +** |
| 108364 | +** (1) They have the same iTab. |
| 108365 | +** (2) They have the same iSortIdx. |
| 108366 | +** (3) The template has same or fewer dependencies than the current loop |
| 108367 | +** (4) The template has the same or lower cost than the current loop |
| 108368 | +** (5) The template uses more terms of the same index but has no additional |
| 108369 | +** dependencies |
| 108370 | +*/ |
| 108371 | +static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
| 108372 | + WhereLoop **ppPrev, *p, *pNext = 0; |
| 108373 | + WhereInfo *pWInfo = pBuilder->pWInfo; |
| 108374 | + sqlite3 *db = pWInfo->pParse->db; |
| 108375 | + |
| 108376 | + /* If pBuilder->pBest is defined, then only keep track of the single |
| 108377 | + ** best WhereLoop. pBuilder->pBest->maskSelf==0 indicates that no |
| 108378 | + ** prior WhereLoops have been evaluated and that the current pTemplate |
| 108379 | + ** is therefore the first and hence the best and should be retained. |
| 108380 | + */ |
| 108381 | + if( (p = pBuilder->pBest)!=0 ){ |
| 108382 | + if( p->maskSelf!=0 ){ |
| 108383 | + WhereCost rCost = whereCostAdd(p->rRun,p->rSetup); |
| 108384 | + WhereCost rTemplate = whereCostAdd(pTemplate->rRun,pTemplate->rSetup); |
| 108385 | + if( rCost < rTemplate ){ |
| 108386 | + testcase( rCost==rTemplate-1 ); |
| 108387 | + goto whereLoopInsert_noop; |
| 108388 | + } |
| 108389 | + if( rCost==rTemplate && (p->prereq & pTemplate->prereq)==p->prereq ){ |
| 108390 | + goto whereLoopInsert_noop; |
| 108391 | + } |
| 108392 | + } |
| 108393 | +#if WHERETRACE_ENABLED |
| 108394 | + if( sqlite3WhereTrace & 0x8 ){ |
| 108395 | + sqlite3DebugPrintf(p->maskSelf==0 ? "ins-init: " : "ins-best: "); |
| 108396 | + whereLoopPrint(pTemplate, pWInfo->pTabList); |
| 108397 | + } |
| 108398 | +#endif |
| 108399 | + whereLoopXfer(db, p, pTemplate); |
| 108400 | + return SQLITE_OK; |
| 108401 | + } |
| 108402 | + |
| 108403 | + /* Search for an existing WhereLoop to overwrite, or which takes |
| 108404 | + ** priority over pTemplate. |
| 108405 | + */ |
| 108406 | + for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
| 108407 | + if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 108408 | + /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 108409 | + ** then those WhereLoops need to be considered separately. Neither is |
| 108410 | + ** a candidate to replace the other. */ |
| 108411 | + continue; |
| 108412 | + } |
| 108413 | + /* In the current implementation, the rSetup value is either zero |
| 108414 | + ** or the cost of building an automatic index (NlogN) and the NlogN |
| 108415 | + ** is the same for compatible WhereLoops. */ |
| 108416 | + assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 108417 | + || p->rSetup==pTemplate->rSetup ); |
| 108418 | + |
| 108419 | + /* whereLoopAddBtree() always generates and inserts the automatic index |
| 108420 | + ** case first. Hence compatible candidate WhereLoops never have a larger |
| 108421 | + ** rSetup. Call this SETUP-INVARIANT */ |
| 108422 | + assert( p->rSetup>=pTemplate->rSetup ); |
| 108423 | + |
| 108424 | + if( (p->prereq & pTemplate->prereq)==p->prereq |
| 108425 | + && p->rSetup<=pTemplate->rSetup |
| 108426 | + && p->rRun<=pTemplate->rRun |
| 108427 | + ){ |
| 108428 | + /* This branch taken when p is equal or better than pTemplate in |
| 108429 | + ** all of (1) dependences (2) setup-cost, and (3) run-cost. */ |
| 108430 | + assert( p->rSetup==pTemplate->rSetup ); |
| 108431 | + if( p->nLTerm<pTemplate->nLTerm |
| 108432 | + && (p->wsFlags & WHERE_INDEXED)!=0 |
| 108433 | + && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 108434 | + && p->u.btree.pIndex==pTemplate->u.btree.pIndex |
| 108435 | + && p->prereq==pTemplate->prereq |
| 108436 | + ){ |
| 108437 | + /* Overwrite an existing WhereLoop with an similar one that uses |
| 108438 | + ** more terms of the index */ |
| 108439 | + pNext = p->pNextLoop; |
| 108440 | + break; |
| 108441 | + }else{ |
| 108442 | + /* pTemplate is not helpful. |
| 108443 | + ** Return without changing or adding anything */ |
| 108444 | + goto whereLoopInsert_noop; |
| 108445 | + } |
| 108446 | + } |
| 108447 | + if( (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 108448 | + && p->rRun>=pTemplate->rRun |
| 108449 | + && ALWAYS(p->rSetup>=pTemplate->rSetup) /* See SETUP-INVARIANT above */ |
| 108450 | + ){ |
| 108451 | + /* Overwrite an existing WhereLoop with a better one: one that is |
| 108452 | + ** better at one of (1) dependences, (2) setup-cost, or (3) run-cost |
| 108453 | + ** and is no worse in any of those categories. */ |
| 108454 | + pNext = p->pNextLoop; |
| 108455 | + break; |
| 108456 | + } |
| 108457 | + } |
| 108458 | + |
| 108459 | + /* If we reach this point it means that either p[] should be overwritten |
| 108460 | + ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 108461 | + ** WhereLoop and insert it. |
| 108462 | + */ |
| 108463 | +#if WHERETRACE_ENABLED |
| 108464 | + if( sqlite3WhereTrace & 0x8 ){ |
| 108465 | + if( p!=0 ){ |
| 108466 | + sqlite3DebugPrintf("ins-del: "); |
| 108467 | + whereLoopPrint(p, pWInfo->pTabList); |
| 108468 | + } |
| 108469 | + sqlite3DebugPrintf("ins-new: "); |
| 108470 | + whereLoopPrint(pTemplate, pWInfo->pTabList); |
| 108471 | + } |
| 108472 | +#endif |
| 108473 | + if( p==0 ){ |
| 108474 | + p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
| 108475 | + if( p==0 ) return SQLITE_NOMEM; |
| 108476 | + whereLoopInit(p); |
| 108477 | + } |
| 108478 | + whereLoopXfer(db, p, pTemplate); |
| 108479 | + p->pNextLoop = pNext; |
| 108480 | + *ppPrev = p; |
| 108481 | + if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 108482 | + Index *pIndex = p->u.btree.pIndex; |
| 108483 | + if( pIndex && pIndex->tnum==0 ){ |
| 108484 | + p->u.btree.pIndex = 0; |
| 108485 | + } |
| 108486 | + } |
| 108487 | + return SQLITE_OK; |
| 108488 | + |
| 108489 | + /* Jump here if the insert is a no-op */ |
| 108490 | +whereLoopInsert_noop: |
| 108491 | +#if WHERETRACE_ENABLED |
| 108492 | + if( sqlite3WhereTrace & 0x8 ){ |
| 108493 | + sqlite3DebugPrintf(pBuilder->pBest ? "ins-skip: " : "ins-noop: "); |
| 108494 | + whereLoopPrint(pTemplate, pWInfo->pTabList); |
| 108495 | + } |
| 108496 | +#endif |
| 108497 | + return SQLITE_OK; |
| 108498 | +} |
| 108499 | + |
| 108500 | +/* |
| 108501 | +** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex. |
| 108502 | +** Try to match one more. |
| 108503 | +** |
| 108504 | +** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 108505 | +** INTEGER PRIMARY KEY. |
| 108506 | +*/ |
| 108507 | +static int whereLoopAddBtreeIndex( |
| 108508 | + WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 108509 | + struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 108510 | + Index *pProbe, /* An index on pSrc */ |
| 108511 | + WhereCost nInMul /* log(Number of iterations due to IN) */ |
| 108512 | +){ |
| 108513 | + WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 108514 | + Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 108515 | + sqlite3 *db = pParse->db; /* Database connection malloc context */ |
| 108516 | + WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 108517 | + WhereTerm *pTerm; /* A WhereTerm under consideration */ |
| 108518 | + int opMask; /* Valid operators for constraints */ |
| 108519 | + WhereScan scan; /* Iterator for WHERE terms */ |
| 108520 | + Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 108521 | + u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 108522 | + int saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 108523 | + u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 108524 | + WhereCost saved_nOut; /* Original value of pNew->nOut */ |
| 108525 | + int iCol; /* Index of the column in the table */ |
| 108526 | + int rc = SQLITE_OK; /* Return code */ |
| 108527 | + WhereCost nRowEst; /* Estimated index selectivity */ |
| 108528 | + WhereCost rLogSize; /* Logarithm of table size */ |
| 108529 | + WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
| 108530 | + |
| 108531 | + pNew = pBuilder->pNew; |
| 108532 | + if( db->mallocFailed ) return SQLITE_NOMEM; |
| 108533 | + |
| 108534 | + assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 108535 | + assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 108536 | + if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 108537 | + opMask = WO_LT|WO_LE; |
| 108538 | + }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 108539 | + opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
| 108540 | + }else{ |
| 108541 | + opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE; |
| 108542 | + } |
| 108543 | + if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
| 108544 | + |
| 108545 | + assert( pNew->u.btree.nEq<=pProbe->nColumn ); |
| 108546 | + if( pNew->u.btree.nEq < pProbe->nColumn ){ |
| 108547 | + iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 108548 | + nRowEst = whereCost(pProbe->aiRowEst[pNew->u.btree.nEq+1]); |
| 108549 | + if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 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(whereCost(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; /* Baseline cost is log2(N). Adjustments below */ |
| 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 ...)": TUNING: the SELECT returns 25 rows */ |
| 108578 | + nIn = 46; assert( 46==whereCost(25) ); |
| 108579 | + }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 108580 | + /* "x IN (value, value, ...)" */ |
| 108581 | + nIn = whereCost(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 | + assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 ); |
| 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 | + /* TUNING: IS NULL selects 2 rows */ |
| 108603 | + nIn = 10; assert( 10==whereCost(2) ); |
| 108604 | + pNew->nOut = nRowEst + nInMul + nIn; |
| 108605 | + }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ |
| 108606 | + testcase( pTerm->eOperator & WO_GT ); |
| 108607 | + testcase( pTerm->eOperator & WO_GE ); |
| 108608 | + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
| 108609 | + pBtm = pTerm; |
| 108610 | + pTop = 0; |
| 108611 | + }else{ |
| 108612 | + assert( pTerm->eOperator & (WO_LT|WO_LE) ); |
| 108613 | + testcase( pTerm->eOperator & WO_LT ); |
| 108614 | + testcase( pTerm->eOperator & WO_LE ); |
| 108615 | + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
| 108616 | + pTop = pTerm; |
| 108617 | + pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
| 108618 | + pNew->aLTerm[pNew->nLTerm-2] : 0; |
| 108619 | + } |
| 108620 | + if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 108621 | + /* Adjust nOut and rRun for STAT3 range values */ |
| 108622 | + WhereCost rDiv; |
| 108623 | + whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq, |
| 108624 | + pBtm, pTop, &rDiv); |
| 108625 | + pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10; |
| 108626 | + } |
| 108627 | +#ifdef SQLITE_ENABLE_STAT3 |
| 108628 | + if( pNew->u.btree.nEq==1 && pProbe->nSample ){ |
| 108629 | + tRowcnt nOut = 0; |
| 108630 | + if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ |
| 108631 | + testcase( pTerm->eOperator & WO_EQ ); |
| 108632 | + testcase( pTerm->eOperator & WO_ISNULL ); |
| 108633 | + rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut); |
| 108634 | + }else if( (pTerm->eOperator & WO_IN) |
| 108635 | + && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){ |
| 108636 | + rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut); |
| 108637 | + } |
| 108638 | + if( rc==SQLITE_OK ) pNew->nOut = whereCost(nOut); |
| 108639 | + } |
| 108640 | +#endif |
| 108641 | + if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
| 108642 | + /* Each row involves a step of the index, then a binary search of |
| 108643 | + ** the main table */ |
| 108644 | + pNew->rRun = whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10); |
| 108645 | + } |
| 108646 | + /* Step cost for each output row */ |
| 108647 | + pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut); |
| 108648 | + /* TBD: Adjust nOut for additional constraints */ |
| 108649 | + rc = whereLoopInsert(pBuilder, pNew); |
| 108650 | + if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
| 108651 | + && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0)) |
| 108652 | + ){ |
| 108653 | + whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
| 108654 | + } |
| 108655 | + } |
| 108656 | + pNew->prereq = saved_prereq; |
| 108657 | + pNew->u.btree.nEq = saved_nEq; |
| 108658 | + pNew->wsFlags = saved_wsFlags; |
| 108659 | + pNew->nOut = saved_nOut; |
| 108660 | + pNew->nLTerm = saved_nLTerm; |
| 108661 | + return rc; |
| 108662 | +} |
| 108663 | + |
| 108664 | +/* |
| 108665 | +** Return True if it is possible that pIndex might be useful in |
| 108666 | +** implementing the ORDER BY clause in pBuilder. |
| 108667 | +** |
| 108668 | +** Return False if pBuilder does not contain an ORDER BY clause or |
| 108669 | +** if there is no way for pIndex to be useful in implementing that |
| 108670 | +** ORDER BY clause. |
| 108671 | +*/ |
| 108672 | +static int indexMightHelpWithOrderBy( |
| 108673 | + WhereLoopBuilder *pBuilder, |
| 108674 | + Index *pIndex, |
| 108675 | + int iCursor |
| 108676 | +){ |
| 108677 | + ExprList *pOB; |
| 108678 | + int ii, jj; |
| 108679 | + |
| 108680 | + if( pIndex->bUnordered ) return 0; |
| 108681 | + if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
| 108682 | + for(ii=0; ii<pOB->nExpr; ii++){ |
| 108683 | + Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
| 108684 | + if( pExpr->op!=TK_COLUMN ) return 0; |
| 108685 | + if( pExpr->iTable==iCursor ){ |
| 108686 | + for(jj=0; jj<pIndex->nColumn; jj++){ |
| 108687 | + if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 108688 | + } |
| 108689 | + } |
| 108690 | + } |
| 108691 | + return 0; |
| 108692 | +} |
| 108693 | + |
| 108694 | +/* |
| 108695 | +** Return a bitmask where 1s indicate that the corresponding column of |
| 108696 | +** the table is used by an index. Only the first 63 columns are considered. |
| 108697 | +*/ |
| 108698 | +static Bitmask columnsInIndex(Index *pIdx){ |
| 108699 | + Bitmask m = 0; |
| 108700 | + int j; |
| 108701 | + for(j=pIdx->nColumn-1; j>=0; j--){ |
| 108702 | + int x = pIdx->aiColumn[j]; |
| 108703 | + testcase( x==BMS-1 ); |
| 108704 | + testcase( x==BMS-2 ); |
| 108705 | + if( x<BMS-1 ) m |= MASKBIT(x); |
| 108706 | + } |
| 108707 | + return m; |
| 108708 | +} |
| 108709 | + |
| 108710 | + |
| 108711 | +/* |
| 108712 | +** Add all WhereLoop objects a single table of the join were the table |
| 108713 | +** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 108714 | +** a b-tree table, not a virtual table. |
| 108715 | +*/ |
| 108716 | +static int whereLoopAddBtree( |
| 108717 | + WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 108718 | + Bitmask mExtra /* Extra prerequesites for using this table */ |
| 108719 | +){ |
| 108720 | + WhereInfo *pWInfo; /* WHERE analysis context */ |
| 108721 | + Index *pProbe; /* An index we are evaluating */ |
| 108722 | + Index sPk; /* A fake index object for the primary key */ |
| 108723 | + tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 108724 | + int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 108725 | + SrcList *pTabList; /* The FROM clause */ |
| 108726 | + struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
| 108727 | + WhereLoop *pNew; /* Template WhereLoop object */ |
| 108728 | + int rc = SQLITE_OK; /* Return code */ |
| 108729 | + int iSortIdx = 1; /* Index number */ |
| 108730 | + int b; /* A boolean value */ |
| 108731 | + WhereCost rSize; /* number of rows in the table */ |
| 108732 | + WhereCost rLogSize; /* Logarithm of the number of rows in the table */ |
| 108733 | + |
| 108734 | + pNew = pBuilder->pNew; |
| 108735 | + pWInfo = pBuilder->pWInfo; |
| 108736 | + pTabList = pWInfo->pTabList; |
| 108737 | + pSrc = pTabList->a + pNew->iTab; |
| 108738 | + assert( !IsVirtual(pSrc->pTab) ); |
| 108739 | + |
| 108740 | + if( pSrc->pIndex ){ |
| 108741 | + /* An INDEXED BY clause specifies a particular index to use */ |
| 108742 | + pProbe = pSrc->pIndex; |
| 108743 | + }else{ |
| 108744 | + /* There is no INDEXED BY clause. Create a fake Index object in local |
| 108745 | + ** variable sPk to represent the rowid primary key index. Make this |
| 108746 | + ** fake index the first in a chain of Index objects with all of the real |
| 108747 | + ** indices to follow */ |
| 108748 | + Index *pFirst; /* First of real indices on the table */ |
| 108749 | + memset(&sPk, 0, sizeof(Index)); |
| 108750 | + sPk.nColumn = 1; |
| 108751 | + sPk.aiColumn = &aiColumnPk; |
| 108752 | + sPk.aiRowEst = aiRowEstPk; |
| 108753 | + sPk.onError = OE_Replace; |
| 108754 | + sPk.pTable = pSrc->pTab; |
| 108755 | + aiRowEstPk[0] = pSrc->pTab->nRowEst; |
| 108756 | + aiRowEstPk[1] = 1; |
| 108757 | + pFirst = pSrc->pTab->pIndex; |
| 108758 | + if( pSrc->notIndexed==0 ){ |
| 108759 | + /* The real indices of the table are only considered if the |
| 108760 | + ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 108761 | + sPk.pNext = pFirst; |
| 108762 | + } |
| 108763 | + pProbe = &sPk; |
| 108764 | + } |
| 108765 | + rSize = whereCost(pSrc->pTab->nRowEst); |
| 108766 | + rLogSize = estLog(rSize); |
| 108767 | + |
| 108768 | + /* Automatic indexes */ |
| 108769 | + if( !pBuilder->pBest |
| 108770 | + && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 108771 | + && pSrc->pIndex==0 |
| 108772 | + && !pSrc->viaCoroutine |
| 108773 | + && !pSrc->notIndexed |
| 108774 | + && !pSrc->isCorrelated |
| 108775 | + ){ |
| 108776 | + /* Generate auto-index WhereLoops */ |
| 108777 | + WhereClause *pWC = pBuilder->pWC; |
| 108778 | + WhereTerm *pTerm; |
| 108779 | + WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 108780 | + for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| 108781 | + if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 108782 | + if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 108783 | + pNew->u.btree.nEq = 1; |
| 108784 | + pNew->u.btree.pIndex = 0; |
| 108785 | + pNew->nLTerm = 1; |
| 108786 | + pNew->aLTerm[0] = pTerm; |
| 108787 | + /* TUNING: One-time cost for computing the automatic index is |
| 108788 | + ** approximately 6*N*log2(N) where N is the number of rows in |
| 108789 | + ** the table being indexed. */ |
| 108790 | + pNew->rSetup = rLogSize + rSize + 26; assert( 26==whereCost(6) ); |
| 108791 | + /* TUNING: Each index lookup yields 10 rows in the table */ |
| 108792 | + pNew->nOut = 33; assert( 33==whereCost(10) ); |
| 108793 | + pNew->rRun = whereCostAdd(rLogSize,pNew->nOut); |
| 108794 | + pNew->wsFlags = WHERE_TEMP_INDEX; |
| 108795 | + pNew->prereq = mExtra | pTerm->prereqRight; |
| 108796 | + rc = whereLoopInsert(pBuilder, pNew); |
| 108797 | + } |
| 108798 | + } |
| 108799 | + } |
| 108800 | + |
| 108801 | + /* Loop over all indices |
| 108802 | + */ |
| 108803 | + for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
| 108804 | + pNew->u.btree.nEq = 0; |
| 108805 | + pNew->nLTerm = 0; |
| 108806 | + pNew->iSortIdx = 0; |
| 108807 | + pNew->rSetup = 0; |
| 108808 | + pNew->prereq = mExtra; |
| 108809 | + pNew->nOut = rSize; |
| 108810 | + pNew->u.btree.pIndex = pProbe; |
| 108811 | + b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
| 108812 | + /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 108813 | + assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
| 108814 | + if( pProbe->tnum<=0 ){ |
| 108815 | + /* Integer primary key index */ |
| 108816 | + pNew->wsFlags = WHERE_IPK; |
| 108817 | + |
| 108818 | + /* Full table scan */ |
| 108819 | + pNew->iSortIdx = b ? iSortIdx : 0; |
| 108820 | + /* TUNING: Cost of full table scan is 3*(N + log2(N)). |
| 108821 | + ** + The extra 3 factor is to encourage the use of indexed lookups |
| 108822 | + ** over full scans. A smaller constant 2 is used for covering |
| 108823 | + ** index scans so that a covering index scan will be favored over |
| 108824 | + ** a table scan. */ |
| 108825 | + pNew->rRun = whereCostAdd(rSize,rLogSize) + 16; |
| 108826 | + rc = whereLoopInsert(pBuilder, pNew); |
| 108827 | + if( rc ) break; |
| 108828 | + }else{ |
| 108829 | + Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe); |
| 108830 | + pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 108831 | + |
| 108832 | + /* Full scan via index */ |
| 108833 | + if( b |
| 108834 | + || ( m==0 |
| 108835 | + && pProbe->bUnordered==0 |
| 108836 | + && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 108837 | + && sqlite3GlobalConfig.bUseCis |
| 108838 | + && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 108839 | + ) |
| 108840 | + ){ |
| 108841 | + pNew->iSortIdx = b ? iSortIdx : 0; |
| 108842 | + if( m==0 ){ |
| 108843 | + /* TUNING: Cost of a covering index scan is 2*(N + log2(N)). |
| 108844 | + ** + The extra 2 factor is to encourage the use of indexed lookups |
| 108845 | + ** over index scans. A table scan uses a factor of 3 so that |
| 108846 | + ** index scans are favored over table scans. |
| 108847 | + ** + If this covering index might also help satisfy the ORDER BY |
| 108848 | + ** clause, then the cost is fudged down slightly so that this |
| 108849 | + ** index is favored above other indices that have no hope of |
| 108850 | + ** helping with the ORDER BY. */ |
| 108851 | + pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b; |
| 108852 | + }else{ |
| 108853 | + assert( b!=0 ); |
| 108854 | + /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N) |
| 108855 | + ** which we will simplify to just N*log2(N) */ |
| 108856 | + pNew->rRun = rSize + rLogSize; |
| 108857 | + } |
| 108858 | + rc = whereLoopInsert(pBuilder, pNew); |
| 108859 | + if( rc ) break; |
| 108860 | + } |
| 108861 | + } |
| 108862 | + rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
| 108863 | + |
| 108864 | + /* If there was an INDEXED BY clause, then only that one index is |
| 108865 | + ** considered. */ |
| 108866 | + if( pSrc->pIndex ) break; |
| 108867 | + } |
| 108868 | + return rc; |
| 108869 | +} |
| 108870 | + |
| 108871 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 108872 | +/* |
| 108873 | +** Add all WhereLoop objects for a table of the join identified by |
| 108874 | +** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
| 108875 | +*/ |
| 108876 | +static int whereLoopAddVirtual( |
| 108877 | + WhereLoopBuilder *pBuilder /* WHERE clause information */ |
| 108878 | +){ |
| 108879 | + WhereInfo *pWInfo; /* WHERE analysis context */ |
| 108880 | + Parse *pParse; /* The parsing context */ |
| 108881 | + WhereClause *pWC; /* The WHERE clause */ |
| 108882 | + struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 108883 | + Table *pTab; |
| 108884 | + sqlite3 *db; |
| 108885 | + sqlite3_index_info *pIdxInfo; |
| 108886 | + struct sqlite3_index_constraint *pIdxCons; |
| 108887 | + struct sqlite3_index_constraint_usage *pUsage; |
| 108888 | + WhereTerm *pTerm; |
| 108889 | + int i, j; |
| 108890 | + int iTerm, mxTerm; |
| 108891 | + int nConstraint; |
| 108892 | + int seenIn = 0; /* True if an IN operator is seen */ |
| 108893 | + int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 108894 | + int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 108895 | + WhereLoop *pNew; |
| 108896 | + int rc = SQLITE_OK; |
| 108897 | + |
| 108898 | + pWInfo = pBuilder->pWInfo; |
| 108899 | + pParse = pWInfo->pParse; |
| 108900 | + db = pParse->db; |
| 108901 | + pWC = pBuilder->pWC; |
| 108902 | + pNew = pBuilder->pNew; |
| 108903 | + pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
| 108904 | + pTab = pSrc->pTab; |
| 108905 | + assert( IsVirtual(pTab) ); |
| 108906 | + pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
| 108907 | + if( pIdxInfo==0 ) return SQLITE_NOMEM; |
| 108908 | + pNew->prereq = 0; |
| 108909 | + pNew->rSetup = 0; |
| 108910 | + pNew->wsFlags = WHERE_VIRTUALTABLE; |
| 108911 | + pNew->nLTerm = 0; |
| 108912 | + pNew->u.vtab.needFree = 0; |
| 108913 | + pUsage = pIdxInfo->aConstraintUsage; |
| 108914 | + nConstraint = pIdxInfo->nConstraint; |
| 108915 | + if( whereLoopResize(db, pNew, nConstraint) ){ |
| 108916 | + sqlite3DbFree(db, pIdxInfo); |
| 108917 | + return SQLITE_NOMEM; |
| 108918 | + } |
| 108919 | + |
| 108920 | + for(iPhase=0; iPhase<=3; iPhase++){ |
| 108921 | + if( !seenIn && (iPhase&1)!=0 ){ |
| 108922 | + iPhase++; |
| 108923 | + if( iPhase>3 ) break; |
| 108924 | + } |
| 108925 | + if( !seenVar && iPhase>1 ) break; |
| 108926 | + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 108927 | + for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 108928 | + j = pIdxCons->iTermOffset; |
| 108929 | + pTerm = &pWC->a[j]; |
| 108930 | + switch( iPhase ){ |
| 108931 | + case 0: /* Constants without IN operator */ |
| 108932 | + pIdxCons->usable = 0; |
| 108933 | + if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 108934 | + seenIn = 1; |
| 108935 | + } |
| 108936 | + if( pTerm->prereqRight!=0 ){ |
| 108937 | + seenVar = 1; |
| 108938 | + }else if( (pTerm->eOperator & WO_IN)==0 ){ |
| 108939 | + pIdxCons->usable = 1; |
| 108940 | + } |
| 108941 | + break; |
| 108942 | + case 1: /* Constants with IN operators */ |
| 108943 | + assert( seenIn ); |
| 108944 | + pIdxCons->usable = (pTerm->prereqRight==0); |
| 108945 | + break; |
| 108946 | + case 2: /* Variables without IN */ |
| 108947 | + assert( seenVar ); |
| 108948 | + pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 108949 | + break; |
| 108950 | + default: /* Variables with IN */ |
| 108951 | + assert( seenVar && seenIn ); |
| 108952 | + pIdxCons->usable = 1; |
| 108953 | + break; |
| 108954 | + } |
| 108955 | + } |
| 108956 | + memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 108957 | + if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 108958 | + pIdxInfo->idxStr = 0; |
| 108959 | + pIdxInfo->idxNum = 0; |
| 108960 | + pIdxInfo->needToFreeIdxStr = 0; |
| 108961 | + pIdxInfo->orderByConsumed = 0; |
| 108962 | + pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
| 108963 | + rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 108964 | + if( rc ) goto whereLoopAddVtab_exit; |
| 108965 | + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 108966 | + pNew->prereq = 0; |
| 108967 | + mxTerm = -1; |
| 108968 | + assert( pNew->nLSlot>=nConstraint ); |
| 108969 | + for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
| 108970 | + pNew->u.vtab.omitMask = 0; |
| 108971 | + for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 108972 | + if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 108973 | + j = pIdxCons->iTermOffset; |
| 108974 | + if( iTerm>=nConstraint |
| 108975 | + || j<0 |
| 108976 | + || j>=pWC->nTerm |
| 108977 | + || pNew->aLTerm[iTerm]!=0 |
| 108978 | + ){ |
| 108979 | + rc = SQLITE_ERROR; |
| 108980 | + sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 108981 | + goto whereLoopAddVtab_exit; |
| 108982 | + } |
| 108983 | + testcase( iTerm==nConstraint-1 ); |
| 108984 | + testcase( j==0 ); |
| 108985 | + testcase( j==pWC->nTerm-1 ); |
| 108986 | + pTerm = &pWC->a[j]; |
| 108987 | + pNew->prereq |= pTerm->prereqRight; |
| 108988 | + assert( iTerm<pNew->nLSlot ); |
| 108989 | + pNew->aLTerm[iTerm] = pTerm; |
| 108990 | + if( iTerm>mxTerm ) mxTerm = iTerm; |
| 108991 | + testcase( iTerm==15 ); |
| 108992 | + testcase( iTerm==16 ); |
| 108993 | + if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
| 108994 | + if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 108995 | + if( pUsage[i].omit==0 ){ |
| 108996 | + /* Do not attempt to use an IN constraint if the virtual table |
| 108997 | + ** says that the equivalent EQ constraint cannot be safely omitted. |
| 108998 | + ** If we do attempt to use such a constraint, some rows might be |
| 108999 | + ** repeated in the output. */ |
| 109000 | + break; |
| 109001 | + } |
| 109002 | + /* A virtual table that is constrained by an IN clause may not |
| 109003 | + ** consume the ORDER BY clause because (1) the order of IN terms |
| 109004 | + ** is not necessarily related to the order of output terms and |
| 109005 | + ** (2) Multiple outputs from a single IN value will not merge |
| 109006 | + ** together. */ |
| 109007 | + pIdxInfo->orderByConsumed = 0; |
| 109008 | + } |
| 109009 | + } |
| 109010 | + } |
| 109011 | + if( i>=nConstraint ){ |
| 109012 | + pNew->nLTerm = mxTerm+1; |
| 109013 | + assert( pNew->nLTerm<=pNew->nLSlot ); |
| 109014 | + pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 109015 | + pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 109016 | + pIdxInfo->needToFreeIdxStr = 0; |
| 109017 | + pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
| 109018 | + pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) |
| 109019 | + && pIdxInfo->orderByConsumed); |
| 109020 | + pNew->rSetup = 0; |
| 109021 | + pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost); |
| 109022 | + /* TUNING: Every virtual table query returns 25 rows */ |
| 109023 | + pNew->nOut = 46; assert( 46==whereCost(25) ); |
| 109024 | + whereLoopInsert(pBuilder, pNew); |
| 109025 | + if( pNew->u.vtab.needFree ){ |
| 109026 | + sqlite3_free(pNew->u.vtab.idxStr); |
| 109027 | + pNew->u.vtab.needFree = 0; |
| 109028 | + } |
| 109029 | + } |
| 109030 | + } |
| 109031 | + |
| 109032 | +whereLoopAddVtab_exit: |
| 109033 | + if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 109034 | + sqlite3DbFree(db, pIdxInfo); |
| 109035 | + return rc; |
| 109036 | +} |
| 109037 | +#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 109038 | + |
| 109039 | +/* |
| 109040 | +** Add WhereLoop entries to handle OR terms. This works for either |
| 109041 | +** btrees or virtual tables. |
| 109042 | +*/ |
| 109043 | +static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
| 109044 | + WhereInfo *pWInfo = pBuilder->pWInfo; |
| 109045 | + WhereClause *pWC; |
| 109046 | + WhereLoop *pNew; |
| 109047 | + WhereTerm *pTerm, *pWCEnd; |
| 109048 | + int rc = SQLITE_OK; |
| 109049 | + int iCur; |
| 109050 | + WhereClause tempWC; |
| 109051 | + WhereLoopBuilder sSubBuild; |
| 109052 | + WhereLoop sBest; |
| 109053 | + struct SrcList_item *pItem; |
| 109054 | + |
| 109055 | + pWC = pBuilder->pWC; |
| 109056 | + if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
| 109057 | + pWCEnd = pWC->a + pWC->nTerm; |
| 109058 | + pNew = pBuilder->pNew; |
| 109059 | + |
| 109060 | + for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 109061 | + if( (pTerm->eOperator & WO_OR)!=0 |
| 109062 | + && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 109063 | + ){ |
| 109064 | + WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 109065 | + WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 109066 | + WhereTerm *pOrTerm; |
| 109067 | + WhereCost rTotal = 0; |
| 109068 | + WhereCost nRow = 0; |
| 109069 | + Bitmask prereq = mExtra; |
| 109070 | + |
| 109071 | + whereLoopInit(&sBest); |
| 109072 | + pItem = pWInfo->pTabList->a + pNew->iTab; |
| 109073 | + iCur = pItem->iCursor; |
| 109074 | + sSubBuild = *pBuilder; |
| 109075 | + sSubBuild.pOrderBy = 0; |
| 109076 | + sSubBuild.pBest = &sBest; |
| 109077 | + |
| 109078 | + for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
| 109079 | + if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 109080 | + sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 109081 | + }else if( pOrTerm->leftCursor==iCur ){ |
| 109082 | + tempWC.pWInfo = pWC->pWInfo; |
| 109083 | + tempWC.pOuter = pWC; |
| 109084 | + tempWC.op = TK_AND; |
| 109085 | + tempWC.nTerm = 1; |
| 109086 | + tempWC.a = pOrTerm; |
| 109087 | + sSubBuild.pWC = &tempWC; |
| 109088 | + }else{ |
| 109089 | + continue; |
| 109090 | + } |
| 109091 | + sBest.maskSelf = 0; |
| 109092 | + sBest.rSetup = 0; |
| 109093 | + sBest.rRun = 0; |
| 109094 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 109095 | + if( IsVirtual(pItem->pTab) ){ |
| 109096 | + rc = whereLoopAddVirtual(&sSubBuild); |
| 109097 | + }else |
| 109098 | +#endif |
| 109099 | + { |
| 109100 | + rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 109101 | + } |
| 109102 | + /* sBest.maskSelf is always zero if an error occurs */ |
| 109103 | + assert( rc==SQLITE_OK || sBest.maskSelf==0 ); |
| 109104 | + if( sBest.maskSelf==0 ) break; |
| 109105 | + assert( sBest.rSetup==0 ); |
| 109106 | + rTotal = whereCostAdd(rTotal, sBest.rRun); |
| 109107 | + nRow = whereCostAdd(nRow, sBest.nOut); |
| 109108 | + prereq |= sBest.prereq; |
| 109109 | + } |
| 109110 | + assert( pNew->nLSlot>=1 ); |
| 109111 | + if( sBest.maskSelf ){ |
| 109112 | + pNew->nLTerm = 1; |
| 109113 | + pNew->aLTerm[0] = pTerm; |
| 109114 | + pNew->wsFlags = WHERE_MULTI_OR; |
| 109115 | + pNew->rSetup = 0; |
| 109116 | + /* TUNING: Multiple by 3.5 for the secondary table lookup */ |
| 109117 | + pNew->rRun = rTotal + 18; assert( 18==whereCost(7)-whereCost(2) ); |
| 109118 | + pNew->nOut = nRow; |
| 109119 | + pNew->prereq = prereq; |
| 109120 | + memset(&pNew->u, 0, sizeof(pNew->u)); |
| 109121 | + rc = whereLoopInsert(pBuilder, pNew); |
| 109122 | + } |
| 109123 | + whereLoopClear(pWInfo->pParse->db, &sBest); |
| 109124 | + } |
| 109125 | + } |
| 109126 | + return rc; |
| 109127 | +} |
| 109128 | + |
| 109129 | +/* |
| 109130 | +** Add all WhereLoop objects for all tables |
| 109131 | +*/ |
| 109132 | +static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
| 109133 | + WhereInfo *pWInfo = pBuilder->pWInfo; |
| 109134 | + Bitmask mExtra = 0; |
| 109135 | + Bitmask mPrior = 0; |
| 109136 | + int iTab; |
| 109137 | + SrcList *pTabList = pWInfo->pTabList; |
| 109138 | + struct SrcList_item *pItem; |
| 109139 | + sqlite3 *db = pWInfo->pParse->db; |
| 109140 | + int nTabList = pWInfo->nLevel; |
| 109141 | + int rc = SQLITE_OK; |
| 109142 | + u8 priorJoinType = 0; |
| 109143 | + WhereLoop *pNew; |
| 109144 | + |
| 109145 | + /* Loop over the tables in the join, from left to right */ |
| 109146 | + pNew = pBuilder->pNew; |
| 109147 | + whereLoopInit(pNew); |
| 109148 | + for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
| 109149 | + pNew->iTab = iTab; |
| 109150 | + pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
| 109151 | + if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
| 109152 | + mExtra = mPrior; |
| 109153 | + } |
| 109154 | + priorJoinType = pItem->jointype; |
| 109155 | + if( IsVirtual(pItem->pTab) ){ |
| 109156 | + rc = whereLoopAddVirtual(pBuilder); |
| 109157 | + }else{ |
| 109158 | + rc = whereLoopAddBtree(pBuilder, mExtra); |
| 109159 | + } |
| 109160 | + if( rc==SQLITE_OK ){ |
| 109161 | + rc = whereLoopAddOr(pBuilder, mExtra); |
| 109162 | + } |
| 109163 | + mPrior |= pNew->maskSelf; |
| 109164 | + if( rc || db->mallocFailed ) break; |
| 109165 | + } |
| 109166 | + whereLoopClear(db, pNew); |
| 109167 | + return rc; |
| 109168 | +} |
| 109169 | + |
| 109170 | +/* |
| 109171 | +** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
| 109172 | +** parameters) to see if it outputs rows in the requested ORDER BY |
| 109173 | +** (or GROUP BY) without requiring a separate source operation. Return: |
| 109174 | +** |
| 109175 | +** 0: ORDER BY is not satisfied. Sorting required |
| 109176 | +** 1: ORDER BY is satisfied. Omit sorting |
| 109177 | +** -1: Unknown at this time |
| 109178 | +** |
| 109179 | +*/ |
| 109180 | +static int wherePathSatisfiesOrderBy( |
| 109181 | + WhereInfo *pWInfo, /* The WHERE clause */ |
| 109182 | + ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
| 109183 | + WherePath *pPath, /* The WherePath to check */ |
| 109184 | + u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 109185 | + u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
| 109186 | + WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
| 109187 | + Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
| 109188 | +){ |
| 109189 | + u8 revSet; /* True if rev is known */ |
| 109190 | + u8 rev; /* Composite sort order */ |
| 109191 | + u8 revIdx; /* Index sort order */ |
| 109192 | + u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 109193 | + u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 109194 | + u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
| 109195 | + u16 nColumn; /* Number of columns in pIndex */ |
| 109196 | + u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 109197 | + int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 109198 | + int i, j; /* Loop counters */ |
| 109199 | + int iCur; /* Cursor number for current WhereLoop */ |
| 109200 | + int iColumn; /* A column number within table iCur */ |
| 109201 | + WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
| 109202 | + WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 109203 | + Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 109204 | + CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 109205 | + Index *pIndex; /* The index associated with pLoop */ |
| 109206 | + sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 109207 | + Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 109208 | + Bitmask obDone; /* Mask of all ORDER BY terms */ |
| 109209 | + Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
| 109210 | + Bitmask ready; /* Mask of inner loops */ |
| 109211 | + |
| 109212 | + /* |
| 109213 | + ** We say the WhereLoop is "one-row" if it generates no more than one |
| 109214 | + ** row of output. A WhereLoop is one-row if all of the following are true: |
| 109215 | + ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 109216 | + ** (b) The index is unique |
| 109217 | + ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 109218 | + ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
| 109219 | + ** |
| 109220 | + ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 109221 | + ** that WhereLoop that are in the ORDER BY clause are different for every |
| 109222 | + ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 109223 | + ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 109224 | + ** is not order-distinct. To be order-distinct is not quite the same as being |
| 109225 | + ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 109226 | + ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 109227 | + ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 109228 | + ** |
| 109229 | + ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 109230 | + ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 109231 | + ** automatically order-distinct. |
| 109232 | + */ |
| 109233 | + |
| 109234 | + assert( pOrderBy!=0 ); |
| 109235 | + |
| 109236 | + /* Sortability of virtual tables is determined by the xBestIndex method |
| 109237 | + ** of the virtual table itself */ |
| 109238 | + if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ |
| 109239 | + testcase( nLoop>0 ); /* True when outer loops are one-row and match |
| 109240 | + ** no ORDER BY terms */ |
| 109241 | + return pLast->u.vtab.isOrdered; |
| 109242 | + } |
| 109243 | + if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
| 109244 | + |
| 109245 | + nOrderBy = pOrderBy->nExpr; |
| 109246 | + testcase( nOrderBy==BMS-1 ); |
| 109247 | + if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 109248 | + isOrderDistinct = 1; |
| 109249 | + obDone = MASKBIT(nOrderBy)-1; |
| 109250 | + orderDistinctMask = 0; |
| 109251 | + ready = 0; |
| 109252 | + for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
| 109253 | + if( iLoop>0 ) ready |= pLoop->maskSelf; |
| 109254 | + pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
| 109255 | + assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 109256 | + iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
| 109257 | + |
| 109258 | + /* Mark off any ORDER BY term X that is a column in the table of |
| 109259 | + ** the current loop for which there is term in the WHERE |
| 109260 | + ** clause of the form X IS NULL or X=? that reference only outer |
| 109261 | + ** loops. |
| 109262 | + */ |
| 109263 | + for(i=0; i<nOrderBy; i++){ |
| 109264 | + if( MASKBIT(i) & obSat ) continue; |
| 109265 | + pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 109266 | + if( pOBExpr->op!=TK_COLUMN ) continue; |
| 109267 | + if( pOBExpr->iTable!=iCur ) continue; |
| 109268 | + pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 109269 | + ~ready, WO_EQ|WO_ISNULL, 0); |
| 109270 | + if( pTerm==0 ) continue; |
| 109271 | + if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){ |
| 109272 | + const char *z1, *z2; |
| 109273 | + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 109274 | + if( !pColl ) pColl = db->pDfltColl; |
| 109275 | + z1 = pColl->zName; |
| 109276 | + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 109277 | + if( !pColl ) pColl = db->pDfltColl; |
| 109278 | + z2 = pColl->zName; |
| 109279 | + if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 109280 | + } |
| 109281 | + obSat |= MASKBIT(i); |
| 109282 | + } |
| 109283 | + |
| 109284 | + if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 109285 | + if( pLoop->wsFlags & WHERE_IPK ){ |
| 109286 | + pIndex = 0; |
| 109287 | + nColumn = 0; |
| 109288 | + }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
| 109289 | + return 0; |
| 109290 | + }else{ |
| 109291 | + nColumn = pIndex->nColumn; |
| 109292 | + isOrderDistinct = pIndex->onError!=OE_None; |
| 109293 | + } |
| 109294 | + |
| 109295 | + /* Loop through all columns of the index and deal with the ones |
| 109296 | + ** that are not constrained by == or IN. |
| 109297 | + */ |
| 109298 | + rev = revSet = 0; |
| 109299 | + distinctColumns = 0; |
| 109300 | + for(j=0; j<=nColumn; j++){ |
| 109301 | + u8 bOnce; /* True to run the ORDER BY search loop */ |
| 109302 | + |
| 109303 | + /* Skip over == and IS NULL terms */ |
| 109304 | + if( j<pLoop->u.btree.nEq |
| 109305 | + && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
| 109306 | + ){ |
| 109307 | + if( i & WO_ISNULL ){ |
| 109308 | + testcase( isOrderDistinct ); |
| 109309 | + isOrderDistinct = 0; |
| 109310 | + } |
| 109311 | + continue; |
| 109312 | + } |
| 109313 | + |
| 109314 | + /* Get the column number in the table (iColumn) and sort order |
| 109315 | + ** (revIdx) for the j-th column of the index. |
| 109316 | + */ |
| 109317 | + if( j<nColumn ){ |
| 109318 | + /* Normal index columns */ |
| 109319 | + iColumn = pIndex->aiColumn[j]; |
| 109320 | + revIdx = pIndex->aSortOrder[j]; |
| 109321 | + if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
| 109322 | + }else{ |
| 109323 | + /* The ROWID column at the end */ |
| 109324 | + assert( j==nColumn ); |
| 109325 | + iColumn = -1; |
| 109326 | + revIdx = 0; |
| 109327 | + } |
| 109328 | + |
| 109329 | + /* An unconstrained column that might be NULL means that this |
| 109330 | + ** WhereLoop is not well-ordered |
| 109331 | + */ |
| 109332 | + if( isOrderDistinct |
| 109333 | + && iColumn>=0 |
| 109334 | + && j>=pLoop->u.btree.nEq |
| 109335 | + && pIndex->pTable->aCol[iColumn].notNull==0 |
| 109336 | + ){ |
| 109337 | + isOrderDistinct = 0; |
| 109338 | + } |
| 109339 | + |
| 109340 | + /* Find the ORDER BY term that corresponds to the j-th column |
| 109341 | + ** of the index and and mark that ORDER BY term off |
| 109342 | + */ |
| 109343 | + bOnce = 1; |
| 109344 | + isMatch = 0; |
| 109345 | + for(i=0; bOnce && i<nOrderBy; i++){ |
| 109346 | + if( MASKBIT(i) & obSat ) continue; |
| 109347 | + pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 109348 | + testcase( wctrlFlags & WHERE_GROUPBY ); |
| 109349 | + testcase( wctrlFlags & WHERE_DISTINCTBY ); |
| 109350 | + if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
| 109351 | + if( pOBExpr->op!=TK_COLUMN ) continue; |
| 109352 | + if( pOBExpr->iTable!=iCur ) continue; |
| 109353 | + if( pOBExpr->iColumn!=iColumn ) continue; |
| 109354 | + if( iColumn>=0 ){ |
| 109355 | + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 109356 | + if( !pColl ) pColl = db->pDfltColl; |
| 109357 | + if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 109358 | + } |
| 109359 | + isMatch = 1; |
| 109360 | + break; |
| 109361 | + } |
| 109362 | + if( isMatch ){ |
| 109363 | + if( iColumn<0 ){ |
| 109364 | + testcase( distinctColumns==0 ); |
| 109365 | + distinctColumns = 1; |
| 109366 | + } |
| 109367 | + obSat |= MASKBIT(i); |
| 109368 | + if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 109369 | + /* Make sure the sort order is compatible in an ORDER BY clause. |
| 109370 | + ** Sort order is irrelevant for a GROUP BY clause. */ |
| 109371 | + if( revSet ){ |
| 109372 | + if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; |
| 109373 | + }else{ |
| 109374 | + rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 109375 | + if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 109376 | + revSet = 1; |
| 109377 | + } |
| 109378 | + } |
| 109379 | + }else{ |
| 109380 | + /* No match found */ |
| 109381 | + if( j==0 || j<nColumn ){ |
| 109382 | + testcase( isOrderDistinct!=0 ); |
| 109383 | + isOrderDistinct = 0; |
| 109384 | + } |
| 109385 | + break; |
| 109386 | + } |
| 109387 | + } /* end Loop over all index columns */ |
| 109388 | + if( distinctColumns ){ |
| 109389 | + testcase( isOrderDistinct==0 ); |
| 109390 | + isOrderDistinct = 1; |
| 109391 | + } |
| 109392 | + } /* end-if not one-row */ |
| 109393 | + |
| 109394 | + /* Mark off any other ORDER BY terms that reference pLoop */ |
| 109395 | + if( isOrderDistinct ){ |
| 109396 | + orderDistinctMask |= pLoop->maskSelf; |
| 109397 | + for(i=0; i<nOrderBy; i++){ |
| 109398 | + Expr *p; |
| 109399 | + if( MASKBIT(i) & obSat ) continue; |
| 109400 | + p = pOrderBy->a[i].pExpr; |
| 109401 | + if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){ |
| 109402 | + obSat |= MASKBIT(i); |
| 109403 | + } |
| 109404 | + } |
| 109405 | + } |
| 109406 | + } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
| 109407 | + if( obSat==obDone ) return 1; |
| 109408 | + if( !isOrderDistinct ) return 0; |
| 109409 | + return -1; |
| 109410 | +} |
| 109411 | + |
| 109412 | +#ifdef WHERETRACE_ENABLED |
| 109413 | +/* For debugging use only: */ |
| 109414 | +static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 109415 | + static char zName[65]; |
| 109416 | + int i; |
| 109417 | + for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 109418 | + if( pLast ) zName[i++] = pLast->cId; |
| 109419 | + zName[i] = 0; |
| 109420 | + return zName; |
| 109421 | +} |
| 109422 | +#endif |
| 109423 | + |
| 109424 | + |
| 109425 | +/* |
| 109426 | +** Given the list of WhereLoop objects on pWInfo->pLoops, this routine |
| 109427 | +** attempts to find the lowest cost path that visits each WhereLoop |
| 109428 | +** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 109429 | +** |
| 109430 | +** Assume that the total number of output rows that will need to be sorted |
| 109431 | +** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 109432 | +** costs if nRowEst==0. |
| 109433 | +** |
| 109434 | +** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 109435 | +** error occurs. |
| 109436 | +*/ |
| 109437 | +static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){ |
| 109438 | + int mxChoice; /* Maximum number of simultaneous paths tracked */ |
| 109439 | + int nLoop; /* Number of terms in the join */ |
| 109440 | + Parse *pParse; /* Parsing context */ |
| 109441 | + sqlite3 *db; /* The database connection */ |
| 109442 | + int iLoop; /* Loop counter over the terms of the join */ |
| 109443 | + int ii, jj; /* Loop counters */ |
| 109444 | + WhereCost rCost; /* Cost of a path */ |
| 109445 | + WhereCost mxCost = 0; /* Maximum cost of a set of paths */ |
| 109446 | + WhereCost rSortCost; /* Cost to do a sort */ |
| 109447 | + int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 109448 | + WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 109449 | + WherePath *aTo; /* The nTo best paths at the current level */ |
| 109450 | + WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 109451 | + WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 109452 | + WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 109453 | + WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 109454 | + char *pSpace; /* Temporary memory used by this routine */ |
| 109455 | + |
| 109456 | + pParse = pWInfo->pParse; |
| 109457 | + db = pParse->db; |
| 109458 | + nLoop = pWInfo->nLevel; |
| 109459 | + /* TUNING: For simple queries, only the best path is tracked. |
| 109460 | + ** For 2-way joins, the 5 best paths are followed. |
| 109461 | + ** For joins of 3 or more tables, track the 10 best paths */ |
| 109462 | + mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10); |
| 109463 | + assert( nLoop<=pWInfo->pTabList->nSrc ); |
| 109464 | + WHERETRACE(0x002, ("---- begin solver\n")); |
| 109465 | + |
| 109466 | + /* Allocate and initialize space for aTo and aFrom */ |
| 109467 | + ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 109468 | + pSpace = sqlite3DbMallocRaw(db, ii); |
| 109469 | + if( pSpace==0 ) return SQLITE_NOMEM; |
| 109470 | + aTo = (WherePath*)pSpace; |
| 109471 | + aFrom = aTo+mxChoice; |
| 109472 | + memset(aFrom, 0, sizeof(aFrom[0])); |
| 109473 | + pX = (WhereLoop**)(aFrom+mxChoice); |
| 109474 | + for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
| 109475 | + pFrom->aLoop = pX; |
| 109476 | + } |
| 109477 | + |
| 109478 | + /* Seed the search with a single WherePath containing zero WhereLoops. |
| 109479 | + ** |
| 109480 | + ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 109481 | + ** of computing an automatic index is not paid back within the first 25 |
| 109482 | + ** rows, then do not use the automatic index. */ |
| 109483 | + aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==whereCost(25) ); |
| 109484 | + nFrom = 1; |
| 109485 | + |
| 109486 | + /* Precompute the cost of sorting the final result set, if the caller |
| 109487 | + ** to sqlite3WhereBegin() was concerned about sorting */ |
| 109488 | + rSortCost = 0; |
| 109489 | + if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
| 109490 | + aFrom[0].isOrderedValid = 1; |
| 109491 | + }else{ |
| 109492 | + /* TUNING: Estimated cost of sorting is N*log2(N) where N is the |
| 109493 | + ** number of output rows. */ |
| 109494 | + rSortCost = nRowEst + estLog(nRowEst); |
| 109495 | + WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost)); |
| 109496 | + } |
| 109497 | + |
| 109498 | + /* Compute successively longer WherePaths using the previous generation |
| 109499 | + ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 109500 | + ** best paths at each generation */ |
| 109501 | + for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 109502 | + nTo = 0; |
| 109503 | + for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 109504 | + for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 109505 | + Bitmask maskNew; |
| 109506 | + Bitmask revMask = 0; |
| 109507 | + u8 isOrderedValid = pFrom->isOrderedValid; |
| 109508 | + u8 isOrdered = pFrom->isOrdered; |
| 109509 | + if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 109510 | + if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
| 109511 | + /* At this point, pWLoop is a candidate to be the next loop. |
| 109512 | + ** Compute its cost */ |
| 109513 | + rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 109514 | + rCost = whereCostAdd(rCost, pFrom->rCost); |
| 109515 | + maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
| 109516 | + if( !isOrderedValid ){ |
| 109517 | + switch( wherePathSatisfiesOrderBy(pWInfo, |
| 109518 | + pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
| 109519 | + iLoop, pWLoop, &revMask) ){ |
| 109520 | + case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ |
| 109521 | + isOrdered = 1; |
| 109522 | + isOrderedValid = 1; |
| 109523 | + break; |
| 109524 | + case 0: /* No. pFrom+pWLoop will require a separate sort */ |
| 109525 | + isOrdered = 0; |
| 109526 | + isOrderedValid = 1; |
| 109527 | + rCost = whereCostAdd(rCost, rSortCost); |
| 109528 | + break; |
| 109529 | + default: /* Cannot tell yet. Try again on the next iteration */ |
| 109530 | + break; |
| 109531 | + } |
| 109532 | + }else{ |
| 109533 | + revMask = pFrom->revLoop; |
| 109534 | + } |
| 109535 | + /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 109536 | + for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
| 109537 | + if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){ |
| 109538 | + testcase( jj==nTo-1 ); |
| 109539 | + break; |
| 109540 | + } |
| 109541 | + } |
| 109542 | + if( jj>=nTo ){ |
| 109543 | + if( nTo>=mxChoice && rCost>=mxCost ){ |
| 109544 | +#ifdef WHERETRACE_ENABLED |
| 109545 | + if( sqlite3WhereTrace&0x4 ){ |
| 109546 | + sqlite3DebugPrintf("Skip %s cost=%3d order=%c\n", |
| 109547 | + wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 109548 | + isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 109549 | + } |
| 109550 | +#endif |
| 109551 | + continue; |
| 109552 | + } |
| 109553 | + /* Add a new Path to the aTo[] set */ |
| 109554 | + if( nTo<mxChoice ){ |
| 109555 | + /* Increase the size of the aTo set by one */ |
| 109556 | + jj = nTo++; |
| 109557 | + }else{ |
| 109558 | + /* New path replaces the prior worst to keep count below mxChoice */ |
| 109559 | + for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); } |
| 109560 | + } |
| 109561 | + pTo = &aTo[jj]; |
| 109562 | +#ifdef WHERETRACE_ENABLED |
| 109563 | + if( sqlite3WhereTrace&0x4 ){ |
| 109564 | + sqlite3DebugPrintf("New %s cost=%-3d order=%c\n", |
| 109565 | + wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 109566 | + isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 109567 | + } |
| 109568 | +#endif |
| 109569 | + }else{ |
| 109570 | + if( pTo->rCost<=rCost ){ |
| 109571 | +#ifdef WHERETRACE_ENABLED |
| 109572 | + if( sqlite3WhereTrace&0x4 ){ |
| 109573 | + sqlite3DebugPrintf( |
| 109574 | + "Skip %s cost=%-3d order=%c", |
| 109575 | + wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 109576 | + isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 109577 | + sqlite3DebugPrintf(" vs %s cost=%-3d order=%c\n", |
| 109578 | + wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 109579 | + pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 109580 | + } |
| 109581 | +#endif |
| 109582 | + testcase( pTo->rCost==rCost ); |
| 109583 | + continue; |
| 109584 | + } |
| 109585 | + testcase( pTo->rCost==rCost+1 ); |
| 109586 | + /* A new and better score for a previously created equivalent path */ |
| 109587 | +#ifdef WHERETRACE_ENABLED |
| 109588 | + if( sqlite3WhereTrace&0x4 ){ |
| 109589 | + sqlite3DebugPrintf( |
| 109590 | + "Update %s cost=%-3d order=%c", |
| 109591 | + wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 109592 | + isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 109593 | + sqlite3DebugPrintf(" was %s cost=%-3d order=%c\n", |
| 109594 | + wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 109595 | + pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 109596 | + } |
| 109597 | +#endif |
| 109598 | + } |
| 109599 | + /* pWLoop is a winner. Add it to the set of best so far */ |
| 109600 | + pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
| 109601 | + pTo->revLoop = revMask; |
| 109602 | + pTo->nRow = pFrom->nRow + pWLoop->nOut; |
| 109603 | + pTo->rCost = rCost; |
| 109604 | + pTo->isOrderedValid = isOrderedValid; |
| 109605 | + pTo->isOrdered = isOrdered; |
| 109606 | + memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 109607 | + pTo->aLoop[iLoop] = pWLoop; |
| 109608 | + if( nTo>=mxChoice ){ |
| 109609 | + mxCost = aTo[0].rCost; |
| 109610 | + for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
| 109611 | + if( pTo->rCost>mxCost ) mxCost = pTo->rCost; |
| 109612 | + } |
| 109613 | + } |
| 109614 | + } |
| 109615 | + } |
| 109616 | + |
| 109617 | +#ifdef WHERETRACE_ENABLED |
| 109618 | + if( sqlite3WhereTrace>=2 ){ |
| 109619 | + sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
| 109620 | + for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
| 109621 | + sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
| 109622 | + wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 109623 | + pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 109624 | + if( pTo->isOrderedValid && pTo->isOrdered ){ |
| 109625 | + sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 109626 | + }else{ |
| 109627 | + sqlite3DebugPrintf("\n"); |
| 109628 | + } |
| 109629 | + } |
| 109630 | + } |
| 109631 | +#endif |
| 109632 | + |
| 109633 | + /* Swap the roles of aFrom and aTo for the next generation */ |
| 109634 | + pFrom = aTo; |
| 109635 | + aTo = aFrom; |
| 109636 | + aFrom = pFrom; |
| 109637 | + nFrom = nTo; |
| 109638 | + } |
| 109639 | + |
| 109640 | + if( nFrom==0 ){ |
| 109641 | + sqlite3ErrorMsg(pParse, "no query solution"); |
| 109642 | + sqlite3DbFree(db, pSpace); |
| 109643 | + return SQLITE_ERROR; |
| 109644 | + } |
| 109645 | + |
| 109646 | + /* Find the lowest cost path. pFrom will be left pointing to that path */ |
| 109647 | + pFrom = aFrom; |
| 109648 | + assert( nFrom==1 ); |
| 109649 | +#if 0 /* The following is needed if nFrom is ever more than 1 */ |
| 109650 | + for(ii=1; ii<nFrom; ii++){ |
| 109651 | + if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 109652 | + } |
| 109653 | +#endif |
| 109654 | + assert( pWInfo->nLevel==nLoop ); |
| 109655 | + /* Load the lowest cost path into pWInfo */ |
| 109656 | + for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 109657 | + WhereLevel *pLevel = pWInfo->a + iLoop; |
| 109658 | + pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
| 109659 | + pLevel->iFrom = pWLoop->iTab; |
| 109660 | + pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
| 109661 | + } |
| 109662 | + if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 109663 | + && pWInfo->pDistinct |
| 109664 | + && nRowEst |
| 109665 | + ){ |
| 109666 | + Bitmask notUsed; |
| 109667 | + int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinct, pFrom, |
| 109668 | + WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
| 109669 | + if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 109670 | + } |
| 109671 | + if( pFrom->isOrdered ){ |
| 109672 | + if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 109673 | + pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 109674 | + }else{ |
| 109675 | + pWInfo->bOBSat = 1; |
| 109676 | + pWInfo->revMask = pFrom->revLoop; |
| 109677 | + } |
| 109678 | + } |
| 109679 | + pWInfo->nRowOut = pFrom->nRow; |
| 109680 | + |
| 109681 | + /* Free temporary memory and return success */ |
| 109682 | + sqlite3DbFree(db, pSpace); |
| 109683 | + return SQLITE_OK; |
| 109684 | +} |
| 109685 | + |
| 109686 | +/* |
| 109687 | +** Most queries use only a single table (they are not joins) and have |
| 109688 | +** simple == constraints against indexed fields. This routine attempts |
| 109689 | +** to plan those simple cases using much less ceremony than the |
| 109690 | +** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 109691 | +** times for the common case. |
| 109692 | +** |
| 109693 | +** Return non-zero on success, if this query can be handled by this |
| 109694 | +** no-frills query planner. Return zero if this query needs the |
| 109695 | +** general-purpose query planner. |
| 109696 | +*/ |
| 109697 | +static int whereShortCut(WhereLoopBuilder *pBuilder){ |
| 109698 | + WhereInfo *pWInfo; |
| 109699 | + struct SrcList_item *pItem; |
| 109700 | + WhereClause *pWC; |
| 109701 | + WhereTerm *pTerm; |
| 109702 | + WhereLoop *pLoop; |
| 109703 | + int iCur; |
| 109704 | + int j; |
| 109705 | + Table *pTab; |
| 109706 | + Index *pIdx; |
| 109707 | + |
| 109708 | + pWInfo = pBuilder->pWInfo; |
| 109709 | + if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
| 109710 | + assert( pWInfo->pTabList->nSrc>=1 ); |
| 109711 | + pItem = pWInfo->pTabList->a; |
| 109712 | + pTab = pItem->pTab; |
| 109713 | + if( IsVirtual(pTab) ) return 0; |
| 109714 | + if( pItem->zIndex ) return 0; |
| 109715 | + iCur = pItem->iCursor; |
| 109716 | + pWC = &pWInfo->sWC; |
| 109717 | + pLoop = pBuilder->pNew; |
| 109718 | + pLoop->wsFlags = 0; |
| 109719 | + pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
| 109720 | + if( pTerm ){ |
| 109721 | + pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 109722 | + pLoop->aLTerm[0] = pTerm; |
| 109723 | + pLoop->nLTerm = 1; |
| 109724 | + pLoop->u.btree.nEq = 1; |
| 109725 | + /* TUNING: Cost of a rowid lookup is 10 */ |
| 109726 | + pLoop->rRun = 33; /* 33==whereCost(10) */ |
| 109727 | + }else{ |
| 109728 | + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 109729 | + if( pIdx->onError==OE_None ) continue; |
| 109730 | + for(j=0; j<pIdx->nColumn; j++){ |
| 109731 | + pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
| 109732 | + if( pTerm==0 ) break; |
| 109733 | + whereLoopResize(pWInfo->pParse->db, pLoop, j); |
| 109734 | + pLoop->aLTerm[j] = pTerm; |
| 109735 | + } |
| 109736 | + if( j!=pIdx->nColumn ) continue; |
| 109737 | + pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
| 109738 | + if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
| 109739 | + pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 109740 | + } |
| 109741 | + pLoop->nLTerm = j; |
| 109742 | + pLoop->u.btree.nEq = j; |
| 109743 | + pLoop->u.btree.pIndex = pIdx; |
| 109744 | + /* TUNING: Cost of a unique index lookup is 15 */ |
| 109745 | + pLoop->rRun = 39; /* 39==whereCost(15) */ |
| 109746 | + break; |
| 109747 | + } |
| 109748 | + } |
| 109749 | + if( pLoop->wsFlags ){ |
| 109750 | + pLoop->nOut = (WhereCost)1; |
| 109751 | + pWInfo->a[0].pWLoop = pLoop; |
| 109752 | + pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 109753 | + pWInfo->a[0].iTabCur = iCur; |
| 109754 | + pWInfo->nRowOut = 1; |
| 109755 | + if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1; |
| 109756 | + if( pWInfo->pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 109757 | +#ifdef SQLITE_DEBUG |
| 109758 | + pLoop->cId = '0'; |
| 109759 | +#endif |
| 109760 | + return 1; |
| 109761 | + } |
| 109762 | + return 0; |
| 109763 | +} |
| 109336 | 109764 | |
| 109337 | 109765 | /* |
| 109338 | 109766 | ** Generate the beginning of the loop used for WHERE clause processing. |
| 109339 | 109767 | ** The return value is a pointer to an opaque structure that contains |
| 109340 | 109768 | ** information needed to terminate the loop. Later, the calling routine |
| | @@ -109410,19 +109838,10 @@ |
| 109410 | 109838 | ** ORDER BY CLAUSE PROCESSING |
| 109411 | 109839 | ** |
| 109412 | 109840 | ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
| 109413 | 109841 | ** if there is one. If there is no ORDER BY clause or if this routine |
| 109414 | 109842 | ** 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 | 109843 | */ |
| 109425 | 109844 | SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( |
| 109426 | 109845 | Parse *pParse, /* The parser context */ |
| 109427 | 109846 | SrcList *pTabList, /* A list of all tables to be scanned */ |
| 109428 | 109847 | Expr *pWhere, /* The WHERE clause */ |
| | @@ -109434,22 +109853,21 @@ |
| 109434 | 109853 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| 109435 | 109854 | int nTabList; /* Number of elements in pTabList */ |
| 109436 | 109855 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 109437 | 109856 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 109438 | 109857 | Bitmask notReady; /* Cursors that are not yet positioned */ |
| 109439 | | - WhereBestIdx sWBI; /* Best index search context */ |
| 109858 | + WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
| 109440 | 109859 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
| 109441 | 109860 | 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 | 109861 | int ii; /* Loop counter */ |
| 109445 | 109862 | sqlite3 *db; /* Database connection */ |
| 109863 | + int rc; /* Return code */ |
| 109446 | 109864 | |
| 109447 | 109865 | |
| 109448 | 109866 | /* Variable initialization */ |
| 109449 | | - memset(&sWBI, 0, sizeof(sWBI)); |
| 109450 | | - sWBI.pParse = pParse; |
| 109867 | + memset(&sWLB, 0, sizeof(sWLB)); |
| 109868 | + sWLB.pOrderBy = pOrderBy; |
| 109451 | 109869 | |
| 109452 | 109870 | /* The number of tables in the FROM clause is limited by the number of |
| 109453 | 109871 | ** bits in a Bitmask |
| 109454 | 109872 | */ |
| 109455 | 109873 | testcase( pTabList->nSrc==BMS ); |
| | @@ -109472,49 +109890,59 @@ |
| 109472 | 109890 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 109473 | 109891 | ** some architectures. Hence the ROUND8() below. |
| 109474 | 109892 | */ |
| 109475 | 109893 | db = pParse->db; |
| 109476 | 109894 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
| 109477 | | - pWInfo = sqlite3DbMallocZero(db, |
| 109478 | | - nByteWInfo + |
| 109479 | | - sizeof(WhereClause) + |
| 109480 | | - sizeof(WhereMaskSet) |
| 109481 | | - ); |
| 109895 | + pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
| 109482 | 109896 | if( db->mallocFailed ){ |
| 109483 | 109897 | sqlite3DbFree(db, pWInfo); |
| 109484 | 109898 | pWInfo = 0; |
| 109485 | 109899 | goto whereBeginError; |
| 109486 | 109900 | } |
| 109487 | 109901 | pWInfo->nLevel = nTabList; |
| 109488 | 109902 | pWInfo->pParse = pParse; |
| 109489 | 109903 | pWInfo->pTabList = pTabList; |
| 109904 | + pWInfo->pOrderBy = pOrderBy; |
| 109905 | + pWInfo->pDistinct = pDistinct; |
| 109490 | 109906 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
| 109491 | | - pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo]; |
| 109492 | 109907 | pWInfo->wctrlFlags = wctrlFlags; |
| 109493 | 109908 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 109494 | | - pMaskSet = (WhereMaskSet*)&sWBI.pWC[1]; |
| 109495 | | - sWBI.aLevel = pWInfo->a; |
| 109909 | + pMaskSet = &pWInfo->sMaskSet; |
| 109910 | + sWLB.pWInfo = pWInfo; |
| 109911 | + sWLB.pWC = &pWInfo->sWC; |
| 109912 | + sWLB.pNew = (WhereLoop*)&pWInfo->a[nTabList]; |
| 109913 | + whereLoopInit(sWLB.pNew); |
| 109914 | +#ifdef SQLITE_DEBUG |
| 109915 | + sWLB.pNew->cId = '*'; |
| 109916 | +#endif |
| 109496 | 109917 | |
| 109497 | 109918 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 109498 | 109919 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 109499 | 109920 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0; |
| 109500 | 109921 | |
| 109501 | 109922 | /* Split the WHERE clause into separate subexpressions where each |
| 109502 | 109923 | ** subexpression is separated by an AND operator. |
| 109503 | 109924 | */ |
| 109504 | 109925 | initMaskSet(pMaskSet); |
| 109505 | | - whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags); |
| 109926 | + whereClauseInit(&pWInfo->sWC, pWInfo); |
| 109506 | 109927 | sqlite3ExprCodeConstants(pParse, pWhere); |
| 109507 | | - whereSplit(sWBI.pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
| 109928 | + whereSplit(&pWInfo->sWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
| 109508 | 109929 | |
| 109509 | 109930 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 109510 | 109931 | ** expression and either jump over all of the code or fall thru. |
| 109511 | 109932 | */ |
| 109512 | 109933 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
| 109513 | 109934 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
| 109514 | 109935 | pWhere = 0; |
| 109515 | 109936 | } |
| 109937 | + |
| 109938 | + /* Special case: No FROM clause |
| 109939 | + */ |
| 109940 | + if( nTabList==0 ){ |
| 109941 | + if( pOrderBy ) pWInfo->bOBSat = 1; |
| 109942 | + if( pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 109943 | + } |
| 109516 | 109944 | |
| 109517 | 109945 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 109518 | 109946 | ** |
| 109519 | 109947 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 109520 | 109948 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| | @@ -109547,337 +109975,153 @@ |
| 109547 | 109975 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 109548 | 109976 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 109549 | 109977 | ** want to analyze these virtual terms, so start analyzing at the end |
| 109550 | 109978 | ** and work forward so that the added virtual terms are never processed. |
| 109551 | 109979 | */ |
| 109552 | | - exprAnalyzeAll(pTabList, sWBI.pWC); |
| 109980 | + exprAnalyzeAll(pTabList, &pWInfo->sWC); |
| 109553 | 109981 | if( db->mallocFailed ){ |
| 109554 | 109982 | goto whereBeginError; |
| 109555 | 109983 | } |
| 109984 | + |
| 109985 | + /* If the ORDER BY (or GROUP BY) clause contains references to general |
| 109986 | + ** expressions, then we won't be able to satisfy it using indices, so |
| 109987 | + ** go ahead and disable it now. |
| 109988 | + */ |
| 109989 | + if( pOrderBy && pDistinct ){ |
| 109990 | + for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 109991 | + Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr); |
| 109992 | + if( pExpr->op!=TK_COLUMN ){ |
| 109993 | + pWInfo->pOrderBy = pOrderBy = 0; |
| 109994 | + break; |
| 109995 | + }else if( pExpr->iColumn<0 ){ |
| 109996 | + break; |
| 109997 | + } |
| 109998 | + } |
| 109999 | + } |
| 109556 | 110000 | |
| 109557 | 110001 | /* Check if the DISTINCT qualifier, if there is one, is redundant. |
| 109558 | 110002 | ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to |
| 109559 | 110003 | ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT. |
| 109560 | 110004 | */ |
| 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")); |
| 109817 | | - if( pParse->nErr || db->mallocFailed ){ |
| 109818 | | - goto whereBeginError; |
| 109819 | | - } |
| 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 | | - } |
| 110005 | + if( pDistinct ){ |
| 110006 | + if( isDistinctRedundant(pParse,pTabList,&pWInfo->sWC,pDistinct) ){ |
| 110007 | + pDistinct = 0; |
| 110008 | + pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 110009 | + }else if( pOrderBy==0 ){ |
| 110010 | + pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
| 110011 | + pWInfo->pOrderBy = pDistinct; |
| 110012 | + } |
| 110013 | + } |
| 110014 | + |
| 110015 | + /* Construct the WhereLoop objects */ |
| 110016 | + WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
| 110017 | + if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
| 110018 | + rc = whereLoopAddAll(&sWLB); |
| 110019 | + if( rc ) goto whereBeginError; |
| 110020 | + |
| 110021 | + /* Display all of the WhereLoop objects if wheretrace is enabled */ |
| 110022 | +#ifdef WHERETRACE_ENABLED |
| 110023 | + if( sqlite3WhereTrace ){ |
| 110024 | + WhereLoop *p; |
| 110025 | + int i = 0; |
| 110026 | + static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 110027 | + "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
| 110028 | + for(p=pWInfo->pLoops; p; p=p->pNextLoop){ |
| 110029 | + p->cId = zLabel[(i++)%sizeof(zLabel)]; |
| 110030 | + whereLoopPrint(p, pTabList); |
| 110031 | + } |
| 110032 | + } |
| 110033 | +#endif |
| 110034 | + |
| 110035 | + wherePathSolver(pWInfo, 0); |
| 110036 | + if( db->mallocFailed ) goto whereBeginError; |
| 110037 | + if( pWInfo->pOrderBy ){ |
| 110038 | + wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
| 110039 | + if( db->mallocFailed ) goto whereBeginError; |
| 110040 | + } |
| 110041 | + } |
| 110042 | + if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
| 110043 | + pWInfo->revMask = (Bitmask)(-1); |
| 110044 | + } |
| 110045 | + if( pParse->nErr || NEVER(db->mallocFailed) ){ |
| 110046 | + goto whereBeginError; |
| 110047 | + } |
| 110048 | +#ifdef WHERETRACE_ENABLED |
| 110049 | + if( sqlite3WhereTrace ){ |
| 110050 | + int ii; |
| 110051 | + sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 110052 | + if( pWInfo->bOBSat ){ |
| 110053 | + sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask); |
| 110054 | + } |
| 110055 | + switch( pWInfo->eDistinct ){ |
| 110056 | + case WHERE_DISTINCT_UNIQUE: { |
| 110057 | + sqlite3DebugPrintf(" DISTINCT=unique"); |
| 110058 | + break; |
| 110059 | + } |
| 110060 | + case WHERE_DISTINCT_ORDERED: { |
| 110061 | + sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 110062 | + break; |
| 110063 | + } |
| 110064 | + case WHERE_DISTINCT_UNORDERED: { |
| 110065 | + sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 110066 | + break; |
| 110067 | + } |
| 110068 | + } |
| 110069 | + sqlite3DebugPrintf("\n"); |
| 110070 | + for(ii=0; ii<nTabList; ii++){ |
| 110071 | + whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList); |
| 110072 | + } |
| 110073 | + } |
| 110074 | +#endif |
| 110075 | + WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
| 110076 | + pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
| 109834 | 110077 | |
| 109835 | 110078 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 109836 | 110079 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 109837 | 110080 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 109838 | 110081 | ** the statement to update a single row. |
| 109839 | 110082 | */ |
| 109840 | 110083 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
| 109841 | | - if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){ |
| 110084 | + if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 110085 | + && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
| 109842 | 110086 | pWInfo->okOnePass = 1; |
| 109843 | | - pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY; |
| 110087 | + pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 109844 | 110088 | } |
| 109845 | 110089 | |
| 109846 | 110090 | /* Open all tables in the pTabList and any indices selected for |
| 109847 | 110091 | ** searching those tables. |
| 109848 | 110092 | */ |
| 109849 | 110093 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 109850 | 110094 | notReady = ~(Bitmask)0; |
| 109851 | | - pWInfo->nRowOut = (double)1; |
| 109852 | 110095 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
| 109853 | 110096 | Table *pTab; /* Table to open */ |
| 109854 | 110097 | int iDb; /* Index of database containing table/index */ |
| 109855 | 110098 | struct SrcList_item *pTabItem; |
| 110099 | + WhereLoop *pLoop; |
| 109856 | 110100 | |
| 109857 | 110101 | pTabItem = &pTabList->a[pLevel->iFrom]; |
| 109858 | 110102 | pTab = pTabItem->pTab; |
| 109859 | | - pWInfo->nRowOut *= pLevel->plan.nRow; |
| 109860 | 110103 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 110104 | + pLoop = pLevel->pWLoop; |
| 109861 | 110105 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
| 109862 | 110106 | /* Do nothing */ |
| 109863 | 110107 | }else |
| 109864 | 110108 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 109865 | | - if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 110109 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 109866 | 110110 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 109867 | 110111 | int iCur = pTabItem->iCursor; |
| 109868 | 110112 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| 109869 | 110113 | }else if( IsVirtual(pTab) ){ |
| 109870 | 110114 | /* noop */ |
| 109871 | 110115 | }else |
| 109872 | 110116 | #endif |
| 109873 | | - if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 |
| 110117 | + if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 109874 | 110118 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
| 109875 | 110119 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 109876 | 110120 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 109877 | | - testcase( pTab->nCol==BMS-1 ); |
| 109878 | | - testcase( pTab->nCol==BMS ); |
| 110121 | + testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); |
| 110122 | + testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); |
| 109879 | 110123 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
| 109880 | 110124 | Bitmask b = pTabItem->colUsed; |
| 109881 | 110125 | int n = 0; |
| 109882 | 110126 | for(; b; b=b>>1, n++){} |
| 109883 | 110127 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| | @@ -109886,26 +110130,27 @@ |
| 109886 | 110130 | } |
| 109887 | 110131 | }else{ |
| 109888 | 110132 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 109889 | 110133 | } |
| 109890 | 110134 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 109891 | | - if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 109892 | | - constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel); |
| 110135 | + if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 110136 | + constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel); |
| 109893 | 110137 | }else |
| 109894 | 110138 | #endif |
| 109895 | | - if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 109896 | | - Index *pIx = pLevel->plan.u.pIdx; |
| 110139 | + if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 110140 | + Index *pIx = pLoop->u.btree.pIndex; |
| 109897 | 110141 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
| 109898 | | - int iIndexCur = pLevel->iIdxCur; |
| 110142 | + /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */ |
| 110143 | + int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++; |
| 109899 | 110144 | assert( pIx->pSchema==pTab->pSchema ); |
| 109900 | 110145 | assert( iIndexCur>=0 ); |
| 109901 | 110146 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
| 109902 | 110147 | (char*)pKey, P4_KEYINFO_HANDOFF); |
| 109903 | 110148 | VdbeComment((v, "%s", pIx->zName)); |
| 109904 | 110149 | } |
| 109905 | 110150 | sqlite3CodeVerifySchema(pParse, iDb); |
| 109906 | | - notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor); |
| 110151 | + notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
| 109907 | 110152 | } |
| 109908 | 110153 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 109909 | 110154 | if( db->mallocFailed ) goto whereBeginError; |
| 109910 | 110155 | |
| 109911 | 110156 | /* Generate the code to do the search. Each iteration of the for |
| | @@ -109914,70 +110159,15 @@ |
| 109914 | 110159 | */ |
| 109915 | 110160 | notReady = ~(Bitmask)0; |
| 109916 | 110161 | for(ii=0; ii<nTabList; ii++){ |
| 109917 | 110162 | pLevel = &pWInfo->a[ii]; |
| 109918 | 110163 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
| 109919 | | - notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady); |
| 110164 | + notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 109920 | 110165 | pWInfo->iContinue = pLevel->addrCont; |
| 109921 | 110166 | } |
| 109922 | 110167 | |
| 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 | | - */ |
| 110168 | + /* Done. */ |
| 109979 | 110169 | return pWInfo; |
| 109980 | 110170 | |
| 109981 | 110171 | /* Jump here if malloc fails */ |
| 109982 | 110172 | whereBeginError: |
| 109983 | 110173 | if( pWInfo ){ |
| | @@ -109994,24 +110184,26 @@ |
| 109994 | 110184 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ |
| 109995 | 110185 | Parse *pParse = pWInfo->pParse; |
| 109996 | 110186 | Vdbe *v = pParse->pVdbe; |
| 109997 | 110187 | int i; |
| 109998 | 110188 | WhereLevel *pLevel; |
| 110189 | + WhereLoop *pLoop; |
| 109999 | 110190 | SrcList *pTabList = pWInfo->pTabList; |
| 110000 | 110191 | sqlite3 *db = pParse->db; |
| 110001 | 110192 | |
| 110002 | 110193 | /* Generate loop termination code. |
| 110003 | 110194 | */ |
| 110004 | 110195 | sqlite3ExprCacheClear(pParse); |
| 110005 | 110196 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
| 110006 | 110197 | pLevel = &pWInfo->a[i]; |
| 110198 | + pLoop = pLevel->pWLoop; |
| 110007 | 110199 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
| 110008 | 110200 | if( pLevel->op!=OP_Noop ){ |
| 110009 | 110201 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
| 110010 | 110202 | sqlite3VdbeChangeP5(v, pLevel->p5); |
| 110011 | 110203 | } |
| 110012 | | - if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
| 110204 | + if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
| 110013 | 110205 | struct InLoop *pIn; |
| 110014 | 110206 | int j; |
| 110015 | 110207 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
| 110016 | 110208 | for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
| 110017 | 110209 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| | @@ -110022,16 +110214,16 @@ |
| 110022 | 110214 | } |
| 110023 | 110215 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
| 110024 | 110216 | if( pLevel->iLeftJoin ){ |
| 110025 | 110217 | int addr; |
| 110026 | 110218 | 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 ){ |
| 110219 | + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 110220 | + || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 110221 | + if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
| 110030 | 110222 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 110031 | 110223 | } |
| 110032 | | - if( pLevel->iIdxCur>=0 ){ |
| 110224 | + if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 110033 | 110225 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
| 110034 | 110226 | } |
| 110035 | 110227 | if( pLevel->op==OP_Return ){ |
| 110036 | 110228 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 110037 | 110229 | }else{ |
| | @@ -110052,42 +110244,41 @@ |
| 110052 | 110244 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
| 110053 | 110245 | Index *pIdx = 0; |
| 110054 | 110246 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
| 110055 | 110247 | Table *pTab = pTabItem->pTab; |
| 110056 | 110248 | assert( pTab!=0 ); |
| 110249 | + pLoop = pLevel->pWLoop; |
| 110057 | 110250 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 110058 | 110251 | && pTab->pSelect==0 |
| 110059 | 110252 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
| 110060 | 110253 | ){ |
| 110061 | | - int ws = pLevel->plan.wsFlags; |
| 110254 | + int ws = pLoop->wsFlags; |
| 110062 | 110255 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
| 110063 | 110256 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 110064 | 110257 | } |
| 110065 | | - if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){ |
| 110258 | + if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_TEMP_INDEX))==0 ){ |
| 110066 | 110259 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 110067 | 110260 | } |
| 110068 | 110261 | } |
| 110069 | 110262 | |
| 110070 | | - /* If this scan uses an index, make code substitutions to read data |
| 110071 | | - ** from the index in preference to the table. Sometimes, this means |
| 110072 | | - ** the table need never be read from. This is a performance boost, |
| 110073 | | - ** as the vdbe level waits until the table is read before actually |
| 110074 | | - ** seeking the table cursor to the record corresponding to the current |
| 110075 | | - ** position in the index. |
| 110263 | + /* If this scan uses an index, make VDBE code substitutions to read data |
| 110264 | + ** from the index instead of from the table where possible. In some cases |
| 110265 | + ** this optimization prevents the table from ever being read, which can |
| 110266 | + ** yield a significant performance boost. |
| 110076 | 110267 | ** |
| 110077 | 110268 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 110078 | 110269 | ** sqlite3WhereEnd will have created code that references the table |
| 110079 | 110270 | ** directly. This loop scans all that code looking for opcodes |
| 110080 | 110271 | ** that reference the table and converts them into opcodes that |
| 110081 | 110272 | ** reference the index. |
| 110082 | 110273 | */ |
| 110083 | | - if( pLevel->plan.wsFlags & WHERE_INDEXED ){ |
| 110084 | | - pIdx = pLevel->plan.u.pIdx; |
| 110085 | | - }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){ |
| 110274 | + if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 110275 | + pIdx = pLoop->u.btree.pIndex; |
| 110276 | + }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 110086 | 110277 | pIdx = pLevel->u.pCovidx; |
| 110087 | 110278 | } |
| 110088 | | - if( pIdx && !db->mallocFailed){ |
| 110279 | + if( pIdx && !db->mallocFailed ){ |
| 110089 | 110280 | int k, j, last; |
| 110090 | 110281 | VdbeOp *pOp; |
| 110091 | 110282 | |
| 110092 | 110283 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 110093 | 110284 | last = sqlite3VdbeCurrentAddr(v); |
| | @@ -110099,12 +110290,11 @@ |
| 110099 | 110290 | pOp->p2 = j; |
| 110100 | 110291 | pOp->p1 = pLevel->iIdxCur; |
| 110101 | 110292 | break; |
| 110102 | 110293 | } |
| 110103 | 110294 | } |
| 110104 | | - assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 |
| 110105 | | - || j<pIdx->nColumn ); |
| 110295 | + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn ); |
| 110106 | 110296 | }else if( pOp->opcode==OP_Rowid ){ |
| 110107 | 110297 | pOp->p1 = pLevel->iIdxCur; |
| 110108 | 110298 | pOp->opcode = OP_IdxRowid; |
| 110109 | 110299 | } |
| 110110 | 110300 | } |
| | @@ -115480,11 +115670,11 @@ |
| 115480 | 115670 | } |
| 115481 | 115671 | |
| 115482 | 115672 | /* |
| 115483 | 115673 | ** Another built-in collating sequence: NOCASE. |
| 115484 | 115674 | ** |
| 115485 | | -** This collating sequence is intended to be used for "case independant |
| 115675 | +** This collating sequence is intended to be used for "case independent |
| 115486 | 115676 | ** comparison". SQLite's knowledge of upper and lower case equivalents |
| 115487 | 115677 | ** extends only to the 26 characters used in the English language. |
| 115488 | 115678 | ** |
| 115489 | 115679 | ** At the moment there is only a UTF-8 implementation. |
| 115490 | 115680 | */ |
| | @@ -115627,16 +115817,10 @@ |
| 115627 | 115817 | "statements or unfinished backups"); |
| 115628 | 115818 | sqlite3_mutex_leave(db->mutex); |
| 115629 | 115819 | return SQLITE_BUSY; |
| 115630 | 115820 | } |
| 115631 | 115821 | |
| 115632 | | - /* If a transaction is open, roll it back. This also ensures that if |
| 115633 | | - ** any database schemas have been modified by the current transaction |
| 115634 | | - ** they are reset. And that the required b-tree mutex is held to make |
| 115635 | | - ** the the pager rollback and schema reset an atomic operation. */ |
| 115636 | | - sqlite3RollbackAll(db, SQLITE_OK); |
| 115637 | | - |
| 115638 | 115822 | #ifdef SQLITE_ENABLE_SQLLOG |
| 115639 | 115823 | if( sqlite3GlobalConfig.xSqllog ){ |
| 115640 | 115824 | /* Closing the handle. Fourth parameter is passed the value 2. */ |
| 115641 | 115825 | sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2); |
| 115642 | 115826 | } |
| | @@ -115686,10 +115870,16 @@ |
| 115686 | 115870 | /* If we reach this point, it means that the database connection has |
| 115687 | 115871 | ** closed all sqlite3_stmt and sqlite3_backup objects and has been |
| 115688 | 115872 | ** passed to sqlite3_close (meaning that it is a zombie). Therefore, |
| 115689 | 115873 | ** go ahead and free all resources. |
| 115690 | 115874 | */ |
| 115875 | + |
| 115876 | + /* If a transaction is open, roll it back. This also ensures that if |
| 115877 | + ** any database schemas have been modified by an uncommitted transaction |
| 115878 | + ** they are reset. And that the required b-tree mutex is held to make |
| 115879 | + ** the pager rollback and schema reset an atomic operation. */ |
| 115880 | + sqlite3RollbackAll(db, SQLITE_OK); |
| 115691 | 115881 | |
| 115692 | 115882 | /* Free any outstanding Savepoint structures. */ |
| 115693 | 115883 | sqlite3CloseSavepoints(db); |
| 115694 | 115884 | |
| 115695 | 115885 | /* Close all database connections */ |
| | @@ -115787,19 +115977,26 @@ |
| 115787 | 115977 | SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){ |
| 115788 | 115978 | int i; |
| 115789 | 115979 | int inTrans = 0; |
| 115790 | 115980 | assert( sqlite3_mutex_held(db->mutex) ); |
| 115791 | 115981 | sqlite3BeginBenignMalloc(); |
| 115982 | + |
| 115983 | + /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). |
| 115984 | + ** This is important in case the transaction being rolled back has |
| 115985 | + ** modified the database schema. If the b-tree mutexes are not taken |
| 115986 | + ** here, then another shared-cache connection might sneak in between |
| 115987 | + ** the database rollback and schema reset, which can cause false |
| 115988 | + ** corruption reports in some cases. */ |
| 115792 | 115989 | sqlite3BtreeEnterAll(db); |
| 115990 | + |
| 115793 | 115991 | for(i=0; i<db->nDb; i++){ |
| 115794 | 115992 | Btree *p = db->aDb[i].pBt; |
| 115795 | 115993 | if( p ){ |
| 115796 | 115994 | if( sqlite3BtreeIsInTrans(p) ){ |
| 115797 | 115995 | inTrans = 1; |
| 115798 | 115996 | } |
| 115799 | 115997 | sqlite3BtreeRollback(p, tripCode); |
| 115800 | | - db->aDb[i].inTrans = 0; |
| 115801 | 115998 | } |
| 115802 | 115999 | } |
| 115803 | 116000 | sqlite3VtabRollback(db); |
| 115804 | 116001 | sqlite3EndBenignMalloc(); |
| 115805 | 116002 | |
| | @@ -117562,12 +117759,10 @@ |
| 117562 | 117759 | /* |
| 117563 | 117760 | ** Test to see whether or not the database connection is in autocommit |
| 117564 | 117761 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 117565 | 117762 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 117566 | 117763 | ** by the next COMMIT or ROLLBACK. |
| 117567 | | -** |
| 117568 | | -******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** |
| 117569 | 117764 | */ |
| 117570 | 117765 | SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 117571 | 117766 | return db->autoCommit; |
| 117572 | 117767 | } |
| 117573 | 117768 | |
| | @@ -119051,10 +119246,22 @@ |
| 119051 | 119246 | |
| 119052 | 119247 | #endif /* _FTS3_HASH_H_ */ |
| 119053 | 119248 | |
| 119054 | 119249 | /************** End of fts3_hash.h *******************************************/ |
| 119055 | 119250 | /************** Continuing where we left off in fts3Int.h ********************/ |
| 119251 | + |
| 119252 | +/* |
| 119253 | +** This constant determines the maximum depth of an FTS expression tree |
| 119254 | +** that the library will create and use. FTS uses recursion to perform |
| 119255 | +** various operations on the query tree, so the disadvantage of a large |
| 119256 | +** limit is that it may allow very large queries to use large amounts |
| 119257 | +** of stack space (perhaps causing a stack overflow). |
| 119258 | +*/ |
| 119259 | +#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH |
| 119260 | +# define SQLITE_FTS3_MAX_EXPR_DEPTH 12 |
| 119261 | +#endif |
| 119262 | + |
| 119056 | 119263 | |
| 119057 | 119264 | /* |
| 119058 | 119265 | ** This constant controls how often segments are merged. Once there are |
| 119059 | 119266 | ** FTS3_MERGE_COUNT segments of level N, they are merged into a single |
| 119060 | 119267 | ** segment of level N+1. |
| | @@ -120709,11 +120916,11 @@ |
| 120709 | 120916 | /* By default use a full table scan. This is an expensive option, |
| 120710 | 120917 | ** so search through the constraints to see if a more efficient |
| 120711 | 120918 | ** strategy is possible. |
| 120712 | 120919 | */ |
| 120713 | 120920 | pInfo->idxNum = FTS3_FULLSCAN_SEARCH; |
| 120714 | | - pInfo->estimatedCost = 500000; |
| 120921 | + pInfo->estimatedCost = 5000000; |
| 120715 | 120922 | for(i=0; i<pInfo->nConstraint; i++){ |
| 120716 | 120923 | struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i]; |
| 120717 | 120924 | if( pCons->usable==0 ) continue; |
| 120718 | 120925 | |
| 120719 | 120926 | /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */ |
| | @@ -122270,15 +122477,11 @@ |
| 122270 | 122477 | ); |
| 122271 | 122478 | if( rc!=SQLITE_OK ){ |
| 122272 | 122479 | return rc; |
| 122273 | 122480 | } |
| 122274 | 122481 | |
| 122275 | | - rc = sqlite3Fts3ReadLock(p); |
| 122276 | | - if( rc!=SQLITE_OK ) return rc; |
| 122277 | | - |
| 122278 | 122482 | rc = fts3EvalStart(pCsr); |
| 122279 | | - |
| 122280 | 122483 | sqlite3Fts3SegmentsClose(p); |
| 122281 | 122484 | if( rc!=SQLITE_OK ) return rc; |
| 122282 | 122485 | pCsr->pNextId = pCsr->aDoclist; |
| 122283 | 122486 | pCsr->iPrevId = 0; |
| 122284 | 122487 | } |
| | @@ -126129,30 +126332,30 @@ |
| 126129 | 126332 | int iDefaultCol, /* Default column to query */ |
| 126130 | 126333 | const char *z, int n, /* Text of MATCH query */ |
| 126131 | 126334 | Fts3Expr **ppExpr, /* OUT: Parsed query structure */ |
| 126132 | 126335 | char **pzErr /* OUT: Error message (sqlite3_malloc) */ |
| 126133 | 126336 | ){ |
| 126134 | | - static const int MAX_EXPR_DEPTH = 12; |
| 126135 | 126337 | int rc = fts3ExprParseUnbalanced( |
| 126136 | 126338 | pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr |
| 126137 | 126339 | ); |
| 126138 | 126340 | |
| 126139 | 126341 | /* Rebalance the expression. And check that its depth does not exceed |
| 126140 | | - ** MAX_EXPR_DEPTH. */ |
| 126342 | + ** SQLITE_FTS3_MAX_EXPR_DEPTH. */ |
| 126141 | 126343 | if( rc==SQLITE_OK && *ppExpr ){ |
| 126142 | | - rc = fts3ExprBalance(ppExpr, MAX_EXPR_DEPTH); |
| 126344 | + rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); |
| 126143 | 126345 | if( rc==SQLITE_OK ){ |
| 126144 | | - rc = fts3ExprCheckDepth(*ppExpr, MAX_EXPR_DEPTH); |
| 126346 | + rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); |
| 126145 | 126347 | } |
| 126146 | 126348 | } |
| 126147 | 126349 | |
| 126148 | 126350 | if( rc!=SQLITE_OK ){ |
| 126149 | 126351 | sqlite3Fts3ExprFree(*ppExpr); |
| 126150 | 126352 | *ppExpr = 0; |
| 126151 | 126353 | if( rc==SQLITE_TOOBIG ){ |
| 126152 | 126354 | *pzErr = sqlite3_mprintf( |
| 126153 | | - "FTS expression tree is too large (maximum depth %d)", MAX_EXPR_DEPTH |
| 126355 | + "FTS expression tree is too large (maximum depth %d)", |
| 126356 | + SQLITE_FTS3_MAX_EXPR_DEPTH |
| 126154 | 126357 | ); |
| 126155 | 126358 | rc = SQLITE_ERROR; |
| 126156 | 126359 | }else if( rc==SQLITE_ERROR ){ |
| 126157 | 126360 | *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z); |
| 126158 | 126361 | } |
| | @@ -129110,41 +129313,34 @@ |
| 129110 | 129313 | *pRC = rc; |
| 129111 | 129314 | } |
| 129112 | 129315 | |
| 129113 | 129316 | |
| 129114 | 129317 | /* |
| 129115 | | -** This function ensures that the caller has obtained a shared-cache |
| 129116 | | -** table-lock on the %_content table. This is required before reading |
| 129117 | | -** data from the fts3 table. If this lock is not acquired first, then |
| 129118 | | -** the caller may end up holding read-locks on the %_segments and %_segdir |
| 129119 | | -** tables, but no read-lock on the %_content table. If this happens |
| 129120 | | -** a second connection will be able to write to the fts3 table, but |
| 129121 | | -** attempting to commit those writes might return SQLITE_LOCKED or |
| 129122 | | -** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain |
| 129123 | | -** write-locks on the %_segments and %_segdir ** tables). |
| 129124 | | -** |
| 129125 | | -** We try to avoid this because if FTS3 returns any error when committing |
| 129126 | | -** a transaction, the whole transaction will be rolled back. And this is |
| 129127 | | -** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can |
| 129128 | | -** still happen if the user reads data directly from the %_segments or |
| 129129 | | -** %_segdir tables instead of going through FTS3 though. |
| 129130 | | -** |
| 129131 | | -** This reasoning does not apply to a content=xxx table. |
| 129132 | | -*/ |
| 129133 | | -SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){ |
| 129134 | | - int rc; /* Return code */ |
| 129135 | | - sqlite3_stmt *pStmt; /* Statement used to obtain lock */ |
| 129136 | | - |
| 129137 | | - if( p->zContentTbl==0 ){ |
| 129138 | | - rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0); |
| 129318 | +** This function ensures that the caller has obtained an exclusive |
| 129319 | +** shared-cache table-lock on the %_segdir table. This is required before |
| 129320 | +** writing data to the fts3 table. If this lock is not acquired first, then |
| 129321 | +** the caller may end up attempting to take this lock as part of committing |
| 129322 | +** a transaction, causing SQLite to return SQLITE_LOCKED or |
| 129323 | +** LOCKED_SHAREDCACHEto a COMMIT command. |
| 129324 | +** |
| 129325 | +** It is best to avoid this because if FTS3 returns any error when |
| 129326 | +** committing a transaction, the whole transaction will be rolled back. |
| 129327 | +** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. |
| 129328 | +** It can still happen if the user locks the underlying tables directly |
| 129329 | +** instead of accessing them via FTS. |
| 129330 | +*/ |
| 129331 | +static int fts3Writelock(Fts3Table *p){ |
| 129332 | + int rc = SQLITE_OK; |
| 129333 | + |
| 129334 | + if( p->nPendingData==0 ){ |
| 129335 | + sqlite3_stmt *pStmt; |
| 129336 | + rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0); |
| 129139 | 129337 | if( rc==SQLITE_OK ){ |
| 129140 | 129338 | sqlite3_bind_null(pStmt, 1); |
| 129141 | 129339 | sqlite3_step(pStmt); |
| 129142 | 129340 | rc = sqlite3_reset(pStmt); |
| 129143 | 129341 | } |
| 129144 | | - }else{ |
| 129145 | | - rc = SQLITE_OK; |
| 129146 | 129342 | } |
| 129147 | 129343 | |
| 129148 | 129344 | return rc; |
| 129149 | 129345 | } |
| 129150 | 129346 | |
| | @@ -133918,10 +134114,13 @@ |
| 133918 | 134114 | goto update_out; |
| 133919 | 134115 | } |
| 133920 | 134116 | aSzIns = &aSzDel[p->nColumn+1]; |
| 133921 | 134117 | memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2); |
| 133922 | 134118 | |
| 134119 | + rc = fts3Writelock(p); |
| 134120 | + if( rc!=SQLITE_OK ) goto update_out; |
| 134121 | + |
| 133923 | 134122 | /* If this is an INSERT operation, or an UPDATE that modifies the rowid |
| 133924 | 134123 | ** value, then this operation requires constraint handling. |
| 133925 | 134124 | ** |
| 133926 | 134125 | ** If the on-conflict mode is REPLACE, this means that the existing row |
| 133927 | 134126 | ** should be deleted from the database before inserting the new row. Or, |
| | @@ -136051,32 +136250,31 @@ |
| 136051 | 136250 | 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004, |
| 136052 | 136251 | 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002, |
| 136053 | 136252 | 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803, |
| 136054 | 136253 | 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07, |
| 136055 | 136254 | 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02, |
| 136056 | | - 0x037FFC02, 0x03E3FC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, |
| 136057 | | - 0x03F4F802, 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, |
| 136058 | | - 0x03F95013, 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, |
| 136059 | | - 0x03FCEC06, 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, |
| 136060 | | - 0x04040003, 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, |
| 136061 | | - 0x040E7C01, 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, |
| 136062 | | - 0x04280403, 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, |
| 136063 | | - 0x04294009, 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, |
| 136064 | | - 0x04420003, 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, |
| 136065 | | - 0x04460003, 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, |
| 136066 | | - 0x05BD442E, 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, |
| 136067 | | - 0x07480046, 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, |
| 136068 | | - 0x075C5401, 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, |
| 136069 | | - 0x075EA401, 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, |
| 136070 | | - 0x07C2800F, 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, |
| 136071 | | - 0x07C4C03C, 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, |
| 136072 | | - 0x07C94002, 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, |
| 136073 | | - 0x07CE8025, 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, |
| 136074 | | - 0x07D108B6, 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, |
| 136075 | | - 0x07D7EC46, 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, |
| 136076 | | - 0x38008060, 0x380400F0, 0x3C000001, 0x3FFFF401, 0x40000001, |
| 136077 | | - 0x43FFF401, |
| 136255 | + 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802, |
| 136256 | + 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013, |
| 136257 | + 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06, |
| 136258 | + 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003, |
| 136259 | + 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01, |
| 136260 | + 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403, |
| 136261 | + 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009, |
| 136262 | + 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003, |
| 136263 | + 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003, |
| 136264 | + 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E, |
| 136265 | + 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046, |
| 136266 | + 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401, |
| 136267 | + 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401, |
| 136268 | + 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F, |
| 136269 | + 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C, |
| 136270 | + 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002, |
| 136271 | + 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025, |
| 136272 | + 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6, |
| 136273 | + 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46, |
| 136274 | + 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060, |
| 136275 | + 0x380400F0, |
| 136078 | 136276 | }; |
| 136079 | 136277 | static const unsigned int aAscii[4] = { |
| 136080 | 136278 | 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001, |
| 136081 | 136279 | }; |
| 136082 | 136280 | |
| | @@ -139705,11 +139903,11 @@ |
| 139705 | 139903 | ** operator) using the ICU uregex_XX() APIs. |
| 139706 | 139904 | ** |
| 139707 | 139905 | ** * Implementations of the SQL scalar upper() and lower() functions |
| 139708 | 139906 | ** for case mapping. |
| 139709 | 139907 | ** |
| 139710 | | -** * Integration of ICU and SQLite collation seqences. |
| 139908 | +** * Integration of ICU and SQLite collation sequences. |
| 139711 | 139909 | ** |
| 139712 | 139910 | ** * An implementation of the LIKE operator that uses ICU to |
| 139713 | 139911 | ** provide case-independent matching. |
| 139714 | 139912 | */ |
| 139715 | 139913 | |
| 139716 | 139914 | |