Fossil SCM
Incorporate the NGQP (Next-Generation Query Planner) branch of SQLite for the purpose of testing SQLite.
Commit
8b109c2288c178fb3e6bd63913b5a44d63b5acbd
Parent
a6dad6508c0e95b…
2 files changed
+2584
-2411
+11
-1
+2584
-2411
| --- src/sqlite3.c | ||
| +++ src/sqlite3.c | ||
| @@ -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-14 02:51:48 b920bb70bb009b7c54e7667544c9810c5ee25e19" | |
| 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>1 ); | |
| 76740 | + pParse->nQueryLoop = 1; | |
| 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 |
| @@ -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 = 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( 1==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 ); |
| @@ -97608,11 +97521,11 @@ | ||
| 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 | 97525 | }else{ |
| 97613 | - if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n; | |
| 97526 | + if( p->nSelectRow > n ) 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 | + && p->nSelectRow > 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); |
| @@ -100468,11 +100385,11 @@ | ||
| 100468 | 100385 | ** in the right order to begin with. |
| 100469 | 100386 | */ |
| 100470 | 100387 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
| 100471 | 100388 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0); |
| 100472 | 100389 | if( pWInfo==0 ) goto select_end; |
| 100473 | - if( pWInfo->nOBSat==pGroupBy->nExpr ){ | |
| 100390 | + if( sqlite3WhereIsOrdered(pWInfo) ){ | |
| 100474 | 100391 | /* The optimizer is able to deliver rows in group by order so |
| 100475 | 100392 | ** we do not have to sort. The OP_OpenEphemeral table will be |
| 100476 | 100393 | ** cancelled later because we still need to use the pKeyInfo |
| 100477 | 100394 | */ |
| 100478 | 100395 | groupBySort = 0; |
| @@ -100749,12 +100666,12 @@ | ||
| 100749 | 100666 | sqlite3ExprListDelete(db, pDel); |
| 100750 | 100667 | goto select_end; |
| 100751 | 100668 | } |
| 100752 | 100669 | updateAccumulator(pParse, &sAggInfo); |
| 100753 | 100670 | assert( pMinMax==0 || pMinMax->nExpr==1 ); |
| 100754 | - if( pWInfo->nOBSat>0 ){ | |
| 100755 | - sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak); | |
| 100671 | + if( sqlite3WhereIsOrdered(pWInfo) ){ | |
| 100672 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo)); | |
| 100756 | 100673 | VdbeComment((v, "%s() by index", |
| 100757 | 100674 | (flag==WHERE_ORDERBY_MIN?"min":"max"))); |
| 100758 | 100675 | } |
| 100759 | 100676 | sqlite3WhereEnd(pWInfo); |
| 100760 | 100677 | finalizeAggFunctions(pParse, &sAggInfo); |
| @@ -102109,11 +102026,11 @@ | ||
| 102109 | 102026 | } |
| 102110 | 102027 | |
| 102111 | 102028 | /* |
| 102112 | 102029 | ** This is called to code the required FOR EACH ROW triggers for an operation |
| 102113 | 102030 | ** 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 | |
| 102031 | +** is given by the op parameter. The tr_tm parameter determines whether the | |
| 102115 | 102032 | ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then |
| 102116 | 102033 | ** parameter pChanges is passed the list of columns being modified. |
| 102117 | 102034 | ** |
| 102118 | 102035 | ** If there are no triggers that fire at the specified time for the specified |
| 102119 | 102036 | ** operation on pTab, this function is a no-op. |
| @@ -102560,11 +102477,11 @@ | ||
| 102560 | 102477 | sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); |
| 102561 | 102478 | pWInfo = sqlite3WhereBegin( |
| 102562 | 102479 | pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0 |
| 102563 | 102480 | ); |
| 102564 | 102481 | if( pWInfo==0 ) goto update_cleanup; |
| 102565 | - okOnePass = pWInfo->okOnePass; | |
| 102482 | + okOnePass = sqlite3WhereOkOnePass(pWInfo); | |
| 102566 | 102483 | |
| 102567 | 102484 | /* Remember the rowid of every item to be updated. |
| 102568 | 102485 | */ |
| 102569 | 102486 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid); |
| 102570 | 102487 | if( !okOnePass ){ |
| @@ -104397,22 +104314,165 @@ | ||
| 104397 | 104314 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 104398 | 104315 | /***/ int sqlite3WhereTrace = 0; |
| 104399 | 104316 | #endif |
| 104400 | 104317 | #if defined(SQLITE_DEBUG) \ |
| 104401 | 104318 | && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
| 104402 | -# define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X | |
| 104319 | +# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X | |
| 104320 | +# define WHERETRACE_ENABLED 1 | |
| 104403 | 104321 | #else |
| 104404 | -# define WHERETRACE(X) | |
| 104322 | +# define WHERETRACE(K,X) | |
| 104405 | 104323 | #endif |
| 104406 | 104324 | |
| 104407 | 104325 | /* Forward reference |
| 104408 | 104326 | */ |
| 104409 | 104327 | typedef struct WhereClause WhereClause; |
| 104410 | 104328 | typedef struct WhereMaskSet WhereMaskSet; |
| 104411 | 104329 | typedef struct WhereOrInfo WhereOrInfo; |
| 104412 | 104330 | typedef struct WhereAndInfo WhereAndInfo; |
| 104413 | -typedef struct WhereCost WhereCost; | |
| 104331 | +typedef struct WhereLevel WhereLevel; | |
| 104332 | +typedef struct WhereLoop WhereLoop; | |
| 104333 | +typedef struct WherePath WherePath; | |
| 104334 | +typedef struct WhereTerm WhereTerm; | |
| 104335 | +typedef struct WhereLoopBuilder WhereLoopBuilder; | |
| 104336 | +typedef struct WhereScan WhereScan; | |
| 104337 | + | |
| 104338 | +/* | |
| 104339 | +** Cost X is tracked as 10*log2(X) stored in a 16-bit integer. The | |
| 104340 | +** maximum cost for ordinary tables is 64*(2**63) which becomes 6900. | |
| 104341 | +** (Virtual tables can return a larger cost, but let's assume they do not.) | |
| 104342 | +** So all costs can be stored in a 16-bit unsigned integer without risk | |
| 104343 | +** of overflow. | |
| 104344 | +** | |
| 104345 | +** Costs are estimates, so don't go to the computational trouble to compute | |
| 104346 | +** 10*log2(X) exactly. Instead, a close estimate is used. Any value of | |
| 104347 | +** X<=1 is stored as 0. X=2 is 10. X=3 is 16. X=1000 is 99. etc. | |
| 104348 | +** | |
| 104349 | +** The tool/wherecosttest.c source file implements a command-line program | |
| 104350 | +** that will convert between WhereCost to integers and do addition and | |
| 104351 | +** multiplication on WhereCost values. That command-line program is a | |
| 104352 | +** useful utility to have around when working with this module. | |
| 104353 | +*/ | |
| 104354 | +typedef unsigned short int WhereCost; | |
| 104355 | + | |
| 104356 | +/* | |
| 104357 | +** This object contains information needed to implement a single nestd | |
| 104358 | +** loop in WHERE clause. | |
| 104359 | +** | |
| 104360 | +** Contrast this object with WhereLoop. This object describes the | |
| 104361 | +** implementation of the loop. WhereLoop describes the algorithm. | |
| 104362 | +** This object contains a pointer to the WhereLoop algorithm as one of | |
| 104363 | +** its elements. | |
| 104364 | +** | |
| 104365 | +** The WhereInfo object contains a single instance of this object for | |
| 104366 | +** each term in the FROM clause (which is to say, for each of the | |
| 104367 | +** nested loops as implemented). The order of WhereLevel objects determines | |
| 104368 | +** the loop nested order, with WhereInfo.a[0] being the outer loop and | |
| 104369 | +** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. | |
| 104370 | +*/ | |
| 104371 | +struct WhereLevel { | |
| 104372 | + int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ | |
| 104373 | + int iTabCur; /* The VDBE cursor used to access the table */ | |
| 104374 | + int iIdxCur; /* The VDBE cursor used to access pIdx */ | |
| 104375 | + int addrBrk; /* Jump here to break out of the loop */ | |
| 104376 | + int addrNxt; /* Jump here to start the next IN combination */ | |
| 104377 | + int addrCont; /* Jump here to continue with the next loop cycle */ | |
| 104378 | + int addrFirst; /* First instruction of interior of the loop */ | |
| 104379 | + u8 iFrom; /* Which entry in the FROM clause */ | |
| 104380 | + u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ | |
| 104381 | + int p1, p2; /* Operands of the opcode used to ends the loop */ | |
| 104382 | + union { /* Information that depends on pWLoop->wsFlags */ | |
| 104383 | + struct { | |
| 104384 | + int nIn; /* Number of entries in aInLoop[] */ | |
| 104385 | + struct InLoop { | |
| 104386 | + int iCur; /* The VDBE cursor used by this IN operator */ | |
| 104387 | + int addrInTop; /* Top of the IN loop */ | |
| 104388 | + u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ | |
| 104389 | + } *aInLoop; /* Information about each nested IN operator */ | |
| 104390 | + } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ | |
| 104391 | + Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ | |
| 104392 | + } u; | |
| 104393 | + struct WhereLoop *pWLoop; /* The selected WhereLoop object */ | |
| 104394 | +}; | |
| 104395 | + | |
| 104396 | +/* | |
| 104397 | +** Each instance of this object represents an algorithm for evaluating one | |
| 104398 | +** term of a join. Every term of the FROM clause will have at least | |
| 104399 | +** one corresponding WhereLoop object (unless INDEXED BY constraints | |
| 104400 | +** prevent a query solution - which is an error) and many terms of the | |
| 104401 | +** FROM clause will have multiple WhereLoop objects, each describing a | |
| 104402 | +** potential way of implementing that FROM-clause term, together with | |
| 104403 | +** dependencies and cost estimates for using the chosen algorithm. | |
| 104404 | +** | |
| 104405 | +** Query planning consists of building up a collection of these WhereLoop | |
| 104406 | +** objects, then computing a particular sequence of WhereLoop objects, with | |
| 104407 | +** one WhereLoop object per FROM clause term, that satisfy all dependencies | |
| 104408 | +** and that minimize the overall cost. | |
| 104409 | +*/ | |
| 104410 | +struct WhereLoop { | |
| 104411 | + Bitmask prereq; /* Bitmask of other loops that must run first */ | |
| 104412 | + Bitmask maskSelf; /* Bitmask identifying table iTab */ | |
| 104413 | +#ifdef SQLITE_DEBUG | |
| 104414 | + char cId; /* Symbolic ID of this loop for debugging use */ | |
| 104415 | +#endif | |
| 104416 | + u8 iTab; /* Position in FROM clause of table for this loop */ | |
| 104417 | + u8 iSortIdx; /* Sorting index number. 0==None */ | |
| 104418 | + WhereCost rSetup; /* One-time setup cost (ex: create transient index) */ | |
| 104419 | + WhereCost rRun; /* Cost of running each loop */ | |
| 104420 | + WhereCost nOut; /* Estimated number of output rows */ | |
| 104421 | + union { | |
| 104422 | + struct { /* Information for internal btree tables */ | |
| 104423 | + int nEq; /* Number of equality constraints */ | |
| 104424 | + Index *pIndex; /* Index used, or NULL */ | |
| 104425 | + } btree; | |
| 104426 | + struct { /* Information for virtual tables */ | |
| 104427 | + int idxNum; /* Index number */ | |
| 104428 | + u8 needFree; /* True if sqlite3_free(idxStr) is needed */ | |
| 104429 | + u8 isOrdered; /* True if satisfies ORDER BY */ | |
| 104430 | + u16 omitMask; /* Terms that may be omitted */ | |
| 104431 | + char *idxStr; /* Index identifier string */ | |
| 104432 | + } vtab; | |
| 104433 | + } u; | |
| 104434 | + u32 wsFlags; /* WHERE_* flags describing the plan */ | |
| 104435 | + u16 nLTerm; /* Number of entries in aLTerm[] */ | |
| 104436 | + /**** whereLoopXfer() copies fields above ***********************/ | |
| 104437 | +# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) | |
| 104438 | + u16 nLSlot; /* Number of slots allocated for aLTerm[] */ | |
| 104439 | + WhereTerm **aLTerm; /* WhereTerms used */ | |
| 104440 | + WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ | |
| 104441 | + WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ | |
| 104442 | +}; | |
| 104443 | + | |
| 104444 | +/* Forward declaration of methods */ | |
| 104445 | +static int whereLoopResize(sqlite3*, WhereLoop*, int); | |
| 104446 | + | |
| 104447 | +/* | |
| 104448 | +** Each instance of this object holds a sequence of WhereLoop objects | |
| 104449 | +** that implement some or all of a query plan. | |
| 104450 | +** | |
| 104451 | +** Think of each WhereLoop objects as a node in a graph, which arcs | |
| 104452 | +** showing dependences and costs for travelling between nodes. (That is | |
| 104453 | +** not a completely accurate description because WhereLoop costs are a | |
| 104454 | +** vector, not a scalar, and because dependences are many-to-one, not | |
| 104455 | +** one-to-one as are graph nodes. But it is a useful visualization aid.) | |
| 104456 | +** Then a WherePath object is a path through the graph that visits some | |
| 104457 | +** or all of the WhereLoop objects once. | |
| 104458 | +** | |
| 104459 | +** The "solver" works by creating the N best WherePath objects of length | |
| 104460 | +** 1. Then using those as a basis to compute the N best WherePath objects | |
| 104461 | +** of length 2. And so forth until the length of WherePaths equals the | |
| 104462 | +** number of nodes in the FROM clause. The best (lowest cost) WherePath | |
| 104463 | +** at the end is the choosen query plan. | |
| 104464 | +*/ | |
| 104465 | +struct WherePath { | |
| 104466 | + Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ | |
| 104467 | + Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ | |
| 104468 | + WhereCost nRow; /* Estimated number of rows generated by this path */ | |
| 104469 | + WhereCost rCost; /* Total cost of this path */ | |
| 104470 | + u8 isOrdered; /* True if this path satisfies ORDER BY */ | |
| 104471 | + u8 isOrderedValid; /* True if the isOrdered field is valid */ | |
| 104472 | + WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ | |
| 104473 | +}; | |
| 104414 | 104474 | |
| 104415 | 104475 | /* |
| 104416 | 104476 | ** The query generator uses an array of instances of this structure to |
| 104417 | 104477 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 104418 | 104478 | ** clause subexpression is separated from the others by AND operators, |
| @@ -104461,11 +104521,10 @@ | ||
| 104461 | 104521 | ** |
| 104462 | 104522 | ** The number of terms in a join is limited by the number of bits |
| 104463 | 104523 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 104464 | 104524 | ** is only able to process joins with 64 or fewer tables. |
| 104465 | 104525 | */ |
| 104466 | -typedef struct WhereTerm WhereTerm; | |
| 104467 | 104526 | struct WhereTerm { |
| 104468 | 104527 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
| 104469 | 104528 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 104470 | 104529 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
| 104471 | 104530 | union { |
| @@ -104495,10 +104554,26 @@ | ||
| 104495 | 104554 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 104496 | 104555 | #else |
| 104497 | 104556 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 104498 | 104557 | #endif |
| 104499 | 104558 | |
| 104559 | +/* | |
| 104560 | +** An instance of the WhereScan object is used as an iterator for locating | |
| 104561 | +** terms in the WHERE clause that are useful to the query planner. | |
| 104562 | +*/ | |
| 104563 | +struct WhereScan { | |
| 104564 | + WhereClause *pOrigWC; /* Original, innermost WhereClause */ | |
| 104565 | + WhereClause *pWC; /* WhereClause currently being scanned */ | |
| 104566 | + char *zCollName; /* Required collating sequence, if not NULL */ | |
| 104567 | + char idxaff; /* Must match this affinity, if zCollName!=NULL */ | |
| 104568 | + unsigned char nEquiv; /* Number of entries in aEquiv[] */ | |
| 104569 | + unsigned char iEquiv; /* Next unused slot in aEquiv[] */ | |
| 104570 | + u32 opMask; /* Acceptable operators */ | |
| 104571 | + int k; /* Resume scanning at this->pWC->a[this->k] */ | |
| 104572 | + int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ | |
| 104573 | +}; | |
| 104574 | + | |
| 104500 | 104575 | /* |
| 104501 | 104576 | ** An instance of the following structure holds all information about a |
| 104502 | 104577 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
| 104503 | 104578 | ** |
| 104504 | 104579 | ** Explanation of pOuter: For a WHERE clause of the form |
| @@ -104508,15 +104583,13 @@ | ||
| 104508 | 104583 | ** There are separate WhereClause objects for the whole clause and for |
| 104509 | 104584 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 104510 | 104585 | ** subclauses points to the WhereClause object for the whole clause. |
| 104511 | 104586 | */ |
| 104512 | 104587 | struct WhereClause { |
| 104513 | - Parse *pParse; /* The parser context */ | |
| 104514 | - WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */ | |
| 104588 | + WhereInfo *pWInfo; /* WHERE clause processing context */ | |
| 104515 | 104589 | WhereClause *pOuter; /* Outer conjunction */ |
| 104516 | 104590 | u8 op; /* Split operator. TK_AND or TK_OR */ |
| 104517 | - u16 wctrlFlags; /* Might include WHERE_AND_ONLY */ | |
| 104518 | 104591 | int nTerm; /* Number of terms */ |
| 104519 | 104592 | int nSlot; /* Number of entries in a[] */ |
| 104520 | 104593 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
| 104521 | 104594 | #if defined(SQLITE_SMALL_STACK) |
| 104522 | 104595 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| @@ -104572,23 +104645,59 @@ | ||
| 104572 | 104645 | int n; /* Number of assigned cursor values */ |
| 104573 | 104646 | int ix[BMS]; /* Cursor assigned to each bit */ |
| 104574 | 104647 | }; |
| 104575 | 104648 | |
| 104576 | 104649 | /* |
| 104577 | -** A WhereCost object records a lookup strategy and the estimated | |
| 104578 | -** cost of pursuing that strategy. | |
| 104650 | +** This object is a convenience wrapper holding all information needed | |
| 104651 | +** to construct WhereLoop objects for a particular query. | |
| 104579 | 104652 | */ |
| 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 */ | |
| 104653 | +struct WhereLoopBuilder { | |
| 104654 | + WhereInfo *pWInfo; /* Information about this WHERE */ | |
| 104655 | + WhereClause *pWC; /* WHERE clause terms */ | |
| 104656 | + ExprList *pOrderBy; /* ORDER BY clause */ | |
| 104657 | + WhereLoop *pNew; /* Template WhereLoop */ | |
| 104658 | + WhereLoop *pBest; /* If non-NULL, store single best loop here */ | |
| 104584 | 104659 | }; |
| 104585 | 104660 | |
| 104586 | 104661 | /* |
| 104587 | -** Bitmasks for the operators that indices are able to exploit. An | |
| 104662 | +** The WHERE clause processing routine has two halves. The | |
| 104663 | +** first part does the start of the WHERE loop and the second | |
| 104664 | +** half does the tail of the WHERE loop. An instance of | |
| 104665 | +** this structure is returned by the first half and passed | |
| 104666 | +** into the second half to give some continuity. | |
| 104667 | +** | |
| 104668 | +** An instance of this object holds the complete state of the query | |
| 104669 | +** planner. | |
| 104670 | +*/ | |
| 104671 | +struct WhereInfo { | |
| 104672 | + Parse *pParse; /* Parsing and code generating context */ | |
| 104673 | + SrcList *pTabList; /* List of tables in the join */ | |
| 104674 | + ExprList *pOrderBy; /* The ORDER BY clause or NULL */ | |
| 104675 | + ExprList *pDistinct; /* DISTINCT ON values, or NULL */ | |
| 104676 | + WhereLoop *pLoops; /* List of all WhereLoop objects */ | |
| 104677 | + Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ | |
| 104678 | + WhereCost nRowOut; /* Estimated number of output rows */ | |
| 104679 | + u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ | |
| 104680 | + u8 bOBSat; /* ORDER BY satisfied by indices */ | |
| 104681 | + u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ | |
| 104682 | + u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ | |
| 104683 | + u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ | |
| 104684 | + int iTop; /* The very beginning of the WHERE loop */ | |
| 104685 | + int iContinue; /* Jump here to continue with next record */ | |
| 104686 | + int iBreak; /* Jump here to break out of the loop */ | |
| 104687 | + int nLevel; /* Number of nested loop */ | |
| 104688 | + int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ | |
| 104689 | + WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ | |
| 104690 | + WhereClause sWC; /* Decomposition of the WHERE clause */ | |
| 104691 | + WhereLevel a[1]; /* Information about each nest loop in WHERE */ | |
| 104692 | +}; | |
| 104693 | + | |
| 104694 | +/* | |
| 104695 | +** Bitmasks for the operators on WhereTerm objects. These are all | |
| 104696 | +** operators that are of interest to the query planner. An | |
| 104588 | 104697 | ** OR-ed combination of these values can be used when searching for |
| 104589 | -** terms in the where clause. | |
| 104698 | +** particular WhereTerms within a WhereClause. | |
| 104590 | 104699 | */ |
| 104591 | 104700 | #define WO_IN 0x001 |
| 104592 | 104701 | #define WO_EQ 0x002 |
| 104593 | 104702 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 104594 | 104703 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| @@ -104603,96 +104712,106 @@ | ||
| 104603 | 104712 | |
| 104604 | 104713 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
| 104605 | 104714 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
| 104606 | 104715 | |
| 104607 | 104716 | /* |
| 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; | |
| 104717 | +** These are definitions of bits in the WhereLoop.wsFlags field. | |
| 104718 | +** The particular combination of bits in each WhereLoop help to | |
| 104719 | +** determine the algorithm that WhereLoop represents. | |
| 104720 | +*/ | |
| 104721 | +#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR or x IN (...) or x IS NULL */ | |
| 104722 | +#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ | |
| 104723 | +#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ | |
| 104724 | +#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ | |
| 104725 | +#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ | |
| 104726 | +#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ | |
| 104727 | +#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ | |
| 104728 | +#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ | |
| 104729 | +#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ | |
| 104730 | +#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ | |
| 104731 | +#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ | |
| 104732 | +#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ | |
| 104733 | +#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ | |
| 104734 | +#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ | |
| 104735 | +#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ | |
| 104736 | +#define WHERE_TEMP_INDEX 0x00004000 /* Uses an ephemeral index */ | |
| 104737 | + | |
| 104738 | + | |
| 104739 | +/* Convert a WhereCost value (10 times log2(X)) into its integer value X. | |
| 104740 | +** A rough approximation is used. The value returned is not exact. | |
| 104741 | +*/ | |
| 104742 | +static u64 whereCostToInt(WhereCost x){ | |
| 104743 | + u64 n; | |
| 104744 | + if( x<10 ) return 1; | |
| 104745 | + n = x%10; | |
| 104746 | + x /= 10; | |
| 104747 | + if( n>=5 ) n -= 2; | |
| 104748 | + else if( n>=1 ) n -= 1; | |
| 104749 | + if( x>=3 ) return (n+8)<<(x-3); | |
| 104750 | + return (n+8)>>(3-x); | |
| 104751 | +} | |
| 104752 | + | |
| 104753 | +/* | |
| 104754 | +** Return the estimated number of output rows from a WHERE clause | |
| 104755 | +*/ | |
| 104756 | +SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ | |
| 104757 | + return whereCostToInt(pWInfo->nRowOut); | |
| 104758 | +} | |
| 104759 | + | |
| 104760 | +/* | |
| 104761 | +** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this | |
| 104762 | +** WHERE clause returns outputs for DISTINCT processing. | |
| 104763 | +*/ | |
| 104764 | +SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ | |
| 104765 | + return pWInfo->eDistinct; | |
| 104766 | +} | |
| 104767 | + | |
| 104768 | +/* | |
| 104769 | +** Return TRUE if the WHERE clause returns rows in ORDER BY order. | |
| 104770 | +** Return FALSE if the output needs to be sorted. | |
| 104771 | +*/ | |
| 104772 | +SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ | |
| 104773 | + return pWInfo->bOBSat!=0; | |
| 104774 | +} | |
| 104775 | + | |
| 104776 | +/* | |
| 104777 | +** Return the VDBE address or label to jump to in order to continue | |
| 104778 | +** immediately with the next row of a WHERE clause. | |
| 104779 | +*/ | |
| 104780 | +SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ | |
| 104781 | + return pWInfo->iContinue; | |
| 104782 | +} | |
| 104783 | + | |
| 104784 | +/* | |
| 104785 | +** Return the VDBE address or label to jump to in order to break | |
| 104786 | +** out of a WHERE loop. | |
| 104787 | +*/ | |
| 104788 | +SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ | |
| 104789 | + return pWInfo->iBreak; | |
| 104790 | +} | |
| 104791 | + | |
| 104792 | +/* | |
| 104793 | +** Return TRUE if an UPDATE or DELETE statement can operate directly on | |
| 104794 | +** the rowids returned by a WHERE clause. Return FALSE if doing an | |
| 104795 | +** UPDATE or DELETE might change subsequent WHERE clause results. | |
| 104796 | +*/ | |
| 104797 | +SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo){ | |
| 104798 | + return pWInfo->okOnePass; | |
| 104676 | 104799 | } |
| 104677 | 104800 | |
| 104678 | 104801 | /* |
| 104679 | 104802 | ** Initialize a preallocated WhereClause structure. |
| 104680 | 104803 | */ |
| 104681 | 104804 | static void whereClauseInit( |
| 104682 | 104805 | 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 */ | |
| 104806 | + WhereInfo *pWInfo /* The WHERE processing context */ | |
| 104686 | 104807 | ){ |
| 104687 | - pWC->pParse = pParse; | |
| 104688 | - pWC->pMaskSet = pMaskSet; | |
| 104808 | + pWC->pWInfo = pWInfo; | |
| 104689 | 104809 | pWC->pOuter = 0; |
| 104690 | 104810 | pWC->nTerm = 0; |
| 104691 | 104811 | pWC->nSlot = ArraySize(pWC->aStatic); |
| 104692 | 104812 | pWC->a = pWC->aStatic; |
| 104693 | - pWC->wctrlFlags = wctrlFlags; | |
| 104694 | 104813 | } |
| 104695 | 104814 | |
| 104696 | 104815 | /* Forward reference */ |
| 104697 | 104816 | static void whereClauseClear(WhereClause*); |
| 104698 | 104817 | |
| @@ -104717,11 +104836,11 @@ | ||
| 104717 | 104836 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 104718 | 104837 | */ |
| 104719 | 104838 | static void whereClauseClear(WhereClause *pWC){ |
| 104720 | 104839 | int i; |
| 104721 | 104840 | WhereTerm *a; |
| 104722 | - sqlite3 *db = pWC->pParse->db; | |
| 104841 | + sqlite3 *db = pWC->pWInfo->pParse->db; | |
| 104723 | 104842 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
| 104724 | 104843 | if( a->wtFlags & TERM_DYNAMIC ){ |
| 104725 | 104844 | sqlite3ExprDelete(db, a->pExpr); |
| 104726 | 104845 | } |
| 104727 | 104846 | if( a->wtFlags & TERM_ORINFO ){ |
| @@ -104758,11 +104877,11 @@ | ||
| 104758 | 104877 | WhereTerm *pTerm; |
| 104759 | 104878 | int idx; |
| 104760 | 104879 | testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ |
| 104761 | 104880 | if( pWC->nTerm>=pWC->nSlot ){ |
| 104762 | 104881 | WhereTerm *pOld = pWC->a; |
| 104763 | - sqlite3 *db = pWC->pParse->db; | |
| 104882 | + sqlite3 *db = pWC->pWInfo->pParse->db; | |
| 104764 | 104883 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
| 104765 | 104884 | if( pWC->a==0 ){ |
| 104766 | 104885 | if( wtFlags & TERM_DYNAMIC ){ |
| 104767 | 104886 | sqlite3ExprDelete(db, p); |
| 104768 | 104887 | } |
| @@ -104810,13 +104929,13 @@ | ||
| 104810 | 104929 | whereSplit(pWC, pExpr->pRight, op); |
| 104811 | 104930 | } |
| 104812 | 104931 | } |
| 104813 | 104932 | |
| 104814 | 104933 | /* |
| 104815 | -** Initialize an expression mask set (a WhereMaskSet object) | |
| 104934 | +** Initialize a WhereMaskSet object | |
| 104816 | 104935 | */ |
| 104817 | -#define initMaskSet(P) memset(P, 0, sizeof(*P)) | |
| 104936 | +#define initMaskSet(P) (P)->n=0 | |
| 104818 | 104937 | |
| 104819 | 104938 | /* |
| 104820 | 104939 | ** Return the bitmask for the given cursor number. Return 0 if |
| 104821 | 104940 | ** iCursor is not in the set. |
| 104822 | 104941 | */ |
| @@ -104823,11 +104942,11 @@ | ||
| 104823 | 104942 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 104824 | 104943 | int i; |
| 104825 | 104944 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
| 104826 | 104945 | for(i=0; i<pMaskSet->n; i++){ |
| 104827 | 104946 | if( pMaskSet->ix[i]==iCursor ){ |
| 104828 | - return ((Bitmask)1)<<i; | |
| 104947 | + return MASKBIT(i); | |
| 104829 | 104948 | } |
| 104830 | 104949 | } |
| 104831 | 104950 | return 0; |
| 104832 | 104951 | } |
| 104833 | 104952 | |
| @@ -104843,22 +104962,13 @@ | ||
| 104843 | 104962 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
| 104844 | 104963 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
| 104845 | 104964 | } |
| 104846 | 104965 | |
| 104847 | 104966 | /* |
| 104848 | -** This routine walks (recursively) an expression tree and generates | |
| 104967 | +** These routine walk (recursively) an expression tree and generates | |
| 104849 | 104968 | ** a bitmask indicating which tables are used in that expression |
| 104850 | 104969 | ** 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 | 104970 | */ |
| 104861 | 104971 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 104862 | 104972 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 104863 | 104973 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
| 104864 | 104974 | Bitmask mask = 0; |
| @@ -104908,11 +105018,11 @@ | ||
| 104908 | 105018 | } |
| 104909 | 105019 | |
| 104910 | 105020 | /* |
| 104911 | 105021 | ** Return TRUE if the given operator is one of the operators that is |
| 104912 | 105022 | ** allowed for an indexable WHERE clause term. The allowed operators are |
| 104913 | -** "=", "<", ">", "<=", ">=", and "IN". | |
| 105023 | +** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" | |
| 104914 | 105024 | ** |
| 104915 | 105025 | ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be |
| 104916 | 105026 | ** of one of the following forms: column = expression column > expression |
| 104917 | 105027 | ** column >= expression column < expression column <= expression |
| 104918 | 105028 | ** expression = column expression > column expression >= column |
| @@ -104935,14 +105045,13 @@ | ||
| 104935 | 105045 | /* |
| 104936 | 105046 | ** Commute a comparison operator. Expressions of the form "X op Y" |
| 104937 | 105047 | ** are converted into "Y op X". |
| 104938 | 105048 | ** |
| 104939 | 105049 | ** 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 | |
| 105050 | +** collating sequence, then COLLATE operators are adjusted to ensure | |
| 105051 | +** that the collating sequence does not change. For example: | |
| 105052 | +** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on | |
| 104944 | 105053 | ** the left hand side of a comparison overrides any collation sequence |
| 104945 | 105054 | ** attached to the right. For the same reason the EP_Collate flag |
| 104946 | 105055 | ** is not commuted. |
| 104947 | 105056 | */ |
| 104948 | 105057 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
| @@ -104994,10 +105103,134 @@ | ||
| 104994 | 105103 | assert( op!=TK_LE || c==WO_LE ); |
| 104995 | 105104 | assert( op!=TK_GT || c==WO_GT ); |
| 104996 | 105105 | assert( op!=TK_GE || c==WO_GE ); |
| 104997 | 105106 | return c; |
| 104998 | 105107 | } |
| 105108 | + | |
| 105109 | +/* | |
| 105110 | +** Advance to the next WhereTerm that matches according to the criteria | |
| 105111 | +** established when the pScan object was initialized by whereScanInit(). | |
| 105112 | +** Return NULL if there are no more matching WhereTerms. | |
| 105113 | +*/ | |
| 105114 | +WhereTerm *whereScanNext(WhereScan *pScan){ | |
| 105115 | + int iCur; /* The cursor on the LHS of the term */ | |
| 105116 | + int iColumn; /* The column on the LHS of the term. -1 for IPK */ | |
| 105117 | + Expr *pX; /* An expression being tested */ | |
| 105118 | + WhereClause *pWC; /* Shorthand for pScan->pWC */ | |
| 105119 | + WhereTerm *pTerm; /* The term being tested */ | |
| 105120 | + int k = pScan->k; /* Where to start scanning */ | |
| 105121 | + | |
| 105122 | + while( pScan->iEquiv<=pScan->nEquiv ){ | |
| 105123 | + iCur = pScan->aEquiv[pScan->iEquiv-2]; | |
| 105124 | + iColumn = pScan->aEquiv[pScan->iEquiv-1]; | |
| 105125 | + while( (pWC = pScan->pWC)!=0 ){ | |
| 105126 | + for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ | |
| 105127 | + if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){ | |
| 105128 | + if( (pTerm->eOperator & WO_EQUIV)!=0 | |
| 105129 | + && pScan->nEquiv<ArraySize(pScan->aEquiv) | |
| 105130 | + ){ | |
| 105131 | + int j; | |
| 105132 | + pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); | |
| 105133 | + assert( pX->op==TK_COLUMN ); | |
| 105134 | + for(j=0; j<pScan->nEquiv; j+=2){ | |
| 105135 | + if( pScan->aEquiv[j]==pX->iTable | |
| 105136 | + && pScan->aEquiv[j+1]==pX->iColumn ){ | |
| 105137 | + break; | |
| 105138 | + } | |
| 105139 | + } | |
| 105140 | + if( j==pScan->nEquiv ){ | |
| 105141 | + pScan->aEquiv[j] = pX->iTable; | |
| 105142 | + pScan->aEquiv[j+1] = pX->iColumn; | |
| 105143 | + pScan->nEquiv += 2; | |
| 105144 | + } | |
| 105145 | + } | |
| 105146 | + if( (pTerm->eOperator & pScan->opMask)!=0 ){ | |
| 105147 | + /* Verify the affinity and collating sequence match */ | |
| 105148 | + if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ | |
| 105149 | + CollSeq *pColl; | |
| 105150 | + Parse *pParse = pWC->pWInfo->pParse; | |
| 105151 | + pX = pTerm->pExpr; | |
| 105152 | + if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ | |
| 105153 | + continue; | |
| 105154 | + } | |
| 105155 | + assert(pX->pLeft); | |
| 105156 | + pColl = sqlite3BinaryCompareCollSeq(pParse, | |
| 105157 | + pX->pLeft, pX->pRight); | |
| 105158 | + if( pColl==0 ) pColl = pParse->db->pDfltColl; | |
| 105159 | + if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ | |
| 105160 | + continue; | |
| 105161 | + } | |
| 105162 | + } | |
| 105163 | + if( (pTerm->eOperator & WO_EQ)!=0 | |
| 105164 | + && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN | |
| 105165 | + && pX->iTable==pScan->aEquiv[0] | |
| 105166 | + && pX->iColumn==pScan->aEquiv[1] | |
| 105167 | + ){ | |
| 105168 | + continue; | |
| 105169 | + } | |
| 105170 | + pScan->k = k+1; | |
| 105171 | + return pTerm; | |
| 105172 | + } | |
| 105173 | + } | |
| 105174 | + } | |
| 105175 | + pWC = pScan->pWC = pScan->pWC->pOuter; | |
| 105176 | + k = 0; | |
| 105177 | + } | |
| 105178 | + pScan->pWC = pScan->pOrigWC; | |
| 105179 | + k = 0; | |
| 105180 | + pScan->iEquiv += 2; | |
| 105181 | + } | |
| 105182 | + return 0; | |
| 105183 | +} | |
| 105184 | + | |
| 105185 | +/* | |
| 105186 | +** Initialize a WHERE clause scanner object. Return a pointer to the | |
| 105187 | +** first match. Return NULL if there are no matches. | |
| 105188 | +** | |
| 105189 | +** The scanner will be searching the WHERE clause pWC. It will look | |
| 105190 | +** for terms of the form "X <op> <expr>" where X is column iColumn of table | |
| 105191 | +** iCur. The <op> must be one of the operators described by opMask. | |
| 105192 | +** | |
| 105193 | +** If the search is for X and the WHERE clause contains terms of the | |
| 105194 | +** form X=Y then this routine might also return terms of the form | |
| 105195 | +** "Y <op> <expr>". The number of levels of transitivity is limited, | |
| 105196 | +** but is enough to handle most commonly occurring SQL statements. | |
| 105197 | +** | |
| 105198 | +** If X is not the INTEGER PRIMARY KEY then X must be compatible with | |
| 105199 | +** index pIdx. | |
| 105200 | +*/ | |
| 105201 | +WhereTerm *whereScanInit( | |
| 105202 | + WhereScan *pScan, /* The WhereScan object being initialized */ | |
| 105203 | + WhereClause *pWC, /* The WHERE clause to be scanned */ | |
| 105204 | + int iCur, /* Cursor to scan for */ | |
| 105205 | + int iColumn, /* Column to scan for */ | |
| 105206 | + u32 opMask, /* Operator(s) to scan for */ | |
| 105207 | + Index *pIdx /* Must be compatible with this index */ | |
| 105208 | +){ | |
| 105209 | + int j; | |
| 105210 | + | |
| 105211 | + /* memset(pScan, 0, sizeof(*pScan)); */ | |
| 105212 | + pScan->pOrigWC = pWC; | |
| 105213 | + pScan->pWC = pWC; | |
| 105214 | + if( pIdx && iColumn>=0 ){ | |
| 105215 | + pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; | |
| 105216 | + for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ | |
| 105217 | + if( NEVER(j>=pIdx->nColumn) ) return 0; | |
| 105218 | + } | |
| 105219 | + pScan->zCollName = pIdx->azColl[j]; | |
| 105220 | + }else{ | |
| 105221 | + pScan->idxaff = 0; | |
| 105222 | + pScan->zCollName = 0; | |
| 105223 | + } | |
| 105224 | + pScan->opMask = opMask; | |
| 105225 | + pScan->k = 0; | |
| 105226 | + pScan->aEquiv[0] = iCur; | |
| 105227 | + pScan->aEquiv[1] = iColumn; | |
| 105228 | + pScan->nEquiv = 2; | |
| 105229 | + pScan->iEquiv = 2; | |
| 105230 | + return whereScanNext(pScan); | |
| 105231 | +} | |
| 104999 | 105232 | |
| 105000 | 105233 | /* |
| 105001 | 105234 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 105002 | 105235 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 105003 | 105236 | ** the WO_xx operator codes specified by the op parameter. |
| @@ -105026,98 +105259,32 @@ | ||
| 105026 | 105259 | int iColumn, /* Column number of LHS */ |
| 105027 | 105260 | Bitmask notReady, /* RHS must not overlap with this mask */ |
| 105028 | 105261 | u32 op, /* Mask of WO_xx values describing operator */ |
| 105029 | 105262 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 105030 | 105263 | ){ |
| 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: | |
| 105264 | + WhereTerm *pResult = 0; | |
| 105265 | + WhereTerm *p; | |
| 105266 | + WhereScan scan; | |
| 105267 | + | |
| 105268 | + p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); | |
| 105269 | + while( p ){ | |
| 105270 | + if( (p->prereqRight & notReady)==0 ){ | |
| 105271 | + if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ | |
| 105272 | + return p; | |
| 105273 | + } | |
| 105274 | + if( pResult==0 ) pResult = p; | |
| 105275 | + } | |
| 105276 | + p = whereScanNext(&scan); | |
| 105277 | + } | |
| 105109 | 105278 | return pResult; |
| 105110 | 105279 | } |
| 105111 | 105280 | |
| 105112 | 105281 | /* Forward reference */ |
| 105113 | 105282 | static void exprAnalyze(SrcList*, WhereClause*, int); |
| 105114 | 105283 | |
| 105115 | 105284 | /* |
| 105116 | 105285 | ** Call exprAnalyze on all terms in a WHERE clause. |
| 105117 | -** | |
| 105118 | -** | |
| 105119 | 105286 | */ |
| 105120 | 105287 | static void exprAnalyzeAll( |
| 105121 | 105288 | SrcList *pTabList, /* the FROM clause */ |
| 105122 | 105289 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 105123 | 105290 | ){ |
| @@ -105345,15 +105512,15 @@ | ||
| 105345 | 105512 | static void exprAnalyzeOrTerm( |
| 105346 | 105513 | SrcList *pSrc, /* the FROM clause */ |
| 105347 | 105514 | WhereClause *pWC, /* the complete WHERE clause */ |
| 105348 | 105515 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 105349 | 105516 | ){ |
| 105350 | - Parse *pParse = pWC->pParse; /* Parser context */ | |
| 105517 | + WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ | |
| 105518 | + Parse *pParse = pWInfo->pParse; /* Parser context */ | |
| 105351 | 105519 | sqlite3 *db = pParse->db; /* Database connection */ |
| 105352 | 105520 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 105353 | 105521 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
| 105354 | - WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */ | |
| 105355 | 105522 | int i; /* Loop counters */ |
| 105356 | 105523 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 105357 | 105524 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 105358 | 105525 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 105359 | 105526 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| @@ -105368,11 +105535,11 @@ | ||
| 105368 | 105535 | assert( pExpr->op==TK_OR ); |
| 105369 | 105536 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
| 105370 | 105537 | if( pOrInfo==0 ) return; |
| 105371 | 105538 | pTerm->wtFlags |= TERM_ORINFO; |
| 105372 | 105539 | pOrWc = &pOrInfo->wc; |
| 105373 | - whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags); | |
| 105540 | + whereClauseInit(pOrWc, pWInfo); | |
| 105374 | 105541 | whereSplit(pOrWc, pExpr, TK_OR); |
| 105375 | 105542 | exprAnalyzeAll(pSrc, pOrWc); |
| 105376 | 105543 | if( db->mallocFailed ) return; |
| 105377 | 105544 | assert( pOrWc->nTerm>=2 ); |
| 105378 | 105545 | |
| @@ -105394,20 +105561,20 @@ | ||
| 105394 | 105561 | Bitmask b = 0; |
| 105395 | 105562 | pOrTerm->u.pAndInfo = pAndInfo; |
| 105396 | 105563 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 105397 | 105564 | pOrTerm->eOperator = WO_AND; |
| 105398 | 105565 | pAndWC = &pAndInfo->wc; |
| 105399 | - whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags); | |
| 105566 | + whereClauseInit(pAndWC, pWC->pWInfo); | |
| 105400 | 105567 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 105401 | 105568 | exprAnalyzeAll(pSrc, pAndWC); |
| 105402 | 105569 | pAndWC->pOuter = pWC; |
| 105403 | 105570 | testcase( db->mallocFailed ); |
| 105404 | 105571 | if( !db->mallocFailed ){ |
| 105405 | 105572 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 105406 | 105573 | assert( pAndTerm->pExpr ); |
| 105407 | 105574 | if( allowedOp(pAndTerm->pExpr->op) ){ |
| 105408 | - b |= getMask(pMaskSet, pAndTerm->leftCursor); | |
| 105575 | + b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); | |
| 105409 | 105576 | } |
| 105410 | 105577 | } |
| 105411 | 105578 | } |
| 105412 | 105579 | indexable &= b; |
| 105413 | 105580 | } |
| @@ -105414,14 +105581,14 @@ | ||
| 105414 | 105581 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 105415 | 105582 | /* Skip this term for now. We revisit it when we process the |
| 105416 | 105583 | ** corresponding TERM_VIRTUAL term */ |
| 105417 | 105584 | }else{ |
| 105418 | 105585 | Bitmask b; |
| 105419 | - b = getMask(pMaskSet, pOrTerm->leftCursor); | |
| 105586 | + b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); | |
| 105420 | 105587 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 105421 | 105588 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
| 105422 | - b |= getMask(pMaskSet, pOther->leftCursor); | |
| 105589 | + b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); | |
| 105423 | 105590 | } |
| 105424 | 105591 | indexable &= b; |
| 105425 | 105592 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
| 105426 | 105593 | chngToIN = 0; |
| 105427 | 105594 | }else{ |
| @@ -105479,11 +105646,11 @@ | ||
| 105479 | 105646 | /* This is the 2-bit case and we are on the second iteration and |
| 105480 | 105647 | ** current term is from the first iteration. So skip this term. */ |
| 105481 | 105648 | assert( j==1 ); |
| 105482 | 105649 | continue; |
| 105483 | 105650 | } |
| 105484 | - if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){ | |
| 105651 | + if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ | |
| 105485 | 105652 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 105486 | 105653 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 105487 | 105654 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 105488 | 105655 | ** and use its inversion. */ |
| 105489 | 105656 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| @@ -105498,11 +105665,11 @@ | ||
| 105498 | 105665 | if( i<0 ){ |
| 105499 | 105666 | /* No candidate table+column was found. This can only occur |
| 105500 | 105667 | ** on the second iteration */ |
| 105501 | 105668 | assert( j==1 ); |
| 105502 | 105669 | assert( IsPowerOfTwo(chngToIN) ); |
| 105503 | - assert( chngToIN==getMask(pMaskSet, iCursor) ); | |
| 105670 | + assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); | |
| 105504 | 105671 | break; |
| 105505 | 105672 | } |
| 105506 | 105673 | testcase( j==1 ); |
| 105507 | 105674 | |
| 105508 | 105675 | /* We have found a candidate table and column. Check to see if that |
| @@ -105547,11 +105714,11 @@ | ||
| 105547 | 105714 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
| 105548 | 105715 | assert( pOrTerm->eOperator & WO_EQ ); |
| 105549 | 105716 | assert( pOrTerm->leftCursor==iCursor ); |
| 105550 | 105717 | assert( pOrTerm->u.leftColumn==iColumn ); |
| 105551 | 105718 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
| 105552 | - pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup); | |
| 105719 | + pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); | |
| 105553 | 105720 | pLeft = pOrTerm->pExpr->pLeft; |
| 105554 | 105721 | } |
| 105555 | 105722 | assert( pLeft!=0 ); |
| 105556 | 105723 | pDup = sqlite3ExprDup(db, pLeft, 0); |
| 105557 | 105724 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
| @@ -105596,10 +105763,11 @@ | ||
| 105596 | 105763 | static void exprAnalyze( |
| 105597 | 105764 | SrcList *pSrc, /* the FROM clause */ |
| 105598 | 105765 | WhereClause *pWC, /* the WHERE clause */ |
| 105599 | 105766 | int idxTerm /* Index of the term to be analyzed */ |
| 105600 | 105767 | ){ |
| 105768 | + WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ | |
| 105601 | 105769 | WhereTerm *pTerm; /* The term to be analyzed */ |
| 105602 | 105770 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
| 105603 | 105771 | Expr *pExpr; /* The expression to be analyzed */ |
| 105604 | 105772 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 105605 | 105773 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
| @@ -105606,18 +105774,18 @@ | ||
| 105606 | 105774 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
| 105607 | 105775 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 105608 | 105776 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 105609 | 105777 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
| 105610 | 105778 | int op; /* Top-level operator. pExpr->op */ |
| 105611 | - Parse *pParse = pWC->pParse; /* Parsing context */ | |
| 105779 | + Parse *pParse = pWInfo->pParse; /* Parsing context */ | |
| 105612 | 105780 | sqlite3 *db = pParse->db; /* Database connection */ |
| 105613 | 105781 | |
| 105614 | 105782 | if( db->mallocFailed ){ |
| 105615 | 105783 | return; |
| 105616 | 105784 | } |
| 105617 | 105785 | pTerm = &pWC->a[idxTerm]; |
| 105618 | - pMaskSet = pWC->pMaskSet; | |
| 105786 | + pMaskSet = &pWInfo->sMaskSet; | |
| 105619 | 105787 | pExpr = pTerm->pExpr; |
| 105620 | 105788 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
| 105621 | 105789 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
| 105622 | 105790 | op = pExpr->op; |
| 105623 | 105791 | if( op==TK_IN ){ |
| @@ -105891,15 +106059,12 @@ | ||
| 105891 | 106059 | */ |
| 105892 | 106060 | pTerm->prereqRight |= extraRight; |
| 105893 | 106061 | } |
| 105894 | 106062 | |
| 105895 | 106063 | /* |
| 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. | |
| 106064 | +** This function searches pList for a entry that matches the iCol-th column | |
| 106065 | +** of index pIdx. | |
| 105901 | 106066 | ** |
| 105902 | 106067 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 105903 | 106068 | ** no expression is found, -1 is returned. |
| 105904 | 106069 | */ |
| 105905 | 106070 | static int findIndexCol( |
| @@ -105925,82 +106090,23 @@ | ||
| 105925 | 106090 | } |
| 105926 | 106091 | } |
| 105927 | 106092 | |
| 105928 | 106093 | return -1; |
| 105929 | 106094 | } |
| 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 | 106095 | |
| 105991 | 106096 | /* |
| 105992 | 106097 | ** 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. | |
| 106098 | +** is redundant. | |
| 106099 | +** | |
| 106100 | +** A DISTINCT list is redundant if the database contains some subset of | |
| 106101 | +** columns that are unique and non-null. | |
| 105996 | 106102 | */ |
| 105997 | 106103 | static int isDistinctRedundant( |
| 105998 | - Parse *pParse, | |
| 105999 | - SrcList *pTabList, | |
| 106000 | - WhereClause *pWC, | |
| 106001 | - ExprList *pDistinct | |
| 106104 | + Parse *pParse, /* Parsing context */ | |
| 106105 | + SrcList *pTabList, /* The FROM clause */ | |
| 106106 | + WhereClause *pWC, /* The WHERE clause */ | |
| 106107 | + ExprList *pDistinct /* The result set that needs to be DISTINCT */ | |
| 106002 | 106108 | ){ |
| 106003 | 106109 | Table *pTab; |
| 106004 | 106110 | Index *pIdx; |
| 106005 | 106111 | int i; |
| 106006 | 106112 | int iBase; |
| @@ -106051,35 +106157,90 @@ | ||
| 106051 | 106157 | } |
| 106052 | 106158 | } |
| 106053 | 106159 | |
| 106054 | 106160 | return 0; |
| 106055 | 106161 | } |
| 106162 | + | |
| 106163 | +/* | |
| 106164 | +** The (an approximate) sum of two WhereCosts. This computation is | |
| 106165 | +** not a simple "+" operator because WhereCost is stored as a logarithmic | |
| 106166 | +** value. | |
| 106167 | +** | |
| 106168 | +*/ | |
| 106169 | +static WhereCost whereCostAdd(WhereCost a, WhereCost b){ | |
| 106170 | + static const unsigned char x[] = { | |
| 106171 | + 10, 10, /* 0,1 */ | |
| 106172 | + 9, 9, /* 2,3 */ | |
| 106173 | + 8, 8, /* 4,5 */ | |
| 106174 | + 7, 7, 7, /* 6,7,8 */ | |
| 106175 | + 6, 6, 6, /* 9,10,11 */ | |
| 106176 | + 5, 5, 5, /* 12-14 */ | |
| 106177 | + 4, 4, 4, 4, /* 15-18 */ | |
| 106178 | + 3, 3, 3, 3, 3, 3, /* 19-24 */ | |
| 106179 | + 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ | |
| 106180 | + }; | |
| 106181 | + if( a>=b ){ | |
| 106182 | + if( a>b+49 ) return a; | |
| 106183 | + if( a>b+31 ) return a+1; | |
| 106184 | + return a+x[a-b]; | |
| 106185 | + }else{ | |
| 106186 | + if( b>a+49 ) return b; | |
| 106187 | + if( b>a+31 ) return b+1; | |
| 106188 | + return b+x[b-a]; | |
| 106189 | + } | |
| 106190 | +} | |
| 106056 | 106191 | |
| 106057 | 106192 | /* |
| 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. | |
| 106193 | +** Convert an integer into a WhereCost. In other words, compute a | |
| 106194 | +** good approximatation for 10*log2(x). | |
| 106063 | 106195 | */ |
| 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; | |
| 106196 | +static WhereCost whereCost(tRowcnt x){ | |
| 106197 | + static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; | |
| 106198 | + WhereCost y = 40; | |
| 106199 | + if( x<8 ){ | |
| 106200 | + if( x<2 ) return 0; | |
| 106201 | + while( x<8 ){ y -= 10; x <<= 1; } | |
| 106202 | + }else{ | |
| 106203 | + while( x>255 ){ y += 40; x >>= 4; } | |
| 106204 | + while( x>15 ){ y += 10; x >>= 1; } | |
| 106205 | + } | |
| 106206 | + return a[x&7] + y - 10; | |
| 106207 | +} | |
| 106208 | + | |
| 106209 | +#ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 106210 | +/* | |
| 106211 | +** Convert a double (as received from xBestIndex of a virtual table) | |
| 106212 | +** into a WhereCost. In other words, compute an approximation for | |
| 106213 | +** 10*log2(x). | |
| 106214 | +*/ | |
| 106215 | +static WhereCost whereCostFromDouble(double x){ | |
| 106216 | + u64 a; | |
| 106217 | + WhereCost e; | |
| 106218 | + assert( sizeof(x)==8 && sizeof(a)==8 ); | |
| 106219 | + if( x<=1 ) return 0; | |
| 106220 | + if( x<=2000000000 ) return whereCost((tRowcnt)x); | |
| 106221 | + memcpy(&a, &x, 8); | |
| 106222 | + e = (a>>52) - 1022; | |
| 106223 | + return e*10; | |
| 106224 | +} | |
| 106225 | +#endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
| 106226 | + | |
| 106227 | +/* | |
| 106228 | +** Estimate the logarithm of the input value to base 2. | |
| 106229 | +*/ | |
| 106230 | +static WhereCost estLog(WhereCost N){ | |
| 106231 | + WhereCost x = whereCost(N); | |
| 106232 | + return x>33 ? x - 33 : 0; | |
| 106072 | 106233 | } |
| 106073 | 106234 | |
| 106074 | 106235 | /* |
| 106075 | 106236 | ** Two routines for printing the content of an sqlite3_index_info |
| 106076 | 106237 | ** structure. Used for testing and debugging only. If neither |
| 106077 | 106238 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 106078 | 106239 | ** are no-ops. |
| 106079 | 106240 | */ |
| 106080 | -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG) | |
| 106241 | +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) | |
| 106081 | 106242 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 106082 | 106243 | int i; |
| 106083 | 106244 | if( !sqlite3WhereTrace ) return; |
| 106084 | 106245 | for(i=0; i<p->nConstraint; i++){ |
| 106085 | 106246 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| @@ -106113,111 +106274,10 @@ | ||
| 106113 | 106274 | #else |
| 106114 | 106275 | #define TRACE_IDX_INPUTS(A) |
| 106115 | 106276 | #define TRACE_IDX_OUTPUTS(A) |
| 106116 | 106277 | #endif |
| 106117 | 106278 | |
| 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 | 106279 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 106220 | 106280 | /* |
| 106221 | 106281 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 106222 | 106282 | ** could be used with an index to access pSrc, assuming an appropriate |
| 106223 | 106283 | ** index existed. |
| @@ -106229,92 +106289,17 @@ | ||
| 106229 | 106289 | ){ |
| 106230 | 106290 | char aff; |
| 106231 | 106291 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
| 106232 | 106292 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
| 106233 | 106293 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
| 106294 | + if( pTerm->u.leftColumn<0 ) return 0; | |
| 106234 | 106295 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 106235 | 106296 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 106236 | 106297 | return 1; |
| 106237 | 106298 | } |
| 106238 | 106299 | #endif |
| 106239 | 106300 | |
| 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 | 106301 | |
| 106317 | 106302 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 106318 | 106303 | /* |
| 106319 | 106304 | ** Generate code to construct the Index object for an automatic index |
| 106320 | 106305 | ** and to set up the WhereLevel object pLevel so that the code generator |
| @@ -106340,12 +106325,14 @@ | ||
| 106340 | 106325 | int regRecord; /* Register holding an index record */ |
| 106341 | 106326 | int n; /* Column counter */ |
| 106342 | 106327 | int i; /* Loop counter */ |
| 106343 | 106328 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
| 106344 | 106329 | CollSeq *pColl; /* Collating sequence to on a column */ |
| 106330 | + WhereLoop *pLoop; /* The Loop object */ | |
| 106345 | 106331 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 106346 | 106332 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 106333 | + const int mxConstraint = 10; /* Maximum number of constraints */ | |
| 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. */ |
| 106350 | 106337 | v = pParse->pVdbe; |
| 106351 | 106338 | assert( v!=0 ); |
| @@ -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 | - for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ | |
| 106348 | + for(pTerm=pWC->a; pTerm<pWCEnd && pLoop->nLTerm<mxConstraint; 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,11 @@ | ||
| 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); | |
| 106415 | 106406 | if( (idxCols & cMask)==0 ){ |
| 106416 | 106407 | Expr *pX = pTerm->pExpr; |
| 106417 | 106408 | idxCols |= cMask; |
| 106418 | 106409 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 106419 | 106410 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| @@ -106420,22 +106411,22 @@ | ||
| 106420 | 106411 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
| 106421 | 106412 | n++; |
| 106422 | 106413 | } |
| 106423 | 106414 | } |
| 106424 | 106415 | } |
| 106425 | - assert( (u32)n==pLevel->plan.nEq ); | |
| 106416 | + assert( (u32)n==pLoop->u.btree.nEq ); | |
| 106426 | 106417 | |
| 106427 | 106418 | /* Add additional columns needed to make the automatic index into |
| 106428 | 106419 | ** a covering index */ |
| 106429 | 106420 | for(i=0; i<mxBitCol; i++){ |
| 106430 | - if( extraCols & (((Bitmask)1)<<i) ){ | |
| 106421 | + if( extraCols & MASKBIT(i) ){ | |
| 106431 | 106422 | pIdx->aiColumn[n] = i; |
| 106432 | 106423 | pIdx->azColl[n] = "BINARY"; |
| 106433 | 106424 | n++; |
| 106434 | 106425 | } |
| 106435 | 106426 | } |
| 106436 | - if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ | |
| 106427 | + if( pSrc->colUsed & MASKBIT(BMS-1) ){ | |
| 106437 | 106428 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 106438 | 106429 | pIdx->aiColumn[n] = i; |
| 106439 | 106430 | pIdx->azColl[n] = "BINARY"; |
| 106440 | 106431 | n++; |
| 106441 | 106432 | } |
| @@ -106443,10 +106434,11 @@ | ||
| 106443 | 106434 | assert( n==nColumn ); |
| 106444 | 106435 | |
| 106445 | 106436 | /* Create the automatic index */ |
| 106446 | 106437 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 106447 | 106438 | assert( pLevel->iIdxCur>=0 ); |
| 106439 | + pLevel->iIdxCur = pParse->nTab++; | |
| 106448 | 106440 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
| 106449 | 106441 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
| 106450 | 106442 | VdbeComment((v, "for %s", pTable->zName)); |
| 106451 | 106443 | |
| 106452 | 106444 | /* Fill the automatic index with content */ |
| @@ -106469,26 +106461,25 @@ | ||
| 106469 | 106461 | /* |
| 106470 | 106462 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 106471 | 106463 | ** responsibility of the caller to eventually release the structure |
| 106472 | 106464 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 106473 | 106465 | */ |
| 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; | |
| 106466 | +static sqlite3_index_info *allocateIndexInfo( | |
| 106467 | + Parse *pParse, | |
| 106468 | + WhereClause *pWC, | |
| 106469 | + struct SrcList_item *pSrc, | |
| 106470 | + ExprList *pOrderBy | |
| 106471 | +){ | |
| 106479 | 106472 | int i, j; |
| 106480 | 106473 | int nTerm; |
| 106481 | 106474 | struct sqlite3_index_constraint *pIdxCons; |
| 106482 | 106475 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 106483 | 106476 | struct sqlite3_index_constraint_usage *pUsage; |
| 106484 | 106477 | WhereTerm *pTerm; |
| 106485 | 106478 | int nOrderBy; |
| 106486 | 106479 | sqlite3_index_info *pIdxInfo; |
| 106487 | 106480 | |
| 106488 | - WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName)); | |
| 106489 | - | |
| 106490 | 106481 | /* Count the number of possible WHERE clause constraints referring |
| 106491 | 106482 | ** to this virtual table */ |
| 106492 | 106483 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 106493 | 106484 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 106494 | 106485 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| @@ -106520,11 +106511,10 @@ | ||
| 106520 | 106511 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 106521 | 106512 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 106522 | 106513 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 106523 | 106514 | if( pIdxInfo==0 ){ |
| 106524 | 106515 | sqlite3ErrorMsg(pParse, "out of memory"); |
| 106525 | - /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ | |
| 106526 | 106516 | return 0; |
| 106527 | 106517 | } |
| 106528 | 106518 | |
| 106529 | 106519 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 106530 | 106520 | ** many fields that are declared "const" to prevent xBestIndex from |
| @@ -106576,12 +106566,12 @@ | ||
| 106576 | 106566 | } |
| 106577 | 106567 | |
| 106578 | 106568 | /* |
| 106579 | 106569 | ** The table object reference passed as the second argument to this function |
| 106580 | 106570 | ** 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. | |
| 106571 | +** method of the virtual table with the sqlite3_index_info object that | |
| 106572 | +** comes in as the 3rd argument to this function. | |
| 106583 | 106573 | ** |
| 106584 | 106574 | ** If an error occurs, pParse is populated with an error message and a |
| 106585 | 106575 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 106586 | 106576 | ** part of the sqlite3_index_info structure is left populated. |
| 106587 | 106577 | ** |
| @@ -106592,11 +106582,10 @@ | ||
| 106592 | 106582 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
| 106593 | 106583 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
| 106594 | 106584 | int i; |
| 106595 | 106585 | int rc; |
| 106596 | 106586 | |
| 106597 | - WHERETRACE(("xBestIndex for %s\n", pTab->zName)); | |
| 106598 | 106587 | TRACE_IDX_INPUTS(p); |
| 106599 | 106588 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 106600 | 106589 | TRACE_IDX_OUTPUTS(p); |
| 106601 | 106590 | |
| 106602 | 106591 | if( rc!=SQLITE_OK ){ |
| @@ -106618,211 +106607,12 @@ | ||
| 106618 | 106607 | } |
| 106619 | 106608 | } |
| 106620 | 106609 | |
| 106621 | 106610 | return pParse->nErr; |
| 106622 | 106611 | } |
| 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 */ | |
| 106612 | +#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ | |
| 106613 | + | |
| 106824 | 106614 | |
| 106825 | 106615 | #ifdef SQLITE_ENABLE_STAT3 |
| 106826 | 106616 | /* |
| 106827 | 106617 | ** Estimate the location of a particular key among all keys in an |
| 106828 | 106618 | ** index. Store the results in aStat as follows: |
| @@ -107060,11 +106850,11 @@ | ||
| 107060 | 106850 | Parse *pParse, /* Parsing & code generating context */ |
| 107061 | 106851 | Index *p, /* The index containing the range-compared column; "x" */ |
| 107062 | 106852 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 107063 | 106853 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 107064 | 106854 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 107065 | - double *pRangeDiv /* OUT: Reduce search space by this divisor */ | |
| 106855 | + WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */ | |
| 107066 | 106856 | ){ |
| 107067 | 106857 | int rc = SQLITE_OK; |
| 107068 | 106858 | |
| 107069 | 106859 | #ifdef SQLITE_ENABLE_STAT3 |
| 107070 | 106860 | |
| @@ -107098,29 +106888,35 @@ | ||
| 107098 | 106888 | if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1]; |
| 107099 | 106889 | } |
| 107100 | 106890 | sqlite3ValueFree(pRangeVal); |
| 107101 | 106891 | } |
| 107102 | 106892 | 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); | |
| 106893 | + WhereCost iBase = whereCost(p->aiRowEst[0]); | |
| 106894 | + if( iUpper>iLower ){ | |
| 106895 | + iBase -= whereCost(iUpper - iLower); | |
| 107107 | 106896 | } |
| 107108 | - WHERETRACE(("range scan regions: %u..%u div=%g\n", | |
| 107109 | - (u32)iLower, (u32)iUpper, *pRangeDiv)); | |
| 106897 | + *pRangeDiv = iBase; | |
| 106898 | + WHERETRACE(0x100, ("range scan regions: %u..%u div=%d\n", | |
| 106899 | + (u32)iLower, (u32)iUpper, *pRangeDiv)); | |
| 107110 | 106900 | return SQLITE_OK; |
| 107111 | 106901 | } |
| 107112 | 106902 | } |
| 107113 | 106903 | #else |
| 107114 | 106904 | UNUSED_PARAMETER(pParse); |
| 107115 | 106905 | UNUSED_PARAMETER(p); |
| 107116 | 106906 | UNUSED_PARAMETER(nEq); |
| 107117 | 106907 | #endif |
| 107118 | 106908 | assert( pLower || pUpper ); |
| 107119 | - *pRangeDiv = (double)1; | |
| 107120 | - if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4; | |
| 107121 | - if( pUpper ) *pRangeDiv *= (double)4; | |
| 106909 | + *pRangeDiv = 0; | |
| 106910 | + /* TUNING: Each inequality constraint reduces the search space 4-fold. | |
| 106911 | + ** A BETWEEN operator, therefore, reduces the search space 16-fold */ | |
| 106912 | + if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){ | |
| 106913 | + *pRangeDiv += 20; assert( 20==whereCost(4) ); | |
| 106914 | + } | |
| 106915 | + if( pUpper ){ | |
| 106916 | + *pRangeDiv += 20; assert( 20==whereCost(4) ); | |
| 106917 | + } | |
| 107122 | 106918 | return rc; |
| 107123 | 106919 | } |
| 107124 | 106920 | |
| 107125 | 106921 | #ifdef SQLITE_ENABLE_STAT3 |
| 107126 | 106922 | /* |
| @@ -107142,11 +106938,11 @@ | ||
| 107142 | 106938 | */ |
| 107143 | 106939 | static int whereEqualScanEst( |
| 107144 | 106940 | Parse *pParse, /* Parsing & code generating context */ |
| 107145 | 106941 | Index *p, /* The index whose left-most column is pTerm */ |
| 107146 | 106942 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 107147 | - double *pnRow /* Write the revised row estimate here */ | |
| 106943 | + tRowcnt *pnRow /* Write the revised row estimate here */ | |
| 107148 | 106944 | ){ |
| 107149 | 106945 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
| 107150 | 106946 | u8 aff; /* Column affinity */ |
| 107151 | 106947 | int rc; /* Subfunction return code */ |
| 107152 | 106948 | tRowcnt a[2]; /* Statistics */ |
| @@ -107161,11 +106957,11 @@ | ||
| 107161 | 106957 | pRhs = sqlite3ValueNew(pParse->db); |
| 107162 | 106958 | } |
| 107163 | 106959 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
| 107164 | 106960 | rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 107165 | 106961 | if( rc==SQLITE_OK ){ |
| 107166 | - WHERETRACE(("equality scan regions: %d\n", (int)a[1])); | |
| 106962 | + WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1])); | |
| 107167 | 106963 | *pnRow = a[1]; |
| 107168 | 106964 | } |
| 107169 | 106965 | whereEqualScanEst_cancel: |
| 107170 | 106966 | sqlite3ValueFree(pRhs); |
| 107171 | 106967 | return rc; |
| @@ -107191,16 +106987,16 @@ | ||
| 107191 | 106987 | */ |
| 107192 | 106988 | static int whereInScanEst( |
| 107193 | 106989 | Parse *pParse, /* Parsing & code generating context */ |
| 107194 | 106990 | Index *p, /* The index whose left-most column is pTerm */ |
| 107195 | 106991 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 107196 | - double *pnRow /* Write the revised row estimate here */ | |
| 106992 | + tRowcnt *pnRow /* Write the revised row estimate here */ | |
| 107197 | 106993 | ){ |
| 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 */ | |
| 106994 | + int rc = SQLITE_OK; /* Subfunction return code */ | |
| 106995 | + tRowcnt nEst; /* Number of rows for a single term */ | |
| 106996 | + tRowcnt nRowEst = 0; /* New estimate of the number of rows */ | |
| 106997 | + int i; /* Loop counter */ | |
| 107202 | 106998 | |
| 107203 | 106999 | assert( p->aSample!=0 ); |
| 107204 | 107000 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 107205 | 107001 | nEst = p->aiRowEst[0]; |
| 107206 | 107002 | rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| @@ -107207,888 +107003,15 @@ | ||
| 107207 | 107003 | nRowEst += nEst; |
| 107208 | 107004 | } |
| 107209 | 107005 | if( rc==SQLITE_OK ){ |
| 107210 | 107006 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 107211 | 107007 | *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 | -} | |
| 107008 | + WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst)); | |
| 107009 | + } | |
| 107010 | + return rc; | |
| 107011 | +} | |
| 107012 | +#endif /* defined(SQLITE_ENABLE_STAT3) */ | |
| 108090 | 107013 | |
| 108091 | 107014 | /* |
| 108092 | 107015 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 108093 | 107016 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 108094 | 107017 | ** or USING clause of that join. |
| @@ -108183,10 +107106,11 @@ | ||
| 108183 | 107106 | static int codeEqualityTerm( |
| 108184 | 107107 | Parse *pParse, /* The parsing context */ |
| 108185 | 107108 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
| 108186 | 107109 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 108187 | 107110 | int iEq, /* Index of the equality term within this level */ |
| 107111 | + int bRev, /* True for reverse-order IN operations */ | |
| 108188 | 107112 | int iTarget /* Attempt to leave results in this register */ |
| 108189 | 107113 | ){ |
| 108190 | 107114 | Expr *pX = pTerm->pExpr; |
| 108191 | 107115 | Vdbe *v = pParse->pVdbe; |
| 108192 | 107116 | int iReg; /* Register holding results */ |
| @@ -108200,18 +107124,17 @@ | ||
| 108200 | 107124 | #ifndef SQLITE_OMIT_SUBQUERY |
| 108201 | 107125 | }else{ |
| 108202 | 107126 | int eType; |
| 108203 | 107127 | int iTab; |
| 108204 | 107128 | struct InLoop *pIn; |
| 108205 | - u8 bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0; | |
| 107129 | + WhereLoop *pLoop = pLevel->pWLoop; | |
| 108206 | 107130 | |
| 108207 | - if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 | |
| 108208 | - && pLevel->plan.u.pIdx->aSortOrder[iEq] | |
| 107131 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 | |
| 107132 | + && pLoop->u.btree.pIndex!=0 | |
| 107133 | + && pLoop->u.btree.pIndex->aSortOrder[iEq] | |
| 108209 | 107134 | ){ |
| 108210 | 107135 | testcase( iEq==0 ); |
| 108211 | - testcase( iEq==pLevel->plan.u.pIdx->nColumn-1 ); | |
| 108212 | - testcase( iEq>0 && iEq+1<pLevel->plan.u.pIdx->nColumn ); | |
| 108213 | 107136 | testcase( bRev ); |
| 108214 | 107137 | bRev = !bRev; |
| 108215 | 107138 | } |
| 108216 | 107139 | assert( pX->op==TK_IN ); |
| 108217 | 107140 | iReg = iTarget; |
| @@ -108220,11 +107143,12 @@ | ||
| 108220 | 107143 | testcase( bRev ); |
| 108221 | 107144 | bRev = !bRev; |
| 108222 | 107145 | } |
| 108223 | 107146 | iTab = pX->iTable; |
| 108224 | 107147 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 108225 | - assert( pLevel->plan.wsFlags & WHERE_IN_ABLE ); | |
| 107148 | + assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); | |
| 107149 | + pLoop->wsFlags |= WHERE_IN_ABLE; | |
| 108226 | 107150 | if( pLevel->u.in.nIn==0 ){ |
| 108227 | 107151 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 108228 | 107152 | } |
| 108229 | 107153 | pLevel->u.in.nIn++; |
| 108230 | 107154 | pLevel->u.in.aInLoop = |
| @@ -108292,31 +107216,35 @@ | ||
| 108292 | 107216 | static int codeAllEqualityTerms( |
| 108293 | 107217 | Parse *pParse, /* Parsing context */ |
| 108294 | 107218 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 108295 | 107219 | WhereClause *pWC, /* The WHERE clause */ |
| 108296 | 107220 | 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,20 @@ | ||
| 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( pIndex==0 ) return 0; | |
| 107341 | + if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ | |
| 108415 | 107342 | return 0; |
| 108416 | 107343 | } |
| 108417 | 107344 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
| 108418 | 107345 | txt.db = db; |
| 108419 | 107346 | sqlite3StrAccumAppend(&txt, " (", 2); |
| @@ -108420,15 +107347,15 @@ | ||
| 108420 | 107347 | for(i=0; i<nEq; i++){ |
| 108421 | 107348 | explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "="); |
| 108422 | 107349 | } |
| 108423 | 107350 | |
| 108424 | 107351 | j = i; |
| 108425 | - if( pPlan->wsFlags&WHERE_BTM_LIMIT ){ | |
| 107352 | + if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ | |
| 108426 | 107353 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 108427 | 107354 | explainAppendTerm(&txt, i++, z, ">"); |
| 108428 | 107355 | } |
| 108429 | - if( pPlan->wsFlags&WHERE_TOP_LIMIT ){ | |
| 107356 | + if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ | |
| 108430 | 107357 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 108431 | 107358 | explainAppendTerm(&txt, i, z, "<"); |
| 108432 | 107359 | } |
| 108433 | 107360 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 108434 | 107361 | return sqlite3StrAccumFinish(&txt); |
| @@ -108447,24 +107374,26 @@ | ||
| 108447 | 107374 | int iLevel, /* Value for "level" column of output */ |
| 108448 | 107375 | int iFrom, /* Value for "from" column of output */ |
| 108449 | 107376 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 108450 | 107377 | ){ |
| 108451 | 107378 | if( pParse->explain==2 ){ |
| 108452 | - u32 flags = pLevel->plan.wsFlags; | |
| 108453 | 107379 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 108454 | 107380 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 108455 | 107381 | sqlite3 *db = pParse->db; /* Database handle */ |
| 108456 | 107382 | char *zMsg; /* Text to add to EQP output */ |
| 108457 | - sqlite3_int64 nRow; /* Expected number of rows visited by scan */ | |
| 108458 | 107383 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 108459 | 107384 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 107385 | + WhereLoop *pLoop; /* The controlling WhereLoop object */ | |
| 107386 | + u32 flags; /* Flags that describe this loop */ | |
| 108460 | 107387 | |
| 107388 | + pLoop = pLevel->pWLoop; | |
| 107389 | + flags = pLoop->wsFlags; | |
| 108461 | 107390 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
| 108462 | 107391 | |
| 108463 | - isSearch = (pLevel->plan.nEq>0) | |
| 108464 | - || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 | |
| 108465 | - || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); | |
| 107392 | + isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 | |
| 107393 | + || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) | |
| 107394 | + || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); | |
| 108466 | 107395 | |
| 108467 | 107396 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
| 108468 | 107397 | if( pItem->pSelect ){ |
| 108469 | 107398 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
| 108470 | 107399 | }else{ |
| @@ -108472,24 +107401,26 @@ | ||
| 108472 | 107401 | } |
| 108473 | 107402 | |
| 108474 | 107403 | if( pItem->zAlias ){ |
| 108475 | 107404 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 108476 | 107405 | } |
| 108477 | - if( (flags & WHERE_INDEXED)!=0 ){ | |
| 108478 | - char *zWhere = explainIndexRange(db, pLevel, pItem->pTab); | |
| 107406 | + if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 | |
| 107407 | + && pLoop->u.btree.pIndex!=0 | |
| 107408 | + ){ | |
| 107409 | + char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); | |
| 108479 | 107410 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, |
| 108480 | 107411 | ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""), |
| 108481 | 107412 | ((flags & WHERE_IDX_ONLY)?"COVERING ":""), |
| 108482 | 107413 | ((flags & WHERE_TEMP_INDEX)?"":" "), |
| 108483 | - ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName), | |
| 107414 | + ((flags & WHERE_TEMP_INDEX)?"": pLoop->u.btree.pIndex->zName), | |
| 108484 | 107415 | zWhere |
| 108485 | 107416 | ); |
| 108486 | 107417 | sqlite3DbFree(db, zWhere); |
| 108487 | - }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){ | |
| 107418 | + }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ | |
| 108488 | 107419 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
| 108489 | 107420 | |
| 108490 | - if( flags&WHERE_ROWID_EQ ){ | |
| 107421 | + if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ | |
| 108491 | 107422 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
| 108492 | 107423 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
| 108493 | 107424 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 108494 | 107425 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 108495 | 107426 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
| @@ -108497,22 +107428,15 @@ | ||
| 108497 | 107428 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 108498 | 107429 | } |
| 108499 | 107430 | } |
| 108500 | 107431 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 108501 | 107432 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
| 108502 | - sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx; | |
| 108503 | 107433 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
| 108504 | - pVtabIdx->idxNum, pVtabIdx->idxStr); | |
| 107434 | + pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); | |
| 108505 | 107435 | } |
| 108506 | 107436 | #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); | |
| 107437 | + zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); | |
| 108514 | 107438 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
| 108515 | 107439 | } |
| 108516 | 107440 | } |
| 108517 | 107441 | #else |
| 108518 | 107442 | # define explainOneScan(u,v,w,x,y,z) |
| @@ -108524,19 +107448,19 @@ | ||
| 108524 | 107448 | ** implementation described by pWInfo. |
| 108525 | 107449 | */ |
| 108526 | 107450 | static Bitmask codeOneLoopStart( |
| 108527 | 107451 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 108528 | 107452 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 108529 | - u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ | |
| 108530 | 107453 | Bitmask notReady /* Which tables are currently available */ |
| 108531 | 107454 | ){ |
| 108532 | 107455 | int j, k; /* Loop counters */ |
| 108533 | 107456 | int iCur; /* The VDBE cursor for the table */ |
| 108534 | 107457 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 108535 | 107458 | int omitTable; /* True if we use the index only */ |
| 108536 | 107459 | int bRev; /* True if we need to scan in reverse order */ |
| 108537 | 107460 | WhereLevel *pLevel; /* The where level to be coded */ |
| 107461 | + WhereLoop *pLoop; /* The WhereLoop object being coded */ | |
| 108538 | 107462 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 108539 | 107463 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 108540 | 107464 | Parse *pParse; /* Parsing context */ |
| 108541 | 107465 | Vdbe *v; /* The prepared stmt under constructions */ |
| 108542 | 107466 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
| @@ -108546,17 +107470,18 @@ | ||
| 108546 | 107470 | int iReleaseReg = 0; /* Temp register to free before returning */ |
| 108547 | 107471 | Bitmask newNotReady; /* Return value */ |
| 108548 | 107472 | |
| 108549 | 107473 | pParse = pWInfo->pParse; |
| 108550 | 107474 | v = pParse->pVdbe; |
| 108551 | - pWC = pWInfo->pWC; | |
| 107475 | + pWC = &pWInfo->sWC; | |
| 108552 | 107476 | pLevel = &pWInfo->a[iLevel]; |
| 107477 | + pLoop = pLevel->pWLoop; | |
| 108553 | 107478 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 108554 | 107479 | 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; | |
| 107480 | + bRev = (pWInfo->revMask>>iLevel)&1; | |
| 107481 | + omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 | |
| 107482 | + && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; | |
| 108558 | 107483 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
| 108559 | 107484 | |
| 108560 | 107485 | /* Create labels for the "break" and "continue" instructions |
| 108561 | 107486 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 108562 | 107487 | ** Jump to cont to go immediately to the next iteration of the |
| @@ -108589,51 +107514,41 @@ | ||
| 108589 | 107514 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 108590 | 107515 | pLevel->op = OP_Goto; |
| 108591 | 107516 | }else |
| 108592 | 107517 | |
| 108593 | 107518 | #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 | |
| 107519 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ | |
| 107520 | + /* Case 1: The table is a virtual-table. Use the VFilter and VNext | |
| 108596 | 107521 | ** to access the data. |
| 108597 | 107522 | */ |
| 108598 | 107523 | int iReg; /* P3 Value for OP_VFilter */ |
| 108599 | 107524 | 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; | |
| 107525 | + int nConstraint = pLoop->nLTerm; | |
| 108606 | 107526 | |
| 108607 | 107527 | sqlite3ExprCachePush(pParse); |
| 108608 | 107528 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 108609 | 107529 | 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 | 107530 | for(j=0; j<nConstraint; j++){ |
| 108632 | - if( aUsage[j].omit ){ | |
| 108633 | - int iTerm = aConstraint[j].iTermOffset; | |
| 108634 | - disableTerm(pLevel, &pWC->a[iTerm]); | |
| 107531 | + int iTarget = iReg+j+2; | |
| 107532 | + pTerm = pLoop->aLTerm[j]; | |
| 107533 | + if( pTerm==0 ) continue; | |
| 107534 | + if( pTerm->eOperator & WO_IN ){ | |
| 107535 | + codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); | |
| 107536 | + addrNotFound = pLevel->addrNxt; | |
| 107537 | + }else{ | |
| 107538 | + sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); | |
| 107539 | + } | |
| 107540 | + } | |
| 107541 | + sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); | |
| 107542 | + sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); | |
| 107543 | + sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, | |
| 107544 | + pLoop->u.vtab.idxStr, | |
| 107545 | + pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); | |
| 107546 | + pLoop->u.vtab.needFree = 0; | |
| 107547 | + for(j=0; j<nConstraint && j<16; j++){ | |
| 107548 | + if( (pLoop->u.vtab.omitMask>>j)&1 ){ | |
| 107549 | + disableTerm(pLevel, pLoop->aLTerm[j]); | |
| 108635 | 107550 | } |
| 108636 | 107551 | } |
| 108637 | 107552 | pLevel->op = OP_VNext; |
| 108638 | 107553 | pLevel->p1 = iCur; |
| 108639 | 107554 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| @@ -108640,41 +107555,48 @@ | ||
| 108640 | 107555 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
| 108641 | 107556 | sqlite3ExprCachePop(pParse, 1); |
| 108642 | 107557 | }else |
| 108643 | 107558 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 108644 | 107559 | |
| 108645 | - if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){ | |
| 108646 | - /* Case 1: We can directly reference a single row using an | |
| 107560 | + if( (pLoop->wsFlags & WHERE_IPK)!=0 | |
| 107561 | + && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 | |
| 107562 | + ){ | |
| 107563 | + /* Case 2: We can directly reference a single row using an | |
| 108647 | 107564 | ** equality comparison against the ROWID field. Or |
| 108648 | 107565 | ** we reference multiple rows using a "rowid IN (...)" |
| 108649 | 107566 | ** construct. |
| 108650 | 107567 | */ |
| 107568 | + assert( pLoop->u.btree.nEq==1 ); | |
| 108651 | 107569 | iReleaseReg = sqlite3GetTempReg(pParse); |
| 108652 | - pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0); | |
| 107570 | + pTerm = pLoop->aLTerm[0]; | |
| 108653 | 107571 | assert( pTerm!=0 ); |
| 108654 | 107572 | assert( pTerm->pExpr!=0 ); |
| 108655 | 107573 | assert( omitTable==0 ); |
| 108656 | 107574 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 108657 | - iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, iReleaseReg); | |
| 107575 | + iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); | |
| 108658 | 107576 | addrNxt = pLevel->addrNxt; |
| 108659 | 107577 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 108660 | 107578 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
| 108661 | 107579 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
| 108662 | 107580 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 108663 | 107581 | VdbeComment((v, "pk")); |
| 108664 | 107582 | 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. | |
| 107583 | + }else if( (pLoop->wsFlags & WHERE_IPK)!=0 | |
| 107584 | + && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 | |
| 107585 | + ){ | |
| 107586 | + /* Case 3: We have an inequality comparison against the ROWID field. | |
| 108667 | 107587 | */ |
| 108668 | 107588 | int testOp = OP_Noop; |
| 108669 | 107589 | int start; |
| 108670 | 107590 | int memEndValue = 0; |
| 108671 | 107591 | WhereTerm *pStart, *pEnd; |
| 108672 | 107592 | |
| 108673 | 107593 | 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); | |
| 107594 | + j = 0; | |
| 107595 | + pStart = pEnd = 0; | |
| 107596 | + if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; | |
| 107597 | + if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; | |
| 108676 | 107598 | if( bRev ){ |
| 108677 | 107599 | pTerm = pStart; |
| 108678 | 107600 | pStart = pEnd; |
| 108679 | 107601 | pEnd = pTerm; |
| 108680 | 107602 | } |
| @@ -108737,12 +107659,12 @@ | ||
| 108737 | 107659 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
| 108738 | 107660 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 108739 | 107661 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 108740 | 107662 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
| 108741 | 107663 | } |
| 108742 | - }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){ | |
| 108743 | - /* Case 3: A scan using an index. | |
| 107664 | + }else if( pLoop->wsFlags & WHERE_INDEXED ){ | |
| 107665 | + /* Case 4: A scan using an index. | |
| 108744 | 107666 | ** |
| 108745 | 107667 | ** The WHERE clause may contain zero or more equality |
| 108746 | 107668 | ** terms ("==" or "IN" operators) that refer to the N |
| 108747 | 107669 | ** left-most columns of the index. It may also contain |
| 108748 | 107670 | ** inequality constraints (>, <, >= or <=) on the indexed |
| @@ -108784,12 +107706,12 @@ | ||
| 108784 | 107706 | static const u8 aEndOp[] = { |
| 108785 | 107707 | OP_Noop, /* 0: (!end_constraints) */ |
| 108786 | 107708 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 108787 | 107709 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 108788 | 107710 | }; |
| 108789 | - int nEq = pLevel->plan.nEq; /* Number of == or IN terms */ | |
| 108790 | - int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ | |
| 107711 | + int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ | |
| 107712 | + int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ | |
| 108791 | 107713 | int regBase; /* Base register holding constraint values */ |
| 108792 | 107714 | int r1; /* Temp register */ |
| 108793 | 107715 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 108794 | 107716 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 108795 | 107717 | int startEq; /* True if range start uses ==, >= or <= */ |
| @@ -108801,11 +107723,11 @@ | ||
| 108801 | 107723 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 108802 | 107724 | int op; /* Instruction opcode */ |
| 108803 | 107725 | char *zStartAff; /* Affinity for start of range constraint */ |
| 108804 | 107726 | char *zEndAff; /* Affinity for end of range constraint */ |
| 108805 | 107727 | |
| 108806 | - pIdx = pLevel->plan.u.pIdx; | |
| 107728 | + pIdx = pLoop->u.btree.pIndex; | |
| 108807 | 107729 | iIdxCur = pLevel->iIdxCur; |
| 108808 | 107730 | k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]); |
| 108809 | 107731 | |
| 108810 | 107732 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 108811 | 107733 | ** was passed to this function to implement a "SELECT min(x) ..." |
| @@ -108813,12 +107735,12 @@ | ||
| 108813 | 107735 | ** a single iteration. This means that the first row returned |
| 108814 | 107736 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 108815 | 107737 | ** the first one after the nEq equality constraints in the index, |
| 108816 | 107738 | ** this requires some special handling. |
| 108817 | 107739 | */ |
| 108818 | - if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0 | |
| 108819 | - && (pLevel->plan.wsFlags&WHERE_ORDERED) | |
| 107740 | + if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 | |
| 107741 | + && (pWInfo->bOBSat!=0) | |
| 108820 | 107742 | && (pIdx->nColumn>nEq) |
| 108821 | 107743 | ){ |
| 108822 | 107744 | /* assert( pOrderBy->nExpr==1 ); */ |
| 108823 | 107745 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 108824 | 107746 | isMinQuery = 1; |
| @@ -108826,25 +107748,26 @@ | ||
| 108826 | 107748 | } |
| 108827 | 107749 | |
| 108828 | 107750 | /* Find any inequality constraint terms for the start and end |
| 108829 | 107751 | ** of the range. |
| 108830 | 107752 | */ |
| 108831 | - if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){ | |
| 108832 | - pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx); | |
| 107753 | + j = nEq; | |
| 107754 | + if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ | |
| 107755 | + pRangeStart = pLoop->aLTerm[j++]; | |
| 108833 | 107756 | nExtraReg = 1; |
| 108834 | 107757 | } |
| 108835 | - if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){ | |
| 108836 | - pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx); | |
| 107758 | + if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ | |
| 107759 | + pRangeEnd = pLoop->aLTerm[j++]; | |
| 108837 | 107760 | nExtraReg = 1; |
| 108838 | 107761 | } |
| 108839 | 107762 | |
| 108840 | 107763 | /* Generate code to evaluate all constraint terms using == or IN |
| 108841 | 107764 | ** and store the values of those terms in an array of registers |
| 108842 | 107765 | ** starting at regBase. |
| 108843 | 107766 | */ |
| 108844 | 107767 | regBase = codeAllEqualityTerms( |
| 108845 | - pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff | |
| 107768 | + pParse, pLevel, pWC, notReady, bRev, nExtraReg, &zStartAff | |
| 108846 | 107769 | ); |
| 108847 | 107770 | zEndAff = sqlite3DbStrDup(pParse->db, zStartAff); |
| 108848 | 107771 | addrNxt = pLevel->addrNxt; |
| 108849 | 107772 | |
| 108850 | 107773 | /* If we are doing a reverse order scan on an ascending index, or |
| @@ -108948,13 +107871,13 @@ | ||
| 108948 | 107871 | /* If there are inequality constraints, check that the value |
| 108949 | 107872 | ** of the table column that the inequality contrains is not NULL. |
| 108950 | 107873 | ** If it is, jump to the next iteration of the loop. |
| 108951 | 107874 | */ |
| 108952 | 107875 | 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 ){ | |
| 107876 | + testcase( pLoop->wsFlags & WHERE_BTM_LIMIT ); | |
| 107877 | + testcase( pLoop->wsFlags & WHERE_TOP_LIMIT ); | |
| 107878 | + if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ | |
| 108956 | 107879 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 108957 | 107880 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 108958 | 107881 | } |
| 108959 | 107882 | sqlite3ReleaseTempReg(pParse, r1); |
| 108960 | 107883 | |
| @@ -108969,28 +107892,28 @@ | ||
| 108969 | 107892 | } |
| 108970 | 107893 | |
| 108971 | 107894 | /* Record the instruction used to terminate the loop. Disable |
| 108972 | 107895 | ** WHERE clause terms made redundant by the index range scan. |
| 108973 | 107896 | */ |
| 108974 | - if( pLevel->plan.wsFlags & WHERE_UNIQUE ){ | |
| 107897 | + if( pLoop->wsFlags & WHERE_ONEROW ){ | |
| 108975 | 107898 | pLevel->op = OP_Noop; |
| 108976 | 107899 | }else if( bRev ){ |
| 108977 | 107900 | pLevel->op = OP_Prev; |
| 108978 | 107901 | }else{ |
| 108979 | 107902 | pLevel->op = OP_Next; |
| 108980 | 107903 | } |
| 108981 | 107904 | pLevel->p1 = iIdxCur; |
| 108982 | - if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){ | |
| 107905 | + if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ | |
| 108983 | 107906 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 108984 | 107907 | }else{ |
| 108985 | 107908 | assert( pLevel->p5==0 ); |
| 108986 | 107909 | } |
| 108987 | 107910 | }else |
| 108988 | 107911 | |
| 108989 | 107912 | #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 | |
| 107913 | + if( pLoop->wsFlags & WHERE_MULTI_OR ){ | |
| 107914 | + /* Case 5: Two or more separately indexed terms connected by OR | |
| 108992 | 107915 | ** |
| 108993 | 107916 | ** Example: |
| 108994 | 107917 | ** |
| 108995 | 107918 | ** CREATE TABLE t1(a,b,c,d); |
| 108996 | 107919 | ** CREATE INDEX i1 ON t1(a); |
| @@ -109039,11 +107962,11 @@ | ||
| 109039 | 107962 | int iRetInit; /* Address of regReturn init */ |
| 109040 | 107963 | int untestedTerms = 0; /* Some terms not completely tested */ |
| 109041 | 107964 | int ii; /* Loop counter */ |
| 109042 | 107965 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
| 109043 | 107966 | |
| 109044 | - pTerm = pLevel->plan.u.pTerm; | |
| 107967 | + pTerm = pLoop->aLTerm[0]; | |
| 109045 | 107968 | assert( pTerm!=0 ); |
| 109046 | 107969 | assert( pTerm->eOperator & WO_OR ); |
| 109047 | 107970 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 109048 | 107971 | pOrWc = &pTerm->u.pOrInfo->wc; |
| 109049 | 107972 | pLevel->op = OP_Return; |
| @@ -109080,11 +108003,11 @@ | ||
| 109080 | 108003 | ** over the top of the loop into the body of it. In this case the |
| 109081 | 108004 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 109082 | 108005 | ** fall through to the next instruction, just as an OP_Next does if |
| 109083 | 108006 | ** called on an uninitialized cursor. |
| 109084 | 108007 | */ |
| 109085 | - if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ | |
| 108008 | + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ | |
| 109086 | 108009 | regRowset = ++pParse->nMem; |
| 109087 | 108010 | regRowid = ++pParse->nMem; |
| 109088 | 108011 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 109089 | 108012 | } |
| 109090 | 108013 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| @@ -109131,15 +108054,15 @@ | ||
| 109131 | 108054 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 109132 | 108055 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
| 109133 | 108056 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
| 109134 | 108057 | assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed ); |
| 109135 | 108058 | if( pSubWInfo ){ |
| 109136 | - WhereLevel *pLvl; | |
| 108059 | + WhereLoop *pSubLoop; | |
| 109137 | 108060 | explainOneScan( |
| 109138 | 108061 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 109139 | 108062 | ); |
| 109140 | - if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ | |
| 108063 | + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ | |
| 109141 | 108064 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 109142 | 108065 | int r; |
| 109143 | 108066 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
| 109144 | 108067 | regRowid, 0); |
| 109145 | 108068 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| @@ -109164,17 +108087,17 @@ | ||
| 109164 | 108087 | ** processed or the index is the same as that used by all previous |
| 109165 | 108088 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 109166 | 108089 | ** pCov to NULL to indicate that no candidate covering index will |
| 109167 | 108090 | ** be available. |
| 109168 | 108091 | */ |
| 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) | |
| 108092 | + pSubLoop = pSubWInfo->a[0].pWLoop; | |
| 108093 | + if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 | |
| 108094 | + && (pSubLoop->wsFlags & WHERE_TEMP_INDEX)==0 | |
| 108095 | + && (ii==0 || pSubLoop->u.btree.pIndex==pCov) | |
| 109173 | 108096 | ){ |
| 109174 | - assert( pLvl->iIdxCur==iCovCur ); | |
| 109175 | - pCov = pLvl->plan.u.pIdx; | |
| 108097 | + assert( pSubWInfo->a[0].iIdxCur==iCovCur ); | |
| 108098 | + pCov = pSubLoop->u.btree.pIndex; | |
| 109176 | 108099 | }else{ |
| 109177 | 108100 | pCov = 0; |
| 109178 | 108101 | } |
| 109179 | 108102 | |
| 109180 | 108103 | /* Finish the loop through table entries that match term pOrTerm. */ |
| @@ -109196,23 +108119,22 @@ | ||
| 109196 | 108119 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 109197 | 108120 | }else |
| 109198 | 108121 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 109199 | 108122 | |
| 109200 | 108123 | { |
| 109201 | - /* Case 5: There is no usable index. We must do a complete | |
| 108124 | + /* Case 6: There is no usable index. We must do a complete | |
| 109202 | 108125 | ** scan of the entire table. |
| 109203 | 108126 | */ |
| 109204 | 108127 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 109205 | 108128 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 109206 | 108129 | assert( bRev==0 || bRev==1 ); |
| 109207 | - assert( omitTable==0 ); | |
| 109208 | 108130 | pLevel->op = aStep[bRev]; |
| 109209 | 108131 | pLevel->p1 = iCur; |
| 109210 | 108132 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
| 109211 | 108133 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 109212 | 108134 | } |
| 109213 | - newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur); | |
| 108135 | + newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); | |
| 109214 | 108136 | |
| 109215 | 108137 | /* Insert code to test every subexpression that can be completely |
| 109216 | 108138 | ** computed using the current set of tables. |
| 109217 | 108139 | ** |
| 109218 | 108140 | ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through |
| @@ -109290,51 +108212,1529 @@ | ||
| 109290 | 108212 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
| 109291 | 108213 | |
| 109292 | 108214 | return newNotReady; |
| 109293 | 108215 | } |
| 109294 | 108216 | |
| 109295 | -#if defined(SQLITE_TEST) | |
| 108217 | +#ifdef WHERETRACE_ENABLED | |
| 109296 | 108218 | /* |
| 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. | |
| 108219 | +** Print a WhereLoop object for debugging purposes | |
| 109301 | 108220 | */ |
| 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[] */ | |
| 108221 | +static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){ | |
| 108222 | + int nb = 1+(pTabList->nSrc+7)/8; | |
| 108223 | + struct SrcList_item *pItem = pTabList->a + p->iTab; | |
| 108224 | + Table *pTab = pItem->pTab; | |
| 108225 | + sqlite3DebugPrintf("%c %2d.%0*llx.%0*llx", p->cId, | |
| 108226 | + p->iTab, nb, p->maskSelf, nb, p->prereq); | |
| 108227 | + sqlite3DebugPrintf(" %8s", | |
| 108228 | + pItem->zAlias ? pItem->zAlias : pTab->zName); | |
| 108229 | + if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ | |
| 108230 | + if( p->u.btree.pIndex ){ | |
| 108231 | + const char *zName = p->u.btree.pIndex->zName; | |
| 108232 | + if( zName==0 ) zName = "ipk"; | |
| 108233 | + if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ | |
| 108234 | + int i = sqlite3Strlen30(zName) - 1; | |
| 108235 | + while( zName[i]!='_' ) i--; | |
| 108236 | + zName += i; | |
| 108237 | + } | |
| 108238 | + sqlite3DebugPrintf(".%-12s %2d", zName, p->u.btree.nEq); | |
| 108239 | + }else{ | |
| 108240 | + sqlite3DebugPrintf("%16s",""); | |
| 108241 | + } | |
| 108242 | + }else{ | |
| 108243 | + char *z; | |
| 108244 | + if( p->u.vtab.idxStr ){ | |
| 108245 | + z = sqlite3_mprintf("(%d,\"%s\",%x)", | |
| 108246 | + p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); | |
| 108247 | + }else{ | |
| 108248 | + z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); | |
| 108249 | + } | |
| 108250 | + sqlite3DebugPrintf(" %-15s", z); | |
| 108251 | + sqlite3_free(z); | |
| 108252 | + } | |
| 108253 | + sqlite3DebugPrintf(" fg %05x N %d", p->wsFlags, p->nLTerm); | |
| 108254 | + sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); | |
| 108255 | +} | |
| 108256 | +#endif | |
| 109304 | 108257 | |
| 109305 | -#endif /* SQLITE_TEST */ | |
| 108258 | +/* | |
| 108259 | +** Convert bulk memory into a valid WhereLoop that can be passed | |
| 108260 | +** to whereLoopClear harmlessly. | |
| 108261 | +*/ | |
| 108262 | +static void whereLoopInit(WhereLoop *p){ | |
| 108263 | + p->aLTerm = p->aLTermSpace; | |
| 108264 | + p->nLTerm = 0; | |
| 108265 | + p->nLSlot = ArraySize(p->aLTermSpace); | |
| 108266 | + p->wsFlags = 0; | |
| 108267 | +} | |
| 109306 | 108268 | |
| 108269 | +/* | |
| 108270 | +** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. | |
| 108271 | +*/ | |
| 108272 | +static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ | |
| 108273 | + if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_TEMP_INDEX) ){ | |
| 108274 | + if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ | |
| 108275 | + sqlite3_free(p->u.vtab.idxStr); | |
| 108276 | + p->u.vtab.needFree = 0; | |
| 108277 | + p->u.vtab.idxStr = 0; | |
| 108278 | + }else if( (p->wsFlags & WHERE_TEMP_INDEX)!=0 && p->u.btree.pIndex!=0 ){ | |
| 108279 | + sqlite3DbFree(db, p->u.btree.pIndex->zColAff); | |
| 108280 | + sqlite3DbFree(db, p->u.btree.pIndex); | |
| 108281 | + p->u.btree.pIndex = 0; | |
| 108282 | + } | |
| 108283 | + } | |
| 108284 | +} | |
| 108285 | + | |
| 108286 | +/* | |
| 108287 | +** Deallocate internal memory used by a WhereLoop object | |
| 108288 | +*/ | |
| 108289 | +static void whereLoopClear(sqlite3 *db, WhereLoop *p){ | |
| 108290 | + if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); | |
| 108291 | + whereLoopClearUnion(db, p); | |
| 108292 | + whereLoopInit(p); | |
| 108293 | +} | |
| 108294 | + | |
| 108295 | +/* | |
| 108296 | +** Increase the memory allocation for pLoop->aLTerm[] to be at least n. | |
| 108297 | +*/ | |
| 108298 | +static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ | |
| 108299 | + WhereTerm **paNew; | |
| 108300 | + if( p->nLSlot>=n ) return SQLITE_OK; | |
| 108301 | + n = (n+7)&~7; | |
| 108302 | + paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); | |
| 108303 | + if( paNew==0 ) return SQLITE_NOMEM; | |
| 108304 | + memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); | |
| 108305 | + if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); | |
| 108306 | + p->aLTerm = paNew; | |
| 108307 | + p->nLSlot = n; | |
| 108308 | + return SQLITE_OK; | |
| 108309 | +} | |
| 108310 | + | |
| 108311 | +/* | |
| 108312 | +** Transfer content from the second pLoop into the first. | |
| 108313 | +*/ | |
| 108314 | +static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ | |
| 108315 | + if( whereLoopResize(db, pTo, pFrom->nLTerm) ) return SQLITE_NOMEM; | |
| 108316 | + whereLoopClearUnion(db, pTo); | |
| 108317 | + memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); | |
| 108318 | + memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); | |
| 108319 | + if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ | |
| 108320 | + pFrom->u.vtab.needFree = 0; | |
| 108321 | + }else if( (pFrom->wsFlags & WHERE_TEMP_INDEX)!=0 ){ | |
| 108322 | + pFrom->u.btree.pIndex = 0; | |
| 108323 | + } | |
| 108324 | + return SQLITE_OK; | |
| 108325 | +} | |
| 108326 | + | |
| 108327 | +/* | |
| 108328 | +** Delete a WhereLoop object | |
| 108329 | +*/ | |
| 108330 | +static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ | |
| 108331 | + whereLoopClear(db, p); | |
| 108332 | + sqlite3DbFree(db, p); | |
| 108333 | +} | |
| 109307 | 108334 | |
| 109308 | 108335 | /* |
| 109309 | 108336 | ** Free a WhereInfo structure |
| 109310 | 108337 | */ |
| 109311 | 108338 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
| 109312 | 108339 | 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); | |
| 108340 | + whereClauseClear(&pWInfo->sWC); | |
| 108341 | + while( pWInfo->pLoops ){ | |
| 108342 | + WhereLoop *p = pWInfo->pLoops; | |
| 108343 | + pWInfo->pLoops = p->pNextLoop; | |
| 108344 | + whereLoopDelete(db, p); | |
| 108345 | + } | |
| 109332 | 108346 | sqlite3DbFree(db, pWInfo); |
| 109333 | 108347 | } |
| 109334 | 108348 | } |
| 109335 | 108349 | |
| 108350 | +/* | |
| 108351 | +** Insert or replace a WhereLoop entry using the template supplied. | |
| 108352 | +** | |
| 108353 | +** An existing WhereLoop entry might be overwritten if the new template | |
| 108354 | +** is better and has fewer dependencies. Or the template will be ignored | |
| 108355 | +** and no insert will occur if an existing WhereLoop is faster and has | |
| 108356 | +** fewer dependencies than the template. Otherwise a new WhereLoop is | |
| 108357 | +** added based on the template. | |
| 108358 | +** | |
| 108359 | +** If pBuilder->pBest is not NULL then we only care about the very | |
| 108360 | +** best template and that template should be stored in pBuilder->pBest. | |
| 108361 | +** If pBuilder->pBest is NULL then a list of the best templates are stored | |
| 108362 | +** in pBuilder->pWInfo->pLoops. | |
| 108363 | +** | |
| 108364 | +** When accumulating multiple loops (when pBuilder->pBest is NULL) we | |
| 108365 | +** still might overwrite similar loops with the new template if the | |
| 108366 | +** template is better. Loops may be overwritten if the following | |
| 108367 | +** conditions are met: | |
| 108368 | +** | |
| 108369 | +** (1) They have the same iTab. | |
| 108370 | +** (2) They have the same iSortIdx. | |
| 108371 | +** (3) The template has same or fewer dependencies than the current loop | |
| 108372 | +** (4) The template has the same or lower cost than the current loop | |
| 108373 | +** (5) The template uses more terms of the same index but has no additional | |
| 108374 | +** dependencies | |
| 108375 | +*/ | |
| 108376 | +static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ | |
| 108377 | + WhereLoop **ppPrev, *p, *pNext = 0; | |
| 108378 | + WhereInfo *pWInfo = pBuilder->pWInfo; | |
| 108379 | + sqlite3 *db = pWInfo->pParse->db; | |
| 108380 | + | |
| 108381 | + /* If pBuilder->pBest is defined, then only keep track of the single | |
| 108382 | + ** best WhereLoop. pBuilder->pBest->maskSelf==0 indicates that no | |
| 108383 | + ** prior WhereLoops have been evaluated and that the current pTemplate | |
| 108384 | + ** is therefore the first and hence the best and should be retained. | |
| 108385 | + */ | |
| 108386 | + if( (p = pBuilder->pBest)!=0 ){ | |
| 108387 | + if( p->maskSelf!=0 ){ | |
| 108388 | + WhereCost rCost = whereCostAdd(p->rRun,p->rSetup); | |
| 108389 | + WhereCost rTemplate = whereCostAdd(pTemplate->rRun,pTemplate->rSetup); | |
| 108390 | + if( rCost < rTemplate ){ | |
| 108391 | + goto whereLoopInsert_noop; | |
| 108392 | + } | |
| 108393 | + if( rCost == rTemplate && p->prereq <= pTemplate->prereq ){ | |
| 108394 | + goto whereLoopInsert_noop; | |
| 108395 | + } | |
| 108396 | + } | |
| 108397 | +#if WHERETRACE_ENABLED | |
| 108398 | + if( sqlite3WhereTrace & 0x8 ){ | |
| 108399 | + sqlite3DebugPrintf(p->maskSelf==0 ? "ins-init: " : "ins-best: "); | |
| 108400 | + whereLoopPrint(pTemplate, pWInfo->pTabList); | |
| 108401 | + } | |
| 108402 | +#endif | |
| 108403 | + whereLoopXfer(db, p, pTemplate); | |
| 108404 | + return SQLITE_OK; | |
| 108405 | + } | |
| 108406 | + | |
| 108407 | + /* Search for an existing WhereLoop to overwrite, or which takes | |
| 108408 | + ** priority over pTemplate. | |
| 108409 | + */ | |
| 108410 | + for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ | |
| 108411 | + if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ) continue; | |
| 108412 | + if( (p->prereq & pTemplate->prereq)==p->prereq | |
| 108413 | + && p->rSetup<=pTemplate->rSetup | |
| 108414 | + && p->rRun<=pTemplate->rRun | |
| 108415 | + ){ | |
| 108416 | + /* p is equal or better than pTemplate */ | |
| 108417 | + if( p->nLTerm<pTemplate->nLTerm | |
| 108418 | + && (p->wsFlags & WHERE_INDEXED)!=0 | |
| 108419 | + && (pTemplate->wsFlags & WHERE_INDEXED)!=0 | |
| 108420 | + && p->u.btree.pIndex==pTemplate->u.btree.pIndex | |
| 108421 | + && p->prereq==pTemplate->prereq | |
| 108422 | + ){ | |
| 108423 | + /* Overwrite an existing WhereLoop with an similar one that uses | |
| 108424 | + ** more terms of the index */ | |
| 108425 | + pNext = p->pNextLoop; | |
| 108426 | + break; | |
| 108427 | + }else if( p->nOut>pTemplate->nOut | |
| 108428 | + && p->rSetup==pTemplate->rSetup | |
| 108429 | + && p->rRun==pTemplate->rRun | |
| 108430 | + ){ | |
| 108431 | + /* Overwrite an existing WhereLoop with the same cost but more | |
| 108432 | + ** outputs */ | |
| 108433 | + pNext = p->pNextLoop; | |
| 108434 | + break; | |
| 108435 | + }else{ | |
| 108436 | + /* pTemplate is not helpful. | |
| 108437 | + ** Return without changing or adding anything */ | |
| 108438 | + goto whereLoopInsert_noop; | |
| 108439 | + } | |
| 108440 | + } | |
| 108441 | + if( (p->prereq & pTemplate->prereq)==pTemplate->prereq | |
| 108442 | + && p->rSetup>=pTemplate->rSetup | |
| 108443 | + && p->rRun>=pTemplate->rRun | |
| 108444 | + ){ | |
| 108445 | + /* Overwrite an existing WhereLoop with a better one */ | |
| 108446 | + pNext = p->pNextLoop; | |
| 108447 | + break; | |
| 108448 | + } | |
| 108449 | + } | |
| 108450 | + | |
| 108451 | + /* If we reach this point it means that either p[] should be overwritten | |
| 108452 | + ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new | |
| 108453 | + ** WhereLoop and insert it. | |
| 108454 | + */ | |
| 108455 | +#if WHERETRACE_ENABLED | |
| 108456 | + if( sqlite3WhereTrace & 0x8 ){ | |
| 108457 | + if( p!=0 ){ | |
| 108458 | + sqlite3DebugPrintf("ins-del: "); | |
| 108459 | + whereLoopPrint(p, pWInfo->pTabList); | |
| 108460 | + } | |
| 108461 | + sqlite3DebugPrintf("ins-new: "); | |
| 108462 | + whereLoopPrint(pTemplate, pWInfo->pTabList); | |
| 108463 | + } | |
| 108464 | +#endif | |
| 108465 | + if( p==0 ){ | |
| 108466 | + p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); | |
| 108467 | + if( p==0 ) return SQLITE_NOMEM; | |
| 108468 | + whereLoopInit(p); | |
| 108469 | + } | |
| 108470 | + whereLoopXfer(db, p, pTemplate); | |
| 108471 | + p->pNextLoop = pNext; | |
| 108472 | + *ppPrev = p; | |
| 108473 | + if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ | |
| 108474 | + Index *pIndex = p->u.btree.pIndex; | |
| 108475 | + if( pIndex && pIndex->tnum==0 ){ | |
| 108476 | + p->u.btree.pIndex = 0; | |
| 108477 | + } | |
| 108478 | + } | |
| 108479 | + return SQLITE_OK; | |
| 108480 | + | |
| 108481 | + /* Jump here if the insert is a no-op */ | |
| 108482 | +whereLoopInsert_noop: | |
| 108483 | +#if WHERETRACE_ENABLED | |
| 108484 | + if( sqlite3WhereTrace & 0x8 ){ | |
| 108485 | + sqlite3DebugPrintf(pBuilder->pBest ? "ins-skip: " : "ins-noop: "); | |
| 108486 | + whereLoopPrint(pTemplate, pWInfo->pTabList); | |
| 108487 | + } | |
| 108488 | +#endif | |
| 108489 | + return SQLITE_OK; | |
| 108490 | +} | |
| 108491 | + | |
| 108492 | +/* | |
| 108493 | +** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex. | |
| 108494 | +** Try to match one more. | |
| 108495 | +** | |
| 108496 | +** If pProbe->tnum==0, that means pIndex is a fake index used for the | |
| 108497 | +** INTEGER PRIMARY KEY. | |
| 108498 | +*/ | |
| 108499 | +static int whereLoopAddBtreeIndex( | |
| 108500 | + WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ | |
| 108501 | + struct SrcList_item *pSrc, /* FROM clause term being analyzed */ | |
| 108502 | + Index *pProbe, /* An index on pSrc */ | |
| 108503 | + WhereCost nInMul /* log(Number of iterations due to IN) */ | |
| 108504 | +){ | |
| 108505 | + WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ | |
| 108506 | + Parse *pParse = pWInfo->pParse; /* Parsing context */ | |
| 108507 | + sqlite3 *db = pParse->db; /* Database connection malloc context */ | |
| 108508 | + WhereLoop *pNew; /* Template WhereLoop under construction */ | |
| 108509 | + WhereTerm *pTerm; /* A WhereTerm under consideration */ | |
| 108510 | + int opMask; /* Valid operators for constraints */ | |
| 108511 | + WhereScan scan; /* Iterator for WHERE terms */ | |
| 108512 | + Bitmask saved_prereq; /* Original value of pNew->prereq */ | |
| 108513 | + u16 saved_nLTerm; /* Original value of pNew->nLTerm */ | |
| 108514 | + int saved_nEq; /* Original value of pNew->u.btree.nEq */ | |
| 108515 | + u32 saved_wsFlags; /* Original value of pNew->wsFlags */ | |
| 108516 | + WhereCost saved_nOut; /* Original value of pNew->nOut */ | |
| 108517 | + int iCol; /* Index of the column in the table */ | |
| 108518 | + int rc = SQLITE_OK; /* Return code */ | |
| 108519 | + WhereCost nRowEst; /* Estimated index selectivity */ | |
| 108520 | + WhereCost rLogSize; /* Logarithm of table size */ | |
| 108521 | + WhereTerm *pTop, *pBtm; /* Top and bottom range constraints */ | |
| 108522 | + | |
| 108523 | + pNew = pBuilder->pNew; | |
| 108524 | + if( db->mallocFailed ) return SQLITE_NOMEM; | |
| 108525 | + | |
| 108526 | + assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); | |
| 108527 | + assert( pNew->u.btree.nEq<=pProbe->nColumn ); | |
| 108528 | + assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); | |
| 108529 | + if( pNew->wsFlags & WHERE_BTM_LIMIT ){ | |
| 108530 | + opMask = WO_LT|WO_LE; | |
| 108531 | + }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ | |
| 108532 | + opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; | |
| 108533 | + }else{ | |
| 108534 | + opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE; | |
| 108535 | + } | |
| 108536 | + if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); | |
| 108537 | + | |
| 108538 | + if( pNew->u.btree.nEq < pProbe->nColumn ){ | |
| 108539 | + iCol = pProbe->aiColumn[pNew->u.btree.nEq]; | |
| 108540 | + nRowEst = whereCost(pProbe->aiRowEst[pNew->u.btree.nEq+1]); | |
| 108541 | + }else{ | |
| 108542 | + iCol = -1; | |
| 108543 | + nRowEst = 0; | |
| 108544 | + } | |
| 108545 | + pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, | |
| 108546 | + opMask, pProbe); | |
| 108547 | + saved_nEq = pNew->u.btree.nEq; | |
| 108548 | + saved_nLTerm = pNew->nLTerm; | |
| 108549 | + saved_wsFlags = pNew->wsFlags; | |
| 108550 | + saved_prereq = pNew->prereq; | |
| 108551 | + saved_nOut = pNew->nOut; | |
| 108552 | + pNew->rSetup = 0; | |
| 108553 | + rLogSize = estLog(whereCost(pProbe->aiRowEst[0])); | |
| 108554 | + for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ | |
| 108555 | + int nIn = 0; | |
| 108556 | + if( pTerm->prereqRight & pNew->maskSelf ) continue; | |
| 108557 | + pNew->wsFlags = saved_wsFlags; | |
| 108558 | + pNew->u.btree.nEq = saved_nEq; | |
| 108559 | + pNew->nLTerm = saved_nLTerm; | |
| 108560 | + if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ | |
| 108561 | + pNew->aLTerm[pNew->nLTerm++] = pTerm; | |
| 108562 | + pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; | |
| 108563 | + pNew->rRun = rLogSize; /* Baseline cost is log2(N). Adjustments below */ | |
| 108564 | + if( pTerm->eOperator & WO_IN ){ | |
| 108565 | + Expr *pExpr = pTerm->pExpr; | |
| 108566 | + pNew->wsFlags |= WHERE_COLUMN_IN; | |
| 108567 | + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ | |
| 108568 | + /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ | |
| 108569 | + nIn = 46; assert( 46==whereCost(25) ); | |
| 108570 | + }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ | |
| 108571 | + /* "x IN (value, value, ...)" */ | |
| 108572 | + nIn = whereCost(pExpr->x.pList->nExpr); | |
| 108573 | + } | |
| 108574 | + pNew->rRun += nIn; | |
| 108575 | + pNew->u.btree.nEq++; | |
| 108576 | + pNew->nOut = nRowEst + nInMul + nIn; | |
| 108577 | + }else if( pTerm->eOperator & (WO_EQ) ){ | |
| 108578 | + assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 | |
| 108579 | + || nInMul==0 ); | |
| 108580 | + pNew->wsFlags |= WHERE_COLUMN_EQ; | |
| 108581 | + if( iCol<0 | |
| 108582 | + || (pProbe->onError!=OE_None && nInMul==0 | |
| 108583 | + && pNew->u.btree.nEq==pProbe->nColumn-1) | |
| 108584 | + ){ | |
| 108585 | + testcase( pNew->wsFlags & WHERE_COLUMN_IN ); | |
| 108586 | + pNew->wsFlags |= WHERE_ONEROW; | |
| 108587 | + } | |
| 108588 | + pNew->u.btree.nEq++; | |
| 108589 | + pNew->nOut = nRowEst + nInMul; | |
| 108590 | + }else if( pTerm->eOperator & (WO_ISNULL) ){ | |
| 108591 | + pNew->wsFlags |= WHERE_COLUMN_NULL; | |
| 108592 | + pNew->u.btree.nEq++; | |
| 108593 | + /* TUNING: IS NULL selects 2 rows */ | |
| 108594 | + nIn = 10; assert( 10==whereCost(2) ); | |
| 108595 | + pNew->nOut = nRowEst + nInMul + nIn; | |
| 108596 | + }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ | |
| 108597 | + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; | |
| 108598 | + pBtm = pTerm; | |
| 108599 | + pTop = 0; | |
| 108600 | + }else if( pTerm->eOperator & (WO_LT|WO_LE) ){ | |
| 108601 | + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; | |
| 108602 | + pTop = pTerm; | |
| 108603 | + pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? | |
| 108604 | + pNew->aLTerm[pNew->nLTerm-2] : 0; | |
| 108605 | + } | |
| 108606 | + if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ | |
| 108607 | + /* Adjust nOut and rRun for STAT3 range values */ | |
| 108608 | + WhereCost rDiv; | |
| 108609 | + whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq, | |
| 108610 | + pBtm, pTop, &rDiv); | |
| 108611 | + pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10; | |
| 108612 | + } | |
| 108613 | +#ifdef SQLITE_ENABLE_STAT3 | |
| 108614 | + if( pNew->u.btree.nEq==1 && pProbe->nSample ){ | |
| 108615 | + tRowcnt nOut = 0; | |
| 108616 | + if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ | |
| 108617 | + rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut); | |
| 108618 | + }else if( (pTerm->eOperator & WO_IN) | |
| 108619 | + && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){ | |
| 108620 | + rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut); | |
| 108621 | + } | |
| 108622 | + pNew->nOut = whereCost(nOut); | |
| 108623 | + } | |
| 108624 | +#endif | |
| 108625 | + if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ | |
| 108626 | + /* Each row involves a step of the index, then a binary search of | |
| 108627 | + ** the main table */ | |
| 108628 | + pNew->rRun = whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10); | |
| 108629 | + } | |
| 108630 | + /* Step cost for each output row */ | |
| 108631 | + pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut); | |
| 108632 | + /* TBD: Adjust nOut for additional constraints */ | |
| 108633 | + rc = whereLoopInsert(pBuilder, pNew); | |
| 108634 | + if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 | |
| 108635 | + && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0)) | |
| 108636 | + ){ | |
| 108637 | + whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); | |
| 108638 | + } | |
| 108639 | + } | |
| 108640 | + pNew->prereq = saved_prereq; | |
| 108641 | + pNew->u.btree.nEq = saved_nEq; | |
| 108642 | + pNew->wsFlags = saved_wsFlags; | |
| 108643 | + pNew->nOut = saved_nOut; | |
| 108644 | + pNew->nLTerm = saved_nLTerm; | |
| 108645 | + return rc; | |
| 108646 | +} | |
| 108647 | + | |
| 108648 | +/* | |
| 108649 | +** Return True if it is possible that pIndex might be useful in | |
| 108650 | +** implementing the ORDER BY clause in pBuilder. | |
| 108651 | +** | |
| 108652 | +** Return False if pBuilder does not contain an ORDER BY clause or | |
| 108653 | +** if there is no way for pIndex to be useful in implementing that | |
| 108654 | +** ORDER BY clause. | |
| 108655 | +*/ | |
| 108656 | +static int indexMightHelpWithOrderBy( | |
| 108657 | + WhereLoopBuilder *pBuilder, | |
| 108658 | + Index *pIndex, | |
| 108659 | + int iCursor | |
| 108660 | +){ | |
| 108661 | + ExprList *pOB; | |
| 108662 | + int ii, jj; | |
| 108663 | + | |
| 108664 | + if( pIndex->bUnordered ) return 0; | |
| 108665 | + if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; | |
| 108666 | + for(ii=0; ii<pOB->nExpr; ii++){ | |
| 108667 | + Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); | |
| 108668 | + if( pExpr->op!=TK_COLUMN ) return 0; | |
| 108669 | + if( pExpr->iTable==iCursor ){ | |
| 108670 | + for(jj=0; jj<pIndex->nColumn; jj++){ | |
| 108671 | + if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; | |
| 108672 | + } | |
| 108673 | + } | |
| 108674 | + } | |
| 108675 | + return 0; | |
| 108676 | +} | |
| 108677 | + | |
| 108678 | +/* | |
| 108679 | +** Return a bitmask where 1s indicate that the corresponding column of | |
| 108680 | +** the table is used by an index. Only the first 63 columns are considered. | |
| 108681 | +*/ | |
| 108682 | +static Bitmask columnsInIndex(Index *pIdx){ | |
| 108683 | + Bitmask m = 0; | |
| 108684 | + int j; | |
| 108685 | + for(j=pIdx->nColumn-1; j>=0; j--){ | |
| 108686 | + int x = pIdx->aiColumn[j]; | |
| 108687 | + if( x<BMS-1 ) m |= MASKBIT(x); | |
| 108688 | + } | |
| 108689 | + return m; | |
| 108690 | +} | |
| 108691 | + | |
| 108692 | + | |
| 108693 | +/* | |
| 108694 | +** Add all WhereLoop objects a single table of the join were the table | |
| 108695 | +** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be | |
| 108696 | +** a b-tree table, not a virtual table. | |
| 108697 | +*/ | |
| 108698 | +static int whereLoopAddBtree( | |
| 108699 | + WhereLoopBuilder *pBuilder, /* WHERE clause information */ | |
| 108700 | + Bitmask mExtra /* Extra prerequesites for using this table */ | |
| 108701 | +){ | |
| 108702 | + WhereInfo *pWInfo; /* WHERE analysis context */ | |
| 108703 | + Index *pProbe; /* An index we are evaluating */ | |
| 108704 | + Index sPk; /* A fake index object for the primary key */ | |
| 108705 | + tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ | |
| 108706 | + int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ | |
| 108707 | + SrcList *pTabList; /* The FROM clause */ | |
| 108708 | + struct SrcList_item *pSrc; /* The FROM clause btree term to add */ | |
| 108709 | + WhereLoop *pNew; /* Template WhereLoop object */ | |
| 108710 | + int rc = SQLITE_OK; /* Return code */ | |
| 108711 | + int iSortIdx = 1; /* Index number */ | |
| 108712 | + int b; /* A boolean value */ | |
| 108713 | + WhereCost rSize; /* number of rows in the table */ | |
| 108714 | + WhereCost rLogSize; /* Logarithm of the number of rows in the table */ | |
| 108715 | + | |
| 108716 | + pNew = pBuilder->pNew; | |
| 108717 | + pWInfo = pBuilder->pWInfo; | |
| 108718 | + pTabList = pWInfo->pTabList; | |
| 108719 | + pSrc = pTabList->a + pNew->iTab; | |
| 108720 | + assert( !IsVirtual(pSrc->pTab) ); | |
| 108721 | + | |
| 108722 | + if( pSrc->pIndex ){ | |
| 108723 | + /* An INDEXED BY clause specifies a particular index to use */ | |
| 108724 | + pProbe = pSrc->pIndex; | |
| 108725 | + }else{ | |
| 108726 | + /* There is no INDEXED BY clause. Create a fake Index object in local | |
| 108727 | + ** variable sPk to represent the rowid primary key index. Make this | |
| 108728 | + ** fake index the first in a chain of Index objects with all of the real | |
| 108729 | + ** indices to follow */ | |
| 108730 | + Index *pFirst; /* First of real indices on the table */ | |
| 108731 | + memset(&sPk, 0, sizeof(Index)); | |
| 108732 | + sPk.nColumn = 1; | |
| 108733 | + sPk.aiColumn = &aiColumnPk; | |
| 108734 | + sPk.aiRowEst = aiRowEstPk; | |
| 108735 | + sPk.onError = OE_Replace; | |
| 108736 | + sPk.pTable = pSrc->pTab; | |
| 108737 | + aiRowEstPk[0] = pSrc->pTab->nRowEst; | |
| 108738 | + aiRowEstPk[1] = 1; | |
| 108739 | + pFirst = pSrc->pTab->pIndex; | |
| 108740 | + if( pSrc->notIndexed==0 ){ | |
| 108741 | + /* The real indices of the table are only considered if the | |
| 108742 | + ** NOT INDEXED qualifier is omitted from the FROM clause */ | |
| 108743 | + sPk.pNext = pFirst; | |
| 108744 | + } | |
| 108745 | + pProbe = &sPk; | |
| 108746 | + } | |
| 108747 | + rSize = whereCost(pSrc->pTab->nRowEst); | |
| 108748 | + rLogSize = estLog(rSize); | |
| 108749 | + | |
| 108750 | + /* Automatic indexes */ | |
| 108751 | + if( !pBuilder->pBest | |
| 108752 | + && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 | |
| 108753 | + && pSrc->pIndex==0 | |
| 108754 | + && !pSrc->viaCoroutine | |
| 108755 | + && !pSrc->notIndexed | |
| 108756 | + && !pSrc->isCorrelated | |
| 108757 | + ){ | |
| 108758 | + /* Generate auto-index WhereLoops */ | |
| 108759 | + WhereClause *pWC = pBuilder->pWC; | |
| 108760 | + WhereTerm *pTerm; | |
| 108761 | + WhereTerm *pWCEnd = pWC->a + pWC->nTerm; | |
| 108762 | + for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ | |
| 108763 | + if( pTerm->prereqRight & pNew->maskSelf ) continue; | |
| 108764 | + if( termCanDriveIndex(pTerm, pSrc, 0) ){ | |
| 108765 | + pNew->u.btree.nEq = 1; | |
| 108766 | + pNew->u.btree.pIndex = 0; | |
| 108767 | + pNew->nLTerm = 1; | |
| 108768 | + pNew->aLTerm[0] = pTerm; | |
| 108769 | + /* TUNING: One-time cost for computing the automatic index is | |
| 108770 | + ** approximately 6*N*log2(N) where N is the number of rows in | |
| 108771 | + ** the table being indexed. */ | |
| 108772 | + pNew->rSetup = rLogSize + rSize + 26; assert( 26==whereCost(6) ); | |
| 108773 | + /* TUNING: Each index lookup yields 10 rows in the table */ | |
| 108774 | + pNew->nOut = 33; assert( 33==whereCost(10) ); | |
| 108775 | + pNew->rRun = whereCostAdd(rLogSize,pNew->nOut); | |
| 108776 | + pNew->wsFlags = WHERE_TEMP_INDEX; | |
| 108777 | + pNew->prereq = mExtra | pTerm->prereqRight; | |
| 108778 | + rc = whereLoopInsert(pBuilder, pNew); | |
| 108779 | + } | |
| 108780 | + } | |
| 108781 | + } | |
| 108782 | + | |
| 108783 | + /* Loop over all indices | |
| 108784 | + */ | |
| 108785 | + for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ | |
| 108786 | + pNew->u.btree.nEq = 0; | |
| 108787 | + pNew->nLTerm = 0; | |
| 108788 | + pNew->iSortIdx = 0; | |
| 108789 | + pNew->rSetup = 0; | |
| 108790 | + pNew->prereq = mExtra; | |
| 108791 | + pNew->u.btree.pIndex = pProbe; | |
| 108792 | + b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); | |
| 108793 | + /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ | |
| 108794 | + assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); | |
| 108795 | + if( pProbe->tnum<=0 ){ | |
| 108796 | + /* Integer primary key index */ | |
| 108797 | + pNew->wsFlags = WHERE_IPK; | |
| 108798 | + | |
| 108799 | + /* Full table scan */ | |
| 108800 | + pNew->iSortIdx = b ? iSortIdx : 0; | |
| 108801 | + pNew->nOut = rSize; | |
| 108802 | + /* TUNING: Cost of full table scan is 3*(N + log2(N)). | |
| 108803 | + ** + The extra 3 factor is to encourage the use of indexed lookups | |
| 108804 | + ** over full scans. A smaller constant 2 is used for covering | |
| 108805 | + ** index scans so that a covering index scan will be favored over | |
| 108806 | + ** a table scan. */ | |
| 108807 | + pNew->rRun = whereCostAdd(rSize,rLogSize) + 16; | |
| 108808 | + rc = whereLoopInsert(pBuilder, pNew); | |
| 108809 | + if( rc ) break; | |
| 108810 | + }else{ | |
| 108811 | + Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe); | |
| 108812 | + pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; | |
| 108813 | + | |
| 108814 | + /* Full scan via index */ | |
| 108815 | + if( b | |
| 108816 | + || ( m==0 | |
| 108817 | + && pProbe->bUnordered==0 | |
| 108818 | + && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 | |
| 108819 | + && sqlite3GlobalConfig.bUseCis | |
| 108820 | + && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) | |
| 108821 | + ) | |
| 108822 | + ){ | |
| 108823 | + pNew->iSortIdx = b ? iSortIdx : 0; | |
| 108824 | + pNew->nOut = rSize; | |
| 108825 | + if( m==0 ){ | |
| 108826 | + /* TUNING: Cost of a covering index scan is 2*(N + log2(N)). | |
| 108827 | + ** + The extra 2 factor is to encourage the use of indexed lookups | |
| 108828 | + ** over index scans. A table scan uses a factor of 3 so that | |
| 108829 | + ** index scans are favored over table scans. | |
| 108830 | + ** + If this covering index might also help satisfy the ORDER BY | |
| 108831 | + ** clause, then the cost is fudged down slightly so that this | |
| 108832 | + ** index is favored above other indices that have no hope of | |
| 108833 | + ** helping with the ORDER BY. */ | |
| 108834 | + pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b; | |
| 108835 | + }else{ | |
| 108836 | + assert( b!=0 ); | |
| 108837 | + /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N) | |
| 108838 | + ** which we will simplify to just N*log2(N) */ | |
| 108839 | + pNew->rRun = rSize + rLogSize; | |
| 108840 | + } | |
| 108841 | + rc = whereLoopInsert(pBuilder, pNew); | |
| 108842 | + if( rc ) break; | |
| 108843 | + } | |
| 108844 | + } | |
| 108845 | + rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); | |
| 108846 | + | |
| 108847 | + /* If there was an INDEXED BY clause, then only that one index is | |
| 108848 | + ** considered. */ | |
| 108849 | + if( pSrc->pIndex ) break; | |
| 108850 | + } | |
| 108851 | + return rc; | |
| 108852 | +} | |
| 108853 | + | |
| 108854 | +#ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 108855 | +/* | |
| 108856 | +** Add all WhereLoop objects for a table of the join identified by | |
| 108857 | +** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. | |
| 108858 | +*/ | |
| 108859 | +static int whereLoopAddVirtual( | |
| 108860 | + WhereLoopBuilder *pBuilder, /* WHERE clause information */ | |
| 108861 | + Bitmask mExtra /* Extra prerequesites for using this table */ | |
| 108862 | +){ | |
| 108863 | + WhereInfo *pWInfo; /* WHERE analysis context */ | |
| 108864 | + Parse *pParse; /* The parsing context */ | |
| 108865 | + WhereClause *pWC; /* The WHERE clause */ | |
| 108866 | + struct SrcList_item *pSrc; /* The FROM clause term to search */ | |
| 108867 | + Table *pTab; | |
| 108868 | + sqlite3 *db; | |
| 108869 | + sqlite3_index_info *pIdxInfo; | |
| 108870 | + struct sqlite3_index_constraint *pIdxCons; | |
| 108871 | + struct sqlite3_index_constraint_usage *pUsage; | |
| 108872 | + WhereTerm *pTerm; | |
| 108873 | + int i, j; | |
| 108874 | + int iTerm, mxTerm; | |
| 108875 | + int nConstraint; | |
| 108876 | + int seenIn = 0; /* True if an IN operator is seen */ | |
| 108877 | + int seenVar = 0; /* True if a non-constant constraint is seen */ | |
| 108878 | + int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ | |
| 108879 | + WhereLoop *pNew; | |
| 108880 | + int rc = SQLITE_OK; | |
| 108881 | + | |
| 108882 | + pWInfo = pBuilder->pWInfo; | |
| 108883 | + pParse = pWInfo->pParse; | |
| 108884 | + db = pParse->db; | |
| 108885 | + pWC = pBuilder->pWC; | |
| 108886 | + pNew = pBuilder->pNew; | |
| 108887 | + pSrc = &pWInfo->pTabList->a[pNew->iTab]; | |
| 108888 | + pTab = pSrc->pTab; | |
| 108889 | + assert( IsVirtual(pTab) ); | |
| 108890 | + pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); | |
| 108891 | + if( pIdxInfo==0 ) return SQLITE_NOMEM; | |
| 108892 | + pNew->prereq = 0; | |
| 108893 | + pNew->rSetup = 0; | |
| 108894 | + pNew->wsFlags = WHERE_VIRTUALTABLE; | |
| 108895 | + pNew->nLTerm = 0; | |
| 108896 | + pNew->u.vtab.needFree = 0; | |
| 108897 | + pUsage = pIdxInfo->aConstraintUsage; | |
| 108898 | + nConstraint = pIdxInfo->nConstraint; | |
| 108899 | + if( whereLoopResize(db, pNew, nConstraint) ) return SQLITE_NOMEM; | |
| 108900 | + | |
| 108901 | + for(iPhase=0; iPhase<=3; iPhase++){ | |
| 108902 | + if( !seenIn && (iPhase&1)!=0 ){ | |
| 108903 | + iPhase++; | |
| 108904 | + if( iPhase>3 ) break; | |
| 108905 | + } | |
| 108906 | + if( !seenVar && iPhase>1 ) break; | |
| 108907 | + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; | |
| 108908 | + for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ | |
| 108909 | + j = pIdxCons->iTermOffset; | |
| 108910 | + pTerm = &pWC->a[j]; | |
| 108911 | + switch( iPhase ){ | |
| 108912 | + case 0: /* Constants without IN operator */ | |
| 108913 | + pIdxCons->usable = 0; | |
| 108914 | + if( (pTerm->eOperator & WO_IN)!=0 ){ | |
| 108915 | + seenIn = 1; | |
| 108916 | + }else if( pTerm->prereqRight!=0 ){ | |
| 108917 | + seenVar = 1; | |
| 108918 | + }else{ | |
| 108919 | + pIdxCons->usable = 1; | |
| 108920 | + } | |
| 108921 | + break; | |
| 108922 | + case 1: /* Constants with IN operators */ | |
| 108923 | + assert( seenIn ); | |
| 108924 | + pIdxCons->usable = (pTerm->prereqRight==0); | |
| 108925 | + break; | |
| 108926 | + case 2: /* Variables without IN */ | |
| 108927 | + assert( seenVar ); | |
| 108928 | + pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; | |
| 108929 | + break; | |
| 108930 | + default: /* Variables with IN */ | |
| 108931 | + assert( seenVar && seenIn ); | |
| 108932 | + pIdxCons->usable = 1; | |
| 108933 | + break; | |
| 108934 | + } | |
| 108935 | + } | |
| 108936 | + memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); | |
| 108937 | + if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); | |
| 108938 | + pIdxInfo->idxStr = 0; | |
| 108939 | + pIdxInfo->idxNum = 0; | |
| 108940 | + pIdxInfo->needToFreeIdxStr = 0; | |
| 108941 | + pIdxInfo->orderByConsumed = 0; | |
| 108942 | + pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; | |
| 108943 | + rc = vtabBestIndex(pParse, pTab, pIdxInfo); | |
| 108944 | + if( rc ) goto whereLoopAddVtab_exit; | |
| 108945 | + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; | |
| 108946 | + pNew->prereq = 0; | |
| 108947 | + mxTerm = -1; | |
| 108948 | + assert( pNew->nLSlot>=nConstraint ); | |
| 108949 | + for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; | |
| 108950 | + pNew->u.vtab.omitMask = 0; | |
| 108951 | + for(i=0; i<nConstraint; i++, pIdxCons++){ | |
| 108952 | + if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ | |
| 108953 | + j = pIdxCons->iTermOffset; | |
| 108954 | + if( iTerm>=nConstraint | |
| 108955 | + || j<0 | |
| 108956 | + || j>=pWC->nTerm | |
| 108957 | + || pNew->aLTerm[iTerm]!=0 | |
| 108958 | + ){ | |
| 108959 | + rc = SQLITE_ERROR; | |
| 108960 | + sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); | |
| 108961 | + goto whereLoopAddVtab_exit; | |
| 108962 | + } | |
| 108963 | + pTerm = &pWC->a[j]; | |
| 108964 | + pNew->prereq |= pTerm->prereqRight; | |
| 108965 | + assert( iTerm<pNew->nLSlot ); | |
| 108966 | + pNew->aLTerm[iTerm] = pTerm; | |
| 108967 | + if( iTerm>mxTerm ) mxTerm = iTerm; | |
| 108968 | + if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; | |
| 108969 | + if( (pTerm->eOperator & WO_IN)!=0 ){ | |
| 108970 | + if( pUsage[i].omit==0 ){ | |
| 108971 | + /* Do not attempt to use an IN constraint if the virtual table | |
| 108972 | + ** says that the equivalent EQ constraint cannot be safely omitted. | |
| 108973 | + ** If we do attempt to use such a constraint, some rows might be | |
| 108974 | + ** repeated in the output. */ | |
| 108975 | + break; | |
| 108976 | + } | |
| 108977 | + /* A virtual table that is constrained by an IN clause may not | |
| 108978 | + ** consume the ORDER BY clause because (1) the order of IN terms | |
| 108979 | + ** is not necessarily related to the order of output terms and | |
| 108980 | + ** (2) Multiple outputs from a single IN value will not merge | |
| 108981 | + ** together. */ | |
| 108982 | + pIdxInfo->orderByConsumed = 0; | |
| 108983 | + } | |
| 108984 | + } | |
| 108985 | + } | |
| 108986 | + if( i>=nConstraint ){ | |
| 108987 | + pNew->nLTerm = mxTerm+1; | |
| 108988 | + assert( pNew->nLTerm<=pNew->nLSlot ); | |
| 108989 | + pNew->u.vtab.idxNum = pIdxInfo->idxNum; | |
| 108990 | + pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; | |
| 108991 | + pIdxInfo->needToFreeIdxStr = 0; | |
| 108992 | + pNew->u.vtab.idxStr = pIdxInfo->idxStr; | |
| 108993 | + pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) | |
| 108994 | + && pIdxInfo->orderByConsumed); | |
| 108995 | + pNew->rSetup = 0; | |
| 108996 | + pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost); | |
| 108997 | + /* TUNING: Every virtual table query returns 25 rows */ | |
| 108998 | + pNew->nOut = 46; assert( 46==whereCost(25) ); | |
| 108999 | + whereLoopInsert(pBuilder, pNew); | |
| 109000 | + if( pNew->u.vtab.needFree ){ | |
| 109001 | + sqlite3_free(pNew->u.vtab.idxStr); | |
| 109002 | + pNew->u.vtab.needFree = 0; | |
| 109003 | + } | |
| 109004 | + } | |
| 109005 | + } | |
| 109006 | + | |
| 109007 | +whereLoopAddVtab_exit: | |
| 109008 | + if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); | |
| 109009 | + sqlite3DbFree(db, pIdxInfo); | |
| 109010 | + return rc; | |
| 109011 | +} | |
| 109012 | +#endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
| 109013 | + | |
| 109014 | +/* | |
| 109015 | +** Add WhereLoop entries to handle OR terms. This works for either | |
| 109016 | +** btrees or virtual tables. | |
| 109017 | +*/ | |
| 109018 | +static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ | |
| 109019 | + WhereInfo *pWInfo = pBuilder->pWInfo; | |
| 109020 | + WhereClause *pWC; | |
| 109021 | + WhereLoop *pNew; | |
| 109022 | + WhereTerm *pTerm, *pWCEnd; | |
| 109023 | + int rc = SQLITE_OK; | |
| 109024 | + int iCur; | |
| 109025 | + WhereClause tempWC; | |
| 109026 | + WhereLoopBuilder sSubBuild; | |
| 109027 | + WhereLoop sBest; | |
| 109028 | + struct SrcList_item *pItem; | |
| 109029 | + | |
| 109030 | + pWC = pBuilder->pWC; | |
| 109031 | + if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; | |
| 109032 | + pWCEnd = pWC->a + pWC->nTerm; | |
| 109033 | + pNew = pBuilder->pNew; | |
| 109034 | + | |
| 109035 | + for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ | |
| 109036 | + if( (pTerm->eOperator & WO_OR)!=0 | |
| 109037 | + && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 | |
| 109038 | + ){ | |
| 109039 | + WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; | |
| 109040 | + WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; | |
| 109041 | + WhereTerm *pOrTerm; | |
| 109042 | + WhereCost rTotal = 0; | |
| 109043 | + WhereCost nRow = 0; | |
| 109044 | + Bitmask prereq = mExtra; | |
| 109045 | + | |
| 109046 | + whereLoopInit(&sBest); | |
| 109047 | + pItem = pWInfo->pTabList->a + pNew->iTab; | |
| 109048 | + iCur = pItem->iCursor; | |
| 109049 | + sSubBuild = *pBuilder; | |
| 109050 | + sSubBuild.pOrderBy = 0; | |
| 109051 | + sSubBuild.pBest = &sBest; | |
| 109052 | + | |
| 109053 | + for(pOrTerm=pOrWC->a; rc==SQLITE_OK && pOrTerm<pOrWCEnd; pOrTerm++){ | |
| 109054 | + if( (pOrTerm->eOperator & WO_AND)!=0 ){ | |
| 109055 | + sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; | |
| 109056 | + }else if( pOrTerm->leftCursor==iCur ){ | |
| 109057 | + tempWC.pWInfo = pWC->pWInfo; | |
| 109058 | + tempWC.pOuter = pWC; | |
| 109059 | + tempWC.op = TK_AND; | |
| 109060 | + tempWC.nTerm = 1; | |
| 109061 | + tempWC.a = pOrTerm; | |
| 109062 | + sSubBuild.pWC = &tempWC; | |
| 109063 | + }else{ | |
| 109064 | + continue; | |
| 109065 | + } | |
| 109066 | + sBest.maskSelf = 0; | |
| 109067 | + sBest.rSetup = 0; | |
| 109068 | + sBest.rRun = 0; | |
| 109069 | +#ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 109070 | + if( IsVirtual(pItem->pTab) ){ | |
| 109071 | + rc = whereLoopAddVirtual(&sSubBuild, mExtra); | |
| 109072 | + }else | |
| 109073 | +#endif | |
| 109074 | + { | |
| 109075 | + rc = whereLoopAddBtree(&sSubBuild, mExtra); | |
| 109076 | + } | |
| 109077 | + if( sBest.maskSelf==0 ) break; | |
| 109078 | + assert( sBest.rSetup==0 ); | |
| 109079 | + rTotal = whereCostAdd(rTotal, sBest.rRun); | |
| 109080 | + nRow = whereCostAdd(nRow, sBest.nOut); | |
| 109081 | + prereq |= sBest.prereq; | |
| 109082 | + } | |
| 109083 | + assert( pNew->nLSlot>=1 ); | |
| 109084 | + if( sBest.maskSelf ){ | |
| 109085 | + pNew->nLTerm = 1; | |
| 109086 | + pNew->aLTerm[0] = pTerm; | |
| 109087 | + pNew->wsFlags = WHERE_MULTI_OR; | |
| 109088 | + pNew->rSetup = 0; | |
| 109089 | + pNew->rRun = rTotal; | |
| 109090 | + pNew->nOut = nRow; | |
| 109091 | + pNew->prereq = prereq; | |
| 109092 | + memset(&pNew->u, 0, sizeof(pNew->u)); | |
| 109093 | + rc = whereLoopInsert(pBuilder, pNew); | |
| 109094 | + } | |
| 109095 | + whereLoopClear(pWInfo->pParse->db, &sBest); | |
| 109096 | + } | |
| 109097 | + } | |
| 109098 | + return rc; | |
| 109099 | +} | |
| 109100 | + | |
| 109101 | +/* | |
| 109102 | +** Add all WhereLoop objects for all tables | |
| 109103 | +*/ | |
| 109104 | +static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ | |
| 109105 | + WhereInfo *pWInfo = pBuilder->pWInfo; | |
| 109106 | + Bitmask mExtra = 0; | |
| 109107 | + Bitmask mPrior = 0; | |
| 109108 | + int iTab; | |
| 109109 | + SrcList *pTabList = pWInfo->pTabList; | |
| 109110 | + struct SrcList_item *pItem; | |
| 109111 | + sqlite3 *db = pWInfo->pParse->db; | |
| 109112 | + int nTabList = pWInfo->nLevel; | |
| 109113 | + int rc = SQLITE_OK; | |
| 109114 | + u8 priorJoinType = 0; | |
| 109115 | + WhereLoop *pNew; | |
| 109116 | + | |
| 109117 | + /* Loop over the tables in the join, from left to right */ | |
| 109118 | + pNew = pBuilder->pNew; | |
| 109119 | + whereLoopInit(pNew); | |
| 109120 | + for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ | |
| 109121 | + pNew->iTab = iTab; | |
| 109122 | + pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); | |
| 109123 | + if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ | |
| 109124 | + mExtra = mPrior; | |
| 109125 | + } | |
| 109126 | + priorJoinType = pItem->jointype; | |
| 109127 | + if( IsVirtual(pItem->pTab) ){ | |
| 109128 | + rc = whereLoopAddVirtual(pBuilder, mExtra); | |
| 109129 | + }else{ | |
| 109130 | + rc = whereLoopAddBtree(pBuilder, mExtra); | |
| 109131 | + } | |
| 109132 | + if( rc==SQLITE_OK ){ | |
| 109133 | + rc = whereLoopAddOr(pBuilder, mExtra); | |
| 109134 | + } | |
| 109135 | + mPrior |= pNew->maskSelf; | |
| 109136 | + if( rc || db->mallocFailed ) break; | |
| 109137 | + } | |
| 109138 | + whereLoopClear(db, pNew); | |
| 109139 | + return rc; | |
| 109140 | +} | |
| 109141 | + | |
| 109142 | +/* | |
| 109143 | +** Examine a WherePath (with the addition of the extra WhereLoop of the 5th | |
| 109144 | +** parameters) to see if it outputs rows in the requested ORDER BY | |
| 109145 | +** (or GROUP BY) without requiring a separate source operation. Return: | |
| 109146 | +** | |
| 109147 | +** 0: ORDER BY is not satisfied. Sorting required | |
| 109148 | +** 1: ORDER BY is satisfied. Omit sorting | |
| 109149 | +** -1: Unknown at this time | |
| 109150 | +** | |
| 109151 | +*/ | |
| 109152 | +static int wherePathSatisfiesOrderBy( | |
| 109153 | + WhereInfo *pWInfo, /* The WHERE clause */ | |
| 109154 | + ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ | |
| 109155 | + WherePath *pPath, /* The WherePath to check */ | |
| 109156 | + u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ | |
| 109157 | + u16 nLoop, /* Number of entries in pPath->aLoop[] */ | |
| 109158 | + u8 isLastLoop, /* True if pLast is the inner-most loop */ | |
| 109159 | + WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ | |
| 109160 | + Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ | |
| 109161 | +){ | |
| 109162 | + u8 revSet; /* True if rev is known */ | |
| 109163 | + u8 rev; /* Composite sort order */ | |
| 109164 | + u8 revIdx; /* Index sort order */ | |
| 109165 | + u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ | |
| 109166 | + u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ | |
| 109167 | + u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ | |
| 109168 | + u16 nColumn; /* Number of columns in pIndex */ | |
| 109169 | + u16 nOrderBy; /* Number terms in the ORDER BY clause */ | |
| 109170 | + int iLoop; /* Index of WhereLoop in pPath being processed */ | |
| 109171 | + int i, j; /* Loop counters */ | |
| 109172 | + int iCur; /* Cursor number for current WhereLoop */ | |
| 109173 | + int iColumn; /* A column number within table iCur */ | |
| 109174 | + WhereLoop *pLoop; /* Current WhereLoop being processed. */ | |
| 109175 | + WhereTerm *pTerm; /* A single term of the WHERE clause */ | |
| 109176 | + Expr *pOBExpr; /* An expression from the ORDER BY clause */ | |
| 109177 | + CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ | |
| 109178 | + Index *pIndex; /* The index associated with pLoop */ | |
| 109179 | + sqlite3 *db = pWInfo->pParse->db; /* Database connection */ | |
| 109180 | + Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ | |
| 109181 | + Bitmask obDone; /* Mask of all ORDER BY terms */ | |
| 109182 | + Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ | |
| 109183 | + Bitmask ready; /* Mask of inner loops */ | |
| 109184 | + | |
| 109185 | + /* | |
| 109186 | + ** We say the WhereLoop is "one-row" if it generates no more than one | |
| 109187 | + ** row of output. A WhereLoop is one-row if all of the following are true: | |
| 109188 | + ** (a) All index columns match with WHERE_COLUMN_EQ. | |
| 109189 | + ** (b) The index is unique | |
| 109190 | + ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. | |
| 109191 | + ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. | |
| 109192 | + ** | |
| 109193 | + ** We say the WhereLoop is "order-distinct" if the set of columns from | |
| 109194 | + ** that WhereLoop that are in the ORDER BY clause are different for every | |
| 109195 | + ** row of the WhereLoop. Every one-row WhereLoop is automatically | |
| 109196 | + ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause | |
| 109197 | + ** is not order-distinct. To be order-distinct is not quite the same as being | |
| 109198 | + ** UNIQUE since a UNIQUE column or index can have multiple rows that | |
| 109199 | + ** are NULL and NULL values are equivalent for the purpose of order-distinct. | |
| 109200 | + ** To be order-distinct, the columns must be UNIQUE and NOT NULL. | |
| 109201 | + ** | |
| 109202 | + ** The rowid for a table is always UNIQUE and NOT NULL so whenever the | |
| 109203 | + ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is | |
| 109204 | + ** automatically order-distinct. | |
| 109205 | + */ | |
| 109206 | + | |
| 109207 | + assert( pOrderBy!=0 ); | |
| 109208 | + | |
| 109209 | + /* Sortability of virtual tables is determined by the xBestIndex method | |
| 109210 | + ** of the virtual table itself */ | |
| 109211 | + if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ | |
| 109212 | + testcase( nLoop>0 ); /* True when outer loops are one-row and match | |
| 109213 | + ** no ORDER BY terms */ | |
| 109214 | + return pLast->u.vtab.isOrdered; | |
| 109215 | + } | |
| 109216 | + if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; | |
| 109217 | + | |
| 109218 | + nOrderBy = pOrderBy->nExpr; | |
| 109219 | + if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ | |
| 109220 | + isOrderDistinct = 1; | |
| 109221 | + obDone = MASKBIT(nOrderBy)-1; | |
| 109222 | + orderDistinctMask = 0; | |
| 109223 | + ready = 0; | |
| 109224 | + for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ | |
| 109225 | + if( iLoop>0 ) ready |= pLoop->maskSelf; | |
| 109226 | + pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; | |
| 109227 | + assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); | |
| 109228 | + iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; | |
| 109229 | + | |
| 109230 | + /* Mark off any ORDER BY term X that is a column in the table of | |
| 109231 | + ** the current loop for which there is term in the WHERE | |
| 109232 | + ** clause of the form X IS NULL or X=? that reference only outer | |
| 109233 | + ** loops. | |
| 109234 | + */ | |
| 109235 | + for(i=0; i<nOrderBy; i++){ | |
| 109236 | + if( MASKBIT(i) & obSat ) continue; | |
| 109237 | + pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); | |
| 109238 | + if( pOBExpr->op!=TK_COLUMN ) continue; | |
| 109239 | + if( pOBExpr->iTable!=iCur ) continue; | |
| 109240 | + pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, | |
| 109241 | + ~ready, WO_EQ|WO_ISNULL, 0); | |
| 109242 | + if( pTerm==0 ) continue; | |
| 109243 | + if( pOBExpr->iColumn>=0 ){ | |
| 109244 | + const char *z1, *z2; | |
| 109245 | + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); | |
| 109246 | + if( !pColl ) pColl = db->pDfltColl; | |
| 109247 | + z1 = pColl->zName; | |
| 109248 | + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); | |
| 109249 | + if( !pColl ) pColl = db->pDfltColl; | |
| 109250 | + z2 = pColl->zName; | |
| 109251 | + if( sqlite3StrICmp(z1, z2)!=0 ) continue; | |
| 109252 | + } | |
| 109253 | + obSat |= MASKBIT(i); | |
| 109254 | + } | |
| 109255 | + | |
| 109256 | + if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ | |
| 109257 | + if( pLoop->wsFlags & WHERE_IPK ){ | |
| 109258 | + pIndex = 0; | |
| 109259 | + nColumn = 0; | |
| 109260 | + }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ | |
| 109261 | + return 0; | |
| 109262 | + }else{ | |
| 109263 | + nColumn = pIndex->nColumn; | |
| 109264 | + isOrderDistinct = pIndex->onError!=OE_None; | |
| 109265 | + } | |
| 109266 | + | |
| 109267 | + /* For every term of the index that is constrained by == or IS NULL, | |
| 109268 | + ** mark off corresponding ORDER BY terms wherever they occur | |
| 109269 | + ** in the ORDER BY clause. | |
| 109270 | + */ | |
| 109271 | + for(i=0; i<pLoop->u.btree.nEq; i++){ | |
| 109272 | + pTerm = pLoop->aLTerm[i]; | |
| 109273 | + if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))==0 ) continue; | |
| 109274 | + iColumn = pTerm->u.leftColumn; | |
| 109275 | + for(j=0; j<nOrderBy; j++){ | |
| 109276 | + if( MASKBIT(j) & obSat ) continue; | |
| 109277 | + pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[j].pExpr); | |
| 109278 | + if( pOBExpr->op!=TK_COLUMN ) continue; | |
| 109279 | + if( pOBExpr->iTable!=iCur ) continue; | |
| 109280 | + if( pOBExpr->iColumn!=iColumn ) continue; | |
| 109281 | + if( iColumn>=0 ){ | |
| 109282 | + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[j].pExpr); | |
| 109283 | + if( !pColl ) pColl = db->pDfltColl; | |
| 109284 | + if( sqlite3StrICmp(pColl->zName, pIndex->azColl[i])!=0 ) continue; | |
| 109285 | + } | |
| 109286 | + obSat |= MASKBIT(j); | |
| 109287 | + } | |
| 109288 | + if( obSat==obDone ) return 1; | |
| 109289 | + } | |
| 109290 | + | |
| 109291 | + /* Loop through all columns of the index and deal with the ones | |
| 109292 | + ** that are not constrained by == or IN. | |
| 109293 | + */ | |
| 109294 | + rev = revSet = 0; | |
| 109295 | + distinctColumns = 0; | |
| 109296 | + for(j=0; j<=nColumn; j++){ | |
| 109297 | + u8 bOnce; /* True to run the ORDER BY search loop */ | |
| 109298 | + | |
| 109299 | + /* Skip over == and IS NULL terms */ | |
| 109300 | + if( j<pLoop->u.btree.nEq | |
| 109301 | + && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 | |
| 109302 | + ){ | |
| 109303 | + if( i & WO_ISNULL ) isOrderDistinct = 0; | |
| 109304 | + continue; | |
| 109305 | + } | |
| 109306 | + | |
| 109307 | + /* Get the column number in the table (iColumn) and sort order | |
| 109308 | + ** (revIdx) for the j-th column of the index. | |
| 109309 | + */ | |
| 109310 | + if( j<nColumn ){ | |
| 109311 | + /* Normal index columns */ | |
| 109312 | + iColumn = pIndex->aiColumn[j]; | |
| 109313 | + revIdx = pIndex->aSortOrder[j]; | |
| 109314 | + if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; | |
| 109315 | + }else{ | |
| 109316 | + /* The ROWID column at the end */ | |
| 109317 | + iColumn = -1; | |
| 109318 | + revIdx = 0; | |
| 109319 | + } | |
| 109320 | + | |
| 109321 | + /* An unconstrained column that might be NULL means that this | |
| 109322 | + ** WhereLoop is not well-ordered | |
| 109323 | + */ | |
| 109324 | + if( isOrderDistinct | |
| 109325 | + && iColumn>=0 | |
| 109326 | + && j>=pLoop->u.btree.nEq | |
| 109327 | + && pIndex->pTable->aCol[iColumn].notNull==0 | |
| 109328 | + ){ | |
| 109329 | + isOrderDistinct = 0; | |
| 109330 | + } | |
| 109331 | + | |
| 109332 | + /* Find the ORDER BY term that corresponds to the j-th column | |
| 109333 | + ** of the index and and mark that ORDER BY term off | |
| 109334 | + */ | |
| 109335 | + bOnce = 1; | |
| 109336 | + isMatch = 0; | |
| 109337 | + for(i=0; bOnce && i<nOrderBy; i++){ | |
| 109338 | + if( MASKBIT(i) & obSat ) continue; | |
| 109339 | + pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); | |
| 109340 | + if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; | |
| 109341 | + if( pOBExpr->op!=TK_COLUMN ) continue; | |
| 109342 | + if( pOBExpr->iTable!=iCur ) continue; | |
| 109343 | + if( pOBExpr->iColumn!=iColumn ) continue; | |
| 109344 | + if( iColumn>=0 ){ | |
| 109345 | + pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); | |
| 109346 | + if( !pColl ) pColl = db->pDfltColl; | |
| 109347 | + if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; | |
| 109348 | + } | |
| 109349 | + isMatch = 1; | |
| 109350 | + break; | |
| 109351 | + } | |
| 109352 | + if( isMatch ){ | |
| 109353 | + if( iColumn<0 ) distinctColumns = 1; | |
| 109354 | + obSat |= MASKBIT(i); | |
| 109355 | + if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ | |
| 109356 | + /* Make sure the sort order is compatible in an ORDER BY clause. | |
| 109357 | + ** Sort order is irrelevant for a GROUP BY clause. */ | |
| 109358 | + if( revSet ){ | |
| 109359 | + if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; | |
| 109360 | + }else{ | |
| 109361 | + rev = revIdx ^ pOrderBy->a[i].sortOrder; | |
| 109362 | + if( rev ) *pRevMask |= MASKBIT(iLoop); | |
| 109363 | + revSet = 1; | |
| 109364 | + } | |
| 109365 | + } | |
| 109366 | + }else{ | |
| 109367 | + /* No match found */ | |
| 109368 | + if( j==0 || j<nColumn ) isOrderDistinct = 0; | |
| 109369 | + break; | |
| 109370 | + } | |
| 109371 | + } /* end Loop over all index columns */ | |
| 109372 | + if( distinctColumns ) isOrderDistinct = 1; | |
| 109373 | + } /* end-if not one-row */ | |
| 109374 | + | |
| 109375 | + /* Mark off any other ORDER BY terms that reference pLoop */ | |
| 109376 | + if( isOrderDistinct ){ | |
| 109377 | + orderDistinctMask |= pLoop->maskSelf; | |
| 109378 | + for(i=0; i<nOrderBy; i++){ | |
| 109379 | + Expr *p; | |
| 109380 | + if( MASKBIT(i) & obSat ) continue; | |
| 109381 | + p = pOrderBy->a[i].pExpr; | |
| 109382 | + if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){ | |
| 109383 | + obSat |= MASKBIT(i); | |
| 109384 | + } | |
| 109385 | + } | |
| 109386 | + } | |
| 109387 | + } /* End the loop over all WhereLoops from outer-most down to inner-most */ | |
| 109388 | + if( obSat==obDone ) return 1; | |
| 109389 | + if( !isOrderDistinct ) return 0; | |
| 109390 | + if( isLastLoop ) return 1; | |
| 109391 | + return -1; | |
| 109392 | +} | |
| 109393 | + | |
| 109394 | +#ifdef WHERETRACE_ENABLED | |
| 109395 | +/* For debugging use only: */ | |
| 109396 | +static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ | |
| 109397 | + static char zName[65]; | |
| 109398 | + int i; | |
| 109399 | + for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } | |
| 109400 | + if( pLast ) zName[i++] = pLast->cId; | |
| 109401 | + zName[i] = 0; | |
| 109402 | + return zName; | |
| 109403 | +} | |
| 109404 | +#endif | |
| 109405 | + | |
| 109406 | + | |
| 109407 | +/* | |
| 109408 | +** Given the list of WhereLoop objects on pWInfo->pLoops, this routine | |
| 109409 | +** attempts to find the lowest cost path that visits each WhereLoop | |
| 109410 | +** once. This path is then loaded into the pWInfo->a[].pWLoop fields. | |
| 109411 | +** | |
| 109412 | +** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation | |
| 109413 | +** error occurs. | |
| 109414 | +*/ | |
| 109415 | +static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){ | |
| 109416 | + int mxChoice; /* Maximum number of simultaneous paths tracked */ | |
| 109417 | + int nLoop; /* Number of terms in the join */ | |
| 109418 | + Parse *pParse; /* Parsing context */ | |
| 109419 | + sqlite3 *db; /* The database connection */ | |
| 109420 | + int iLoop; /* Loop counter over the terms of the join */ | |
| 109421 | + int ii, jj; /* Loop counters */ | |
| 109422 | + WhereCost rCost; /* Cost of a path */ | |
| 109423 | + WhereCost mxCost; /* Maximum cost of a set of paths */ | |
| 109424 | + WhereCost rSortCost; /* Cost to do a sort */ | |
| 109425 | + int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ | |
| 109426 | + WherePath *aFrom; /* All nFrom paths at the previous level */ | |
| 109427 | + WherePath *aTo; /* The nTo best paths at the current level */ | |
| 109428 | + WherePath *pFrom; /* An element of aFrom[] that we are working on */ | |
| 109429 | + WherePath *pTo; /* An element of aTo[] that we are working on */ | |
| 109430 | + WhereLoop *pWLoop; /* One of the WhereLoop objects */ | |
| 109431 | + WhereLoop **pX; /* Used to divy up the pSpace memory */ | |
| 109432 | + char *pSpace; /* Temporary memory used by this routine */ | |
| 109433 | + | |
| 109434 | + pParse = pWInfo->pParse; | |
| 109435 | + db = pParse->db; | |
| 109436 | + nLoop = pWInfo->nLevel; | |
| 109437 | + /* TUNING: For simple queries, only the best path is tracked. | |
| 109438 | + ** For 2-way joins, the 5 best paths are followed. | |
| 109439 | + ** For joins of 3 or more tables, track the 10 best paths */ | |
| 109440 | + mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10); | |
| 109441 | + assert( nLoop<=pWInfo->pTabList->nSrc ); | |
| 109442 | + WHERETRACE(0x002, ("---- begin solver\n")); | |
| 109443 | + | |
| 109444 | + /* Allocate and initialize space for aTo and aFrom */ | |
| 109445 | + ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; | |
| 109446 | + pSpace = sqlite3DbMallocRaw(db, ii); | |
| 109447 | + if( pSpace==0 ) return SQLITE_NOMEM; | |
| 109448 | + aTo = (WherePath*)pSpace; | |
| 109449 | + aFrom = aTo+mxChoice; | |
| 109450 | + memset(aFrom, 0, sizeof(aFrom[0])); | |
| 109451 | + pX = (WhereLoop**)(aFrom+mxChoice); | |
| 109452 | + for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ | |
| 109453 | + pFrom->aLoop = pX; | |
| 109454 | + } | |
| 109455 | + | |
| 109456 | + /* Seed the search with a single WherePath containing zero WhereLoops. | |
| 109457 | + ** | |
| 109458 | + ** TUNING: Do not let the number of iterations go above 25. If the cost | |
| 109459 | + ** of computing an automatic index is not paid back within the first 25 | |
| 109460 | + ** rows, then do not use the automatic index. */ | |
| 109461 | + aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==whereCost(25) ); | |
| 109462 | + nFrom = 1; | |
| 109463 | + | |
| 109464 | + /* Precompute the cost of sorting the final result set, if the caller | |
| 109465 | + ** to sqlite3WhereBegin() was concerned about sorting */ | |
| 109466 | + rSortCost = 0; | |
| 109467 | + if( pWInfo->pOrderBy==0 || nRowEst==0 ){ | |
| 109468 | + aFrom[0].isOrderedValid = 1; | |
| 109469 | + }else{ | |
| 109470 | + /* TUNING: Estimated cost of sorting is N*log2(N) where N is the | |
| 109471 | + ** number of output rows. */ | |
| 109472 | + rSortCost = nRowEst + estLog(nRowEst); | |
| 109473 | + WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost)); | |
| 109474 | + } | |
| 109475 | + | |
| 109476 | + /* Compute successively longer WherePaths using the previous generation | |
| 109477 | + ** of WherePaths as the basis for the next. Keep track of the mxChoice | |
| 109478 | + ** best paths at each generation */ | |
| 109479 | + for(iLoop=0; iLoop<nLoop; iLoop++){ | |
| 109480 | + nTo = 0; | |
| 109481 | + for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ | |
| 109482 | + for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ | |
| 109483 | + Bitmask maskNew; | |
| 109484 | + Bitmask revMask = 0; | |
| 109485 | + u8 isOrderedValid = pFrom->isOrderedValid; | |
| 109486 | + u8 isOrdered = pFrom->isOrdered; | |
| 109487 | + if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; | |
| 109488 | + if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; | |
| 109489 | + /* At this point, pWLoop is a candidate to be the next loop. | |
| 109490 | + ** Compute its cost */ | |
| 109491 | + rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); | |
| 109492 | + rCost = whereCostAdd(rCost, pFrom->rCost); | |
| 109493 | + maskNew = pFrom->maskLoop | pWLoop->maskSelf; | |
| 109494 | + if( !isOrderedValid ){ | |
| 109495 | + switch( wherePathSatisfiesOrderBy(pWInfo, | |
| 109496 | + pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, | |
| 109497 | + iLoop, iLoop==nLoop-1, pWLoop, &revMask) ){ | |
| 109498 | + case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ | |
| 109499 | + isOrdered = 1; | |
| 109500 | + isOrderedValid = 1; | |
| 109501 | + break; | |
| 109502 | + case 0: /* No. pFrom+pWLoop will require a separate sort */ | |
| 109503 | + isOrdered = 0; | |
| 109504 | + isOrderedValid = 1; | |
| 109505 | + rCost = whereCostAdd(rCost, rSortCost); | |
| 109506 | + break; | |
| 109507 | + default: /* Cannot tell yet. Try again on the next iteration */ | |
| 109508 | + break; | |
| 109509 | + } | |
| 109510 | + }else{ | |
| 109511 | + revMask = pFrom->revLoop; | |
| 109512 | + } | |
| 109513 | + /* Check to see if pWLoop should be added to the mxChoice best so far */ | |
| 109514 | + for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ | |
| 109515 | + if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){ | |
| 109516 | + break; | |
| 109517 | + } | |
| 109518 | + } | |
| 109519 | + if( jj>=nTo ){ | |
| 109520 | + if( nTo>=mxChoice && rCost>=mxCost ){ | |
| 109521 | +#ifdef WHERETRACE_ENABLED | |
| 109522 | + if( sqlite3WhereTrace&0x4 ){ | |
| 109523 | + sqlite3DebugPrintf("Skip %s cost=%3d order=%c\n", | |
| 109524 | + wherePathName(pFrom, iLoop, pWLoop), rCost, | |
| 109525 | + isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); | |
| 109526 | + } | |
| 109527 | +#endif | |
| 109528 | + continue; | |
| 109529 | + } | |
| 109530 | + /* Add a new Path to the aTo[] set */ | |
| 109531 | + if( nTo<mxChoice ){ | |
| 109532 | + /* Increase the size of the aTo set by one */ | |
| 109533 | + jj = nTo++; | |
| 109534 | + }else{ | |
| 109535 | + /* New path replaces the prior worst to keep count below mxChoice */ | |
| 109536 | + for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); } | |
| 109537 | + } | |
| 109538 | + pTo = &aTo[jj]; | |
| 109539 | +#ifdef WHERETRACE_ENABLED | |
| 109540 | + if( sqlite3WhereTrace&0x4 ){ | |
| 109541 | + sqlite3DebugPrintf("New %s cost=%-3d order=%c\n", | |
| 109542 | + wherePathName(pFrom, iLoop, pWLoop), rCost, | |
| 109543 | + isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); | |
| 109544 | + } | |
| 109545 | +#endif | |
| 109546 | + }else{ | |
| 109547 | + if( pTo->rCost<=rCost ){ | |
| 109548 | +#ifdef WHERETRACE_ENABLED | |
| 109549 | + if( sqlite3WhereTrace&0x4 ){ | |
| 109550 | + sqlite3DebugPrintf( | |
| 109551 | + "Skip %s cost=%-3d order=%c", | |
| 109552 | + wherePathName(pFrom, iLoop, pWLoop), rCost, | |
| 109553 | + isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); | |
| 109554 | + sqlite3DebugPrintf(" vs %s cost=%-3d order=%c\n", | |
| 109555 | + wherePathName(pTo, iLoop+1, 0), pTo->rCost, | |
| 109556 | + pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); | |
| 109557 | + } | |
| 109558 | +#endif | |
| 109559 | + continue; | |
| 109560 | + } | |
| 109561 | + /* A new and better score for a previously created equivalent path */ | |
| 109562 | +#ifdef WHERETRACE_ENABLED | |
| 109563 | + if( sqlite3WhereTrace&0x4 ){ | |
| 109564 | + sqlite3DebugPrintf( | |
| 109565 | + "Update %s cost=%-3d order=%c", | |
| 109566 | + wherePathName(pFrom, iLoop, pWLoop), rCost, | |
| 109567 | + isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); | |
| 109568 | + sqlite3DebugPrintf(" was %s cost=%-3d order=%c\n", | |
| 109569 | + wherePathName(pTo, iLoop+1, 0), pTo->rCost, | |
| 109570 | + pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); | |
| 109571 | + } | |
| 109572 | +#endif | |
| 109573 | + } | |
| 109574 | + /* pWLoop is a winner. Add it to the set of best so far */ | |
| 109575 | + pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; | |
| 109576 | + pTo->revLoop = revMask; | |
| 109577 | + pTo->nRow = pFrom->nRow + pWLoop->nOut; | |
| 109578 | + pTo->rCost = rCost; | |
| 109579 | + pTo->isOrderedValid = isOrderedValid; | |
| 109580 | + pTo->isOrdered = isOrdered; | |
| 109581 | + memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); | |
| 109582 | + pTo->aLoop[iLoop] = pWLoop; | |
| 109583 | + if( nTo>=mxChoice ){ | |
| 109584 | + mxCost = aTo[0].rCost; | |
| 109585 | + for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ | |
| 109586 | + if( pTo->rCost>mxCost ) mxCost = pTo->rCost; | |
| 109587 | + } | |
| 109588 | + } | |
| 109589 | + } | |
| 109590 | + } | |
| 109591 | + | |
| 109592 | +#ifdef WHERETRACE_ENABLED | |
| 109593 | + if( sqlite3WhereTrace>=2 ){ | |
| 109594 | + sqlite3DebugPrintf("---- after round %d ----\n", iLoop); | |
| 109595 | + for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ | |
| 109596 | + sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", | |
| 109597 | + wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, | |
| 109598 | + pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); | |
| 109599 | + if( pTo->isOrderedValid && pTo->isOrdered ){ | |
| 109600 | + sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); | |
| 109601 | + }else{ | |
| 109602 | + sqlite3DebugPrintf("\n"); | |
| 109603 | + } | |
| 109604 | + } | |
| 109605 | + } | |
| 109606 | +#endif | |
| 109607 | + | |
| 109608 | + /* Swap the roles of aFrom and aTo for the next generation */ | |
| 109609 | + pFrom = aTo; | |
| 109610 | + aTo = aFrom; | |
| 109611 | + aFrom = pFrom; | |
| 109612 | + nFrom = nTo; | |
| 109613 | + } | |
| 109614 | + | |
| 109615 | + if( nFrom==0 ){ | |
| 109616 | + sqlite3ErrorMsg(pParse, "no query solution"); | |
| 109617 | + sqlite3DbFree(db, pSpace); | |
| 109618 | + return SQLITE_ERROR; | |
| 109619 | + } | |
| 109620 | + | |
| 109621 | + /* Find the lowest cost path. pFrom will be left pointing to that path */ | |
| 109622 | + pFrom = aFrom; | |
| 109623 | + for(ii=1; ii<nFrom; ii++){ | |
| 109624 | + if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; | |
| 109625 | + } | |
| 109626 | + assert( pWInfo->nLevel==nLoop ); | |
| 109627 | + /* Load the lowest cost path into pWInfo */ | |
| 109628 | + for(iLoop=0; iLoop<nLoop; iLoop++){ | |
| 109629 | + WhereLevel *pLevel = pWInfo->a + iLoop; | |
| 109630 | + pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; | |
| 109631 | + pLevel->iFrom = pWLoop->iTab; | |
| 109632 | + pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; | |
| 109633 | + } | |
| 109634 | + if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 | |
| 109635 | + && pWInfo->pDistinct | |
| 109636 | + && nRowEst | |
| 109637 | + ){ | |
| 109638 | + Bitmask notUsed; | |
| 109639 | + int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinct, pFrom, | |
| 109640 | + WHERE_DISTINCTBY, nLoop-1, 1, pFrom->aLoop[nLoop-1], ¬Used); | |
| 109641 | + if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; | |
| 109642 | + } | |
| 109643 | + if( pFrom->isOrdered ){ | |
| 109644 | + if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ | |
| 109645 | + pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; | |
| 109646 | + }else{ | |
| 109647 | + pWInfo->bOBSat = 1; | |
| 109648 | + pWInfo->revMask = pFrom->revLoop; | |
| 109649 | + } | |
| 109650 | + } | |
| 109651 | + pWInfo->nRowOut = pFrom->nRow; | |
| 109652 | + | |
| 109653 | + /* Free temporary memory and return success */ | |
| 109654 | + sqlite3DbFree(db, pSpace); | |
| 109655 | + return SQLITE_OK; | |
| 109656 | +} | |
| 109657 | + | |
| 109658 | +/* | |
| 109659 | +** Most queries use only a single table (they are not joins) and have | |
| 109660 | +** simple == constraints against indexed fields. This routine attempts | |
| 109661 | +** to plan those simple cases using much less ceremony than the | |
| 109662 | +** general-purpose query planner, and thereby yield faster sqlite3_prepare() | |
| 109663 | +** times for the common case. | |
| 109664 | +** | |
| 109665 | +** Return non-zero on success, if this query can be handled by this | |
| 109666 | +** no-frills query planner. Return zero if this query needs the | |
| 109667 | +** general-purpose query planner. | |
| 109668 | +*/ | |
| 109669 | +static int whereShortCut(WhereLoopBuilder *pBuilder){ | |
| 109670 | + WhereInfo *pWInfo; | |
| 109671 | + struct SrcList_item *pItem; | |
| 109672 | + WhereClause *pWC; | |
| 109673 | + WhereTerm *pTerm; | |
| 109674 | + WhereLoop *pLoop; | |
| 109675 | + int iCur; | |
| 109676 | + int j; | |
| 109677 | + Table *pTab; | |
| 109678 | + Index *pIdx; | |
| 109679 | + | |
| 109680 | + pWInfo = pBuilder->pWInfo; | |
| 109681 | + if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; | |
| 109682 | + assert( pWInfo->pTabList->nSrc>=1 ); | |
| 109683 | + pItem = pWInfo->pTabList->a; | |
| 109684 | + pTab = pItem->pTab; | |
| 109685 | + if( IsVirtual(pTab) ) return 0; | |
| 109686 | + if( pItem->zIndex ) return 0; | |
| 109687 | + iCur = pItem->iCursor; | |
| 109688 | + pWC = &pWInfo->sWC; | |
| 109689 | + pLoop = pBuilder->pNew; | |
| 109690 | + pLoop->wsFlags = 0; | |
| 109691 | + pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); | |
| 109692 | + if( pTerm ){ | |
| 109693 | + pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; | |
| 109694 | + pLoop->aLTerm[0] = pTerm; | |
| 109695 | + pLoop->nLTerm = 1; | |
| 109696 | + pLoop->u.btree.nEq = 1; | |
| 109697 | + /* TUNING: Cost of a rowid lookup is 10 */ | |
| 109698 | + pLoop->rRun = 33; /* 33==whereCost(10) */ | |
| 109699 | + }else{ | |
| 109700 | + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ | |
| 109701 | + if( pIdx->onError==OE_None ) continue; | |
| 109702 | + for(j=0; j<pIdx->nColumn; j++){ | |
| 109703 | + pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); | |
| 109704 | + if( pTerm==0 ) break; | |
| 109705 | + whereLoopResize(pWInfo->pParse->db, pLoop, j); | |
| 109706 | + pLoop->aLTerm[j] = pTerm; | |
| 109707 | + } | |
| 109708 | + if( j!=pIdx->nColumn ) continue; | |
| 109709 | + pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; | |
| 109710 | + if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ | |
| 109711 | + pLoop->wsFlags |= WHERE_IDX_ONLY; | |
| 109712 | + } | |
| 109713 | + pLoop->nLTerm = j; | |
| 109714 | + pLoop->u.btree.nEq = j; | |
| 109715 | + pLoop->u.btree.pIndex = pIdx; | |
| 109716 | + /* TUNING: Cost of a unique index lookup is 15 */ | |
| 109717 | + pLoop->rRun = 39; /* 39==whereCost(15) */ | |
| 109718 | + break; | |
| 109719 | + } | |
| 109720 | + } | |
| 109721 | + if( pLoop->wsFlags ){ | |
| 109722 | + pLoop->nOut = (WhereCost)1; | |
| 109723 | + pWInfo->a[0].pWLoop = pLoop; | |
| 109724 | + pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); | |
| 109725 | + pWInfo->a[0].iTabCur = iCur; | |
| 109726 | + pWInfo->nRowOut = 1; | |
| 109727 | + if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1; | |
| 109728 | + if( pWInfo->pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; | |
| 109729 | +#ifdef SQLITE_DEBUG | |
| 109730 | + pLoop->cId = '0'; | |
| 109731 | +#endif | |
| 109732 | + return 1; | |
| 109733 | + } | |
| 109734 | + return 0; | |
| 109735 | +} | |
| 109336 | 109736 | |
| 109337 | 109737 | /* |
| 109338 | 109738 | ** Generate the beginning of the loop used for WHERE clause processing. |
| 109339 | 109739 | ** The return value is a pointer to an opaque structure that contains |
| 109340 | 109740 | ** information needed to terminate the loop. Later, the calling routine |
| @@ -109410,19 +109810,10 @@ | ||
| 109410 | 109810 | ** ORDER BY CLAUSE PROCESSING |
| 109411 | 109811 | ** |
| 109412 | 109812 | ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
| 109413 | 109813 | ** if there is one. If there is no ORDER BY clause or if this routine |
| 109414 | 109814 | ** 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 | 109815 | */ |
| 109425 | 109816 | SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( |
| 109426 | 109817 | Parse *pParse, /* The parser context */ |
| 109427 | 109818 | SrcList *pTabList, /* A list of all tables to be scanned */ |
| 109428 | 109819 | Expr *pWhere, /* The WHERE clause */ |
| @@ -109434,22 +109825,21 @@ | ||
| 109434 | 109825 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| 109435 | 109826 | int nTabList; /* Number of elements in pTabList */ |
| 109436 | 109827 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 109437 | 109828 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 109438 | 109829 | Bitmask notReady; /* Cursors that are not yet positioned */ |
| 109439 | - WhereBestIdx sWBI; /* Best index search context */ | |
| 109830 | + WhereLoopBuilder sWLB; /* The WhereLoop builder */ | |
| 109440 | 109831 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
| 109441 | 109832 | 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 | 109833 | int ii; /* Loop counter */ |
| 109445 | 109834 | sqlite3 *db; /* Database connection */ |
| 109835 | + int rc; /* Return code */ | |
| 109446 | 109836 | |
| 109447 | 109837 | |
| 109448 | 109838 | /* Variable initialization */ |
| 109449 | - memset(&sWBI, 0, sizeof(sWBI)); | |
| 109450 | - sWBI.pParse = pParse; | |
| 109839 | + memset(&sWLB, 0, sizeof(sWLB)); | |
| 109840 | + sWLB.pOrderBy = pOrderBy; | |
| 109451 | 109841 | |
| 109452 | 109842 | /* The number of tables in the FROM clause is limited by the number of |
| 109453 | 109843 | ** bits in a Bitmask |
| 109454 | 109844 | */ |
| 109455 | 109845 | testcase( pTabList->nSrc==BMS ); |
| @@ -109472,49 +109862,59 @@ | ||
| 109472 | 109862 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 109473 | 109863 | ** some architectures. Hence the ROUND8() below. |
| 109474 | 109864 | */ |
| 109475 | 109865 | db = pParse->db; |
| 109476 | 109866 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
| 109477 | - pWInfo = sqlite3DbMallocZero(db, | |
| 109478 | - nByteWInfo + | |
| 109479 | - sizeof(WhereClause) + | |
| 109480 | - sizeof(WhereMaskSet) | |
| 109481 | - ); | |
| 109867 | + pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); | |
| 109482 | 109868 | if( db->mallocFailed ){ |
| 109483 | 109869 | sqlite3DbFree(db, pWInfo); |
| 109484 | 109870 | pWInfo = 0; |
| 109485 | 109871 | goto whereBeginError; |
| 109486 | 109872 | } |
| 109487 | 109873 | pWInfo->nLevel = nTabList; |
| 109488 | 109874 | pWInfo->pParse = pParse; |
| 109489 | 109875 | pWInfo->pTabList = pTabList; |
| 109876 | + pWInfo->pOrderBy = pOrderBy; | |
| 109877 | + pWInfo->pDistinct = pDistinct; | |
| 109490 | 109878 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
| 109491 | - pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo]; | |
| 109492 | 109879 | pWInfo->wctrlFlags = wctrlFlags; |
| 109493 | 109880 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 109494 | - pMaskSet = (WhereMaskSet*)&sWBI.pWC[1]; | |
| 109495 | - sWBI.aLevel = pWInfo->a; | |
| 109881 | + pMaskSet = &pWInfo->sMaskSet; | |
| 109882 | + sWLB.pWInfo = pWInfo; | |
| 109883 | + sWLB.pWC = &pWInfo->sWC; | |
| 109884 | + sWLB.pNew = (WhereLoop*)&pWInfo->a[nTabList]; | |
| 109885 | + whereLoopInit(sWLB.pNew); | |
| 109886 | +#ifdef SQLITE_DEBUG | |
| 109887 | + sWLB.pNew->cId = '*'; | |
| 109888 | +#endif | |
| 109496 | 109889 | |
| 109497 | 109890 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 109498 | 109891 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 109499 | 109892 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0; |
| 109500 | 109893 | |
| 109501 | 109894 | /* Split the WHERE clause into separate subexpressions where each |
| 109502 | 109895 | ** subexpression is separated by an AND operator. |
| 109503 | 109896 | */ |
| 109504 | 109897 | initMaskSet(pMaskSet); |
| 109505 | - whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags); | |
| 109898 | + whereClauseInit(&pWInfo->sWC, pWInfo); | |
| 109506 | 109899 | sqlite3ExprCodeConstants(pParse, pWhere); |
| 109507 | - whereSplit(sWBI.pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ | |
| 109900 | + whereSplit(&pWInfo->sWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ | |
| 109508 | 109901 | |
| 109509 | 109902 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 109510 | 109903 | ** expression and either jump over all of the code or fall thru. |
| 109511 | 109904 | */ |
| 109512 | 109905 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
| 109513 | 109906 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
| 109514 | 109907 | pWhere = 0; |
| 109515 | 109908 | } |
| 109909 | + | |
| 109910 | + /* Special case: No FROM clause | |
| 109911 | + */ | |
| 109912 | + if( nTabList==0 ){ | |
| 109913 | + if( pOrderBy ) pWInfo->bOBSat = 1; | |
| 109914 | + if( pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; | |
| 109915 | + } | |
| 109516 | 109916 | |
| 109517 | 109917 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 109518 | 109918 | ** |
| 109519 | 109919 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 109520 | 109920 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| @@ -109547,332 +109947,149 @@ | ||
| 109547 | 109947 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 109548 | 109948 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 109549 | 109949 | ** want to analyze these virtual terms, so start analyzing at the end |
| 109550 | 109950 | ** and work forward so that the added virtual terms are never processed. |
| 109551 | 109951 | */ |
| 109552 | - exprAnalyzeAll(pTabList, sWBI.pWC); | |
| 109952 | + exprAnalyzeAll(pTabList, &pWInfo->sWC); | |
| 109553 | 109953 | if( db->mallocFailed ){ |
| 109554 | 109954 | goto whereBeginError; |
| 109555 | 109955 | } |
| 109956 | + | |
| 109957 | + /* If the ORDER BY (or GROUP BY) clause contains references to general | |
| 109958 | + ** expressions, then we won't be able to satisfy it using indices, so | |
| 109959 | + ** go ahead and disable it now. | |
| 109960 | + */ | |
| 109961 | + if( pOrderBy && pDistinct ){ | |
| 109962 | + for(ii=0; ii<pOrderBy->nExpr; ii++){ | |
| 109963 | + Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr); | |
| 109964 | + if( pExpr->op!=TK_COLUMN ){ | |
| 109965 | + pWInfo->pOrderBy = pOrderBy = 0; | |
| 109966 | + break; | |
| 109967 | + }else if( pExpr->iColumn<0 ){ | |
| 109968 | + break; | |
| 109969 | + } | |
| 109970 | + } | |
| 109971 | + } | |
| 109556 | 109972 | |
| 109557 | 109973 | /* Check if the DISTINCT qualifier, if there is one, is redundant. |
| 109558 | 109974 | ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to |
| 109559 | 109975 | ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT. |
| 109560 | 109976 | */ |
| 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")); | |
| 109977 | + if( pDistinct ){ | |
| 109978 | + if( isDistinctRedundant(pParse,pTabList,&pWInfo->sWC,pDistinct) ){ | |
| 109979 | + pDistinct = 0; | |
| 109980 | + pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; | |
| 109981 | + }else if( pOrderBy==0 ){ | |
| 109982 | + pWInfo->wctrlFlags |= WHERE_DISTINCTBY; | |
| 109983 | + pWInfo->pOrderBy = pDistinct; | |
| 109984 | + } | |
| 109985 | + } | |
| 109986 | + | |
| 109987 | + /* Construct the WhereLoop objects */ | |
| 109988 | + WHERETRACE(0xffff,("*** Optimizer Start ***\n")); | |
| 109989 | + if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ | |
| 109990 | + rc = whereLoopAddAll(&sWLB); | |
| 109991 | + if( rc ) goto whereBeginError; | |
| 109992 | + | |
| 109993 | + /* Display all of the WhereLoop objects if wheretrace is enabled */ | |
| 109994 | +#ifdef WHERETRACE_ENABLED | |
| 109995 | + if( sqlite3WhereTrace ){ | |
| 109996 | + WhereLoop *p; | |
| 109997 | + int i = 0; | |
| 109998 | + static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" | |
| 109999 | + "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; | |
| 110000 | + for(p=pWInfo->pLoops; p; p=p->pNextLoop){ | |
| 110001 | + p->cId = zLabel[(i++)%sizeof(zLabel)]; | |
| 110002 | + whereLoopPrint(p, pTabList); | |
| 110003 | + } | |
| 110004 | + } | |
| 110005 | +#endif | |
| 110006 | + | |
| 110007 | + wherePathSolver(pWInfo, 0); | |
| 110008 | + if( db->mallocFailed ) goto whereBeginError; | |
| 110009 | + if( pWInfo->pOrderBy ){ | |
| 110010 | + wherePathSolver(pWInfo, pWInfo->nRowOut); | |
| 110011 | + if( db->mallocFailed ) goto whereBeginError; | |
| 110012 | + } | |
| 110013 | + } | |
| 110014 | + if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ | |
| 110015 | + pWInfo->revMask = (Bitmask)(-1); | |
| 110016 | + } | |
| 109817 | 110017 | if( pParse->nErr || db->mallocFailed ){ |
| 109818 | 110018 | goto whereBeginError; |
| 109819 | 110019 | } |
| 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 | - } | |
| 110020 | +#ifdef WHERETRACE_ENABLED | |
| 110021 | + if( sqlite3WhereTrace ){ | |
| 110022 | + int ii; | |
| 110023 | + sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); | |
| 110024 | + if( pWInfo->bOBSat ){ | |
| 110025 | + sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask); | |
| 110026 | + } | |
| 110027 | + switch( pWInfo->eDistinct ){ | |
| 110028 | + case WHERE_DISTINCT_UNIQUE: { | |
| 110029 | + sqlite3DebugPrintf(" DISTINCT=unique"); | |
| 110030 | + break; | |
| 110031 | + } | |
| 110032 | + case WHERE_DISTINCT_ORDERED: { | |
| 110033 | + sqlite3DebugPrintf(" DISTINCT=ordered"); | |
| 110034 | + break; | |
| 110035 | + } | |
| 110036 | + case WHERE_DISTINCT_UNORDERED: { | |
| 110037 | + sqlite3DebugPrintf(" DISTINCT=unordered"); | |
| 110038 | + break; | |
| 110039 | + } | |
| 110040 | + } | |
| 110041 | + sqlite3DebugPrintf("\n"); | |
| 110042 | + for(ii=0; ii<nTabList; ii++){ | |
| 110043 | + whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList); | |
| 110044 | + } | |
| 110045 | + } | |
| 110046 | +#endif | |
| 110047 | + WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); | |
| 110048 | + pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; | |
| 109834 | 110049 | |
| 109835 | 110050 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 109836 | 110051 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 109837 | 110052 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 109838 | 110053 | ** the statement to update a single row. |
| 109839 | 110054 | */ |
| 109840 | 110055 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
| 109841 | - if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){ | |
| 110056 | + if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 | |
| 110057 | + && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ | |
| 109842 | 110058 | pWInfo->okOnePass = 1; |
| 109843 | - pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY; | |
| 110059 | + pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; | |
| 109844 | 110060 | } |
| 109845 | 110061 | |
| 109846 | 110062 | /* Open all tables in the pTabList and any indices selected for |
| 109847 | 110063 | ** searching those tables. |
| 109848 | 110064 | */ |
| 109849 | 110065 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 109850 | 110066 | notReady = ~(Bitmask)0; |
| 109851 | - pWInfo->nRowOut = (double)1; | |
| 110067 | + pWInfo->nRowOut = (WhereCost)1; | |
| 109852 | 110068 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
| 109853 | 110069 | Table *pTab; /* Table to open */ |
| 109854 | 110070 | int iDb; /* Index of database containing table/index */ |
| 109855 | 110071 | struct SrcList_item *pTabItem; |
| 110072 | + WhereLoop *pLoop; | |
| 109856 | 110073 | |
| 109857 | 110074 | pTabItem = &pTabList->a[pLevel->iFrom]; |
| 109858 | 110075 | pTab = pTabItem->pTab; |
| 109859 | - pWInfo->nRowOut *= pLevel->plan.nRow; | |
| 109860 | 110076 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 110077 | + pLoop = pLevel->pWLoop; | |
| 109861 | 110078 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
| 109862 | 110079 | /* Do nothing */ |
| 109863 | 110080 | }else |
| 109864 | 110081 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 109865 | - if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){ | |
| 110082 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ | |
| 109866 | 110083 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 109867 | 110084 | int iCur = pTabItem->iCursor; |
| 109868 | 110085 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| 109869 | 110086 | }else if( IsVirtual(pTab) ){ |
| 109870 | 110087 | /* noop */ |
| 109871 | 110088 | }else |
| 109872 | 110089 | #endif |
| 109873 | - if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 | |
| 110090 | + if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 | |
| 109874 | 110091 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
| 109875 | 110092 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 109876 | 110093 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 109877 | 110094 | testcase( pTab->nCol==BMS-1 ); |
| 109878 | 110095 | testcase( pTab->nCol==BMS ); |
| @@ -109886,26 +110103,27 @@ | ||
| 109886 | 110103 | } |
| 109887 | 110104 | }else{ |
| 109888 | 110105 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 109889 | 110106 | } |
| 109890 | 110107 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 109891 | - if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){ | |
| 109892 | - constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel); | |
| 110108 | + if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){ | |
| 110109 | + constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel); | |
| 109893 | 110110 | }else |
| 109894 | 110111 | #endif |
| 109895 | - if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ | |
| 109896 | - Index *pIx = pLevel->plan.u.pIdx; | |
| 110112 | + if( pLoop->wsFlags & WHERE_INDEXED ){ | |
| 110113 | + Index *pIx = pLoop->u.btree.pIndex; | |
| 109897 | 110114 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
| 109898 | - int iIndexCur = pLevel->iIdxCur; | |
| 110115 | + /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */ | |
| 110116 | + int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++; | |
| 109899 | 110117 | assert( pIx->pSchema==pTab->pSchema ); |
| 109900 | 110118 | assert( iIndexCur>=0 ); |
| 109901 | 110119 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
| 109902 | 110120 | (char*)pKey, P4_KEYINFO_HANDOFF); |
| 109903 | 110121 | VdbeComment((v, "%s", pIx->zName)); |
| 109904 | 110122 | } |
| 109905 | 110123 | sqlite3CodeVerifySchema(pParse, iDb); |
| 109906 | - notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor); | |
| 110124 | + notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); | |
| 109907 | 110125 | } |
| 109908 | 110126 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 109909 | 110127 | if( db->mallocFailed ) goto whereBeginError; |
| 109910 | 110128 | |
| 109911 | 110129 | /* Generate the code to do the search. Each iteration of the for |
| @@ -109914,70 +110132,15 @@ | ||
| 109914 | 110132 | */ |
| 109915 | 110133 | notReady = ~(Bitmask)0; |
| 109916 | 110134 | for(ii=0; ii<nTabList; ii++){ |
| 109917 | 110135 | pLevel = &pWInfo->a[ii]; |
| 109918 | 110136 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
| 109919 | - notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady); | |
| 110137 | + notReady = codeOneLoopStart(pWInfo, ii, notReady); | |
| 109920 | 110138 | pWInfo->iContinue = pLevel->addrCont; |
| 109921 | 110139 | } |
| 109922 | 110140 | |
| 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 | - */ | |
| 110141 | + /* Done. */ | |
| 109979 | 110142 | return pWInfo; |
| 109980 | 110143 | |
| 109981 | 110144 | /* Jump here if malloc fails */ |
| 109982 | 110145 | whereBeginError: |
| 109983 | 110146 | if( pWInfo ){ |
| @@ -109994,24 +110157,26 @@ | ||
| 109994 | 110157 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ |
| 109995 | 110158 | Parse *pParse = pWInfo->pParse; |
| 109996 | 110159 | Vdbe *v = pParse->pVdbe; |
| 109997 | 110160 | int i; |
| 109998 | 110161 | WhereLevel *pLevel; |
| 110162 | + WhereLoop *pLoop; | |
| 109999 | 110163 | SrcList *pTabList = pWInfo->pTabList; |
| 110000 | 110164 | sqlite3 *db = pParse->db; |
| 110001 | 110165 | |
| 110002 | 110166 | /* Generate loop termination code. |
| 110003 | 110167 | */ |
| 110004 | 110168 | sqlite3ExprCacheClear(pParse); |
| 110005 | 110169 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
| 110006 | 110170 | pLevel = &pWInfo->a[i]; |
| 110171 | + pLoop = pLevel->pWLoop; | |
| 110007 | 110172 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
| 110008 | 110173 | if( pLevel->op!=OP_Noop ){ |
| 110009 | 110174 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
| 110010 | 110175 | sqlite3VdbeChangeP5(v, pLevel->p5); |
| 110011 | 110176 | } |
| 110012 | - if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ | |
| 110177 | + if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ | |
| 110013 | 110178 | struct InLoop *pIn; |
| 110014 | 110179 | int j; |
| 110015 | 110180 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
| 110016 | 110181 | for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
| 110017 | 110182 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| @@ -110022,16 +110187,16 @@ | ||
| 110022 | 110187 | } |
| 110023 | 110188 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
| 110024 | 110189 | if( pLevel->iLeftJoin ){ |
| 110025 | 110190 | int addr; |
| 110026 | 110191 | 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 ){ | |
| 110192 | + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 | |
| 110193 | + || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); | |
| 110194 | + if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ | |
| 110030 | 110195 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 110031 | 110196 | } |
| 110032 | - if( pLevel->iIdxCur>=0 ){ | |
| 110197 | + if( pLoop->wsFlags & WHERE_INDEXED ){ | |
| 110033 | 110198 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
| 110034 | 110199 | } |
| 110035 | 110200 | if( pLevel->op==OP_Return ){ |
| 110036 | 110201 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 110037 | 110202 | }else{ |
| @@ -110052,19 +110217,20 @@ | ||
| 110052 | 110217 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
| 110053 | 110218 | Index *pIdx = 0; |
| 110054 | 110219 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
| 110055 | 110220 | Table *pTab = pTabItem->pTab; |
| 110056 | 110221 | assert( pTab!=0 ); |
| 110222 | + pLoop = pLevel->pWLoop; | |
| 110057 | 110223 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 110058 | 110224 | && pTab->pSelect==0 |
| 110059 | 110225 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
| 110060 | 110226 | ){ |
| 110061 | - int ws = pLevel->plan.wsFlags; | |
| 110227 | + int ws = pLoop->wsFlags; | |
| 110062 | 110228 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
| 110063 | 110229 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 110064 | 110230 | } |
| 110065 | - if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){ | |
| 110231 | + if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_TEMP_INDEX))==0 ){ | |
| 110066 | 110232 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 110067 | 110233 | } |
| 110068 | 110234 | } |
| 110069 | 110235 | |
| 110070 | 110236 | /* If this scan uses an index, make code substitutions to read data |
| @@ -110078,16 +110244,16 @@ | ||
| 110078 | 110244 | ** sqlite3WhereEnd will have created code that references the table |
| 110079 | 110245 | ** directly. This loop scans all that code looking for opcodes |
| 110080 | 110246 | ** that reference the table and converts them into opcodes that |
| 110081 | 110247 | ** reference the index. |
| 110082 | 110248 | */ |
| 110083 | - if( pLevel->plan.wsFlags & WHERE_INDEXED ){ | |
| 110084 | - pIdx = pLevel->plan.u.pIdx; | |
| 110085 | - }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){ | |
| 110249 | + if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ | |
| 110250 | + pIdx = pLoop->u.btree.pIndex; | |
| 110251 | + }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ | |
| 110086 | 110252 | pIdx = pLevel->u.pCovidx; |
| 110087 | 110253 | } |
| 110088 | - if( pIdx && !db->mallocFailed){ | |
| 110254 | + if( pIdx && !db->mallocFailed ){ | |
| 110089 | 110255 | int k, j, last; |
| 110090 | 110256 | VdbeOp *pOp; |
| 110091 | 110257 | |
| 110092 | 110258 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 110093 | 110259 | last = sqlite3VdbeCurrentAddr(v); |
| @@ -110099,12 +110265,11 @@ | ||
| 110099 | 110265 | pOp->p2 = j; |
| 110100 | 110266 | pOp->p1 = pLevel->iIdxCur; |
| 110101 | 110267 | break; |
| 110102 | 110268 | } |
| 110103 | 110269 | } |
| 110104 | - assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 | |
| 110105 | - || j<pIdx->nColumn ); | |
| 110270 | + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn ); | |
| 110106 | 110271 | }else if( pOp->opcode==OP_Rowid ){ |
| 110107 | 110272 | pOp->p1 = pLevel->iIdxCur; |
| 110108 | 110273 | pOp->opcode = OP_IdxRowid; |
| 110109 | 110274 | } |
| 110110 | 110275 | } |
| @@ -115480,11 +115645,11 @@ | ||
| 115480 | 115645 | } |
| 115481 | 115646 | |
| 115482 | 115647 | /* |
| 115483 | 115648 | ** Another built-in collating sequence: NOCASE. |
| 115484 | 115649 | ** |
| 115485 | -** This collating sequence is intended to be used for "case independant | |
| 115650 | +** This collating sequence is intended to be used for "case independent | |
| 115486 | 115651 | ** comparison". SQLite's knowledge of upper and lower case equivalents |
| 115487 | 115652 | ** extends only to the 26 characters used in the English language. |
| 115488 | 115653 | ** |
| 115489 | 115654 | ** At the moment there is only a UTF-8 implementation. |
| 115490 | 115655 | */ |
| @@ -115627,16 +115792,10 @@ | ||
| 115627 | 115792 | "statements or unfinished backups"); |
| 115628 | 115793 | sqlite3_mutex_leave(db->mutex); |
| 115629 | 115794 | return SQLITE_BUSY; |
| 115630 | 115795 | } |
| 115631 | 115796 | |
| 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 | 115797 | #ifdef SQLITE_ENABLE_SQLLOG |
| 115639 | 115798 | if( sqlite3GlobalConfig.xSqllog ){ |
| 115640 | 115799 | /* Closing the handle. Fourth parameter is passed the value 2. */ |
| 115641 | 115800 | sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2); |
| 115642 | 115801 | } |
| @@ -115686,10 +115845,16 @@ | ||
| 115686 | 115845 | /* If we reach this point, it means that the database connection has |
| 115687 | 115846 | ** closed all sqlite3_stmt and sqlite3_backup objects and has been |
| 115688 | 115847 | ** passed to sqlite3_close (meaning that it is a zombie). Therefore, |
| 115689 | 115848 | ** go ahead and free all resources. |
| 115690 | 115849 | */ |
| 115850 | + | |
| 115851 | + /* If a transaction is open, roll it back. This also ensures that if | |
| 115852 | + ** any database schemas have been modified by an uncommitted transaction | |
| 115853 | + ** they are reset. And that the required b-tree mutex is held to make | |
| 115854 | + ** the pager rollback and schema reset an atomic operation. */ | |
| 115855 | + sqlite3RollbackAll(db, SQLITE_OK); | |
| 115691 | 115856 | |
| 115692 | 115857 | /* Free any outstanding Savepoint structures. */ |
| 115693 | 115858 | sqlite3CloseSavepoints(db); |
| 115694 | 115859 | |
| 115695 | 115860 | /* Close all database connections */ |
| @@ -115787,19 +115952,26 @@ | ||
| 115787 | 115952 | SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){ |
| 115788 | 115953 | int i; |
| 115789 | 115954 | int inTrans = 0; |
| 115790 | 115955 | assert( sqlite3_mutex_held(db->mutex) ); |
| 115791 | 115956 | sqlite3BeginBenignMalloc(); |
| 115957 | + | |
| 115958 | + /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). | |
| 115959 | + ** This is important in case the transaction being rolled back has | |
| 115960 | + ** modified the database schema. If the b-tree mutexes are not taken | |
| 115961 | + ** here, then another shared-cache connection might sneak in between | |
| 115962 | + ** the database rollback and schema reset, which can cause false | |
| 115963 | + ** corruption reports in some cases. */ | |
| 115792 | 115964 | sqlite3BtreeEnterAll(db); |
| 115965 | + | |
| 115793 | 115966 | for(i=0; i<db->nDb; i++){ |
| 115794 | 115967 | Btree *p = db->aDb[i].pBt; |
| 115795 | 115968 | if( p ){ |
| 115796 | 115969 | if( sqlite3BtreeIsInTrans(p) ){ |
| 115797 | 115970 | inTrans = 1; |
| 115798 | 115971 | } |
| 115799 | 115972 | sqlite3BtreeRollback(p, tripCode); |
| 115800 | - db->aDb[i].inTrans = 0; | |
| 115801 | 115973 | } |
| 115802 | 115974 | } |
| 115803 | 115975 | sqlite3VtabRollback(db); |
| 115804 | 115976 | sqlite3EndBenignMalloc(); |
| 115805 | 115977 | |
| @@ -117562,12 +117734,10 @@ | ||
| 117562 | 117734 | /* |
| 117563 | 117735 | ** Test to see whether or not the database connection is in autocommit |
| 117564 | 117736 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 117565 | 117737 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 117566 | 117738 | ** by the next COMMIT or ROLLBACK. |
| 117567 | -** | |
| 117568 | -******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** | |
| 117569 | 117739 | */ |
| 117570 | 117740 | SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 117571 | 117741 | return db->autoCommit; |
| 117572 | 117742 | } |
| 117573 | 117743 | |
| @@ -119051,10 +119221,22 @@ | ||
| 119051 | 119221 | |
| 119052 | 119222 | #endif /* _FTS3_HASH_H_ */ |
| 119053 | 119223 | |
| 119054 | 119224 | /************** End of fts3_hash.h *******************************************/ |
| 119055 | 119225 | /************** Continuing where we left off in fts3Int.h ********************/ |
| 119226 | + | |
| 119227 | +/* | |
| 119228 | +** This constant determines the maximum depth of an FTS expression tree | |
| 119229 | +** that the library will create and use. FTS uses recursion to perform | |
| 119230 | +** various operations on the query tree, so the disadvantage of a large | |
| 119231 | +** limit is that it may allow very large queries to use large amounts | |
| 119232 | +** of stack space (perhaps causing a stack overflow). | |
| 119233 | +*/ | |
| 119234 | +#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH | |
| 119235 | +# define SQLITE_FTS3_MAX_EXPR_DEPTH 12 | |
| 119236 | +#endif | |
| 119237 | + | |
| 119056 | 119238 | |
| 119057 | 119239 | /* |
| 119058 | 119240 | ** This constant controls how often segments are merged. Once there are |
| 119059 | 119241 | ** FTS3_MERGE_COUNT segments of level N, they are merged into a single |
| 119060 | 119242 | ** segment of level N+1. |
| @@ -120709,11 +120891,11 @@ | ||
| 120709 | 120891 | /* By default use a full table scan. This is an expensive option, |
| 120710 | 120892 | ** so search through the constraints to see if a more efficient |
| 120711 | 120893 | ** strategy is possible. |
| 120712 | 120894 | */ |
| 120713 | 120895 | pInfo->idxNum = FTS3_FULLSCAN_SEARCH; |
| 120714 | - pInfo->estimatedCost = 500000; | |
| 120896 | + pInfo->estimatedCost = 5000000; | |
| 120715 | 120897 | for(i=0; i<pInfo->nConstraint; i++){ |
| 120716 | 120898 | struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i]; |
| 120717 | 120899 | if( pCons->usable==0 ) continue; |
| 120718 | 120900 | |
| 120719 | 120901 | /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */ |
| @@ -122270,15 +122452,11 @@ | ||
| 122270 | 122452 | ); |
| 122271 | 122453 | if( rc!=SQLITE_OK ){ |
| 122272 | 122454 | return rc; |
| 122273 | 122455 | } |
| 122274 | 122456 | |
| 122275 | - rc = sqlite3Fts3ReadLock(p); | |
| 122276 | - if( rc!=SQLITE_OK ) return rc; | |
| 122277 | - | |
| 122278 | 122457 | rc = fts3EvalStart(pCsr); |
| 122279 | - | |
| 122280 | 122458 | sqlite3Fts3SegmentsClose(p); |
| 122281 | 122459 | if( rc!=SQLITE_OK ) return rc; |
| 122282 | 122460 | pCsr->pNextId = pCsr->aDoclist; |
| 122283 | 122461 | pCsr->iPrevId = 0; |
| 122284 | 122462 | } |
| @@ -126129,30 +126307,30 @@ | ||
| 126129 | 126307 | int iDefaultCol, /* Default column to query */ |
| 126130 | 126308 | const char *z, int n, /* Text of MATCH query */ |
| 126131 | 126309 | Fts3Expr **ppExpr, /* OUT: Parsed query structure */ |
| 126132 | 126310 | char **pzErr /* OUT: Error message (sqlite3_malloc) */ |
| 126133 | 126311 | ){ |
| 126134 | - static const int MAX_EXPR_DEPTH = 12; | |
| 126135 | 126312 | int rc = fts3ExprParseUnbalanced( |
| 126136 | 126313 | pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr |
| 126137 | 126314 | ); |
| 126138 | 126315 | |
| 126139 | 126316 | /* Rebalance the expression. And check that its depth does not exceed |
| 126140 | - ** MAX_EXPR_DEPTH. */ | |
| 126317 | + ** SQLITE_FTS3_MAX_EXPR_DEPTH. */ | |
| 126141 | 126318 | if( rc==SQLITE_OK && *ppExpr ){ |
| 126142 | - rc = fts3ExprBalance(ppExpr, MAX_EXPR_DEPTH); | |
| 126319 | + rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); | |
| 126143 | 126320 | if( rc==SQLITE_OK ){ |
| 126144 | - rc = fts3ExprCheckDepth(*ppExpr, MAX_EXPR_DEPTH); | |
| 126321 | + rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); | |
| 126145 | 126322 | } |
| 126146 | 126323 | } |
| 126147 | 126324 | |
| 126148 | 126325 | if( rc!=SQLITE_OK ){ |
| 126149 | 126326 | sqlite3Fts3ExprFree(*ppExpr); |
| 126150 | 126327 | *ppExpr = 0; |
| 126151 | 126328 | if( rc==SQLITE_TOOBIG ){ |
| 126152 | 126329 | *pzErr = sqlite3_mprintf( |
| 126153 | - "FTS expression tree is too large (maximum depth %d)", MAX_EXPR_DEPTH | |
| 126330 | + "FTS expression tree is too large (maximum depth %d)", | |
| 126331 | + SQLITE_FTS3_MAX_EXPR_DEPTH | |
| 126154 | 126332 | ); |
| 126155 | 126333 | rc = SQLITE_ERROR; |
| 126156 | 126334 | }else if( rc==SQLITE_ERROR ){ |
| 126157 | 126335 | *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z); |
| 126158 | 126336 | } |
| @@ -129110,41 +129288,34 @@ | ||
| 129110 | 129288 | *pRC = rc; |
| 129111 | 129289 | } |
| 129112 | 129290 | |
| 129113 | 129291 | |
| 129114 | 129292 | /* |
| 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); | |
| 129293 | +** This function ensures that the caller has obtained an exclusive | |
| 129294 | +** shared-cache table-lock on the %_segdir table. This is required before | |
| 129295 | +** writing data to the fts3 table. If this lock is not acquired first, then | |
| 129296 | +** the caller may end up attempting to take this lock as part of committing | |
| 129297 | +** a transaction, causing SQLite to return SQLITE_LOCKED or | |
| 129298 | +** LOCKED_SHAREDCACHEto a COMMIT command. | |
| 129299 | +** | |
| 129300 | +** It is best to avoid this because if FTS3 returns any error when | |
| 129301 | +** committing a transaction, the whole transaction will be rolled back. | |
| 129302 | +** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. | |
| 129303 | +** It can still happen if the user locks the underlying tables directly | |
| 129304 | +** instead of accessing them via FTS. | |
| 129305 | +*/ | |
| 129306 | +static int fts3Writelock(Fts3Table *p){ | |
| 129307 | + int rc = SQLITE_OK; | |
| 129308 | + | |
| 129309 | + if( p->nPendingData==0 ){ | |
| 129310 | + sqlite3_stmt *pStmt; | |
| 129311 | + rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0); | |
| 129139 | 129312 | if( rc==SQLITE_OK ){ |
| 129140 | 129313 | sqlite3_bind_null(pStmt, 1); |
| 129141 | 129314 | sqlite3_step(pStmt); |
| 129142 | 129315 | rc = sqlite3_reset(pStmt); |
| 129143 | 129316 | } |
| 129144 | - }else{ | |
| 129145 | - rc = SQLITE_OK; | |
| 129146 | 129317 | } |
| 129147 | 129318 | |
| 129148 | 129319 | return rc; |
| 129149 | 129320 | } |
| 129150 | 129321 | |
| @@ -133918,10 +134089,13 @@ | ||
| 133918 | 134089 | goto update_out; |
| 133919 | 134090 | } |
| 133920 | 134091 | aSzIns = &aSzDel[p->nColumn+1]; |
| 133921 | 134092 | memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2); |
| 133922 | 134093 | |
| 134094 | + rc = fts3Writelock(p); | |
| 134095 | + if( rc!=SQLITE_OK ) goto update_out; | |
| 134096 | + | |
| 133923 | 134097 | /* If this is an INSERT operation, or an UPDATE that modifies the rowid |
| 133924 | 134098 | ** value, then this operation requires constraint handling. |
| 133925 | 134099 | ** |
| 133926 | 134100 | ** If the on-conflict mode is REPLACE, this means that the existing row |
| 133927 | 134101 | ** should be deleted from the database before inserting the new row. Or, |
| @@ -136051,32 +136225,31 @@ | ||
| 136051 | 136225 | 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004, |
| 136052 | 136226 | 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002, |
| 136053 | 136227 | 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803, |
| 136054 | 136228 | 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07, |
| 136055 | 136229 | 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, | |
| 136230 | + 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802, | |
| 136231 | + 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013, | |
| 136232 | + 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06, | |
| 136233 | + 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003, | |
| 136234 | + 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01, | |
| 136235 | + 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403, | |
| 136236 | + 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009, | |
| 136237 | + 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003, | |
| 136238 | + 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003, | |
| 136239 | + 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E, | |
| 136240 | + 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046, | |
| 136241 | + 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401, | |
| 136242 | + 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401, | |
| 136243 | + 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F, | |
| 136244 | + 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C, | |
| 136245 | + 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002, | |
| 136246 | + 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025, | |
| 136247 | + 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6, | |
| 136248 | + 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46, | |
| 136249 | + 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060, | |
| 136250 | + 0x380400F0, | |
| 136078 | 136251 | }; |
| 136079 | 136252 | static const unsigned int aAscii[4] = { |
| 136080 | 136253 | 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001, |
| 136081 | 136254 | }; |
| 136082 | 136255 | |
| @@ -139705,11 +139878,11 @@ | ||
| 139705 | 139878 | ** operator) using the ICU uregex_XX() APIs. |
| 139706 | 139879 | ** |
| 139707 | 139880 | ** * Implementations of the SQL scalar upper() and lower() functions |
| 139708 | 139881 | ** for case mapping. |
| 139709 | 139882 | ** |
| 139710 | -** * Integration of ICU and SQLite collation seqences. | |
| 139883 | +** * Integration of ICU and SQLite collation sequences. | |
| 139711 | 139884 | ** |
| 139712 | 139885 | ** * An implementation of the LIKE operator that uses ICU to |
| 139713 | 139886 | ** provide case-independent matching. |
| 139714 | 139887 | */ |
| 139715 | 139888 | |
| 139716 | 139889 |
| --- src/sqlite3.c | |
| +++ src/sqlite3.c | |
| @@ -352,11 +352,11 @@ | |
| 352 | |
| 353 | /* |
| 354 | ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. |
| 355 | ** 0 means mutexes are permanently disable and the library is never |
| 356 | ** threadsafe. 1 means the library is serialized which is the highest |
| 357 | ** level of threadsafety. 2 means the libary is multithreaded - multiple |
| 358 | ** threads can use SQLite as long as no two threads try to use the same |
| 359 | ** database connection at the same time. |
| 360 | ** |
| 361 | ** Older versions of SQLite used an optional THREADSAFE macro. |
| 362 | ** We support that for legacy. |
| @@ -431,24 +431,16 @@ | |
| 431 | # define SQLITE_MALLOC_SOFT_LIMIT 1024 |
| 432 | #endif |
| 433 | |
| 434 | /* |
| 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. |
| 446 | */ |
| 447 | #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) \ |
| 448 | && !defined(__APPLE__) && SQLITE_THREADSAFE |
| 449 | # define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */ |
| 450 | #endif |
| 451 | |
| 452 | /* |
| 453 | ** The TCL headers are only needed when compiling the TCL bindings. |
| 454 | */ |
| @@ -678,11 +670,11 @@ | |
| 678 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 679 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 680 | */ |
| 681 | #define SQLITE_VERSION "3.7.17" |
| 682 | #define SQLITE_VERSION_NUMBER 3007017 |
| 683 | #define SQLITE_SOURCE_ID "2013-05-15 18:34:17 00231fb0127960d700de3549e34e82f8ec1b5819" |
| 684 | |
| 685 | /* |
| 686 | ** CAPI3REF: Run-Time Library Version Numbers |
| 687 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 688 | ** |
| @@ -5087,10 +5079,15 @@ | |
| 5087 | */ |
| 5088 | SQLITE_API int sqlite3_key( |
| 5089 | sqlite3 *db, /* Database to be rekeyed */ |
| 5090 | const void *pKey, int nKey /* The key */ |
| 5091 | ); |
| 5092 | |
| 5093 | /* |
| 5094 | ** Change the key on an open database. If the current database is not |
| 5095 | ** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the |
| 5096 | ** database is decrypted. |
| @@ -5100,10 +5097,15 @@ | |
| 5100 | */ |
| 5101 | SQLITE_API int sqlite3_rekey( |
| 5102 | sqlite3 *db, /* Database to be rekeyed */ |
| 5103 | const void *pKey, int nKey /* The new key */ |
| 5104 | ); |
| 5105 | |
| 5106 | /* |
| 5107 | ** Specify the activation key for a SEE database. Unless |
| 5108 | ** activated, none of the SEE routines will work. |
| 5109 | */ |
| @@ -8151,10 +8153,16 @@ | |
| 8151 | */ |
| 8152 | #ifndef offsetof |
| 8153 | #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) |
| 8154 | #endif |
| 8155 | |
| 8156 | /* |
| 8157 | ** Check to see if this machine uses EBCDIC. (Yes, believe it or |
| 8158 | ** not, there are still machines out there that use EBCDIC.) |
| 8159 | */ |
| 8160 | #if 'A' == '\301' |
| @@ -8476,13 +8484,11 @@ | |
| 8476 | typedef struct TriggerStep TriggerStep; |
| 8477 | typedef struct UnpackedRecord UnpackedRecord; |
| 8478 | typedef struct VTable VTable; |
| 8479 | typedef struct VtabCtx VtabCtx; |
| 8480 | typedef struct Walker Walker; |
| 8481 | typedef struct WherePlan WherePlan; |
| 8482 | typedef struct WhereInfo WhereInfo; |
| 8483 | typedef struct WhereLevel WhereLevel; |
| 8484 | |
| 8485 | /* |
| 8486 | ** Defer sourcing vdbe.h and btree.h until after the "u8" and |
| 8487 | ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque |
| 8488 | ** pointer types (i.e. FuncDef) defined above. |
| @@ -9915,11 +9921,10 @@ | |
| 9915 | ** databases may be attached. |
| 9916 | */ |
| 9917 | struct Db { |
| 9918 | char *zName; /* Name of this database */ |
| 9919 | Btree *pBt; /* The B*Tree structure for this database file */ |
| 9920 | u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */ |
| 9921 | u8 safety_level; /* How aggressive at syncing data to disk */ |
| 9922 | Schema *pSchema; /* Pointer to database schema (possibly shared) */ |
| 9923 | }; |
| 9924 | |
| 9925 | /* |
| @@ -10713,10 +10718,11 @@ | |
| 10713 | int tnum; /* DB Page containing root of this index */ |
| 10714 | u16 nColumn; /* Number of columns in table used by this index */ |
| 10715 | u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 10716 | unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ |
| 10717 | unsigned bUnordered:1; /* Use this index for == or IN queries only */ |
| 10718 | #ifdef SQLITE_ENABLE_STAT3 |
| 10719 | int nSample; /* Number of elements in aSample[] */ |
| 10720 | tRowcnt avgEq; /* Average nEq value for key values not in aSample */ |
| 10721 | IndexSample *aSample; /* Samples of the left-most key */ |
| 10722 | #endif |
| @@ -11058,10 +11064,15 @@ | |
| 11058 | /* |
| 11059 | ** The number of bits in a Bitmask. "BMS" means "BitMask Size". |
| 11060 | */ |
| 11061 | #define BMS ((int)(sizeof(Bitmask)*8)) |
| 11062 | |
| 11063 | /* |
| 11064 | ** The following structure describes the FROM clause of a SELECT statement. |
| 11065 | ** Each table or subquery in the FROM clause is a separate element of |
| 11066 | ** the SrcList.a[] array. |
| 11067 | ** |
| @@ -11078,12 +11089,12 @@ | |
| 11078 | ** |
| 11079 | ** In the colUsed field, the high-order bit (bit 63) is set if the table |
| 11080 | ** contains more than 63 columns and the 64-th or later column is used. |
| 11081 | */ |
| 11082 | struct SrcList { |
| 11083 | i16 nSrc; /* Number of tables or subqueries in the FROM clause */ |
| 11084 | i16 nAlloc; /* Number of entries allocated in a[] below */ |
| 11085 | struct SrcList_item { |
| 11086 | Schema *pSchema; /* Schema to which this item is fixed */ |
| 11087 | char *zDatabase; /* Name of database holding this table */ |
| 11088 | char *zName; /* Name of the table */ |
| 11089 | char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ |
| @@ -11117,83 +11128,10 @@ | |
| 11117 | #define JT_RIGHT 0x0010 /* Right outer join */ |
| 11118 | #define JT_OUTER 0x0020 /* The "OUTER" keyword is present */ |
| 11119 | #define JT_ERROR 0x0040 /* unknown or unsupported join type */ |
| 11120 | |
| 11121 | |
| 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 | /* |
| 11196 | ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin() |
| 11197 | ** and the WhereInfo.wctrlFlags member. |
| 11198 | */ |
| 11199 | #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */ |
| @@ -11203,37 +11141,15 @@ | |
| 11203 | #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ |
| 11204 | #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ |
| 11205 | #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ |
| 11206 | #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ |
| 11207 | #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 */ |
| 11235 | #define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */ |
| 11236 | #define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */ |
| 11237 | #define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */ |
| 11238 | #define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */ |
| 11239 | |
| @@ -11303,11 +11219,11 @@ | |
| 11303 | ExprList *pEList; /* The fields of the result */ |
| 11304 | u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ |
| 11305 | u16 selFlags; /* Various SF_* values */ |
| 11306 | int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ |
| 11307 | int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */ |
| 11308 | double nSelectRow; /* Estimated number of result rows */ |
| 11309 | SrcList *pSrc; /* The FROM clause */ |
| 11310 | Expr *pWhere; /* The WHERE clause */ |
| 11311 | ExprList *pGroupBy; /* The GROUP BY clause */ |
| 11312 | Expr *pHaving; /* The HAVING clause */ |
| 11313 | ExprList *pOrderBy; /* The ORDER BY clause */ |
| @@ -11487,11 +11403,11 @@ | |
| 11487 | AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ |
| 11488 | |
| 11489 | /* Information used while coding trigger programs. */ |
| 11490 | Parse *pToplevel; /* Parse structure for main program (or NULL) */ |
| 11491 | Table *pTriggerTab; /* Table triggers are being coded for */ |
| 11492 | double nQueryLoop; /* Estimated number of iterations of a query */ |
| 11493 | u32 oldmask; /* Mask of old.* columns referenced */ |
| 11494 | u32 newmask; /* Mask of new.* columns referenced */ |
| 11495 | u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ |
| 11496 | u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ |
| 11497 | u8 disableTriggers; /* True to disable triggers */ |
| @@ -12057,10 +11973,16 @@ | |
| 12057 | #endif |
| 12058 | SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*); |
| 12059 | SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int); |
| 12060 | SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); |
| 12061 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*); |
| 12062 | SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8); |
| 12063 | SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int); |
| 12064 | SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int); |
| 12065 | SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int); |
| 12066 | SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*); |
| @@ -19960,17 +19882,11 @@ | |
| 19960 | if( flag_plussign ) prefix = '+'; |
| 19961 | else if( flag_blanksign ) prefix = ' '; |
| 19962 | else prefix = 0; |
| 19963 | } |
| 19964 | 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 | for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} |
| 19971 | #endif |
| 19972 | if( xtype==etFLOAT ) realvalue += rounder; |
| 19973 | /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 19974 | exp = 0; |
| 19975 | if( sqlite3IsNaN((double)realvalue) ){ |
| 19976 | bufpt = "NaN"; |
| @@ -26866,19 +26782,23 @@ | |
| 26866 | } |
| 26867 | return SQLITE_OK; |
| 26868 | } |
| 26869 | case SQLITE_FCNTL_MMAP_SIZE: { |
| 26870 | i64 newLimit = *(i64*)pArg; |
| 26871 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 26872 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 26873 | } |
| 26874 | *(i64*)pArg = pFile->mmapSizeMax; |
| 26875 | if( newLimit>=0 ){ |
| 26876 | pFile->mmapSizeMax = newLimit; |
| 26877 | if( newLimit<pFile->mmapSize ) pFile->mmapSize = newLimit; |
| 26878 | } |
| 26879 | return SQLITE_OK; |
| 26880 | } |
| 26881 | #ifdef SQLITE_DEBUG |
| 26882 | /* The pager calls this method to signal that it has done |
| 26883 | ** a rollback and that the database is therefore unchanged and |
| 26884 | ** it hence it is OK for the transaction change counter to be |
| @@ -28244,11 +28164,11 @@ | |
| 28244 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
| 28245 | pNew->h = h; |
| 28246 | pNew->pVfs = pVfs; |
| 28247 | pNew->zPath = zFilename; |
| 28248 | pNew->ctrlFlags = (u8)ctrlFlags; |
| 28249 | pNew->mmapSizeMax = sqlite3GlobalConfig.mxMmap; |
| 28250 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 28251 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 28252 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
| 28253 | } |
| 28254 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
| @@ -30799,17 +30719,10 @@ | |
| 30799 | ** This file mapping API is common to both Win32 and WinRT. |
| 30800 | */ |
| 30801 | WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
| 30802 | #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */ |
| 30803 | |
| 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 | /* |
| 30812 | ** Some Microsoft compilers lack this definition. |
| 30813 | */ |
| 30814 | #ifndef INVALID_FILE_ATTRIBUTES |
| 30815 | # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
| @@ -33531,10 +33444,13 @@ | |
| 33531 | } |
| 33532 | } |
| 33533 | |
| 33534 | /* Forward declaration */ |
| 33535 | static int getTempname(int nBuf, char *zBuf); |
| 33536 | |
| 33537 | /* |
| 33538 | ** Control and query of the open file handle. |
| 33539 | */ |
| 33540 | static int winFileControl(sqlite3_file *id, int op, void *pArg){ |
| @@ -33614,17 +33530,24 @@ | |
| 33614 | return SQLITE_OK; |
| 33615 | } |
| 33616 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 33617 | case SQLITE_FCNTL_MMAP_SIZE: { |
| 33618 | i64 newLimit = *(i64*)pArg; |
| 33619 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 33620 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 33621 | } |
| 33622 | *(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; |
| 33626 | } |
| 33627 | #endif |
| 33628 | } |
| 33629 | OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h)); |
| 33630 | return SQLITE_NOTFOUND; |
| @@ -33652,19 +33575,19 @@ | |
| 33652 | winFile *p = (winFile*)id; |
| 33653 | return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | |
| 33654 | ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0); |
| 33655 | } |
| 33656 | |
| 33657 | #ifndef SQLITE_OMIT_WAL |
| 33658 | |
| 33659 | /* |
| 33660 | ** Windows will only let you create file view mappings |
| 33661 | ** on allocation size granularity boundaries. |
| 33662 | ** During sqlite3_os_init() we do a GetSystemInfo() |
| 33663 | ** to get the granularity size. |
| 33664 | */ |
| 33665 | SYSTEM_INFO winSysInfo; |
| 33666 | |
| 33667 | /* |
| 33668 | ** Helper functions to obtain and relinquish the global mutex. The |
| 33669 | ** global mutex is used to protect the winLockInfo objects used by |
| 33670 | ** this file, all of which may be shared by multiple threads. |
| @@ -34961,11 +34884,11 @@ | |
| 34961 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 34962 | pFile->hMap = NULL; |
| 34963 | pFile->pMapRegion = 0; |
| 34964 | pFile->mmapSize = 0; |
| 34965 | pFile->mmapSizeActual = 0; |
| 34966 | pFile->mmapSizeMax = sqlite3GlobalConfig.mxMmap; |
| 34967 | #endif |
| 34968 | |
| 34969 | OpenCounter(+1); |
| 34970 | return rc; |
| 34971 | } |
| @@ -37220,11 +37143,11 @@ | |
| 37220 | PCache1 *pCache; /* The newly created page cache */ |
| 37221 | PGroup *pGroup; /* The group the new page cache will belong to */ |
| 37222 | int sz; /* Bytes of memory required to allocate the new cache */ |
| 37223 | |
| 37224 | /* |
| 37225 | ** The seperateCache variable is true if each PCache has its own private |
| 37226 | ** PGroup. In other words, separateCache is true for mode (1) where no |
| 37227 | ** mutexing is required. |
| 37228 | ** |
| 37229 | ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT |
| 37230 | ** |
| @@ -42544,11 +42467,12 @@ | |
| 42544 | /* Before the first write, give the VFS a hint of what the final |
| 42545 | ** file size will be. |
| 42546 | */ |
| 42547 | assert( rc!=SQLITE_OK || isOpen(pPager->fd) ); |
| 42548 | if( rc==SQLITE_OK |
| 42549 | && (pList->pDirty ? pPager->dbSize : pList->pgno+1)>pPager->dbHintSize |
| 42550 | ){ |
| 42551 | sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize; |
| 42552 | sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile); |
| 42553 | pPager->dbHintSize = pPager->dbSize; |
| 42554 | } |
| @@ -43509,11 +43433,11 @@ | |
| 43509 | ** requested page is not already stored in the cache, then no |
| 43510 | ** actual disk read occurs. In this case the memory image of the |
| 43511 | ** page is initialized to all zeros. |
| 43512 | ** |
| 43513 | ** If noContent is true, it means that we do not care about the contents |
| 43514 | ** of the page. This occurs in two seperate scenarios: |
| 43515 | ** |
| 43516 | ** a) When reading a free-list leaf page from the database, and |
| 43517 | ** |
| 43518 | ** b) When a savepoint is being rolled back and we need to load |
| 43519 | ** a new page into the cache to be filled with the data read |
| @@ -44919,11 +44843,31 @@ | |
| 44919 | pagerReportSize(pPager); |
| 44920 | } |
| 44921 | SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){ |
| 44922 | return pPager->pCodec; |
| 44923 | } |
| 44924 | #endif |
| 44925 | |
| 44926 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 44927 | /* |
| 44928 | ** Move the page pPg to location pgno in the file. |
| 44929 | ** |
| @@ -45474,25 +45418,10 @@ | |
| 45474 | assert( pPager->eState==PAGER_READER ); |
| 45475 | return sqlite3WalFramesize(pPager->pWal); |
| 45476 | } |
| 45477 | #endif |
| 45478 | |
| 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 | #endif /* SQLITE_OMIT_DISKIO */ |
| 45495 | |
| 45496 | /************** End of pager.c ***********************************************/ |
| 45497 | /************** Begin file wal.c *********************************************/ |
| 45498 | /* |
| @@ -50759,11 +50688,11 @@ | |
| 50759 | if( rc ) return rc; |
| 50760 | top = get2byteNotZero(&data[hdr+5]); |
| 50761 | }else if( gap+2<=top ){ |
| 50762 | /* Search the freelist looking for a free slot big enough to satisfy |
| 50763 | ** the request. The allocation is made from the first free slot in |
| 50764 | ** the list that is large enough to accomadate it. |
| 50765 | */ |
| 50766 | int pc, addr; |
| 50767 | for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ |
| 50768 | int size; /* Size of the free slot */ |
| 50769 | if( pc>usableSize-4 || pc<addr+4 ){ |
| @@ -52702,11 +52631,11 @@ | |
| 52702 | return rc; |
| 52703 | } |
| 52704 | |
| 52705 | /* |
| 52706 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
| 52707 | ** is commited for an auto-vacuum database. |
| 52708 | ** |
| 52709 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 52710 | ** the database file should be truncated to during the commit process. |
| 52711 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 52712 | ** pages are in use. |
| @@ -58045,16 +57974,10 @@ | |
| 58045 | ************************************************************************* |
| 58046 | ** This file contains the implementation of the sqlite3_backup_XXX() |
| 58047 | ** API functions and the related features. |
| 58048 | */ |
| 58049 | |
| 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 | /* |
| 58057 | ** Structure allocated for each backup operation. |
| 58058 | */ |
| 58059 | struct sqlite3_backup { |
| 58060 | sqlite3* pDestDb; /* Destination database handle */ |
| @@ -61942,11 +61865,11 @@ | |
| 61942 | /* |
| 61943 | ** If the Vdbe passed as the first argument opened a statement-transaction, |
| 61944 | ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or |
| 61945 | ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement |
| 61946 | ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the |
| 61947 | ** statement transaction is commtted. |
| 61948 | ** |
| 61949 | ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. |
| 61950 | ** Otherwise SQLITE_OK. |
| 61951 | */ |
| 61952 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
| @@ -64011,17 +63934,10 @@ | |
| 64011 | int iType = sqlite3_value_type( columnMem(pStmt,i) ); |
| 64012 | columnMallocFailure(pStmt); |
| 64013 | return iType; |
| 64014 | } |
| 64015 | |
| 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 | /* |
| 64024 | ** Convert the N-th element of pStmt->pColName[] into a string using |
| 64025 | ** xFunc() then return that string. If N is out of range, return 0. |
| 64026 | ** |
| 64027 | ** There are up to 5 names for each column. useType determines which |
| @@ -64643,18 +64559,18 @@ | |
| 64643 | pVar = &utf8; |
| 64644 | } |
| 64645 | #endif |
| 64646 | nOut = pVar->n; |
| 64647 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 64648 | if( n>SQLITE_TRACE_SIZE_LIMIT ){ |
| 64649 | nOut = SQLITE_TRACE_SIZE_LIMIT; |
| 64650 | while( nOut<pVar->n && (pVar->z[n]&0xc0)==0x80 ){ n++; } |
| 64651 | } |
| 64652 | #endif |
| 64653 | sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z); |
| 64654 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 64655 | if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-n); |
| 64656 | #endif |
| 64657 | #ifndef SQLITE_OMIT_UTF16 |
| 64658 | if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8); |
| 64659 | #endif |
| 64660 | }else if( pVar->flags & MEM_Zero ){ |
| @@ -64670,11 +64586,11 @@ | |
| 64670 | for(i=0; i<nOut; i++){ |
| 64671 | sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff); |
| 64672 | } |
| 64673 | sqlite3StrAccumAppend(&out, "'", 1); |
| 64674 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 64675 | if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-n); |
| 64676 | #endif |
| 64677 | } |
| 64678 | } |
| 64679 | } |
| 64680 | return sqlite3StrAccumFinish(&out); |
| @@ -68269,12 +68185,12 @@ | |
| 68269 | ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is |
| 68270 | ** obtained on the database file when a write-transaction is started. No |
| 68271 | ** other process can start another write transaction while this transaction is |
| 68272 | ** underway. Starting a write transaction also creates a rollback journal. A |
| 68273 | ** 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. |
| 68276 | ** |
| 68277 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 68278 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 68279 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 68280 | ** More specifically, a statement transaction is opened iff the database |
| @@ -72224,11 +72140,11 @@ | |
| 72224 | ** |
| 72225 | ** For the purposes of this comparison, EOF is considered greater than any |
| 72226 | ** other key value. If the keys are equal (only possible with two EOF |
| 72227 | ** values), it doesn't matter which index is stored. |
| 72228 | ** |
| 72229 | ** The (N/4) elements of aTree[] that preceed the final (N/2) described |
| 72230 | ** above contains the index of the smallest of each block of 4 iterators. |
| 72231 | ** And so on. So that aTree[1] contains the index of the iterator that |
| 72232 | ** currently points to the smallest key value. aTree[0] is unused. |
| 72233 | ** |
| 72234 | ** Example: |
| @@ -73499,16 +73415,10 @@ | |
| 73499 | ** a power-of-two allocation. This mimimizes wasted space in power-of-two |
| 73500 | ** memory allocators. |
| 73501 | */ |
| 73502 | #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*))) |
| 73503 | |
| 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 | /* |
| 73511 | ** The rollback journal is composed of a linked list of these structures. |
| 73512 | */ |
| 73513 | struct FileChunk { |
| 73514 | FileChunk *pNext; /* Next chunk in the journal */ |
| @@ -75045,12 +74955,12 @@ | |
| 75045 | ** |
| 75046 | ** Minor point: If this is the case, then the expression will be |
| 75047 | ** re-evaluated for each reference to it. |
| 75048 | */ |
| 75049 | sNC.pEList = p->pEList; |
| 75050 | if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; |
| 75051 | sNC.ncFlags |= NC_AsMaybe; |
| 75052 | if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort; |
| 75053 | sNC.ncFlags &= ~NC_AsMaybe; |
| 75054 | |
| 75055 | /* The ORDER BY and GROUP BY clauses may not refer to terms in |
| 75056 | ** outer queries |
| @@ -76817,19 +76727,19 @@ | |
| 76817 | |
| 76818 | if( eType==0 ){ |
| 76819 | /* Could not found an existing table or index to use as the RHS b-tree. |
| 76820 | ** We will have to generate an ephemeral table to do the job. |
| 76821 | */ |
| 76822 | double savedNQueryLoop = pParse->nQueryLoop; |
| 76823 | int rMayHaveNull = 0; |
| 76824 | eType = IN_INDEX_EPH; |
| 76825 | if( prNotFound ){ |
| 76826 | *prNotFound = rMayHaveNull = ++pParse->nMem; |
| 76827 | sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound); |
| 76828 | }else{ |
| 76829 | testcase( pParse->nQueryLoop>(double)1 ); |
| 76830 | pParse->nQueryLoop = (double)1; |
| 76831 | if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){ |
| 76832 | eType = IN_INDEX_ROWID; |
| 76833 | } |
| 76834 | } |
| 76835 | sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); |
| @@ -76867,11 +76777,11 @@ | |
| 76867 | ** the register given by rMayHaveNull to NULL. Calling routines will take |
| 76868 | ** care of changing this register value to non-NULL if the RHS is NULL-free. |
| 76869 | ** |
| 76870 | ** If rMayHaveNull is zero, that means that the subquery is being used |
| 76871 | ** for membership testing only. There is no need to initialize any |
| 76872 | ** registers to indicate the presense or absence of NULLs on the RHS. |
| 76873 | ** |
| 76874 | ** For a SELECT or EXISTS operator, return the register that holds the |
| 76875 | ** result. For IN operators or if an error occurs, the return value is 0. |
| 76876 | */ |
| 76877 | #ifndef SQLITE_OMIT_SUBQUERY |
| @@ -80267,11 +80177,11 @@ | |
| 80267 | ** |
| 80268 | ** Additional tables might be added in future releases of SQLite. |
| 80269 | ** The sqlite_stat2 table is not created or used unless the SQLite version |
| 80270 | ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled |
| 80271 | ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. |
| 80272 | ** The sqlite_stat2 table is superceded by sqlite_stat3, which is only |
| 80273 | ** created and used by SQLite versions 3.7.9 and later and with |
| 80274 | ** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3 |
| 80275 | ** is a superset of sqlite_stat2. |
| 80276 | ** |
| 80277 | ** Format of sqlite_stat1: |
| @@ -83455,10 +83365,11 @@ | |
| 83455 | zColl = sqlite3NameFromToken(db, pToken); |
| 83456 | if( !zColl ) return; |
| 83457 | |
| 83458 | if( sqlite3LocateCollSeq(pParse, zColl) ){ |
| 83459 | Index *pIdx; |
| 83460 | p->aCol[i].zColl = zColl; |
| 83461 | |
| 83462 | /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
| 83463 | ** then an index may have been created on this column before the |
| 83464 | ** collation type was added. Correct this if it is the case. |
| @@ -84874,10 +84785,11 @@ | |
| 84874 | zExtra = (char *)(&pIndex->zName[nName+1]); |
| 84875 | memcpy(pIndex->zName, zName, nName+1); |
| 84876 | pIndex->pTable = pTab; |
| 84877 | pIndex->nColumn = pList->nExpr; |
| 84878 | pIndex->onError = (u8)onError; |
| 84879 | pIndex->autoIndex = (u8)(pName==0); |
| 84880 | pIndex->pSchema = db->aDb[iDb].pSchema; |
| 84881 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 84882 | |
| 84883 | /* Check to see if we should honor DESC requests on index columns |
| @@ -84932,10 +84844,11 @@ | |
| 84932 | goto exit_create_index; |
| 84933 | } |
| 84934 | pIndex->azColl[i] = zColl; |
| 84935 | requestedSortOrder = pListItem->sortOrder & sortOrderMask; |
| 84936 | pIndex->aSortOrder[i] = (u8)requestedSortOrder; |
| 84937 | } |
| 84938 | sqlite3DefaultRowEst(pIndex); |
| 84939 | |
| 84940 | if( pTab==pParse->pNewTable ){ |
| 84941 | /* This routine has been called to create an automatic index as a |
| @@ -87378,11 +87291,11 @@ | |
| 87378 | ** of x. If x is text, then we actually count UTF-8 characters. |
| 87379 | ** If x is a blob, then we count bytes. |
| 87380 | ** |
| 87381 | ** If p1 is negative, then we begin abs(p1) from the end of x[]. |
| 87382 | ** |
| 87383 | ** If p2 is negative, return the p2 characters preceeding p1. |
| 87384 | */ |
| 87385 | static void substrFunc( |
| 87386 | sqlite3_context *context, |
| 87387 | int argc, |
| 87388 | sqlite3_value **argv |
| @@ -88037,14 +87950,10 @@ | |
| 88037 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 88038 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 88039 | }; |
| 88040 | |
| 88041 | /* |
| 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 | ** Implementation of the QUOTE() function. This function takes a single |
| 88047 | ** argument. If the argument is numeric, the return value is the same as |
| 88048 | ** the argument. If the argument is NULL, the return value is the string |
| 88049 | ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 88050 | ** single-quote escapes. |
| @@ -88229,11 +88138,11 @@ | |
| 88229 | } |
| 88230 | |
| 88231 | /* |
| 88232 | ** The replace() function. Three arguments are all strings: call |
| 88233 | ** 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 |
| 88235 | ** must be exact. Collating sequences are not used. |
| 88236 | */ |
| 88237 | static void replaceFunc( |
| 88238 | sqlite3_context *context, |
| 88239 | int argc, |
| @@ -94147,15 +94056,19 @@ | |
| 94147 | sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz); |
| 94148 | } |
| 94149 | } |
| 94150 | } |
| 94151 | sz = -1; |
| 94152 | if( sqlite3_file_control(db,zDb,SQLITE_FCNTL_MMAP_SIZE,&sz)==SQLITE_OK ){ |
| 94153 | #if SQLITE_MAX_MMAP_SIZE==0 |
| 94154 | sz = 0; |
| 94155 | #endif |
| 94156 | returnSingleInt(pParse, "mmap_size", sz); |
| 94157 | } |
| 94158 | }else |
| 94159 | |
| 94160 | /* |
| 94161 | ** PRAGMA temp_store |
| @@ -94682,11 +94595,11 @@ | |
| 94682 | #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX |
| 94683 | # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 |
| 94684 | #endif |
| 94685 | |
| 94686 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
| 94687 | /* Pragma "quick_check" is an experimental reduced version of |
| 94688 | ** integrity_check designed to detect most database corruption |
| 94689 | ** without most of the overhead of a full integrity-check. |
| 94690 | */ |
| 94691 | if( sqlite3StrICmp(zLeft, "integrity_check")==0 |
| 94692 | || sqlite3StrICmp(zLeft, "quick_check")==0 |
| @@ -95140,14 +95053,14 @@ | |
| 95140 | }else |
| 95141 | #endif |
| 95142 | |
| 95143 | #ifdef SQLITE_HAS_CODEC |
| 95144 | if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){ |
| 95145 | sqlite3_key(db, zRight, sqlite3Strlen30(zRight)); |
| 95146 | }else |
| 95147 | if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){ |
| 95148 | sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight)); |
| 95149 | }else |
| 95150 | if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 || |
| 95151 | sqlite3StrICmp(zLeft, "hexrekey")==0) ){ |
| 95152 | int i, h1, h2; |
| 95153 | char zKey[40]; |
| @@ -95155,13 +95068,13 @@ | |
| 95155 | h1 += 9*(1&(h1>>6)); |
| 95156 | h2 += 9*(1&(h2>>6)); |
| 95157 | zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4); |
| 95158 | } |
| 95159 | if( (zLeft[3] & 0xf)==0xb ){ |
| 95160 | sqlite3_key(db, zKey, i/2); |
| 95161 | }else{ |
| 95162 | sqlite3_rekey(db, zKey, i/2); |
| 95163 | } |
| 95164 | }else |
| 95165 | #endif |
| 95166 | #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) |
| 95167 | if( sqlite3StrICmp(zLeft, "activate_extensions")==0 && zRight ){ |
| @@ -95792,11 +95705,11 @@ | |
| 95792 | } |
| 95793 | |
| 95794 | sqlite3VtabUnlockList(db); |
| 95795 | |
| 95796 | pParse->db = db; |
| 95797 | pParse->nQueryLoop = (double)1; |
| 95798 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
| 95799 | char *zSqlCopy; |
| 95800 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 95801 | testcase( nBytes==mxLen ); |
| 95802 | testcase( nBytes==mxLen+1 ); |
| @@ -95814,11 +95727,11 @@ | |
| 95814 | pParse->zTail = &zSql[nBytes]; |
| 95815 | } |
| 95816 | }else{ |
| 95817 | sqlite3RunParser(pParse, zSql, &zErrMsg); |
| 95818 | } |
| 95819 | assert( 1==(int)pParse->nQueryLoop ); |
| 95820 | |
| 95821 | if( db->mallocFailed ){ |
| 95822 | pParse->rc = SQLITE_NOMEM; |
| 95823 | } |
| 95824 | if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; |
| @@ -96178,11 +96091,11 @@ | |
| 96178 | sqlite3DbFree(db, p); |
| 96179 | } |
| 96180 | } |
| 96181 | |
| 96182 | /* |
| 96183 | ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the |
| 96184 | ** type of join. Return an integer constant that expresses that type |
| 96185 | ** in terms of the following bit values: |
| 96186 | ** |
| 96187 | ** JT_INNER |
| 96188 | ** JT_CROSS |
| @@ -97592,11 +97505,11 @@ | |
| 97592 | int addr1, n; |
| 97593 | if( p->iLimit ) return; |
| 97594 | |
| 97595 | /* |
| 97596 | ** "LIMIT -1" always shows all rows. There is some |
| 97597 | ** contraversy about what the correct behavior should be. |
| 97598 | ** The current implementation interprets "LIMIT 0" to mean |
| 97599 | ** no rows. |
| 97600 | */ |
| 97601 | sqlite3ExprCacheClear(pParse); |
| 97602 | assert( p->pOffset==0 || p->pLimit!=0 ); |
| @@ -97608,11 +97521,11 @@ | |
| 97608 | sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); |
| 97609 | VdbeComment((v, "LIMIT counter")); |
| 97610 | if( n==0 ){ |
| 97611 | sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak); |
| 97612 | }else{ |
| 97613 | if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n; |
| 97614 | } |
| 97615 | }else{ |
| 97616 | sqlite3ExprCode(pParse, p->pLimit, iLimit); |
| 97617 | sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); |
| 97618 | VdbeComment((v, "LIMIT counter")); |
| @@ -97802,13 +97715,13 @@ | |
| 97802 | pDelete = p->pPrior; |
| 97803 | p->pPrior = pPrior; |
| 97804 | p->nSelectRow += pPrior->nSelectRow; |
| 97805 | if( pPrior->pLimit |
| 97806 | && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit) |
| 97807 | && p->nSelectRow > (double)nLimit |
| 97808 | ){ |
| 97809 | p->nSelectRow = (double)nLimit; |
| 97810 | } |
| 97811 | if( addr ){ |
| 97812 | sqlite3VdbeJumpHere(v, addr); |
| 97813 | } |
| 97814 | break; |
| @@ -99953,15 +99866,14 @@ | |
| 99953 | Parse *pParse, /* Parse context */ |
| 99954 | Table *pTab, /* Table being queried */ |
| 99955 | Index *pIdx /* Index used to optimize scan, or NULL */ |
| 99956 | ){ |
| 99957 | if( pParse->explain==2 ){ |
| 99958 | char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)", |
| 99959 | pTab->zName, |
| 99960 | pIdx ? "USING COVERING INDEX " : "", |
| 99961 | pIdx ? pIdx->zName : "", |
| 99962 | pTab->nRowEst |
| 99963 | ); |
| 99964 | sqlite3VdbeAddOp4( |
| 99965 | pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC |
| 99966 | ); |
| 99967 | } |
| @@ -100115,11 +100027,11 @@ | |
| 100115 | } |
| 100116 | continue; |
| 100117 | } |
| 100118 | |
| 100119 | /* Increment Parse.nHeight by the height of the largest expression |
| 100120 | ** tree refered to by this, the parent select. The child select |
| 100121 | ** may contain expression trees of at most |
| 100122 | ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit |
| 100123 | ** more conservative than necessary, but much easier than enforcing |
| 100124 | ** an exact limit. |
| 100125 | */ |
| @@ -100308,11 +100220,11 @@ | |
| 100308 | } |
| 100309 | |
| 100310 | /* Set the limiter. |
| 100311 | */ |
| 100312 | iEnd = sqlite3VdbeMakeLabel(v); |
| 100313 | p->nSelectRow = (double)LARGEST_INT64; |
| 100314 | computeLimitRegisters(pParse, p, iEnd); |
| 100315 | if( p->iLimit==0 && addrSortIndex>=0 ){ |
| 100316 | sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen; |
| 100317 | p->selFlags |= SF_UseSorter; |
| 100318 | } |
| @@ -100336,13 +100248,17 @@ | |
| 100336 | ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0); |
| 100337 | |
| 100338 | /* Begin the database scan. */ |
| 100339 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0); |
| 100340 | 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; |
| 100344 | |
| 100345 | /* If sorting index that was created by a prior OP_OpenEphemeral |
| 100346 | ** instruction ended up not being needed, then change the OP_OpenEphemeral |
| 100347 | ** into an OP_Noop. |
| 100348 | */ |
| @@ -100351,11 +100267,12 @@ | |
| 100351 | p->addrOpenEphm[2] = -1; |
| 100352 | } |
| 100353 | |
| 100354 | /* Use the standard inner loop. */ |
| 100355 | selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest, |
| 100356 | pWInfo->iContinue, pWInfo->iBreak); |
| 100357 | |
| 100358 | /* End the database scan loop. |
| 100359 | */ |
| 100360 | sqlite3WhereEnd(pWInfo); |
| 100361 | }else{ |
| @@ -100384,13 +100301,13 @@ | |
| 100384 | pItem->iAlias = 0; |
| 100385 | } |
| 100386 | for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ |
| 100387 | pItem->iAlias = 0; |
| 100388 | } |
| 100389 | if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100; |
| 100390 | }else{ |
| 100391 | p->nSelectRow = (double)1; |
| 100392 | } |
| 100393 | |
| 100394 | |
| 100395 | /* Create a label to jump to when we want to abort the query */ |
| 100396 | addrEnd = sqlite3VdbeMakeLabel(v); |
| @@ -100468,11 +100385,11 @@ | |
| 100468 | ** in the right order to begin with. |
| 100469 | */ |
| 100470 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
| 100471 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0); |
| 100472 | if( pWInfo==0 ) goto select_end; |
| 100473 | if( pWInfo->nOBSat==pGroupBy->nExpr ){ |
| 100474 | /* The optimizer is able to deliver rows in group by order so |
| 100475 | ** we do not have to sort. The OP_OpenEphemeral table will be |
| 100476 | ** cancelled later because we still need to use the pKeyInfo |
| 100477 | */ |
| 100478 | groupBySort = 0; |
| @@ -100749,12 +100666,12 @@ | |
| 100749 | sqlite3ExprListDelete(db, pDel); |
| 100750 | goto select_end; |
| 100751 | } |
| 100752 | updateAccumulator(pParse, &sAggInfo); |
| 100753 | assert( pMinMax==0 || pMinMax->nExpr==1 ); |
| 100754 | if( pWInfo->nOBSat>0 ){ |
| 100755 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak); |
| 100756 | VdbeComment((v, "%s() by index", |
| 100757 | (flag==WHERE_ORDERBY_MIN?"min":"max"))); |
| 100758 | } |
| 100759 | sqlite3WhereEnd(pWInfo); |
| 100760 | finalizeAggFunctions(pParse, &sAggInfo); |
| @@ -102109,11 +102026,11 @@ | |
| 102109 | } |
| 102110 | |
| 102111 | /* |
| 102112 | ** This is called to code the required FOR EACH ROW triggers for an operation |
| 102113 | ** 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 |
| 102115 | ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then |
| 102116 | ** parameter pChanges is passed the list of columns being modified. |
| 102117 | ** |
| 102118 | ** If there are no triggers that fire at the specified time for the specified |
| 102119 | ** operation on pTab, this function is a no-op. |
| @@ -102560,11 +102477,11 @@ | |
| 102560 | sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); |
| 102561 | pWInfo = sqlite3WhereBegin( |
| 102562 | pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0 |
| 102563 | ); |
| 102564 | if( pWInfo==0 ) goto update_cleanup; |
| 102565 | okOnePass = pWInfo->okOnePass; |
| 102566 | |
| 102567 | /* Remember the rowid of every item to be updated. |
| 102568 | */ |
| 102569 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid); |
| 102570 | if( !okOnePass ){ |
| @@ -104397,22 +104314,165 @@ | |
| 104397 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 104398 | /***/ int sqlite3WhereTrace = 0; |
| 104399 | #endif |
| 104400 | #if defined(SQLITE_DEBUG) \ |
| 104401 | && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
| 104402 | # define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X |
| 104403 | #else |
| 104404 | # define WHERETRACE(X) |
| 104405 | #endif |
| 104406 | |
| 104407 | /* Forward reference |
| 104408 | */ |
| 104409 | typedef struct WhereClause WhereClause; |
| 104410 | typedef struct WhereMaskSet WhereMaskSet; |
| 104411 | typedef struct WhereOrInfo WhereOrInfo; |
| 104412 | typedef struct WhereAndInfo WhereAndInfo; |
| 104413 | typedef struct WhereCost WhereCost; |
| 104414 | |
| 104415 | /* |
| 104416 | ** The query generator uses an array of instances of this structure to |
| 104417 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 104418 | ** clause subexpression is separated from the others by AND operators, |
| @@ -104461,11 +104521,10 @@ | |
| 104461 | ** |
| 104462 | ** The number of terms in a join is limited by the number of bits |
| 104463 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 104464 | ** is only able to process joins with 64 or fewer tables. |
| 104465 | */ |
| 104466 | typedef struct WhereTerm WhereTerm; |
| 104467 | struct WhereTerm { |
| 104468 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
| 104469 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 104470 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
| 104471 | union { |
| @@ -104495,10 +104554,26 @@ | |
| 104495 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 104496 | #else |
| 104497 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 104498 | #endif |
| 104499 | |
| 104500 | /* |
| 104501 | ** An instance of the following structure holds all information about a |
| 104502 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
| 104503 | ** |
| 104504 | ** Explanation of pOuter: For a WHERE clause of the form |
| @@ -104508,15 +104583,13 @@ | |
| 104508 | ** There are separate WhereClause objects for the whole clause and for |
| 104509 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 104510 | ** subclauses points to the WhereClause object for the whole clause. |
| 104511 | */ |
| 104512 | struct WhereClause { |
| 104513 | Parse *pParse; /* The parser context */ |
| 104514 | WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */ |
| 104515 | WhereClause *pOuter; /* Outer conjunction */ |
| 104516 | u8 op; /* Split operator. TK_AND or TK_OR */ |
| 104517 | u16 wctrlFlags; /* Might include WHERE_AND_ONLY */ |
| 104518 | int nTerm; /* Number of terms */ |
| 104519 | int nSlot; /* Number of entries in a[] */ |
| 104520 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
| 104521 | #if defined(SQLITE_SMALL_STACK) |
| 104522 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| @@ -104572,23 +104645,59 @@ | |
| 104572 | int n; /* Number of assigned cursor values */ |
| 104573 | int ix[BMS]; /* Cursor assigned to each bit */ |
| 104574 | }; |
| 104575 | |
| 104576 | /* |
| 104577 | ** A WhereCost object records a lookup strategy and the estimated |
| 104578 | ** cost of pursuing that strategy. |
| 104579 | */ |
| 104580 | struct WhereCost { |
| 104581 | WherePlan plan; /* The lookup strategy */ |
| 104582 | double rCost; /* Overall cost of pursuing this search strategy */ |
| 104583 | Bitmask used; /* Bitmask of cursors used by this plan */ |
| 104584 | }; |
| 104585 | |
| 104586 | /* |
| 104587 | ** Bitmasks for the operators that indices are able to exploit. An |
| 104588 | ** OR-ed combination of these values can be used when searching for |
| 104589 | ** terms in the where clause. |
| 104590 | */ |
| 104591 | #define WO_IN 0x001 |
| 104592 | #define WO_EQ 0x002 |
| 104593 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 104594 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| @@ -104603,96 +104712,106 @@ | |
| 104603 | |
| 104604 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
| 104605 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
| 104606 | |
| 104607 | /* |
| 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; |
| 104676 | } |
| 104677 | |
| 104678 | /* |
| 104679 | ** Initialize a preallocated WhereClause structure. |
| 104680 | */ |
| 104681 | static void whereClauseInit( |
| 104682 | 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 */ |
| 104686 | ){ |
| 104687 | pWC->pParse = pParse; |
| 104688 | pWC->pMaskSet = pMaskSet; |
| 104689 | pWC->pOuter = 0; |
| 104690 | pWC->nTerm = 0; |
| 104691 | pWC->nSlot = ArraySize(pWC->aStatic); |
| 104692 | pWC->a = pWC->aStatic; |
| 104693 | pWC->wctrlFlags = wctrlFlags; |
| 104694 | } |
| 104695 | |
| 104696 | /* Forward reference */ |
| 104697 | static void whereClauseClear(WhereClause*); |
| 104698 | |
| @@ -104717,11 +104836,11 @@ | |
| 104717 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 104718 | */ |
| 104719 | static void whereClauseClear(WhereClause *pWC){ |
| 104720 | int i; |
| 104721 | WhereTerm *a; |
| 104722 | sqlite3 *db = pWC->pParse->db; |
| 104723 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
| 104724 | if( a->wtFlags & TERM_DYNAMIC ){ |
| 104725 | sqlite3ExprDelete(db, a->pExpr); |
| 104726 | } |
| 104727 | if( a->wtFlags & TERM_ORINFO ){ |
| @@ -104758,11 +104877,11 @@ | |
| 104758 | WhereTerm *pTerm; |
| 104759 | int idx; |
| 104760 | testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ |
| 104761 | if( pWC->nTerm>=pWC->nSlot ){ |
| 104762 | WhereTerm *pOld = pWC->a; |
| 104763 | sqlite3 *db = pWC->pParse->db; |
| 104764 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
| 104765 | if( pWC->a==0 ){ |
| 104766 | if( wtFlags & TERM_DYNAMIC ){ |
| 104767 | sqlite3ExprDelete(db, p); |
| 104768 | } |
| @@ -104810,13 +104929,13 @@ | |
| 104810 | whereSplit(pWC, pExpr->pRight, op); |
| 104811 | } |
| 104812 | } |
| 104813 | |
| 104814 | /* |
| 104815 | ** Initialize an expression mask set (a WhereMaskSet object) |
| 104816 | */ |
| 104817 | #define initMaskSet(P) memset(P, 0, sizeof(*P)) |
| 104818 | |
| 104819 | /* |
| 104820 | ** Return the bitmask for the given cursor number. Return 0 if |
| 104821 | ** iCursor is not in the set. |
| 104822 | */ |
| @@ -104823,11 +104942,11 @@ | |
| 104823 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 104824 | int i; |
| 104825 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
| 104826 | for(i=0; i<pMaskSet->n; i++){ |
| 104827 | if( pMaskSet->ix[i]==iCursor ){ |
| 104828 | return ((Bitmask)1)<<i; |
| 104829 | } |
| 104830 | } |
| 104831 | return 0; |
| 104832 | } |
| 104833 | |
| @@ -104843,22 +104962,13 @@ | |
| 104843 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
| 104844 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
| 104845 | } |
| 104846 | |
| 104847 | /* |
| 104848 | ** This routine walks (recursively) an expression tree and generates |
| 104849 | ** a bitmask indicating which tables are used in that expression |
| 104850 | ** 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 | */ |
| 104861 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 104862 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 104863 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
| 104864 | Bitmask mask = 0; |
| @@ -104908,11 +105018,11 @@ | |
| 104908 | } |
| 104909 | |
| 104910 | /* |
| 104911 | ** Return TRUE if the given operator is one of the operators that is |
| 104912 | ** allowed for an indexable WHERE clause term. The allowed operators are |
| 104913 | ** "=", "<", ">", "<=", ">=", and "IN". |
| 104914 | ** |
| 104915 | ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be |
| 104916 | ** of one of the following forms: column = expression column > expression |
| 104917 | ** column >= expression column < expression column <= expression |
| 104918 | ** expression = column expression > column expression >= column |
| @@ -104935,14 +105045,13 @@ | |
| 104935 | /* |
| 104936 | ** Commute a comparison operator. Expressions of the form "X op Y" |
| 104937 | ** are converted into "Y op X". |
| 104938 | ** |
| 104939 | ** 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 |
| 104944 | ** the left hand side of a comparison overrides any collation sequence |
| 104945 | ** attached to the right. For the same reason the EP_Collate flag |
| 104946 | ** is not commuted. |
| 104947 | */ |
| 104948 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
| @@ -104994,10 +105103,134 @@ | |
| 104994 | assert( op!=TK_LE || c==WO_LE ); |
| 104995 | assert( op!=TK_GT || c==WO_GT ); |
| 104996 | assert( op!=TK_GE || c==WO_GE ); |
| 104997 | return c; |
| 104998 | } |
| 104999 | |
| 105000 | /* |
| 105001 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 105002 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 105003 | ** the WO_xx operator codes specified by the op parameter. |
| @@ -105026,98 +105259,32 @@ | |
| 105026 | int iColumn, /* Column number of LHS */ |
| 105027 | Bitmask notReady, /* RHS must not overlap with this mask */ |
| 105028 | u32 op, /* Mask of WO_xx values describing operator */ |
| 105029 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 105030 | ){ |
| 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: |
| 105109 | return pResult; |
| 105110 | } |
| 105111 | |
| 105112 | /* Forward reference */ |
| 105113 | static void exprAnalyze(SrcList*, WhereClause*, int); |
| 105114 | |
| 105115 | /* |
| 105116 | ** Call exprAnalyze on all terms in a WHERE clause. |
| 105117 | ** |
| 105118 | ** |
| 105119 | */ |
| 105120 | static void exprAnalyzeAll( |
| 105121 | SrcList *pTabList, /* the FROM clause */ |
| 105122 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 105123 | ){ |
| @@ -105345,15 +105512,15 @@ | |
| 105345 | static void exprAnalyzeOrTerm( |
| 105346 | SrcList *pSrc, /* the FROM clause */ |
| 105347 | WhereClause *pWC, /* the complete WHERE clause */ |
| 105348 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 105349 | ){ |
| 105350 | Parse *pParse = pWC->pParse; /* Parser context */ |
| 105351 | sqlite3 *db = pParse->db; /* Database connection */ |
| 105352 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 105353 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
| 105354 | WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */ |
| 105355 | int i; /* Loop counters */ |
| 105356 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 105357 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 105358 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 105359 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| @@ -105368,11 +105535,11 @@ | |
| 105368 | assert( pExpr->op==TK_OR ); |
| 105369 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
| 105370 | if( pOrInfo==0 ) return; |
| 105371 | pTerm->wtFlags |= TERM_ORINFO; |
| 105372 | pOrWc = &pOrInfo->wc; |
| 105373 | whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
| 105374 | whereSplit(pOrWc, pExpr, TK_OR); |
| 105375 | exprAnalyzeAll(pSrc, pOrWc); |
| 105376 | if( db->mallocFailed ) return; |
| 105377 | assert( pOrWc->nTerm>=2 ); |
| 105378 | |
| @@ -105394,20 +105561,20 @@ | |
| 105394 | Bitmask b = 0; |
| 105395 | pOrTerm->u.pAndInfo = pAndInfo; |
| 105396 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 105397 | pOrTerm->eOperator = WO_AND; |
| 105398 | pAndWC = &pAndInfo->wc; |
| 105399 | whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
| 105400 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 105401 | exprAnalyzeAll(pSrc, pAndWC); |
| 105402 | pAndWC->pOuter = pWC; |
| 105403 | testcase( db->mallocFailed ); |
| 105404 | if( !db->mallocFailed ){ |
| 105405 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 105406 | assert( pAndTerm->pExpr ); |
| 105407 | if( allowedOp(pAndTerm->pExpr->op) ){ |
| 105408 | b |= getMask(pMaskSet, pAndTerm->leftCursor); |
| 105409 | } |
| 105410 | } |
| 105411 | } |
| 105412 | indexable &= b; |
| 105413 | } |
| @@ -105414,14 +105581,14 @@ | |
| 105414 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 105415 | /* Skip this term for now. We revisit it when we process the |
| 105416 | ** corresponding TERM_VIRTUAL term */ |
| 105417 | }else{ |
| 105418 | Bitmask b; |
| 105419 | b = getMask(pMaskSet, pOrTerm->leftCursor); |
| 105420 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 105421 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
| 105422 | b |= getMask(pMaskSet, pOther->leftCursor); |
| 105423 | } |
| 105424 | indexable &= b; |
| 105425 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
| 105426 | chngToIN = 0; |
| 105427 | }else{ |
| @@ -105479,11 +105646,11 @@ | |
| 105479 | /* This is the 2-bit case and we are on the second iteration and |
| 105480 | ** current term is from the first iteration. So skip this term. */ |
| 105481 | assert( j==1 ); |
| 105482 | continue; |
| 105483 | } |
| 105484 | if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){ |
| 105485 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 105486 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 105487 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 105488 | ** and use its inversion. */ |
| 105489 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| @@ -105498,11 +105665,11 @@ | |
| 105498 | if( i<0 ){ |
| 105499 | /* No candidate table+column was found. This can only occur |
| 105500 | ** on the second iteration */ |
| 105501 | assert( j==1 ); |
| 105502 | assert( IsPowerOfTwo(chngToIN) ); |
| 105503 | assert( chngToIN==getMask(pMaskSet, iCursor) ); |
| 105504 | break; |
| 105505 | } |
| 105506 | testcase( j==1 ); |
| 105507 | |
| 105508 | /* We have found a candidate table and column. Check to see if that |
| @@ -105547,11 +105714,11 @@ | |
| 105547 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
| 105548 | assert( pOrTerm->eOperator & WO_EQ ); |
| 105549 | assert( pOrTerm->leftCursor==iCursor ); |
| 105550 | assert( pOrTerm->u.leftColumn==iColumn ); |
| 105551 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
| 105552 | pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup); |
| 105553 | pLeft = pOrTerm->pExpr->pLeft; |
| 105554 | } |
| 105555 | assert( pLeft!=0 ); |
| 105556 | pDup = sqlite3ExprDup(db, pLeft, 0); |
| 105557 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
| @@ -105596,10 +105763,11 @@ | |
| 105596 | static void exprAnalyze( |
| 105597 | SrcList *pSrc, /* the FROM clause */ |
| 105598 | WhereClause *pWC, /* the WHERE clause */ |
| 105599 | int idxTerm /* Index of the term to be analyzed */ |
| 105600 | ){ |
| 105601 | WhereTerm *pTerm; /* The term to be analyzed */ |
| 105602 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
| 105603 | Expr *pExpr; /* The expression to be analyzed */ |
| 105604 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 105605 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
| @@ -105606,18 +105774,18 @@ | |
| 105606 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
| 105607 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 105608 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 105609 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
| 105610 | int op; /* Top-level operator. pExpr->op */ |
| 105611 | Parse *pParse = pWC->pParse; /* Parsing context */ |
| 105612 | sqlite3 *db = pParse->db; /* Database connection */ |
| 105613 | |
| 105614 | if( db->mallocFailed ){ |
| 105615 | return; |
| 105616 | } |
| 105617 | pTerm = &pWC->a[idxTerm]; |
| 105618 | pMaskSet = pWC->pMaskSet; |
| 105619 | pExpr = pTerm->pExpr; |
| 105620 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
| 105621 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
| 105622 | op = pExpr->op; |
| 105623 | if( op==TK_IN ){ |
| @@ -105891,15 +106059,12 @@ | |
| 105891 | */ |
| 105892 | pTerm->prereqRight |= extraRight; |
| 105893 | } |
| 105894 | |
| 105895 | /* |
| 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. |
| 105901 | ** |
| 105902 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 105903 | ** no expression is found, -1 is returned. |
| 105904 | */ |
| 105905 | static int findIndexCol( |
| @@ -105925,82 +106090,23 @@ | |
| 105925 | } |
| 105926 | } |
| 105927 | |
| 105928 | return -1; |
| 105929 | } |
| 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 | |
| 105991 | /* |
| 105992 | ** 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. |
| 105996 | */ |
| 105997 | static int isDistinctRedundant( |
| 105998 | Parse *pParse, |
| 105999 | SrcList *pTabList, |
| 106000 | WhereClause *pWC, |
| 106001 | ExprList *pDistinct |
| 106002 | ){ |
| 106003 | Table *pTab; |
| 106004 | Index *pIdx; |
| 106005 | int i; |
| 106006 | int iBase; |
| @@ -106051,35 +106157,90 @@ | |
| 106051 | } |
| 106052 | } |
| 106053 | |
| 106054 | return 0; |
| 106055 | } |
| 106056 | |
| 106057 | /* |
| 106058 | ** Prepare a crude estimate of the logarithm of the input value. |
| 106059 | ** The results need not be exact. This is only used for estimating |
| 106060 | ** the total cost of performing operations with O(logN) or O(NlogN) |
| 106061 | ** complexity. Because N is just a guess, it is no great tragedy if |
| 106062 | ** logN is a little off. |
| 106063 | */ |
| 106064 | static double estLog(double N){ |
| 106065 | double logN = 1; |
| 106066 | double x = 10; |
| 106067 | while( N>x ){ |
| 106068 | logN += 1; |
| 106069 | x *= 10; |
| 106070 | } |
| 106071 | return logN; |
| 106072 | } |
| 106073 | |
| 106074 | /* |
| 106075 | ** Two routines for printing the content of an sqlite3_index_info |
| 106076 | ** structure. Used for testing and debugging only. If neither |
| 106077 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 106078 | ** are no-ops. |
| 106079 | */ |
| 106080 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG) |
| 106081 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 106082 | int i; |
| 106083 | if( !sqlite3WhereTrace ) return; |
| 106084 | for(i=0; i<p->nConstraint; i++){ |
| 106085 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| @@ -106113,111 +106274,10 @@ | |
| 106113 | #else |
| 106114 | #define TRACE_IDX_INPUTS(A) |
| 106115 | #define TRACE_IDX_OUTPUTS(A) |
| 106116 | #endif |
| 106117 | |
| 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 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 106220 | /* |
| 106221 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 106222 | ** could be used with an index to access pSrc, assuming an appropriate |
| 106223 | ** index existed. |
| @@ -106229,92 +106289,17 @@ | |
| 106229 | ){ |
| 106230 | char aff; |
| 106231 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
| 106232 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
| 106233 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
| 106234 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 106235 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 106236 | return 1; |
| 106237 | } |
| 106238 | #endif |
| 106239 | |
| 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 | |
| 106317 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 106318 | /* |
| 106319 | ** Generate code to construct the Index object for an automatic index |
| 106320 | ** and to set up the WhereLevel object pLevel so that the code generator |
| @@ -106340,12 +106325,14 @@ | |
| 106340 | int regRecord; /* Register holding an index record */ |
| 106341 | int n; /* Column counter */ |
| 106342 | int i; /* Loop counter */ |
| 106343 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
| 106344 | CollSeq *pColl; /* Collating sequence to on a column */ |
| 106345 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 106346 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 106347 | |
| 106348 | /* Generate code to skip over the creation and initialization of the |
| 106349 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 106350 | v = pParse->pVdbe; |
| 106351 | assert( v!=0 ); |
| @@ -106354,54 +106341,58 @@ | |
| 106354 | /* Count the number of columns that will be added to the index |
| 106355 | ** and used to match WHERE clause constraints */ |
| 106356 | nColumn = 0; |
| 106357 | pTable = pSrc->pTab; |
| 106358 | pWCEnd = &pWC->a[pWC->nTerm]; |
| 106359 | idxCols = 0; |
| 106360 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 106361 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 106362 | int iCol = pTerm->u.leftColumn; |
| 106363 | Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol; |
| 106364 | testcase( iCol==BMS ); |
| 106365 | testcase( iCol==BMS-1 ); |
| 106366 | if( (idxCols & cMask)==0 ){ |
| 106367 | nColumn++; |
| 106368 | idxCols |= cMask; |
| 106369 | } |
| 106370 | } |
| 106371 | } |
| 106372 | assert( nColumn>0 ); |
| 106373 | pLevel->plan.nEq = nColumn; |
| 106374 | |
| 106375 | /* Count the number of additional columns needed to create a |
| 106376 | ** covering index. A "covering index" is an index that contains all |
| 106377 | ** columns that are needed by the query. With a covering index, the |
| 106378 | ** original table never needs to be accessed. Automatic indices must |
| 106379 | ** be a covering index because the index will not be updated if the |
| 106380 | ** original table changes and the index and table cannot both be used |
| 106381 | ** if they go out of sync. |
| 106382 | */ |
| 106383 | extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1))); |
| 106384 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
| 106385 | testcase( pTable->nCol==BMS-1 ); |
| 106386 | testcase( pTable->nCol==BMS-2 ); |
| 106387 | for(i=0; i<mxBitCol; i++){ |
| 106388 | if( extraCols & (((Bitmask)1)<<i) ) nColumn++; |
| 106389 | } |
| 106390 | if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ |
| 106391 | nColumn += pTable->nCol - BMS + 1; |
| 106392 | } |
| 106393 | pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ; |
| 106394 | |
| 106395 | /* Construct the Index object to describe this index */ |
| 106396 | nByte = sizeof(Index); |
| 106397 | nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 106398 | nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 106399 | nByte += nColumn; /* Index.aSortOrder */ |
| 106400 | pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 106401 | if( pIdx==0 ) return; |
| 106402 | pLevel->plan.u.pIdx = pIdx; |
| 106403 | pIdx->azColl = (char**)&pIdx[1]; |
| 106404 | pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 106405 | pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 106406 | pIdx->zName = "auto-index"; |
| 106407 | pIdx->nColumn = nColumn; |
| @@ -106409,11 +106400,11 @@ | |
| 106409 | n = 0; |
| 106410 | idxCols = 0; |
| 106411 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 106412 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 106413 | int iCol = pTerm->u.leftColumn; |
| 106414 | Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol; |
| 106415 | if( (idxCols & cMask)==0 ){ |
| 106416 | Expr *pX = pTerm->pExpr; |
| 106417 | idxCols |= cMask; |
| 106418 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 106419 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| @@ -106420,22 +106411,22 @@ | |
| 106420 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
| 106421 | n++; |
| 106422 | } |
| 106423 | } |
| 106424 | } |
| 106425 | assert( (u32)n==pLevel->plan.nEq ); |
| 106426 | |
| 106427 | /* Add additional columns needed to make the automatic index into |
| 106428 | ** a covering index */ |
| 106429 | for(i=0; i<mxBitCol; i++){ |
| 106430 | if( extraCols & (((Bitmask)1)<<i) ){ |
| 106431 | pIdx->aiColumn[n] = i; |
| 106432 | pIdx->azColl[n] = "BINARY"; |
| 106433 | n++; |
| 106434 | } |
| 106435 | } |
| 106436 | if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ |
| 106437 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 106438 | pIdx->aiColumn[n] = i; |
| 106439 | pIdx->azColl[n] = "BINARY"; |
| 106440 | n++; |
| 106441 | } |
| @@ -106443,10 +106434,11 @@ | |
| 106443 | assert( n==nColumn ); |
| 106444 | |
| 106445 | /* Create the automatic index */ |
| 106446 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 106447 | assert( pLevel->iIdxCur>=0 ); |
| 106448 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
| 106449 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
| 106450 | VdbeComment((v, "for %s", pTable->zName)); |
| 106451 | |
| 106452 | /* Fill the automatic index with content */ |
| @@ -106469,26 +106461,25 @@ | |
| 106469 | /* |
| 106470 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 106471 | ** responsibility of the caller to eventually release the structure |
| 106472 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 106473 | */ |
| 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; |
| 106479 | int i, j; |
| 106480 | int nTerm; |
| 106481 | struct sqlite3_index_constraint *pIdxCons; |
| 106482 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 106483 | struct sqlite3_index_constraint_usage *pUsage; |
| 106484 | WhereTerm *pTerm; |
| 106485 | int nOrderBy; |
| 106486 | sqlite3_index_info *pIdxInfo; |
| 106487 | |
| 106488 | WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName)); |
| 106489 | |
| 106490 | /* Count the number of possible WHERE clause constraints referring |
| 106491 | ** to this virtual table */ |
| 106492 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 106493 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 106494 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| @@ -106520,11 +106511,10 @@ | |
| 106520 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 106521 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 106522 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 106523 | if( pIdxInfo==0 ){ |
| 106524 | sqlite3ErrorMsg(pParse, "out of memory"); |
| 106525 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 106526 | return 0; |
| 106527 | } |
| 106528 | |
| 106529 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 106530 | ** many fields that are declared "const" to prevent xBestIndex from |
| @@ -106576,12 +106566,12 @@ | |
| 106576 | } |
| 106577 | |
| 106578 | /* |
| 106579 | ** The table object reference passed as the second argument to this function |
| 106580 | ** must represent a virtual table. This function invokes the xBestIndex() |
| 106581 | ** method of the virtual table with the sqlite3_index_info pointer passed |
| 106582 | ** as the argument. |
| 106583 | ** |
| 106584 | ** If an error occurs, pParse is populated with an error message and a |
| 106585 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 106586 | ** part of the sqlite3_index_info structure is left populated. |
| 106587 | ** |
| @@ -106592,11 +106582,10 @@ | |
| 106592 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
| 106593 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
| 106594 | int i; |
| 106595 | int rc; |
| 106596 | |
| 106597 | WHERETRACE(("xBestIndex for %s\n", pTab->zName)); |
| 106598 | TRACE_IDX_INPUTS(p); |
| 106599 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 106600 | TRACE_IDX_OUTPUTS(p); |
| 106601 | |
| 106602 | if( rc!=SQLITE_OK ){ |
| @@ -106618,211 +106607,12 @@ | |
| 106618 | } |
| 106619 | } |
| 106620 | |
| 106621 | return pParse->nErr; |
| 106622 | } |
| 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 */ |
| 106824 | |
| 106825 | #ifdef SQLITE_ENABLE_STAT3 |
| 106826 | /* |
| 106827 | ** Estimate the location of a particular key among all keys in an |
| 106828 | ** index. Store the results in aStat as follows: |
| @@ -107060,11 +106850,11 @@ | |
| 107060 | Parse *pParse, /* Parsing & code generating context */ |
| 107061 | Index *p, /* The index containing the range-compared column; "x" */ |
| 107062 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 107063 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 107064 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 107065 | double *pRangeDiv /* OUT: Reduce search space by this divisor */ |
| 107066 | ){ |
| 107067 | int rc = SQLITE_OK; |
| 107068 | |
| 107069 | #ifdef SQLITE_ENABLE_STAT3 |
| 107070 | |
| @@ -107098,29 +106888,35 @@ | |
| 107098 | if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1]; |
| 107099 | } |
| 107100 | sqlite3ValueFree(pRangeVal); |
| 107101 | } |
| 107102 | 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); |
| 107107 | } |
| 107108 | WHERETRACE(("range scan regions: %u..%u div=%g\n", |
| 107109 | (u32)iLower, (u32)iUpper, *pRangeDiv)); |
| 107110 | return SQLITE_OK; |
| 107111 | } |
| 107112 | } |
| 107113 | #else |
| 107114 | UNUSED_PARAMETER(pParse); |
| 107115 | UNUSED_PARAMETER(p); |
| 107116 | UNUSED_PARAMETER(nEq); |
| 107117 | #endif |
| 107118 | assert( pLower || pUpper ); |
| 107119 | *pRangeDiv = (double)1; |
| 107120 | if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4; |
| 107121 | if( pUpper ) *pRangeDiv *= (double)4; |
| 107122 | return rc; |
| 107123 | } |
| 107124 | |
| 107125 | #ifdef SQLITE_ENABLE_STAT3 |
| 107126 | /* |
| @@ -107142,11 +106938,11 @@ | |
| 107142 | */ |
| 107143 | static int whereEqualScanEst( |
| 107144 | Parse *pParse, /* Parsing & code generating context */ |
| 107145 | Index *p, /* The index whose left-most column is pTerm */ |
| 107146 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 107147 | double *pnRow /* Write the revised row estimate here */ |
| 107148 | ){ |
| 107149 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
| 107150 | u8 aff; /* Column affinity */ |
| 107151 | int rc; /* Subfunction return code */ |
| 107152 | tRowcnt a[2]; /* Statistics */ |
| @@ -107161,11 +106957,11 @@ | |
| 107161 | pRhs = sqlite3ValueNew(pParse->db); |
| 107162 | } |
| 107163 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
| 107164 | rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 107165 | if( rc==SQLITE_OK ){ |
| 107166 | WHERETRACE(("equality scan regions: %d\n", (int)a[1])); |
| 107167 | *pnRow = a[1]; |
| 107168 | } |
| 107169 | whereEqualScanEst_cancel: |
| 107170 | sqlite3ValueFree(pRhs); |
| 107171 | return rc; |
| @@ -107191,16 +106987,16 @@ | |
| 107191 | */ |
| 107192 | static int whereInScanEst( |
| 107193 | Parse *pParse, /* Parsing & code generating context */ |
| 107194 | Index *p, /* The index whose left-most column is pTerm */ |
| 107195 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 107196 | double *pnRow /* Write the revised row estimate here */ |
| 107197 | ){ |
| 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 */ |
| 107202 | |
| 107203 | assert( p->aSample!=0 ); |
| 107204 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 107205 | nEst = p->aiRowEst[0]; |
| 107206 | rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| @@ -107207,888 +107003,15 @@ | |
| 107207 | nRowEst += nEst; |
| 107208 | } |
| 107209 | if( rc==SQLITE_OK ){ |
| 107210 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 107211 | *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 | } |
| 108090 | |
| 108091 | /* |
| 108092 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 108093 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 108094 | ** or USING clause of that join. |
| @@ -108183,10 +107106,11 @@ | |
| 108183 | static int codeEqualityTerm( |
| 108184 | Parse *pParse, /* The parsing context */ |
| 108185 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
| 108186 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 108187 | int iEq, /* Index of the equality term within this level */ |
| 108188 | int iTarget /* Attempt to leave results in this register */ |
| 108189 | ){ |
| 108190 | Expr *pX = pTerm->pExpr; |
| 108191 | Vdbe *v = pParse->pVdbe; |
| 108192 | int iReg; /* Register holding results */ |
| @@ -108200,18 +107124,17 @@ | |
| 108200 | #ifndef SQLITE_OMIT_SUBQUERY |
| 108201 | }else{ |
| 108202 | int eType; |
| 108203 | int iTab; |
| 108204 | struct InLoop *pIn; |
| 108205 | u8 bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0; |
| 108206 | |
| 108207 | if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 |
| 108208 | && pLevel->plan.u.pIdx->aSortOrder[iEq] |
| 108209 | ){ |
| 108210 | testcase( iEq==0 ); |
| 108211 | testcase( iEq==pLevel->plan.u.pIdx->nColumn-1 ); |
| 108212 | testcase( iEq>0 && iEq+1<pLevel->plan.u.pIdx->nColumn ); |
| 108213 | testcase( bRev ); |
| 108214 | bRev = !bRev; |
| 108215 | } |
| 108216 | assert( pX->op==TK_IN ); |
| 108217 | iReg = iTarget; |
| @@ -108220,11 +107143,12 @@ | |
| 108220 | testcase( bRev ); |
| 108221 | bRev = !bRev; |
| 108222 | } |
| 108223 | iTab = pX->iTable; |
| 108224 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 108225 | assert( pLevel->plan.wsFlags & WHERE_IN_ABLE ); |
| 108226 | if( pLevel->u.in.nIn==0 ){ |
| 108227 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 108228 | } |
| 108229 | pLevel->u.in.nIn++; |
| 108230 | pLevel->u.in.aInLoop = |
| @@ -108292,31 +107216,35 @@ | |
| 108292 | static int codeAllEqualityTerms( |
| 108293 | Parse *pParse, /* Parsing context */ |
| 108294 | 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 */ |
| 108297 | int nExtraReg, /* Number of extra registers to allocate */ |
| 108298 | char **pzAff /* OUT: Set to point to affinity string */ |
| 108299 | ){ |
| 108300 | int nEq = pLevel->plan.nEq; /* The number of == or IN constraints to code */ |
| 108301 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 108302 | Index *pIdx; /* The index being used for this loop */ |
| 108303 | int iCur = pLevel->iTabCur; /* The cursor of the table */ |
| 108304 | WhereTerm *pTerm; /* A single constraint term */ |
| 108305 | int j; /* Loop counter */ |
| 108306 | int regBase; /* Base register */ |
| 108307 | int nReg; /* Number of registers to allocate */ |
| 108308 | char *zAff; /* Affinity string to return */ |
| 108309 | |
| 108310 | /* 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; |
| 108313 | |
| 108314 | /* Figure out how many memory cells we will need then allocate them. |
| 108315 | */ |
| 108316 | regBase = pParse->nMem + 1; |
| 108317 | nReg = pLevel->plan.nEq + nExtraReg; |
| 108318 | pParse->nMem += nReg; |
| 108319 | |
| 108320 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 108321 | if( !zAff ){ |
| 108322 | pParse->db->mallocFailed = 1; |
| @@ -108325,18 +107253,17 @@ | |
| 108325 | /* Evaluate the equality constraints |
| 108326 | */ |
| 108327 | assert( pIdx->nColumn>=nEq ); |
| 108328 | for(j=0; j<nEq; j++){ |
| 108329 | int r1; |
| 108330 | int k = pIdx->aiColumn[j]; |
| 108331 | pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx); |
| 108332 | if( pTerm==0 ) break; |
| 108333 | /* The following true for indices with redundant columns. |
| 108334 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 108335 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
| 108336 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 108337 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, regBase+j); |
| 108338 | if( r1!=regBase+j ){ |
| 108339 | if( nReg==1 ){ |
| 108340 | sqlite3ReleaseTempReg(pParse, regBase); |
| 108341 | regBase = r1; |
| 108342 | }else{ |
| @@ -108400,20 +107327,20 @@ | |
| 108400 | ** |
| 108401 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 108402 | ** It is the responsibility of the caller to free the buffer when it is |
| 108403 | ** no longer required. |
| 108404 | */ |
| 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; |
| 108409 | int i, j; |
| 108410 | Column *aCol = pTab->aCol; |
| 108411 | int *aiColumn = pIndex->aiColumn; |
| 108412 | StrAccum txt; |
| 108413 | |
| 108414 | if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
| 108415 | return 0; |
| 108416 | } |
| 108417 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
| 108418 | txt.db = db; |
| 108419 | sqlite3StrAccumAppend(&txt, " (", 2); |
| @@ -108420,15 +107347,15 @@ | |
| 108420 | for(i=0; i<nEq; i++){ |
| 108421 | explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "="); |
| 108422 | } |
| 108423 | |
| 108424 | j = i; |
| 108425 | if( pPlan->wsFlags&WHERE_BTM_LIMIT ){ |
| 108426 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 108427 | explainAppendTerm(&txt, i++, z, ">"); |
| 108428 | } |
| 108429 | if( pPlan->wsFlags&WHERE_TOP_LIMIT ){ |
| 108430 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 108431 | explainAppendTerm(&txt, i, z, "<"); |
| 108432 | } |
| 108433 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 108434 | return sqlite3StrAccumFinish(&txt); |
| @@ -108447,24 +107374,26 @@ | |
| 108447 | int iLevel, /* Value for "level" column of output */ |
| 108448 | int iFrom, /* Value for "from" column of output */ |
| 108449 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 108450 | ){ |
| 108451 | if( pParse->explain==2 ){ |
| 108452 | u32 flags = pLevel->plan.wsFlags; |
| 108453 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 108454 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 108455 | sqlite3 *db = pParse->db; /* Database handle */ |
| 108456 | char *zMsg; /* Text to add to EQP output */ |
| 108457 | sqlite3_int64 nRow; /* Expected number of rows visited by scan */ |
| 108458 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 108459 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 108460 | |
| 108461 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
| 108462 | |
| 108463 | isSearch = (pLevel->plan.nEq>0) |
| 108464 | || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 108465 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 108466 | |
| 108467 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
| 108468 | if( pItem->pSelect ){ |
| 108469 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
| 108470 | }else{ |
| @@ -108472,24 +107401,26 @@ | |
| 108472 | } |
| 108473 | |
| 108474 | if( pItem->zAlias ){ |
| 108475 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 108476 | } |
| 108477 | if( (flags & WHERE_INDEXED)!=0 ){ |
| 108478 | char *zWhere = explainIndexRange(db, pLevel, pItem->pTab); |
| 108479 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, |
| 108480 | ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""), |
| 108481 | ((flags & WHERE_IDX_ONLY)?"COVERING ":""), |
| 108482 | ((flags & WHERE_TEMP_INDEX)?"":" "), |
| 108483 | ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName), |
| 108484 | zWhere |
| 108485 | ); |
| 108486 | sqlite3DbFree(db, zWhere); |
| 108487 | }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){ |
| 108488 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
| 108489 | |
| 108490 | if( flags&WHERE_ROWID_EQ ){ |
| 108491 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
| 108492 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
| 108493 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 108494 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 108495 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
| @@ -108497,22 +107428,15 @@ | |
| 108497 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 108498 | } |
| 108499 | } |
| 108500 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 108501 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
| 108502 | sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx; |
| 108503 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
| 108504 | pVtabIdx->idxNum, pVtabIdx->idxStr); |
| 108505 | } |
| 108506 | #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); |
| 108514 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
| 108515 | } |
| 108516 | } |
| 108517 | #else |
| 108518 | # define explainOneScan(u,v,w,x,y,z) |
| @@ -108524,19 +107448,19 @@ | |
| 108524 | ** implementation described by pWInfo. |
| 108525 | */ |
| 108526 | static Bitmask codeOneLoopStart( |
| 108527 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 108528 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 108529 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 108530 | Bitmask notReady /* Which tables are currently available */ |
| 108531 | ){ |
| 108532 | int j, k; /* Loop counters */ |
| 108533 | int iCur; /* The VDBE cursor for the table */ |
| 108534 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 108535 | int omitTable; /* True if we use the index only */ |
| 108536 | int bRev; /* True if we need to scan in reverse order */ |
| 108537 | WhereLevel *pLevel; /* The where level to be coded */ |
| 108538 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 108539 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 108540 | Parse *pParse; /* Parsing context */ |
| 108541 | Vdbe *v; /* The prepared stmt under constructions */ |
| 108542 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
| @@ -108546,17 +107470,18 @@ | |
| 108546 | int iReleaseReg = 0; /* Temp register to free before returning */ |
| 108547 | Bitmask newNotReady; /* Return value */ |
| 108548 | |
| 108549 | pParse = pWInfo->pParse; |
| 108550 | v = pParse->pVdbe; |
| 108551 | pWC = pWInfo->pWC; |
| 108552 | pLevel = &pWInfo->a[iLevel]; |
| 108553 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 108554 | 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; |
| 108558 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
| 108559 | |
| 108560 | /* Create labels for the "break" and "continue" instructions |
| 108561 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 108562 | ** Jump to cont to go immediately to the next iteration of the |
| @@ -108589,51 +107514,41 @@ | |
| 108589 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 108590 | pLevel->op = OP_Goto; |
| 108591 | }else |
| 108592 | |
| 108593 | #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 |
| 108596 | ** to access the data. |
| 108597 | */ |
| 108598 | int iReg; /* P3 Value for OP_VFilter */ |
| 108599 | 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; |
| 108606 | |
| 108607 | sqlite3ExprCachePush(pParse); |
| 108608 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 108609 | 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 | for(j=0; j<nConstraint; j++){ |
| 108632 | if( aUsage[j].omit ){ |
| 108633 | int iTerm = aConstraint[j].iTermOffset; |
| 108634 | disableTerm(pLevel, &pWC->a[iTerm]); |
| 108635 | } |
| 108636 | } |
| 108637 | pLevel->op = OP_VNext; |
| 108638 | pLevel->p1 = iCur; |
| 108639 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| @@ -108640,41 +107555,48 @@ | |
| 108640 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
| 108641 | sqlite3ExprCachePop(pParse, 1); |
| 108642 | }else |
| 108643 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 108644 | |
| 108645 | if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){ |
| 108646 | /* Case 1: We can directly reference a single row using an |
| 108647 | ** equality comparison against the ROWID field. Or |
| 108648 | ** we reference multiple rows using a "rowid IN (...)" |
| 108649 | ** construct. |
| 108650 | */ |
| 108651 | iReleaseReg = sqlite3GetTempReg(pParse); |
| 108652 | pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0); |
| 108653 | assert( pTerm!=0 ); |
| 108654 | assert( pTerm->pExpr!=0 ); |
| 108655 | assert( omitTable==0 ); |
| 108656 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 108657 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, iReleaseReg); |
| 108658 | addrNxt = pLevel->addrNxt; |
| 108659 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 108660 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
| 108661 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
| 108662 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 108663 | VdbeComment((v, "pk")); |
| 108664 | 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. |
| 108667 | */ |
| 108668 | int testOp = OP_Noop; |
| 108669 | int start; |
| 108670 | int memEndValue = 0; |
| 108671 | WhereTerm *pStart, *pEnd; |
| 108672 | |
| 108673 | 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); |
| 108676 | if( bRev ){ |
| 108677 | pTerm = pStart; |
| 108678 | pStart = pEnd; |
| 108679 | pEnd = pTerm; |
| 108680 | } |
| @@ -108737,12 +107659,12 @@ | |
| 108737 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
| 108738 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 108739 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 108740 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
| 108741 | } |
| 108742 | }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){ |
| 108743 | /* Case 3: A scan using an index. |
| 108744 | ** |
| 108745 | ** The WHERE clause may contain zero or more equality |
| 108746 | ** terms ("==" or "IN" operators) that refer to the N |
| 108747 | ** left-most columns of the index. It may also contain |
| 108748 | ** inequality constraints (>, <, >= or <=) on the indexed |
| @@ -108784,12 +107706,12 @@ | |
| 108784 | static const u8 aEndOp[] = { |
| 108785 | OP_Noop, /* 0: (!end_constraints) */ |
| 108786 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 108787 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 108788 | }; |
| 108789 | int nEq = pLevel->plan.nEq; /* Number of == or IN terms */ |
| 108790 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
| 108791 | int regBase; /* Base register holding constraint values */ |
| 108792 | int r1; /* Temp register */ |
| 108793 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 108794 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 108795 | int startEq; /* True if range start uses ==, >= or <= */ |
| @@ -108801,11 +107723,11 @@ | |
| 108801 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 108802 | int op; /* Instruction opcode */ |
| 108803 | char *zStartAff; /* Affinity for start of range constraint */ |
| 108804 | char *zEndAff; /* Affinity for end of range constraint */ |
| 108805 | |
| 108806 | pIdx = pLevel->plan.u.pIdx; |
| 108807 | iIdxCur = pLevel->iIdxCur; |
| 108808 | k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]); |
| 108809 | |
| 108810 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 108811 | ** was passed to this function to implement a "SELECT min(x) ..." |
| @@ -108813,12 +107735,12 @@ | |
| 108813 | ** a single iteration. This means that the first row returned |
| 108814 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 108815 | ** the first one after the nEq equality constraints in the index, |
| 108816 | ** this requires some special handling. |
| 108817 | */ |
| 108818 | if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 108819 | && (pLevel->plan.wsFlags&WHERE_ORDERED) |
| 108820 | && (pIdx->nColumn>nEq) |
| 108821 | ){ |
| 108822 | /* assert( pOrderBy->nExpr==1 ); */ |
| 108823 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 108824 | isMinQuery = 1; |
| @@ -108826,25 +107748,26 @@ | |
| 108826 | } |
| 108827 | |
| 108828 | /* Find any inequality constraint terms for the start and end |
| 108829 | ** of the range. |
| 108830 | */ |
| 108831 | if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){ |
| 108832 | pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx); |
| 108833 | nExtraReg = 1; |
| 108834 | } |
| 108835 | if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){ |
| 108836 | pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx); |
| 108837 | nExtraReg = 1; |
| 108838 | } |
| 108839 | |
| 108840 | /* Generate code to evaluate all constraint terms using == or IN |
| 108841 | ** and store the values of those terms in an array of registers |
| 108842 | ** starting at regBase. |
| 108843 | */ |
| 108844 | regBase = codeAllEqualityTerms( |
| 108845 | pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff |
| 108846 | ); |
| 108847 | zEndAff = sqlite3DbStrDup(pParse->db, zStartAff); |
| 108848 | addrNxt = pLevel->addrNxt; |
| 108849 | |
| 108850 | /* If we are doing a reverse order scan on an ascending index, or |
| @@ -108948,13 +107871,13 @@ | |
| 108948 | /* If there are inequality constraints, check that the value |
| 108949 | ** of the table column that the inequality contrains is not NULL. |
| 108950 | ** If it is, jump to the next iteration of the loop. |
| 108951 | */ |
| 108952 | 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 ){ |
| 108956 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 108957 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 108958 | } |
| 108959 | sqlite3ReleaseTempReg(pParse, r1); |
| 108960 | |
| @@ -108969,28 +107892,28 @@ | |
| 108969 | } |
| 108970 | |
| 108971 | /* Record the instruction used to terminate the loop. Disable |
| 108972 | ** WHERE clause terms made redundant by the index range scan. |
| 108973 | */ |
| 108974 | if( pLevel->plan.wsFlags & WHERE_UNIQUE ){ |
| 108975 | pLevel->op = OP_Noop; |
| 108976 | }else if( bRev ){ |
| 108977 | pLevel->op = OP_Prev; |
| 108978 | }else{ |
| 108979 | pLevel->op = OP_Next; |
| 108980 | } |
| 108981 | pLevel->p1 = iIdxCur; |
| 108982 | if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){ |
| 108983 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 108984 | }else{ |
| 108985 | assert( pLevel->p5==0 ); |
| 108986 | } |
| 108987 | }else |
| 108988 | |
| 108989 | #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 |
| 108992 | ** |
| 108993 | ** Example: |
| 108994 | ** |
| 108995 | ** CREATE TABLE t1(a,b,c,d); |
| 108996 | ** CREATE INDEX i1 ON t1(a); |
| @@ -109039,11 +107962,11 @@ | |
| 109039 | int iRetInit; /* Address of regReturn init */ |
| 109040 | int untestedTerms = 0; /* Some terms not completely tested */ |
| 109041 | int ii; /* Loop counter */ |
| 109042 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
| 109043 | |
| 109044 | pTerm = pLevel->plan.u.pTerm; |
| 109045 | assert( pTerm!=0 ); |
| 109046 | assert( pTerm->eOperator & WO_OR ); |
| 109047 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 109048 | pOrWc = &pTerm->u.pOrInfo->wc; |
| 109049 | pLevel->op = OP_Return; |
| @@ -109080,11 +108003,11 @@ | |
| 109080 | ** over the top of the loop into the body of it. In this case the |
| 109081 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 109082 | ** fall through to the next instruction, just as an OP_Next does if |
| 109083 | ** called on an uninitialized cursor. |
| 109084 | */ |
| 109085 | if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 109086 | regRowset = ++pParse->nMem; |
| 109087 | regRowid = ++pParse->nMem; |
| 109088 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 109089 | } |
| 109090 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| @@ -109131,15 +108054,15 @@ | |
| 109131 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 109132 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
| 109133 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
| 109134 | assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed ); |
| 109135 | if( pSubWInfo ){ |
| 109136 | WhereLevel *pLvl; |
| 109137 | explainOneScan( |
| 109138 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 109139 | ); |
| 109140 | if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 109141 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 109142 | int r; |
| 109143 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
| 109144 | regRowid, 0); |
| 109145 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| @@ -109164,17 +108087,17 @@ | |
| 109164 | ** processed or the index is the same as that used by all previous |
| 109165 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 109166 | ** pCov to NULL to indicate that no candidate covering index will |
| 109167 | ** be available. |
| 109168 | */ |
| 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) |
| 109173 | ){ |
| 109174 | assert( pLvl->iIdxCur==iCovCur ); |
| 109175 | pCov = pLvl->plan.u.pIdx; |
| 109176 | }else{ |
| 109177 | pCov = 0; |
| 109178 | } |
| 109179 | |
| 109180 | /* Finish the loop through table entries that match term pOrTerm. */ |
| @@ -109196,23 +108119,22 @@ | |
| 109196 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 109197 | }else |
| 109198 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 109199 | |
| 109200 | { |
| 109201 | /* Case 5: There is no usable index. We must do a complete |
| 109202 | ** scan of the entire table. |
| 109203 | */ |
| 109204 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 109205 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 109206 | assert( bRev==0 || bRev==1 ); |
| 109207 | assert( omitTable==0 ); |
| 109208 | pLevel->op = aStep[bRev]; |
| 109209 | pLevel->p1 = iCur; |
| 109210 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
| 109211 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 109212 | } |
| 109213 | newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur); |
| 109214 | |
| 109215 | /* Insert code to test every subexpression that can be completely |
| 109216 | ** computed using the current set of tables. |
| 109217 | ** |
| 109218 | ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through |
| @@ -109290,51 +108212,1529 @@ | |
| 109290 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
| 109291 | |
| 109292 | return newNotReady; |
| 109293 | } |
| 109294 | |
| 109295 | #if defined(SQLITE_TEST) |
| 109296 | /* |
| 109297 | ** The following variable holds a text description of query plan generated |
| 109298 | ** by the most recent call to sqlite3WhereBegin(). Each call to WhereBegin |
| 109299 | ** overwrites the previous. This information is used for testing and |
| 109300 | ** analysis only. |
| 109301 | */ |
| 109302 | SQLITE_API char sqlite3_query_plan[BMS*2*40]; /* Text of the join */ |
| 109303 | static int nQPlan = 0; /* Next free slow in _query_plan[] */ |
| 109304 | |
| 109305 | #endif /* SQLITE_TEST */ |
| 109306 | |
| 109307 | |
| 109308 | /* |
| 109309 | ** Free a WhereInfo structure |
| 109310 | */ |
| 109311 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
| 109312 | 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); |
| 109332 | sqlite3DbFree(db, pWInfo); |
| 109333 | } |
| 109334 | } |
| 109335 | |
| 109336 | |
| 109337 | /* |
| 109338 | ** Generate the beginning of the loop used for WHERE clause processing. |
| 109339 | ** The return value is a pointer to an opaque structure that contains |
| 109340 | ** information needed to terminate the loop. Later, the calling routine |
| @@ -109410,19 +109810,10 @@ | |
| 109410 | ** ORDER BY CLAUSE PROCESSING |
| 109411 | ** |
| 109412 | ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
| 109413 | ** if there is one. If there is no ORDER BY clause or if this routine |
| 109414 | ** 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 | */ |
| 109425 | SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( |
| 109426 | Parse *pParse, /* The parser context */ |
| 109427 | SrcList *pTabList, /* A list of all tables to be scanned */ |
| 109428 | Expr *pWhere, /* The WHERE clause */ |
| @@ -109434,22 +109825,21 @@ | |
| 109434 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| 109435 | int nTabList; /* Number of elements in pTabList */ |
| 109436 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 109437 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 109438 | Bitmask notReady; /* Cursors that are not yet positioned */ |
| 109439 | WhereBestIdx sWBI; /* Best index search context */ |
| 109440 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
| 109441 | 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 | int ii; /* Loop counter */ |
| 109445 | sqlite3 *db; /* Database connection */ |
| 109446 | |
| 109447 | |
| 109448 | /* Variable initialization */ |
| 109449 | memset(&sWBI, 0, sizeof(sWBI)); |
| 109450 | sWBI.pParse = pParse; |
| 109451 | |
| 109452 | /* The number of tables in the FROM clause is limited by the number of |
| 109453 | ** bits in a Bitmask |
| 109454 | */ |
| 109455 | testcase( pTabList->nSrc==BMS ); |
| @@ -109472,49 +109862,59 @@ | |
| 109472 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 109473 | ** some architectures. Hence the ROUND8() below. |
| 109474 | */ |
| 109475 | db = pParse->db; |
| 109476 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
| 109477 | pWInfo = sqlite3DbMallocZero(db, |
| 109478 | nByteWInfo + |
| 109479 | sizeof(WhereClause) + |
| 109480 | sizeof(WhereMaskSet) |
| 109481 | ); |
| 109482 | if( db->mallocFailed ){ |
| 109483 | sqlite3DbFree(db, pWInfo); |
| 109484 | pWInfo = 0; |
| 109485 | goto whereBeginError; |
| 109486 | } |
| 109487 | pWInfo->nLevel = nTabList; |
| 109488 | pWInfo->pParse = pParse; |
| 109489 | pWInfo->pTabList = pTabList; |
| 109490 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
| 109491 | pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo]; |
| 109492 | pWInfo->wctrlFlags = wctrlFlags; |
| 109493 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 109494 | pMaskSet = (WhereMaskSet*)&sWBI.pWC[1]; |
| 109495 | sWBI.aLevel = pWInfo->a; |
| 109496 | |
| 109497 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 109498 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 109499 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0; |
| 109500 | |
| 109501 | /* Split the WHERE clause into separate subexpressions where each |
| 109502 | ** subexpression is separated by an AND operator. |
| 109503 | */ |
| 109504 | initMaskSet(pMaskSet); |
| 109505 | whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags); |
| 109506 | sqlite3ExprCodeConstants(pParse, pWhere); |
| 109507 | whereSplit(sWBI.pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
| 109508 | |
| 109509 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 109510 | ** expression and either jump over all of the code or fall thru. |
| 109511 | */ |
| 109512 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
| 109513 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
| 109514 | pWhere = 0; |
| 109515 | } |
| 109516 | |
| 109517 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 109518 | ** |
| 109519 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 109520 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| @@ -109547,332 +109947,149 @@ | |
| 109547 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 109548 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 109549 | ** want to analyze these virtual terms, so start analyzing at the end |
| 109550 | ** and work forward so that the added virtual terms are never processed. |
| 109551 | */ |
| 109552 | exprAnalyzeAll(pTabList, sWBI.pWC); |
| 109553 | if( db->mallocFailed ){ |
| 109554 | goto whereBeginError; |
| 109555 | } |
| 109556 | |
| 109557 | /* Check if the DISTINCT qualifier, if there is one, is redundant. |
| 109558 | ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to |
| 109559 | ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT. |
| 109560 | */ |
| 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 | } |
| 109834 | |
| 109835 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 109836 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 109837 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 109838 | ** the statement to update a single row. |
| 109839 | */ |
| 109840 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
| 109841 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){ |
| 109842 | pWInfo->okOnePass = 1; |
| 109843 | pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY; |
| 109844 | } |
| 109845 | |
| 109846 | /* Open all tables in the pTabList and any indices selected for |
| 109847 | ** searching those tables. |
| 109848 | */ |
| 109849 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 109850 | notReady = ~(Bitmask)0; |
| 109851 | pWInfo->nRowOut = (double)1; |
| 109852 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
| 109853 | Table *pTab; /* Table to open */ |
| 109854 | int iDb; /* Index of database containing table/index */ |
| 109855 | struct SrcList_item *pTabItem; |
| 109856 | |
| 109857 | pTabItem = &pTabList->a[pLevel->iFrom]; |
| 109858 | pTab = pTabItem->pTab; |
| 109859 | pWInfo->nRowOut *= pLevel->plan.nRow; |
| 109860 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 109861 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
| 109862 | /* Do nothing */ |
| 109863 | }else |
| 109864 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 109865 | if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 109866 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 109867 | int iCur = pTabItem->iCursor; |
| 109868 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| 109869 | }else if( IsVirtual(pTab) ){ |
| 109870 | /* noop */ |
| 109871 | }else |
| 109872 | #endif |
| 109873 | if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 |
| 109874 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
| 109875 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 109876 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 109877 | testcase( pTab->nCol==BMS-1 ); |
| 109878 | testcase( pTab->nCol==BMS ); |
| @@ -109886,26 +110103,27 @@ | |
| 109886 | } |
| 109887 | }else{ |
| 109888 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 109889 | } |
| 109890 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 109891 | if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 109892 | constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel); |
| 109893 | }else |
| 109894 | #endif |
| 109895 | if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 109896 | Index *pIx = pLevel->plan.u.pIdx; |
| 109897 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
| 109898 | int iIndexCur = pLevel->iIdxCur; |
| 109899 | assert( pIx->pSchema==pTab->pSchema ); |
| 109900 | assert( iIndexCur>=0 ); |
| 109901 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
| 109902 | (char*)pKey, P4_KEYINFO_HANDOFF); |
| 109903 | VdbeComment((v, "%s", pIx->zName)); |
| 109904 | } |
| 109905 | sqlite3CodeVerifySchema(pParse, iDb); |
| 109906 | notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor); |
| 109907 | } |
| 109908 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 109909 | if( db->mallocFailed ) goto whereBeginError; |
| 109910 | |
| 109911 | /* Generate the code to do the search. Each iteration of the for |
| @@ -109914,70 +110132,15 @@ | |
| 109914 | */ |
| 109915 | notReady = ~(Bitmask)0; |
| 109916 | for(ii=0; ii<nTabList; ii++){ |
| 109917 | pLevel = &pWInfo->a[ii]; |
| 109918 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
| 109919 | notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady); |
| 109920 | pWInfo->iContinue = pLevel->addrCont; |
| 109921 | } |
| 109922 | |
| 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 | */ |
| 109979 | return pWInfo; |
| 109980 | |
| 109981 | /* Jump here if malloc fails */ |
| 109982 | whereBeginError: |
| 109983 | if( pWInfo ){ |
| @@ -109994,24 +110157,26 @@ | |
| 109994 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ |
| 109995 | Parse *pParse = pWInfo->pParse; |
| 109996 | Vdbe *v = pParse->pVdbe; |
| 109997 | int i; |
| 109998 | WhereLevel *pLevel; |
| 109999 | SrcList *pTabList = pWInfo->pTabList; |
| 110000 | sqlite3 *db = pParse->db; |
| 110001 | |
| 110002 | /* Generate loop termination code. |
| 110003 | */ |
| 110004 | sqlite3ExprCacheClear(pParse); |
| 110005 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
| 110006 | pLevel = &pWInfo->a[i]; |
| 110007 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
| 110008 | if( pLevel->op!=OP_Noop ){ |
| 110009 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
| 110010 | sqlite3VdbeChangeP5(v, pLevel->p5); |
| 110011 | } |
| 110012 | if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
| 110013 | struct InLoop *pIn; |
| 110014 | int j; |
| 110015 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
| 110016 | for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
| 110017 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| @@ -110022,16 +110187,16 @@ | |
| 110022 | } |
| 110023 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
| 110024 | if( pLevel->iLeftJoin ){ |
| 110025 | int addr; |
| 110026 | 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 ){ |
| 110030 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 110031 | } |
| 110032 | if( pLevel->iIdxCur>=0 ){ |
| 110033 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
| 110034 | } |
| 110035 | if( pLevel->op==OP_Return ){ |
| 110036 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 110037 | }else{ |
| @@ -110052,19 +110217,20 @@ | |
| 110052 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
| 110053 | Index *pIdx = 0; |
| 110054 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
| 110055 | Table *pTab = pTabItem->pTab; |
| 110056 | assert( pTab!=0 ); |
| 110057 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 110058 | && pTab->pSelect==0 |
| 110059 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
| 110060 | ){ |
| 110061 | int ws = pLevel->plan.wsFlags; |
| 110062 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
| 110063 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 110064 | } |
| 110065 | if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){ |
| 110066 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 110067 | } |
| 110068 | } |
| 110069 | |
| 110070 | /* If this scan uses an index, make code substitutions to read data |
| @@ -110078,16 +110244,16 @@ | |
| 110078 | ** sqlite3WhereEnd will have created code that references the table |
| 110079 | ** directly. This loop scans all that code looking for opcodes |
| 110080 | ** that reference the table and converts them into opcodes that |
| 110081 | ** reference the index. |
| 110082 | */ |
| 110083 | if( pLevel->plan.wsFlags & WHERE_INDEXED ){ |
| 110084 | pIdx = pLevel->plan.u.pIdx; |
| 110085 | }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){ |
| 110086 | pIdx = pLevel->u.pCovidx; |
| 110087 | } |
| 110088 | if( pIdx && !db->mallocFailed){ |
| 110089 | int k, j, last; |
| 110090 | VdbeOp *pOp; |
| 110091 | |
| 110092 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 110093 | last = sqlite3VdbeCurrentAddr(v); |
| @@ -110099,12 +110265,11 @@ | |
| 110099 | pOp->p2 = j; |
| 110100 | pOp->p1 = pLevel->iIdxCur; |
| 110101 | break; |
| 110102 | } |
| 110103 | } |
| 110104 | assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 |
| 110105 | || j<pIdx->nColumn ); |
| 110106 | }else if( pOp->opcode==OP_Rowid ){ |
| 110107 | pOp->p1 = pLevel->iIdxCur; |
| 110108 | pOp->opcode = OP_IdxRowid; |
| 110109 | } |
| 110110 | } |
| @@ -115480,11 +115645,11 @@ | |
| 115480 | } |
| 115481 | |
| 115482 | /* |
| 115483 | ** Another built-in collating sequence: NOCASE. |
| 115484 | ** |
| 115485 | ** This collating sequence is intended to be used for "case independant |
| 115486 | ** comparison". SQLite's knowledge of upper and lower case equivalents |
| 115487 | ** extends only to the 26 characters used in the English language. |
| 115488 | ** |
| 115489 | ** At the moment there is only a UTF-8 implementation. |
| 115490 | */ |
| @@ -115627,16 +115792,10 @@ | |
| 115627 | "statements or unfinished backups"); |
| 115628 | sqlite3_mutex_leave(db->mutex); |
| 115629 | return SQLITE_BUSY; |
| 115630 | } |
| 115631 | |
| 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 | #ifdef SQLITE_ENABLE_SQLLOG |
| 115639 | if( sqlite3GlobalConfig.xSqllog ){ |
| 115640 | /* Closing the handle. Fourth parameter is passed the value 2. */ |
| 115641 | sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2); |
| 115642 | } |
| @@ -115686,10 +115845,16 @@ | |
| 115686 | /* If we reach this point, it means that the database connection has |
| 115687 | ** closed all sqlite3_stmt and sqlite3_backup objects and has been |
| 115688 | ** passed to sqlite3_close (meaning that it is a zombie). Therefore, |
| 115689 | ** go ahead and free all resources. |
| 115690 | */ |
| 115691 | |
| 115692 | /* Free any outstanding Savepoint structures. */ |
| 115693 | sqlite3CloseSavepoints(db); |
| 115694 | |
| 115695 | /* Close all database connections */ |
| @@ -115787,19 +115952,26 @@ | |
| 115787 | SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){ |
| 115788 | int i; |
| 115789 | int inTrans = 0; |
| 115790 | assert( sqlite3_mutex_held(db->mutex) ); |
| 115791 | sqlite3BeginBenignMalloc(); |
| 115792 | sqlite3BtreeEnterAll(db); |
| 115793 | for(i=0; i<db->nDb; i++){ |
| 115794 | Btree *p = db->aDb[i].pBt; |
| 115795 | if( p ){ |
| 115796 | if( sqlite3BtreeIsInTrans(p) ){ |
| 115797 | inTrans = 1; |
| 115798 | } |
| 115799 | sqlite3BtreeRollback(p, tripCode); |
| 115800 | db->aDb[i].inTrans = 0; |
| 115801 | } |
| 115802 | } |
| 115803 | sqlite3VtabRollback(db); |
| 115804 | sqlite3EndBenignMalloc(); |
| 115805 | |
| @@ -117562,12 +117734,10 @@ | |
| 117562 | /* |
| 117563 | ** Test to see whether or not the database connection is in autocommit |
| 117564 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 117565 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 117566 | ** by the next COMMIT or ROLLBACK. |
| 117567 | ** |
| 117568 | ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** |
| 117569 | */ |
| 117570 | SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 117571 | return db->autoCommit; |
| 117572 | } |
| 117573 | |
| @@ -119051,10 +119221,22 @@ | |
| 119051 | |
| 119052 | #endif /* _FTS3_HASH_H_ */ |
| 119053 | |
| 119054 | /************** End of fts3_hash.h *******************************************/ |
| 119055 | /************** Continuing where we left off in fts3Int.h ********************/ |
| 119056 | |
| 119057 | /* |
| 119058 | ** This constant controls how often segments are merged. Once there are |
| 119059 | ** FTS3_MERGE_COUNT segments of level N, they are merged into a single |
| 119060 | ** segment of level N+1. |
| @@ -120709,11 +120891,11 @@ | |
| 120709 | /* By default use a full table scan. This is an expensive option, |
| 120710 | ** so search through the constraints to see if a more efficient |
| 120711 | ** strategy is possible. |
| 120712 | */ |
| 120713 | pInfo->idxNum = FTS3_FULLSCAN_SEARCH; |
| 120714 | pInfo->estimatedCost = 500000; |
| 120715 | for(i=0; i<pInfo->nConstraint; i++){ |
| 120716 | struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i]; |
| 120717 | if( pCons->usable==0 ) continue; |
| 120718 | |
| 120719 | /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */ |
| @@ -122270,15 +122452,11 @@ | |
| 122270 | ); |
| 122271 | if( rc!=SQLITE_OK ){ |
| 122272 | return rc; |
| 122273 | } |
| 122274 | |
| 122275 | rc = sqlite3Fts3ReadLock(p); |
| 122276 | if( rc!=SQLITE_OK ) return rc; |
| 122277 | |
| 122278 | rc = fts3EvalStart(pCsr); |
| 122279 | |
| 122280 | sqlite3Fts3SegmentsClose(p); |
| 122281 | if( rc!=SQLITE_OK ) return rc; |
| 122282 | pCsr->pNextId = pCsr->aDoclist; |
| 122283 | pCsr->iPrevId = 0; |
| 122284 | } |
| @@ -126129,30 +126307,30 @@ | |
| 126129 | int iDefaultCol, /* Default column to query */ |
| 126130 | const char *z, int n, /* Text of MATCH query */ |
| 126131 | Fts3Expr **ppExpr, /* OUT: Parsed query structure */ |
| 126132 | char **pzErr /* OUT: Error message (sqlite3_malloc) */ |
| 126133 | ){ |
| 126134 | static const int MAX_EXPR_DEPTH = 12; |
| 126135 | int rc = fts3ExprParseUnbalanced( |
| 126136 | pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr |
| 126137 | ); |
| 126138 | |
| 126139 | /* Rebalance the expression. And check that its depth does not exceed |
| 126140 | ** MAX_EXPR_DEPTH. */ |
| 126141 | if( rc==SQLITE_OK && *ppExpr ){ |
| 126142 | rc = fts3ExprBalance(ppExpr, MAX_EXPR_DEPTH); |
| 126143 | if( rc==SQLITE_OK ){ |
| 126144 | rc = fts3ExprCheckDepth(*ppExpr, MAX_EXPR_DEPTH); |
| 126145 | } |
| 126146 | } |
| 126147 | |
| 126148 | if( rc!=SQLITE_OK ){ |
| 126149 | sqlite3Fts3ExprFree(*ppExpr); |
| 126150 | *ppExpr = 0; |
| 126151 | if( rc==SQLITE_TOOBIG ){ |
| 126152 | *pzErr = sqlite3_mprintf( |
| 126153 | "FTS expression tree is too large (maximum depth %d)", MAX_EXPR_DEPTH |
| 126154 | ); |
| 126155 | rc = SQLITE_ERROR; |
| 126156 | }else if( rc==SQLITE_ERROR ){ |
| 126157 | *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z); |
| 126158 | } |
| @@ -129110,41 +129288,34 @@ | |
| 129110 | *pRC = rc; |
| 129111 | } |
| 129112 | |
| 129113 | |
| 129114 | /* |
| 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); |
| 129139 | if( rc==SQLITE_OK ){ |
| 129140 | sqlite3_bind_null(pStmt, 1); |
| 129141 | sqlite3_step(pStmt); |
| 129142 | rc = sqlite3_reset(pStmt); |
| 129143 | } |
| 129144 | }else{ |
| 129145 | rc = SQLITE_OK; |
| 129146 | } |
| 129147 | |
| 129148 | return rc; |
| 129149 | } |
| 129150 | |
| @@ -133918,10 +134089,13 @@ | |
| 133918 | goto update_out; |
| 133919 | } |
| 133920 | aSzIns = &aSzDel[p->nColumn+1]; |
| 133921 | memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2); |
| 133922 | |
| 133923 | /* If this is an INSERT operation, or an UPDATE that modifies the rowid |
| 133924 | ** value, then this operation requires constraint handling. |
| 133925 | ** |
| 133926 | ** If the on-conflict mode is REPLACE, this means that the existing row |
| 133927 | ** should be deleted from the database before inserting the new row. Or, |
| @@ -136051,32 +136225,31 @@ | |
| 136051 | 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004, |
| 136052 | 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002, |
| 136053 | 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803, |
| 136054 | 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07, |
| 136055 | 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, |
| 136078 | }; |
| 136079 | static const unsigned int aAscii[4] = { |
| 136080 | 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001, |
| 136081 | }; |
| 136082 | |
| @@ -139705,11 +139878,11 @@ | |
| 139705 | ** operator) using the ICU uregex_XX() APIs. |
| 139706 | ** |
| 139707 | ** * Implementations of the SQL scalar upper() and lower() functions |
| 139708 | ** for case mapping. |
| 139709 | ** |
| 139710 | ** * Integration of ICU and SQLite collation seqences. |
| 139711 | ** |
| 139712 | ** * An implementation of the LIKE operator that uses ICU to |
| 139713 | ** provide case-independent matching. |
| 139714 | */ |
| 139715 | |
| 139716 |
| --- src/sqlite3.c | |
| +++ src/sqlite3.c | |
| @@ -352,11 +352,11 @@ | |
| 352 | |
| 353 | /* |
| 354 | ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. |
| 355 | ** 0 means mutexes are permanently disable and the library is never |
| 356 | ** threadsafe. 1 means the library is serialized which is the highest |
| 357 | ** level of threadsafety. 2 means the library is multithreaded - multiple |
| 358 | ** threads can use SQLite as long as no two threads try to use the same |
| 359 | ** database connection at the same time. |
| 360 | ** |
| 361 | ** Older versions of SQLite used an optional THREADSAFE macro. |
| 362 | ** We support that for legacy. |
| @@ -431,24 +431,16 @@ | |
| 431 | # define SQLITE_MALLOC_SOFT_LIMIT 1024 |
| 432 | #endif |
| 433 | |
| 434 | /* |
| 435 | ** We need to define _XOPEN_SOURCE as follows in order to enable |
| 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. |
| 439 | */ |
| 440 | #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) |
| 441 | # define _XOPEN_SOURCE 600 |
| 442 | #endif |
| 443 | |
| 444 | /* |
| 445 | ** The TCL headers are only needed when compiling the TCL bindings. |
| 446 | */ |
| @@ -678,11 +670,11 @@ | |
| 670 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 671 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 672 | */ |
| 673 | #define SQLITE_VERSION "3.7.17" |
| 674 | #define SQLITE_VERSION_NUMBER 3007017 |
| 675 | #define SQLITE_SOURCE_ID "2013-06-14 02:51:48 b920bb70bb009b7c54e7667544c9810c5ee25e19" |
| 676 | |
| 677 | /* |
| 678 | ** CAPI3REF: Run-Time Library Version Numbers |
| 679 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 680 | ** |
| @@ -5087,10 +5079,15 @@ | |
| 5079 | */ |
| 5080 | SQLITE_API int sqlite3_key( |
| 5081 | sqlite3 *db, /* Database to be rekeyed */ |
| 5082 | const void *pKey, int nKey /* The key */ |
| 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 | ); |
| 5089 | |
| 5090 | /* |
| 5091 | ** Change the key on an open database. If the current database is not |
| 5092 | ** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the |
| 5093 | ** database is decrypted. |
| @@ -5100,10 +5097,15 @@ | |
| 5097 | */ |
| 5098 | SQLITE_API int sqlite3_rekey( |
| 5099 | sqlite3 *db, /* Database to be rekeyed */ |
| 5100 | const void *pKey, int nKey /* The new key */ |
| 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 | ); |
| 5107 | |
| 5108 | /* |
| 5109 | ** Specify the activation key for a SEE database. Unless |
| 5110 | ** activated, none of the SEE routines will work. |
| 5111 | */ |
| @@ -8151,10 +8153,16 @@ | |
| 8153 | */ |
| 8154 | #ifndef offsetof |
| 8155 | #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) |
| 8156 | #endif |
| 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 | |
| 8164 | /* |
| 8165 | ** Check to see if this machine uses EBCDIC. (Yes, believe it or |
| 8166 | ** not, there are still machines out there that use EBCDIC.) |
| 8167 | */ |
| 8168 | #if 'A' == '\301' |
| @@ -8476,13 +8484,11 @@ | |
| 8484 | typedef struct TriggerStep TriggerStep; |
| 8485 | typedef struct UnpackedRecord UnpackedRecord; |
| 8486 | typedef struct VTable VTable; |
| 8487 | typedef struct VtabCtx VtabCtx; |
| 8488 | typedef struct Walker Walker; |
| 8489 | typedef struct WhereInfo WhereInfo; |
| 8490 | |
| 8491 | /* |
| 8492 | ** Defer sourcing vdbe.h and btree.h until after the "u8" and |
| 8493 | ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque |
| 8494 | ** pointer types (i.e. FuncDef) defined above. |
| @@ -9915,11 +9921,10 @@ | |
| 9921 | ** databases may be attached. |
| 9922 | */ |
| 9923 | struct Db { |
| 9924 | char *zName; /* Name of this database */ |
| 9925 | Btree *pBt; /* The B*Tree structure for this database file */ |
| 9926 | u8 safety_level; /* How aggressive at syncing data to disk */ |
| 9927 | Schema *pSchema; /* Pointer to database schema (possibly shared) */ |
| 9928 | }; |
| 9929 | |
| 9930 | /* |
| @@ -10713,10 +10718,11 @@ | |
| 10718 | int tnum; /* DB Page containing root of this index */ |
| 10719 | u16 nColumn; /* Number of columns in table used by this index */ |
| 10720 | u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 10721 | unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ |
| 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 */ |
| 10724 | #ifdef SQLITE_ENABLE_STAT3 |
| 10725 | int nSample; /* Number of elements in aSample[] */ |
| 10726 | tRowcnt avgEq; /* Average nEq value for key values not in aSample */ |
| 10727 | IndexSample *aSample; /* Samples of the left-most key */ |
| 10728 | #endif |
| @@ -11058,10 +11064,15 @@ | |
| 11064 | /* |
| 11065 | ** The number of bits in a Bitmask. "BMS" means "BitMask Size". |
| 11066 | */ |
| 11067 | #define BMS ((int)(sizeof(Bitmask)*8)) |
| 11068 | |
| 11069 | /* |
| 11070 | ** A bit in a Bitmask |
| 11071 | */ |
| 11072 | #define MASKBIT(n) (((Bitmask)1)<<(n)) |
| 11073 | |
| 11074 | /* |
| 11075 | ** The following structure describes the FROM clause of a SELECT statement. |
| 11076 | ** Each table or subquery in the FROM clause is a separate element of |
| 11077 | ** the SrcList.a[] array. |
| 11078 | ** |
| @@ -11078,12 +11089,12 @@ | |
| 11089 | ** |
| 11090 | ** In the colUsed field, the high-order bit (bit 63) is set if the table |
| 11091 | ** contains more than 63 columns and the 64-th or later column is used. |
| 11092 | */ |
| 11093 | struct SrcList { |
| 11094 | u8 nSrc; /* Number of tables or subqueries in the FROM clause */ |
| 11095 | u8 nAlloc; /* Number of entries allocated in a[] below */ |
| 11096 | struct SrcList_item { |
| 11097 | Schema *pSchema; /* Schema to which this item is fixed */ |
| 11098 | char *zDatabase; /* Name of database holding this table */ |
| 11099 | char *zName; /* Name of the table */ |
| 11100 | char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ |
| @@ -11117,83 +11128,10 @@ | |
| 11128 | #define JT_RIGHT 0x0010 /* Right outer join */ |
| 11129 | #define JT_OUTER 0x0020 /* The "OUTER" keyword is present */ |
| 11130 | #define JT_ERROR 0x0040 /* unknown or unsupported join type */ |
| 11131 | |
| 11132 | |
| 11133 | /* |
| 11134 | ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin() |
| 11135 | ** and the WhereInfo.wctrlFlags member. |
| 11136 | */ |
| 11137 | #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */ |
| @@ -11203,37 +11141,15 @@ | |
| 11141 | #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ |
| 11142 | #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ |
| 11143 | #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ |
| 11144 | #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ |
| 11145 | #define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */ |
| 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 | */ |
| 11151 | #define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */ |
| 11152 | #define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */ |
| 11153 | #define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */ |
| 11154 | #define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */ |
| 11155 | |
| @@ -11303,11 +11219,11 @@ | |
| 11219 | ExprList *pEList; /* The fields of the result */ |
| 11220 | u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ |
| 11221 | u16 selFlags; /* Various SF_* values */ |
| 11222 | int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ |
| 11223 | int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */ |
| 11224 | u64 nSelectRow; /* Estimated number of result rows */ |
| 11225 | SrcList *pSrc; /* The FROM clause */ |
| 11226 | Expr *pWhere; /* The WHERE clause */ |
| 11227 | ExprList *pGroupBy; /* The GROUP BY clause */ |
| 11228 | Expr *pHaving; /* The HAVING clause */ |
| 11229 | ExprList *pOrderBy; /* The ORDER BY clause */ |
| @@ -11487,11 +11403,11 @@ | |
| 11403 | AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ |
| 11404 | |
| 11405 | /* Information used while coding trigger programs. */ |
| 11406 | Parse *pToplevel; /* Parse structure for main program (or NULL) */ |
| 11407 | Table *pTriggerTab; /* Table triggers are being coded for */ |
| 11408 | u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ |
| 11409 | u32 oldmask; /* Mask of old.* columns referenced */ |
| 11410 | u32 newmask; /* Mask of new.* columns referenced */ |
| 11411 | u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ |
| 11412 | u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ |
| 11413 | u8 disableTriggers; /* True to disable triggers */ |
| @@ -12057,10 +11973,16 @@ | |
| 11973 | #endif |
| 11974 | SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*); |
| 11975 | SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int); |
| 11976 | SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); |
| 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*); |
| 11984 | SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8); |
| 11985 | SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int); |
| 11986 | SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int); |
| 11987 | SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int); |
| 11988 | SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*); |
| @@ -19960,17 +19882,11 @@ | |
| 19882 | if( flag_plussign ) prefix = '+'; |
| 19883 | else if( flag_blanksign ) prefix = ' '; |
| 19884 | else prefix = 0; |
| 19885 | } |
| 19886 | if( xtype==etGENERIC && precision>0 ) precision--; |
| 19887 | for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} |
| 19888 | if( xtype==etFLOAT ) realvalue += rounder; |
| 19889 | /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 19890 | exp = 0; |
| 19891 | if( sqlite3IsNaN((double)realvalue) ){ |
| 19892 | bufpt = "NaN"; |
| @@ -26866,19 +26782,23 @@ | |
| 26782 | } |
| 26783 | return SQLITE_OK; |
| 26784 | } |
| 26785 | case SQLITE_FCNTL_MMAP_SIZE: { |
| 26786 | i64 newLimit = *(i64*)pArg; |
| 26787 | int rc = SQLITE_OK; |
| 26788 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 26789 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 26790 | } |
| 26791 | *(i64*)pArg = pFile->mmapSizeMax; |
| 26792 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
| 26793 | pFile->mmapSizeMax = newLimit; |
| 26794 | if( pFile->mmapSize>0 ){ |
| 26795 | unixUnmapfile(pFile); |
| 26796 | rc = unixMapfile(pFile, -1); |
| 26797 | } |
| 26798 | } |
| 26799 | return rc; |
| 26800 | } |
| 26801 | #ifdef SQLITE_DEBUG |
| 26802 | /* The pager calls this method to signal that it has done |
| 26803 | ** a rollback and that the database is therefore unchanged and |
| 26804 | ** it hence it is OK for the transaction change counter to be |
| @@ -28244,11 +28164,11 @@ | |
| 28164 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
| 28165 | pNew->h = h; |
| 28166 | pNew->pVfs = pVfs; |
| 28167 | pNew->zPath = zFilename; |
| 28168 | pNew->ctrlFlags = (u8)ctrlFlags; |
| 28169 | pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
| 28170 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 28171 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 28172 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
| 28173 | } |
| 28174 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
| @@ -30799,17 +30719,10 @@ | |
| 30719 | ** This file mapping API is common to both Win32 and WinRT. |
| 30720 | */ |
| 30721 | WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
| 30722 | #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */ |
| 30723 | |
| 30724 | /* |
| 30725 | ** Some Microsoft compilers lack this definition. |
| 30726 | */ |
| 30727 | #ifndef INVALID_FILE_ATTRIBUTES |
| 30728 | # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
| @@ -33531,10 +33444,13 @@ | |
| 33444 | } |
| 33445 | } |
| 33446 | |
| 33447 | /* Forward declaration */ |
| 33448 | static int getTempname(int nBuf, char *zBuf); |
| 33449 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 33450 | static int winMapfile(winFile*, sqlite3_int64); |
| 33451 | #endif |
| 33452 | |
| 33453 | /* |
| 33454 | ** Control and query of the open file handle. |
| 33455 | */ |
| 33456 | static int winFileControl(sqlite3_file *id, int op, void *pArg){ |
| @@ -33614,17 +33530,24 @@ | |
| 33530 | return SQLITE_OK; |
| 33531 | } |
| 33532 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 33533 | case SQLITE_FCNTL_MMAP_SIZE: { |
| 33534 | i64 newLimit = *(i64*)pArg; |
| 33535 | int rc = SQLITE_OK; |
| 33536 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 33537 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 33538 | } |
| 33539 | *(i64*)pArg = pFile->mmapSizeMax; |
| 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; |
| 33549 | } |
| 33550 | #endif |
| 33551 | } |
| 33552 | OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h)); |
| 33553 | return SQLITE_NOTFOUND; |
| @@ -33652,19 +33575,19 @@ | |
| 33575 | winFile *p = (winFile*)id; |
| 33576 | return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | |
| 33577 | ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0); |
| 33578 | } |
| 33579 | |
| 33580 | /* |
| 33581 | ** Windows will only let you create file view mappings |
| 33582 | ** on allocation size granularity boundaries. |
| 33583 | ** During sqlite3_os_init() we do a GetSystemInfo() |
| 33584 | ** to get the granularity size. |
| 33585 | */ |
| 33586 | SYSTEM_INFO winSysInfo; |
| 33587 | |
| 33588 | #ifndef SQLITE_OMIT_WAL |
| 33589 | |
| 33590 | /* |
| 33591 | ** Helper functions to obtain and relinquish the global mutex. The |
| 33592 | ** global mutex is used to protect the winLockInfo objects used by |
| 33593 | ** this file, all of which may be shared by multiple threads. |
| @@ -34961,11 +34884,11 @@ | |
| 34884 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 34885 | pFile->hMap = NULL; |
| 34886 | pFile->pMapRegion = 0; |
| 34887 | pFile->mmapSize = 0; |
| 34888 | pFile->mmapSizeActual = 0; |
| 34889 | pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
| 34890 | #endif |
| 34891 | |
| 34892 | OpenCounter(+1); |
| 34893 | return rc; |
| 34894 | } |
| @@ -37220,11 +37143,11 @@ | |
| 37143 | PCache1 *pCache; /* The newly created page cache */ |
| 37144 | PGroup *pGroup; /* The group the new page cache will belong to */ |
| 37145 | int sz; /* Bytes of memory required to allocate the new cache */ |
| 37146 | |
| 37147 | /* |
| 37148 | ** The separateCache variable is true if each PCache has its own private |
| 37149 | ** PGroup. In other words, separateCache is true for mode (1) where no |
| 37150 | ** mutexing is required. |
| 37151 | ** |
| 37152 | ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT |
| 37153 | ** |
| @@ -42544,11 +42467,12 @@ | |
| 42467 | /* Before the first write, give the VFS a hint of what the final |
| 42468 | ** file size will be. |
| 42469 | */ |
| 42470 | assert( rc!=SQLITE_OK || isOpen(pPager->fd) ); |
| 42471 | if( rc==SQLITE_OK |
| 42472 | && pPager->dbHintSize<pPager->dbSize |
| 42473 | && (pList->pDirty || pList->pgno>pPager->dbHintSize) |
| 42474 | ){ |
| 42475 | sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize; |
| 42476 | sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile); |
| 42477 | pPager->dbHintSize = pPager->dbSize; |
| 42478 | } |
| @@ -43509,11 +43433,11 @@ | |
| 43433 | ** requested page is not already stored in the cache, then no |
| 43434 | ** actual disk read occurs. In this case the memory image of the |
| 43435 | ** page is initialized to all zeros. |
| 43436 | ** |
| 43437 | ** If noContent is true, it means that we do not care about the contents |
| 43438 | ** of the page. This occurs in two scenarios: |
| 43439 | ** |
| 43440 | ** a) When reading a free-list leaf page from the database, and |
| 43441 | ** |
| 43442 | ** b) When a savepoint is being rolled back and we need to load |
| 43443 | ** a new page into the cache to be filled with the data read |
| @@ -44919,11 +44843,31 @@ | |
| 44843 | pagerReportSize(pPager); |
| 44844 | } |
| 44845 | SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){ |
| 44846 | return pPager->pCodec; |
| 44847 | } |
| 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 */ |
| 44869 | |
| 44870 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 44871 | /* |
| 44872 | ** Move the page pPg to location pgno in the file. |
| 44873 | ** |
| @@ -45474,25 +45418,10 @@ | |
| 45418 | assert( pPager->eState==PAGER_READER ); |
| 45419 | return sqlite3WalFramesize(pPager->pWal); |
| 45420 | } |
| 45421 | #endif |
| 45422 | |
| 45423 | #endif /* SQLITE_OMIT_DISKIO */ |
| 45424 | |
| 45425 | /************** End of pager.c ***********************************************/ |
| 45426 | /************** Begin file wal.c *********************************************/ |
| 45427 | /* |
| @@ -50759,11 +50688,11 @@ | |
| 50688 | if( rc ) return rc; |
| 50689 | top = get2byteNotZero(&data[hdr+5]); |
| 50690 | }else if( gap+2<=top ){ |
| 50691 | /* Search the freelist looking for a free slot big enough to satisfy |
| 50692 | ** the request. The allocation is made from the first free slot in |
| 50693 | ** the list that is large enough to accommodate it. |
| 50694 | */ |
| 50695 | int pc, addr; |
| 50696 | for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ |
| 50697 | int size; /* Size of the free slot */ |
| 50698 | if( pc>usableSize-4 || pc<addr+4 ){ |
| @@ -52702,11 +52631,11 @@ | |
| 52631 | return rc; |
| 52632 | } |
| 52633 | |
| 52634 | /* |
| 52635 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
| 52636 | ** is committed for an auto-vacuum database. |
| 52637 | ** |
| 52638 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 52639 | ** the database file should be truncated to during the commit process. |
| 52640 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 52641 | ** pages are in use. |
| @@ -58045,16 +57974,10 @@ | |
| 57974 | ************************************************************************* |
| 57975 | ** This file contains the implementation of the sqlite3_backup_XXX() |
| 57976 | ** API functions and the related features. |
| 57977 | */ |
| 57978 | |
| 57979 | /* |
| 57980 | ** Structure allocated for each backup operation. |
| 57981 | */ |
| 57982 | struct sqlite3_backup { |
| 57983 | sqlite3* pDestDb; /* Destination database handle */ |
| @@ -61942,11 +61865,11 @@ | |
| 61865 | /* |
| 61866 | ** If the Vdbe passed as the first argument opened a statement-transaction, |
| 61867 | ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or |
| 61868 | ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement |
| 61869 | ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the |
| 61870 | ** statement transaction is committed. |
| 61871 | ** |
| 61872 | ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. |
| 61873 | ** Otherwise SQLITE_OK. |
| 61874 | */ |
| 61875 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
| @@ -64011,17 +63934,10 @@ | |
| 63934 | int iType = sqlite3_value_type( columnMem(pStmt,i) ); |
| 63935 | columnMallocFailure(pStmt); |
| 63936 | return iType; |
| 63937 | } |
| 63938 | |
| 63939 | /* |
| 63940 | ** Convert the N-th element of pStmt->pColName[] into a string using |
| 63941 | ** xFunc() then return that string. If N is out of range, return 0. |
| 63942 | ** |
| 63943 | ** There are up to 5 names for each column. useType determines which |
| @@ -64643,18 +64559,18 @@ | |
| 64559 | pVar = &utf8; |
| 64560 | } |
| 64561 | #endif |
| 64562 | nOut = pVar->n; |
| 64563 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 64564 | if( nOut>SQLITE_TRACE_SIZE_LIMIT ){ |
| 64565 | nOut = SQLITE_TRACE_SIZE_LIMIT; |
| 64566 | while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; } |
| 64567 | } |
| 64568 | #endif |
| 64569 | sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z); |
| 64570 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 64571 | if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut); |
| 64572 | #endif |
| 64573 | #ifndef SQLITE_OMIT_UTF16 |
| 64574 | if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8); |
| 64575 | #endif |
| 64576 | }else if( pVar->flags & MEM_Zero ){ |
| @@ -64670,11 +64586,11 @@ | |
| 64586 | for(i=0; i<nOut; i++){ |
| 64587 | sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff); |
| 64588 | } |
| 64589 | sqlite3StrAccumAppend(&out, "'", 1); |
| 64590 | #ifdef SQLITE_TRACE_SIZE_LIMIT |
| 64591 | if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut); |
| 64592 | #endif |
| 64593 | } |
| 64594 | } |
| 64595 | } |
| 64596 | return sqlite3StrAccumFinish(&out); |
| @@ -68269,12 +68185,12 @@ | |
| 68185 | ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is |
| 68186 | ** obtained on the database file when a write-transaction is started. No |
| 68187 | ** other process can start another write transaction while this transaction is |
| 68188 | ** underway. Starting a write transaction also creates a rollback journal. A |
| 68189 | ** write transaction must be started before any changes can be made to the |
| 68190 | ** database. If P2 is greater than or equal to 2 then an EXCLUSIVE lock is |
| 68191 | ** also obtained on the file. |
| 68192 | ** |
| 68193 | ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is |
| 68194 | ** true (this flag is set if the Vdbe may modify more than one row and may |
| 68195 | ** throw an ABORT exception), a statement transaction may also be opened. |
| 68196 | ** More specifically, a statement transaction is opened iff the database |
| @@ -72224,11 +72140,11 @@ | |
| 72140 | ** |
| 72141 | ** For the purposes of this comparison, EOF is considered greater than any |
| 72142 | ** other key value. If the keys are equal (only possible with two EOF |
| 72143 | ** values), it doesn't matter which index is stored. |
| 72144 | ** |
| 72145 | ** The (N/4) elements of aTree[] that precede the final (N/2) described |
| 72146 | ** above contains the index of the smallest of each block of 4 iterators. |
| 72147 | ** And so on. So that aTree[1] contains the index of the iterator that |
| 72148 | ** currently points to the smallest key value. aTree[0] is unused. |
| 72149 | ** |
| 72150 | ** Example: |
| @@ -73499,16 +73415,10 @@ | |
| 73415 | ** a power-of-two allocation. This mimimizes wasted space in power-of-two |
| 73416 | ** memory allocators. |
| 73417 | */ |
| 73418 | #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*))) |
| 73419 | |
| 73420 | /* |
| 73421 | ** The rollback journal is composed of a linked list of these structures. |
| 73422 | */ |
| 73423 | struct FileChunk { |
| 73424 | FileChunk *pNext; /* Next chunk in the journal */ |
| @@ -75045,12 +74955,12 @@ | |
| 74955 | ** |
| 74956 | ** Minor point: If this is the case, then the expression will be |
| 74957 | ** re-evaluated for each reference to it. |
| 74958 | */ |
| 74959 | sNC.pEList = p->pEList; |
| 74960 | sNC.ncFlags |= NC_AsMaybe; |
| 74961 | if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; |
| 74962 | if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort; |
| 74963 | sNC.ncFlags &= ~NC_AsMaybe; |
| 74964 | |
| 74965 | /* The ORDER BY and GROUP BY clauses may not refer to terms in |
| 74966 | ** outer queries |
| @@ -76817,19 +76727,19 @@ | |
| 76727 | |
| 76728 | if( eType==0 ){ |
| 76729 | /* Could not found an existing table or index to use as the RHS b-tree. |
| 76730 | ** We will have to generate an ephemeral table to do the job. |
| 76731 | */ |
| 76732 | u32 savedNQueryLoop = pParse->nQueryLoop; |
| 76733 | int rMayHaveNull = 0; |
| 76734 | eType = IN_INDEX_EPH; |
| 76735 | if( prNotFound ){ |
| 76736 | *prNotFound = rMayHaveNull = ++pParse->nMem; |
| 76737 | sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound); |
| 76738 | }else{ |
| 76739 | testcase( pParse->nQueryLoop>1 ); |
| 76740 | pParse->nQueryLoop = 1; |
| 76741 | if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){ |
| 76742 | eType = IN_INDEX_ROWID; |
| 76743 | } |
| 76744 | } |
| 76745 | sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); |
| @@ -76867,11 +76777,11 @@ | |
| 76777 | ** the register given by rMayHaveNull to NULL. Calling routines will take |
| 76778 | ** care of changing this register value to non-NULL if the RHS is NULL-free. |
| 76779 | ** |
| 76780 | ** If rMayHaveNull is zero, that means that the subquery is being used |
| 76781 | ** for membership testing only. There is no need to initialize any |
| 76782 | ** registers to indicate the presence or absence of NULLs on the RHS. |
| 76783 | ** |
| 76784 | ** For a SELECT or EXISTS operator, return the register that holds the |
| 76785 | ** result. For IN operators or if an error occurs, the return value is 0. |
| 76786 | */ |
| 76787 | #ifndef SQLITE_OMIT_SUBQUERY |
| @@ -80267,11 +80177,11 @@ | |
| 80177 | ** |
| 80178 | ** Additional tables might be added in future releases of SQLite. |
| 80179 | ** The sqlite_stat2 table is not created or used unless the SQLite version |
| 80180 | ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled |
| 80181 | ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. |
| 80182 | ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only |
| 80183 | ** created and used by SQLite versions 3.7.9 and later and with |
| 80184 | ** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3 |
| 80185 | ** is a superset of sqlite_stat2. |
| 80186 | ** |
| 80187 | ** Format of sqlite_stat1: |
| @@ -83455,10 +83365,11 @@ | |
| 83365 | zColl = sqlite3NameFromToken(db, pToken); |
| 83366 | if( !zColl ) return; |
| 83367 | |
| 83368 | if( sqlite3LocateCollSeq(pParse, zColl) ){ |
| 83369 | Index *pIdx; |
| 83370 | sqlite3DbFree(db, p->aCol[i].zColl); |
| 83371 | p->aCol[i].zColl = zColl; |
| 83372 | |
| 83373 | /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
| 83374 | ** then an index may have been created on this column before the |
| 83375 | ** collation type was added. Correct this if it is the case. |
| @@ -84874,10 +84785,11 @@ | |
| 84785 | zExtra = (char *)(&pIndex->zName[nName+1]); |
| 84786 | memcpy(pIndex->zName, zName, nName+1); |
| 84787 | pIndex->pTable = pTab; |
| 84788 | pIndex->nColumn = pList->nExpr; |
| 84789 | pIndex->onError = (u8)onError; |
| 84790 | pIndex->uniqNotNull = onError==OE_Abort; |
| 84791 | pIndex->autoIndex = (u8)(pName==0); |
| 84792 | pIndex->pSchema = db->aDb[iDb].pSchema; |
| 84793 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 84794 | |
| 84795 | /* Check to see if we should honor DESC requests on index columns |
| @@ -84932,10 +84844,11 @@ | |
| 84844 | goto exit_create_index; |
| 84845 | } |
| 84846 | pIndex->azColl[i] = zColl; |
| 84847 | requestedSortOrder = pListItem->sortOrder & sortOrderMask; |
| 84848 | pIndex->aSortOrder[i] = (u8)requestedSortOrder; |
| 84849 | if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0; |
| 84850 | } |
| 84851 | sqlite3DefaultRowEst(pIndex); |
| 84852 | |
| 84853 | if( pTab==pParse->pNewTable ){ |
| 84854 | /* This routine has been called to create an automatic index as a |
| @@ -87378,11 +87291,11 @@ | |
| 87291 | ** of x. If x is text, then we actually count UTF-8 characters. |
| 87292 | ** If x is a blob, then we count bytes. |
| 87293 | ** |
| 87294 | ** If p1 is negative, then we begin abs(p1) from the end of x[]. |
| 87295 | ** |
| 87296 | ** If p2 is negative, return the p2 characters preceding p1. |
| 87297 | */ |
| 87298 | static void substrFunc( |
| 87299 | sqlite3_context *context, |
| 87300 | int argc, |
| 87301 | sqlite3_value **argv |
| @@ -88037,14 +87950,10 @@ | |
| 87950 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 87951 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 87952 | }; |
| 87953 | |
| 87954 | /* |
| 87955 | ** Implementation of the QUOTE() function. This function takes a single |
| 87956 | ** argument. If the argument is numeric, the return value is the same as |
| 87957 | ** the argument. If the argument is NULL, the return value is the string |
| 87958 | ** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 87959 | ** single-quote escapes. |
| @@ -88229,11 +88138,11 @@ | |
| 88138 | } |
| 88139 | |
| 88140 | /* |
| 88141 | ** The replace() function. Three arguments are all strings: call |
| 88142 | ** them A, B, and C. The result is also a string which is derived |
| 88143 | ** from A by replacing every occurrence of B with C. The match |
| 88144 | ** must be exact. Collating sequences are not used. |
| 88145 | */ |
| 88146 | static void replaceFunc( |
| 88147 | sqlite3_context *context, |
| 88148 | int argc, |
| @@ -94147,15 +94056,19 @@ | |
| 94056 | sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz); |
| 94057 | } |
| 94058 | } |
| 94059 | } |
| 94060 | sz = -1; |
| 94061 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz); |
| 94062 | #if SQLITE_MAX_MMAP_SIZE==0 |
| 94063 | sz = 0; |
| 94064 | #endif |
| 94065 | if( rc==SQLITE_OK ){ |
| 94066 | returnSingleInt(pParse, "mmap_size", sz); |
| 94067 | }else if( rc!=SQLITE_NOTFOUND ){ |
| 94068 | pParse->nErr++; |
| 94069 | pParse->rc = rc; |
| 94070 | } |
| 94071 | }else |
| 94072 | |
| 94073 | /* |
| 94074 | ** PRAGMA temp_store |
| @@ -94682,11 +94595,11 @@ | |
| 94595 | #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX |
| 94596 | # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 |
| 94597 | #endif |
| 94598 | |
| 94599 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
| 94600 | /* Pragma "quick_check" is reduced version of |
| 94601 | ** integrity_check designed to detect most database corruption |
| 94602 | ** without most of the overhead of a full integrity-check. |
| 94603 | */ |
| 94604 | if( sqlite3StrICmp(zLeft, "integrity_check")==0 |
| 94605 | || sqlite3StrICmp(zLeft, "quick_check")==0 |
| @@ -95140,14 +95053,14 @@ | |
| 95053 | }else |
| 95054 | #endif |
| 95055 | |
| 95056 | #ifdef SQLITE_HAS_CODEC |
| 95057 | if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){ |
| 95058 | sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); |
| 95059 | }else |
| 95060 | if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){ |
| 95061 | sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); |
| 95062 | }else |
| 95063 | if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 || |
| 95064 | sqlite3StrICmp(zLeft, "hexrekey")==0) ){ |
| 95065 | int i, h1, h2; |
| 95066 | char zKey[40]; |
| @@ -95155,13 +95068,13 @@ | |
| 95068 | h1 += 9*(1&(h1>>6)); |
| 95069 | h2 += 9*(1&(h2>>6)); |
| 95070 | zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4); |
| 95071 | } |
| 95072 | if( (zLeft[3] & 0xf)==0xb ){ |
| 95073 | sqlite3_key_v2(db, zDb, zKey, i/2); |
| 95074 | }else{ |
| 95075 | sqlite3_rekey_v2(db, zDb, zKey, i/2); |
| 95076 | } |
| 95077 | }else |
| 95078 | #endif |
| 95079 | #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) |
| 95080 | if( sqlite3StrICmp(zLeft, "activate_extensions")==0 && zRight ){ |
| @@ -95792,11 +95705,11 @@ | |
| 95705 | } |
| 95706 | |
| 95707 | sqlite3VtabUnlockList(db); |
| 95708 | |
| 95709 | pParse->db = db; |
| 95710 | pParse->nQueryLoop = 1; |
| 95711 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
| 95712 | char *zSqlCopy; |
| 95713 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 95714 | testcase( nBytes==mxLen ); |
| 95715 | testcase( nBytes==mxLen+1 ); |
| @@ -95814,11 +95727,11 @@ | |
| 95727 | pParse->zTail = &zSql[nBytes]; |
| 95728 | } |
| 95729 | }else{ |
| 95730 | sqlite3RunParser(pParse, zSql, &zErrMsg); |
| 95731 | } |
| 95732 | assert( 1==pParse->nQueryLoop ); |
| 95733 | |
| 95734 | if( db->mallocFailed ){ |
| 95735 | pParse->rc = SQLITE_NOMEM; |
| 95736 | } |
| 95737 | if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; |
| @@ -96178,11 +96091,11 @@ | |
| 96091 | sqlite3DbFree(db, p); |
| 96092 | } |
| 96093 | } |
| 96094 | |
| 96095 | /* |
| 96096 | ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the |
| 96097 | ** type of join. Return an integer constant that expresses that type |
| 96098 | ** in terms of the following bit values: |
| 96099 | ** |
| 96100 | ** JT_INNER |
| 96101 | ** JT_CROSS |
| @@ -97592,11 +97505,11 @@ | |
| 97505 | int addr1, n; |
| 97506 | if( p->iLimit ) return; |
| 97507 | |
| 97508 | /* |
| 97509 | ** "LIMIT -1" always shows all rows. There is some |
| 97510 | ** controversy about what the correct behavior should be. |
| 97511 | ** The current implementation interprets "LIMIT 0" to mean |
| 97512 | ** no rows. |
| 97513 | */ |
| 97514 | sqlite3ExprCacheClear(pParse); |
| 97515 | assert( p->pOffset==0 || p->pLimit!=0 ); |
| @@ -97608,11 +97521,11 @@ | |
| 97521 | sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); |
| 97522 | VdbeComment((v, "LIMIT counter")); |
| 97523 | if( n==0 ){ |
| 97524 | sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak); |
| 97525 | }else{ |
| 97526 | if( p->nSelectRow > n ) p->nSelectRow = n; |
| 97527 | } |
| 97528 | }else{ |
| 97529 | sqlite3ExprCode(pParse, p->pLimit, iLimit); |
| 97530 | sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); |
| 97531 | VdbeComment((v, "LIMIT counter")); |
| @@ -97802,13 +97715,13 @@ | |
| 97715 | pDelete = p->pPrior; |
| 97716 | p->pPrior = pPrior; |
| 97717 | p->nSelectRow += pPrior->nSelectRow; |
| 97718 | if( pPrior->pLimit |
| 97719 | && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit) |
| 97720 | && p->nSelectRow > nLimit |
| 97721 | ){ |
| 97722 | p->nSelectRow = nLimit; |
| 97723 | } |
| 97724 | if( addr ){ |
| 97725 | sqlite3VdbeJumpHere(v, addr); |
| 97726 | } |
| 97727 | break; |
| @@ -99953,15 +99866,14 @@ | |
| 99866 | Parse *pParse, /* Parse context */ |
| 99867 | Table *pTab, /* Table being queried */ |
| 99868 | Index *pIdx /* Index used to optimize scan, or NULL */ |
| 99869 | ){ |
| 99870 | if( pParse->explain==2 ){ |
| 99871 | char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s", |
| 99872 | pTab->zName, |
| 99873 | pIdx ? " USING COVERING INDEX " : "", |
| 99874 | pIdx ? pIdx->zName : "" |
| 99875 | ); |
| 99876 | sqlite3VdbeAddOp4( |
| 99877 | pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC |
| 99878 | ); |
| 99879 | } |
| @@ -100115,11 +100027,11 @@ | |
| 100027 | } |
| 100028 | continue; |
| 100029 | } |
| 100030 | |
| 100031 | /* Increment Parse.nHeight by the height of the largest expression |
| 100032 | ** tree referred to by this, the parent select. The child select |
| 100033 | ** may contain expression trees of at most |
| 100034 | ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit |
| 100035 | ** more conservative than necessary, but much easier than enforcing |
| 100036 | ** an exact limit. |
| 100037 | */ |
| @@ -100308,11 +100220,11 @@ | |
| 100220 | } |
| 100221 | |
| 100222 | /* Set the limiter. |
| 100223 | */ |
| 100224 | iEnd = sqlite3VdbeMakeLabel(v); |
| 100225 | p->nSelectRow = LARGEST_INT64; |
| 100226 | computeLimitRegisters(pParse, p, iEnd); |
| 100227 | if( p->iLimit==0 && addrSortIndex>=0 ){ |
| 100228 | sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen; |
| 100229 | p->selFlags |= SF_UseSorter; |
| 100230 | } |
| @@ -100336,13 +100248,17 @@ | |
| 100248 | ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0); |
| 100249 | |
| 100250 | /* Begin the database scan. */ |
| 100251 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0); |
| 100252 | if( pWInfo==0 ) goto select_end; |
| 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; |
| 100260 | |
| 100261 | /* If sorting index that was created by a prior OP_OpenEphemeral |
| 100262 | ** instruction ended up not being needed, then change the OP_OpenEphemeral |
| 100263 | ** into an OP_Noop. |
| 100264 | */ |
| @@ -100351,11 +100267,12 @@ | |
| 100267 | p->addrOpenEphm[2] = -1; |
| 100268 | } |
| 100269 | |
| 100270 | /* Use the standard inner loop. */ |
| 100271 | selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest, |
| 100272 | sqlite3WhereContinueLabel(pWInfo), |
| 100273 | sqlite3WhereBreakLabel(pWInfo)); |
| 100274 | |
| 100275 | /* End the database scan loop. |
| 100276 | */ |
| 100277 | sqlite3WhereEnd(pWInfo); |
| 100278 | }else{ |
| @@ -100384,13 +100301,13 @@ | |
| 100301 | pItem->iAlias = 0; |
| 100302 | } |
| 100303 | for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ |
| 100304 | pItem->iAlias = 0; |
| 100305 | } |
| 100306 | if( p->nSelectRow>100 ) p->nSelectRow = 100; |
| 100307 | }else{ |
| 100308 | p->nSelectRow = 1; |
| 100309 | } |
| 100310 | |
| 100311 | |
| 100312 | /* Create a label to jump to when we want to abort the query */ |
| 100313 | addrEnd = sqlite3VdbeMakeLabel(v); |
| @@ -100468,11 +100385,11 @@ | |
| 100385 | ** in the right order to begin with. |
| 100386 | */ |
| 100387 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
| 100388 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0); |
| 100389 | if( pWInfo==0 ) goto select_end; |
| 100390 | if( sqlite3WhereIsOrdered(pWInfo) ){ |
| 100391 | /* The optimizer is able to deliver rows in group by order so |
| 100392 | ** we do not have to sort. The OP_OpenEphemeral table will be |
| 100393 | ** cancelled later because we still need to use the pKeyInfo |
| 100394 | */ |
| 100395 | groupBySort = 0; |
| @@ -100749,12 +100666,12 @@ | |
| 100666 | sqlite3ExprListDelete(db, pDel); |
| 100667 | goto select_end; |
| 100668 | } |
| 100669 | updateAccumulator(pParse, &sAggInfo); |
| 100670 | assert( pMinMax==0 || pMinMax->nExpr==1 ); |
| 100671 | if( sqlite3WhereIsOrdered(pWInfo) ){ |
| 100672 | sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo)); |
| 100673 | VdbeComment((v, "%s() by index", |
| 100674 | (flag==WHERE_ORDERBY_MIN?"min":"max"))); |
| 100675 | } |
| 100676 | sqlite3WhereEnd(pWInfo); |
| 100677 | finalizeAggFunctions(pParse, &sAggInfo); |
| @@ -102109,11 +102026,11 @@ | |
| 102026 | } |
| 102027 | |
| 102028 | /* |
| 102029 | ** This is called to code the required FOR EACH ROW triggers for an operation |
| 102030 | ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE) |
| 102031 | ** is given by the op parameter. The tr_tm parameter determines whether the |
| 102032 | ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then |
| 102033 | ** parameter pChanges is passed the list of columns being modified. |
| 102034 | ** |
| 102035 | ** If there are no triggers that fire at the specified time for the specified |
| 102036 | ** operation on pTab, this function is a no-op. |
| @@ -102560,11 +102477,11 @@ | |
| 102477 | sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); |
| 102478 | pWInfo = sqlite3WhereBegin( |
| 102479 | pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0 |
| 102480 | ); |
| 102481 | if( pWInfo==0 ) goto update_cleanup; |
| 102482 | okOnePass = sqlite3WhereOkOnePass(pWInfo); |
| 102483 | |
| 102484 | /* Remember the rowid of every item to be updated. |
| 102485 | */ |
| 102486 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid); |
| 102487 | if( !okOnePass ){ |
| @@ -104397,22 +104314,165 @@ | |
| 104314 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 104315 | /***/ int sqlite3WhereTrace = 0; |
| 104316 | #endif |
| 104317 | #if defined(SQLITE_DEBUG) \ |
| 104318 | && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
| 104319 | # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X |
| 104320 | # define WHERETRACE_ENABLED 1 |
| 104321 | #else |
| 104322 | # define WHERETRACE(K,X) |
| 104323 | #endif |
| 104324 | |
| 104325 | /* Forward reference |
| 104326 | */ |
| 104327 | typedef struct WhereClause WhereClause; |
| 104328 | typedef struct WhereMaskSet WhereMaskSet; |
| 104329 | typedef struct WhereOrInfo WhereOrInfo; |
| 104330 | typedef struct WhereAndInfo WhereAndInfo; |
| 104331 | typedef struct WhereLevel WhereLevel; |
| 104332 | typedef struct WhereLoop WhereLoop; |
| 104333 | typedef struct WherePath WherePath; |
| 104334 | typedef struct WhereTerm WhereTerm; |
| 104335 | typedef struct WhereLoopBuilder WhereLoopBuilder; |
| 104336 | typedef struct WhereScan WhereScan; |
| 104337 | |
| 104338 | /* |
| 104339 | ** Cost X is tracked as 10*log2(X) stored in a 16-bit integer. The |
| 104340 | ** maximum cost for ordinary tables is 64*(2**63) which becomes 6900. |
| 104341 | ** (Virtual tables can return a larger cost, but let's assume they do not.) |
| 104342 | ** So all costs can be stored in a 16-bit unsigned integer without risk |
| 104343 | ** of overflow. |
| 104344 | ** |
| 104345 | ** Costs are estimates, so don't go to the computational trouble to compute |
| 104346 | ** 10*log2(X) exactly. Instead, a close estimate is used. Any value of |
| 104347 | ** X<=1 is stored as 0. X=2 is 10. X=3 is 16. X=1000 is 99. etc. |
| 104348 | ** |
| 104349 | ** The tool/wherecosttest.c source file implements a command-line program |
| 104350 | ** that will convert between WhereCost to integers and do addition and |
| 104351 | ** multiplication on WhereCost values. That command-line program is a |
| 104352 | ** useful utility to have around when working with this module. |
| 104353 | */ |
| 104354 | typedef unsigned short int WhereCost; |
| 104355 | |
| 104356 | /* |
| 104357 | ** This object contains information needed to implement a single nestd |
| 104358 | ** loop in WHERE clause. |
| 104359 | ** |
| 104360 | ** Contrast this object with WhereLoop. This object describes the |
| 104361 | ** implementation of the loop. WhereLoop describes the algorithm. |
| 104362 | ** This object contains a pointer to the WhereLoop algorithm as one of |
| 104363 | ** its elements. |
| 104364 | ** |
| 104365 | ** The WhereInfo object contains a single instance of this object for |
| 104366 | ** each term in the FROM clause (which is to say, for each of the |
| 104367 | ** nested loops as implemented). The order of WhereLevel objects determines |
| 104368 | ** the loop nested order, with WhereInfo.a[0] being the outer loop and |
| 104369 | ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. |
| 104370 | */ |
| 104371 | struct WhereLevel { |
| 104372 | int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 104373 | int iTabCur; /* The VDBE cursor used to access the table */ |
| 104374 | int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 104375 | int addrBrk; /* Jump here to break out of the loop */ |
| 104376 | int addrNxt; /* Jump here to start the next IN combination */ |
| 104377 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 104378 | int addrFirst; /* First instruction of interior of the loop */ |
| 104379 | u8 iFrom; /* Which entry in the FROM clause */ |
| 104380 | u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ |
| 104381 | int p1, p2; /* Operands of the opcode used to ends the loop */ |
| 104382 | union { /* Information that depends on pWLoop->wsFlags */ |
| 104383 | struct { |
| 104384 | int nIn; /* Number of entries in aInLoop[] */ |
| 104385 | struct InLoop { |
| 104386 | int iCur; /* The VDBE cursor used by this IN operator */ |
| 104387 | int addrInTop; /* Top of the IN loop */ |
| 104388 | u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 104389 | } *aInLoop; /* Information about each nested IN operator */ |
| 104390 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 104391 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 104392 | } u; |
| 104393 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 104394 | }; |
| 104395 | |
| 104396 | /* |
| 104397 | ** Each instance of this object represents an algorithm for evaluating one |
| 104398 | ** term of a join. Every term of the FROM clause will have at least |
| 104399 | ** one corresponding WhereLoop object (unless INDEXED BY constraints |
| 104400 | ** prevent a query solution - which is an error) and many terms of the |
| 104401 | ** FROM clause will have multiple WhereLoop objects, each describing a |
| 104402 | ** potential way of implementing that FROM-clause term, together with |
| 104403 | ** dependencies and cost estimates for using the chosen algorithm. |
| 104404 | ** |
| 104405 | ** Query planning consists of building up a collection of these WhereLoop |
| 104406 | ** objects, then computing a particular sequence of WhereLoop objects, with |
| 104407 | ** one WhereLoop object per FROM clause term, that satisfy all dependencies |
| 104408 | ** and that minimize the overall cost. |
| 104409 | */ |
| 104410 | struct WhereLoop { |
| 104411 | Bitmask prereq; /* Bitmask of other loops that must run first */ |
| 104412 | Bitmask maskSelf; /* Bitmask identifying table iTab */ |
| 104413 | #ifdef SQLITE_DEBUG |
| 104414 | char cId; /* Symbolic ID of this loop for debugging use */ |
| 104415 | #endif |
| 104416 | u8 iTab; /* Position in FROM clause of table for this loop */ |
| 104417 | u8 iSortIdx; /* Sorting index number. 0==None */ |
| 104418 | WhereCost rSetup; /* One-time setup cost (ex: create transient index) */ |
| 104419 | WhereCost rRun; /* Cost of running each loop */ |
| 104420 | WhereCost nOut; /* Estimated number of output rows */ |
| 104421 | union { |
| 104422 | struct { /* Information for internal btree tables */ |
| 104423 | int nEq; /* Number of equality constraints */ |
| 104424 | Index *pIndex; /* Index used, or NULL */ |
| 104425 | } btree; |
| 104426 | struct { /* Information for virtual tables */ |
| 104427 | int idxNum; /* Index number */ |
| 104428 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 104429 | u8 isOrdered; /* True if satisfies ORDER BY */ |
| 104430 | u16 omitMask; /* Terms that may be omitted */ |
| 104431 | char *idxStr; /* Index identifier string */ |
| 104432 | } vtab; |
| 104433 | } u; |
| 104434 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 104435 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 104436 | /**** whereLoopXfer() copies fields above ***********************/ |
| 104437 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 104438 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 104439 | WhereTerm **aLTerm; /* WhereTerms used */ |
| 104440 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 104441 | WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
| 104442 | }; |
| 104443 | |
| 104444 | /* Forward declaration of methods */ |
| 104445 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 104446 | |
| 104447 | /* |
| 104448 | ** Each instance of this object holds a sequence of WhereLoop objects |
| 104449 | ** that implement some or all of a query plan. |
| 104450 | ** |
| 104451 | ** Think of each WhereLoop objects as a node in a graph, which arcs |
| 104452 | ** showing dependences and costs for travelling between nodes. (That is |
| 104453 | ** not a completely accurate description because WhereLoop costs are a |
| 104454 | ** vector, not a scalar, and because dependences are many-to-one, not |
| 104455 | ** one-to-one as are graph nodes. But it is a useful visualization aid.) |
| 104456 | ** Then a WherePath object is a path through the graph that visits some |
| 104457 | ** or all of the WhereLoop objects once. |
| 104458 | ** |
| 104459 | ** The "solver" works by creating the N best WherePath objects of length |
| 104460 | ** 1. Then using those as a basis to compute the N best WherePath objects |
| 104461 | ** of length 2. And so forth until the length of WherePaths equals the |
| 104462 | ** number of nodes in the FROM clause. The best (lowest cost) WherePath |
| 104463 | ** at the end is the choosen query plan. |
| 104464 | */ |
| 104465 | struct WherePath { |
| 104466 | Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
| 104467 | Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
| 104468 | WhereCost nRow; /* Estimated number of rows generated by this path */ |
| 104469 | WhereCost rCost; /* Total cost of this path */ |
| 104470 | u8 isOrdered; /* True if this path satisfies ORDER BY */ |
| 104471 | u8 isOrderedValid; /* True if the isOrdered field is valid */ |
| 104472 | WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
| 104473 | }; |
| 104474 | |
| 104475 | /* |
| 104476 | ** The query generator uses an array of instances of this structure to |
| 104477 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 104478 | ** clause subexpression is separated from the others by AND operators, |
| @@ -104461,11 +104521,10 @@ | |
| 104521 | ** |
| 104522 | ** The number of terms in a join is limited by the number of bits |
| 104523 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 104524 | ** is only able to process joins with 64 or fewer tables. |
| 104525 | */ |
| 104526 | struct WhereTerm { |
| 104527 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
| 104528 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 104529 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
| 104530 | union { |
| @@ -104495,10 +104554,26 @@ | |
| 104554 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 104555 | #else |
| 104556 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 104557 | #endif |
| 104558 | |
| 104559 | /* |
| 104560 | ** An instance of the WhereScan object is used as an iterator for locating |
| 104561 | ** terms in the WHERE clause that are useful to the query planner. |
| 104562 | */ |
| 104563 | struct WhereScan { |
| 104564 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 104565 | WhereClause *pWC; /* WhereClause currently being scanned */ |
| 104566 | char *zCollName; /* Required collating sequence, if not NULL */ |
| 104567 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 104568 | unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 104569 | unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 104570 | u32 opMask; /* Acceptable operators */ |
| 104571 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 104572 | int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ |
| 104573 | }; |
| 104574 | |
| 104575 | /* |
| 104576 | ** An instance of the following structure holds all information about a |
| 104577 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
| 104578 | ** |
| 104579 | ** Explanation of pOuter: For a WHERE clause of the form |
| @@ -104508,15 +104583,13 @@ | |
| 104583 | ** There are separate WhereClause objects for the whole clause and for |
| 104584 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 104585 | ** subclauses points to the WhereClause object for the whole clause. |
| 104586 | */ |
| 104587 | struct WhereClause { |
| 104588 | WhereInfo *pWInfo; /* WHERE clause processing context */ |
| 104589 | WhereClause *pOuter; /* Outer conjunction */ |
| 104590 | u8 op; /* Split operator. TK_AND or TK_OR */ |
| 104591 | int nTerm; /* Number of terms */ |
| 104592 | int nSlot; /* Number of entries in a[] */ |
| 104593 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
| 104594 | #if defined(SQLITE_SMALL_STACK) |
| 104595 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| @@ -104572,23 +104645,59 @@ | |
| 104645 | int n; /* Number of assigned cursor values */ |
| 104646 | int ix[BMS]; /* Cursor assigned to each bit */ |
| 104647 | }; |
| 104648 | |
| 104649 | /* |
| 104650 | ** This object is a convenience wrapper holding all information needed |
| 104651 | ** to construct WhereLoop objects for a particular query. |
| 104652 | */ |
| 104653 | struct WhereLoopBuilder { |
| 104654 | WhereInfo *pWInfo; /* Information about this WHERE */ |
| 104655 | WhereClause *pWC; /* WHERE clause terms */ |
| 104656 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 104657 | WhereLoop *pNew; /* Template WhereLoop */ |
| 104658 | WhereLoop *pBest; /* If non-NULL, store single best loop here */ |
| 104659 | }; |
| 104660 | |
| 104661 | /* |
| 104662 | ** The WHERE clause processing routine has two halves. The |
| 104663 | ** first part does the start of the WHERE loop and the second |
| 104664 | ** half does the tail of the WHERE loop. An instance of |
| 104665 | ** this structure is returned by the first half and passed |
| 104666 | ** into the second half to give some continuity. |
| 104667 | ** |
| 104668 | ** An instance of this object holds the complete state of the query |
| 104669 | ** planner. |
| 104670 | */ |
| 104671 | struct WhereInfo { |
| 104672 | Parse *pParse; /* Parsing and code generating context */ |
| 104673 | SrcList *pTabList; /* List of tables in the join */ |
| 104674 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
| 104675 | ExprList *pDistinct; /* DISTINCT ON values, or NULL */ |
| 104676 | WhereLoop *pLoops; /* List of all WhereLoop objects */ |
| 104677 | Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
| 104678 | WhereCost nRowOut; /* Estimated number of output rows */ |
| 104679 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
| 104680 | u8 bOBSat; /* ORDER BY satisfied by indices */ |
| 104681 | u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ |
| 104682 | u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
| 104683 | u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ |
| 104684 | int iTop; /* The very beginning of the WHERE loop */ |
| 104685 | int iContinue; /* Jump here to continue with next record */ |
| 104686 | int iBreak; /* Jump here to break out of the loop */ |
| 104687 | int nLevel; /* Number of nested loop */ |
| 104688 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 104689 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 104690 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
| 104691 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 104692 | }; |
| 104693 | |
| 104694 | /* |
| 104695 | ** Bitmasks for the operators on WhereTerm objects. These are all |
| 104696 | ** operators that are of interest to the query planner. An |
| 104697 | ** OR-ed combination of these values can be used when searching for |
| 104698 | ** particular WhereTerms within a WhereClause. |
| 104699 | */ |
| 104700 | #define WO_IN 0x001 |
| 104701 | #define WO_EQ 0x002 |
| 104702 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 104703 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| @@ -104603,96 +104712,106 @@ | |
| 104712 | |
| 104713 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
| 104714 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
| 104715 | |
| 104716 | /* |
| 104717 | ** These are definitions of bits in the WhereLoop.wsFlags field. |
| 104718 | ** The particular combination of bits in each WhereLoop help to |
| 104719 | ** determine the algorithm that WhereLoop represents. |
| 104720 | */ |
| 104721 | #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR or x IN (...) or x IS NULL */ |
| 104722 | #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 104723 | #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 104724 | #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
| 104725 | #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
| 104726 | #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 104727 | #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 104728 | #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 104729 | #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 104730 | #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 104731 | #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 104732 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 104733 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
| 104734 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 104735 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 104736 | #define WHERE_TEMP_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 104737 | |
| 104738 | |
| 104739 | /* Convert a WhereCost value (10 times log2(X)) into its integer value X. |
| 104740 | ** A rough approximation is used. The value returned is not exact. |
| 104741 | */ |
| 104742 | static u64 whereCostToInt(WhereCost x){ |
| 104743 | u64 n; |
| 104744 | if( x<10 ) return 1; |
| 104745 | n = x%10; |
| 104746 | x /= 10; |
| 104747 | if( n>=5 ) n -= 2; |
| 104748 | else if( n>=1 ) n -= 1; |
| 104749 | if( x>=3 ) return (n+8)<<(x-3); |
| 104750 | return (n+8)>>(3-x); |
| 104751 | } |
| 104752 | |
| 104753 | /* |
| 104754 | ** Return the estimated number of output rows from a WHERE clause |
| 104755 | */ |
| 104756 | SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 104757 | return whereCostToInt(pWInfo->nRowOut); |
| 104758 | } |
| 104759 | |
| 104760 | /* |
| 104761 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 104762 | ** WHERE clause returns outputs for DISTINCT processing. |
| 104763 | */ |
| 104764 | SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 104765 | return pWInfo->eDistinct; |
| 104766 | } |
| 104767 | |
| 104768 | /* |
| 104769 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 104770 | ** Return FALSE if the output needs to be sorted. |
| 104771 | */ |
| 104772 | SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
| 104773 | return pWInfo->bOBSat!=0; |
| 104774 | } |
| 104775 | |
| 104776 | /* |
| 104777 | ** Return the VDBE address or label to jump to in order to continue |
| 104778 | ** immediately with the next row of a WHERE clause. |
| 104779 | */ |
| 104780 | SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 104781 | return pWInfo->iContinue; |
| 104782 | } |
| 104783 | |
| 104784 | /* |
| 104785 | ** Return the VDBE address or label to jump to in order to break |
| 104786 | ** out of a WHERE loop. |
| 104787 | */ |
| 104788 | SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 104789 | return pWInfo->iBreak; |
| 104790 | } |
| 104791 | |
| 104792 | /* |
| 104793 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 104794 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 104795 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
| 104796 | */ |
| 104797 | SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo){ |
| 104798 | return pWInfo->okOnePass; |
| 104799 | } |
| 104800 | |
| 104801 | /* |
| 104802 | ** Initialize a preallocated WhereClause structure. |
| 104803 | */ |
| 104804 | static void whereClauseInit( |
| 104805 | WhereClause *pWC, /* The WhereClause to be initialized */ |
| 104806 | WhereInfo *pWInfo /* The WHERE processing context */ |
| 104807 | ){ |
| 104808 | pWC->pWInfo = pWInfo; |
| 104809 | pWC->pOuter = 0; |
| 104810 | pWC->nTerm = 0; |
| 104811 | pWC->nSlot = ArraySize(pWC->aStatic); |
| 104812 | pWC->a = pWC->aStatic; |
| 104813 | } |
| 104814 | |
| 104815 | /* Forward reference */ |
| 104816 | static void whereClauseClear(WhereClause*); |
| 104817 | |
| @@ -104717,11 +104836,11 @@ | |
| 104836 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 104837 | */ |
| 104838 | static void whereClauseClear(WhereClause *pWC){ |
| 104839 | int i; |
| 104840 | WhereTerm *a; |
| 104841 | sqlite3 *db = pWC->pWInfo->pParse->db; |
| 104842 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
| 104843 | if( a->wtFlags & TERM_DYNAMIC ){ |
| 104844 | sqlite3ExprDelete(db, a->pExpr); |
| 104845 | } |
| 104846 | if( a->wtFlags & TERM_ORINFO ){ |
| @@ -104758,11 +104877,11 @@ | |
| 104877 | WhereTerm *pTerm; |
| 104878 | int idx; |
| 104879 | testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ |
| 104880 | if( pWC->nTerm>=pWC->nSlot ){ |
| 104881 | WhereTerm *pOld = pWC->a; |
| 104882 | sqlite3 *db = pWC->pWInfo->pParse->db; |
| 104883 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
| 104884 | if( pWC->a==0 ){ |
| 104885 | if( wtFlags & TERM_DYNAMIC ){ |
| 104886 | sqlite3ExprDelete(db, p); |
| 104887 | } |
| @@ -104810,13 +104929,13 @@ | |
| 104929 | whereSplit(pWC, pExpr->pRight, op); |
| 104930 | } |
| 104931 | } |
| 104932 | |
| 104933 | /* |
| 104934 | ** Initialize a WhereMaskSet object |
| 104935 | */ |
| 104936 | #define initMaskSet(P) (P)->n=0 |
| 104937 | |
| 104938 | /* |
| 104939 | ** Return the bitmask for the given cursor number. Return 0 if |
| 104940 | ** iCursor is not in the set. |
| 104941 | */ |
| @@ -104823,11 +104942,11 @@ | |
| 104942 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 104943 | int i; |
| 104944 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
| 104945 | for(i=0; i<pMaskSet->n; i++){ |
| 104946 | if( pMaskSet->ix[i]==iCursor ){ |
| 104947 | return MASKBIT(i); |
| 104948 | } |
| 104949 | } |
| 104950 | return 0; |
| 104951 | } |
| 104952 | |
| @@ -104843,22 +104962,13 @@ | |
| 104962 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
| 104963 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
| 104964 | } |
| 104965 | |
| 104966 | /* |
| 104967 | ** These routine walk (recursively) an expression tree and generates |
| 104968 | ** a bitmask indicating which tables are used in that expression |
| 104969 | ** tree. |
| 104970 | */ |
| 104971 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 104972 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 104973 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
| 104974 | Bitmask mask = 0; |
| @@ -104908,11 +105018,11 @@ | |
| 105018 | } |
| 105019 | |
| 105020 | /* |
| 105021 | ** Return TRUE if the given operator is one of the operators that is |
| 105022 | ** allowed for an indexable WHERE clause term. The allowed operators are |
| 105023 | ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
| 105024 | ** |
| 105025 | ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be |
| 105026 | ** of one of the following forms: column = expression column > expression |
| 105027 | ** column >= expression column < expression column <= expression |
| 105028 | ** expression = column expression > column expression >= column |
| @@ -104935,14 +105045,13 @@ | |
| 105045 | /* |
| 105046 | ** Commute a comparison operator. Expressions of the form "X op Y" |
| 105047 | ** are converted into "Y op X". |
| 105048 | ** |
| 105049 | ** If left/right precedence rules come into play when determining the |
| 105050 | ** collating sequence, then COLLATE operators are adjusted to ensure |
| 105051 | ** that the collating sequence does not change. For example: |
| 105052 | ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on |
| 105053 | ** the left hand side of a comparison overrides any collation sequence |
| 105054 | ** attached to the right. For the same reason the EP_Collate flag |
| 105055 | ** is not commuted. |
| 105056 | */ |
| 105057 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
| @@ -104994,10 +105103,134 @@ | |
| 105103 | assert( op!=TK_LE || c==WO_LE ); |
| 105104 | assert( op!=TK_GT || c==WO_GT ); |
| 105105 | assert( op!=TK_GE || c==WO_GE ); |
| 105106 | return c; |
| 105107 | } |
| 105108 | |
| 105109 | /* |
| 105110 | ** Advance to the next WhereTerm that matches according to the criteria |
| 105111 | ** established when the pScan object was initialized by whereScanInit(). |
| 105112 | ** Return NULL if there are no more matching WhereTerms. |
| 105113 | */ |
| 105114 | WhereTerm *whereScanNext(WhereScan *pScan){ |
| 105115 | int iCur; /* The cursor on the LHS of the term */ |
| 105116 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 105117 | Expr *pX; /* An expression being tested */ |
| 105118 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 105119 | WhereTerm *pTerm; /* The term being tested */ |
| 105120 | int k = pScan->k; /* Where to start scanning */ |
| 105121 | |
| 105122 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 105123 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 105124 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 105125 | while( (pWC = pScan->pWC)!=0 ){ |
| 105126 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
| 105127 | if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){ |
| 105128 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 105129 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 105130 | ){ |
| 105131 | int j; |
| 105132 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 105133 | assert( pX->op==TK_COLUMN ); |
| 105134 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 105135 | if( pScan->aEquiv[j]==pX->iTable |
| 105136 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 105137 | break; |
| 105138 | } |
| 105139 | } |
| 105140 | if( j==pScan->nEquiv ){ |
| 105141 | pScan->aEquiv[j] = pX->iTable; |
| 105142 | pScan->aEquiv[j+1] = pX->iColumn; |
| 105143 | pScan->nEquiv += 2; |
| 105144 | } |
| 105145 | } |
| 105146 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 105147 | /* Verify the affinity and collating sequence match */ |
| 105148 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 105149 | CollSeq *pColl; |
| 105150 | Parse *pParse = pWC->pWInfo->pParse; |
| 105151 | pX = pTerm->pExpr; |
| 105152 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 105153 | continue; |
| 105154 | } |
| 105155 | assert(pX->pLeft); |
| 105156 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
| 105157 | pX->pLeft, pX->pRight); |
| 105158 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
| 105159 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 105160 | continue; |
| 105161 | } |
| 105162 | } |
| 105163 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 105164 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 105165 | && pX->iTable==pScan->aEquiv[0] |
| 105166 | && pX->iColumn==pScan->aEquiv[1] |
| 105167 | ){ |
| 105168 | continue; |
| 105169 | } |
| 105170 | pScan->k = k+1; |
| 105171 | return pTerm; |
| 105172 | } |
| 105173 | } |
| 105174 | } |
| 105175 | pWC = pScan->pWC = pScan->pWC->pOuter; |
| 105176 | k = 0; |
| 105177 | } |
| 105178 | pScan->pWC = pScan->pOrigWC; |
| 105179 | k = 0; |
| 105180 | pScan->iEquiv += 2; |
| 105181 | } |
| 105182 | return 0; |
| 105183 | } |
| 105184 | |
| 105185 | /* |
| 105186 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 105187 | ** first match. Return NULL if there are no matches. |
| 105188 | ** |
| 105189 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 105190 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 105191 | ** iCur. The <op> must be one of the operators described by opMask. |
| 105192 | ** |
| 105193 | ** If the search is for X and the WHERE clause contains terms of the |
| 105194 | ** form X=Y then this routine might also return terms of the form |
| 105195 | ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 105196 | ** but is enough to handle most commonly occurring SQL statements. |
| 105197 | ** |
| 105198 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 105199 | ** index pIdx. |
| 105200 | */ |
| 105201 | WhereTerm *whereScanInit( |
| 105202 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 105203 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 105204 | int iCur, /* Cursor to scan for */ |
| 105205 | int iColumn, /* Column to scan for */ |
| 105206 | u32 opMask, /* Operator(s) to scan for */ |
| 105207 | Index *pIdx /* Must be compatible with this index */ |
| 105208 | ){ |
| 105209 | int j; |
| 105210 | |
| 105211 | /* memset(pScan, 0, sizeof(*pScan)); */ |
| 105212 | pScan->pOrigWC = pWC; |
| 105213 | pScan->pWC = pWC; |
| 105214 | if( pIdx && iColumn>=0 ){ |
| 105215 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 105216 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 105217 | if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 105218 | } |
| 105219 | pScan->zCollName = pIdx->azColl[j]; |
| 105220 | }else{ |
| 105221 | pScan->idxaff = 0; |
| 105222 | pScan->zCollName = 0; |
| 105223 | } |
| 105224 | pScan->opMask = opMask; |
| 105225 | pScan->k = 0; |
| 105226 | pScan->aEquiv[0] = iCur; |
| 105227 | pScan->aEquiv[1] = iColumn; |
| 105228 | pScan->nEquiv = 2; |
| 105229 | pScan->iEquiv = 2; |
| 105230 | return whereScanNext(pScan); |
| 105231 | } |
| 105232 | |
| 105233 | /* |
| 105234 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 105235 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 105236 | ** the WO_xx operator codes specified by the op parameter. |
| @@ -105026,98 +105259,32 @@ | |
| 105259 | int iColumn, /* Column number of LHS */ |
| 105260 | Bitmask notReady, /* RHS must not overlap with this mask */ |
| 105261 | u32 op, /* Mask of WO_xx values describing operator */ |
| 105262 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 105263 | ){ |
| 105264 | WhereTerm *pResult = 0; |
| 105265 | WhereTerm *p; |
| 105266 | WhereScan scan; |
| 105267 | |
| 105268 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 105269 | while( p ){ |
| 105270 | if( (p->prereqRight & notReady)==0 ){ |
| 105271 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 105272 | return p; |
| 105273 | } |
| 105274 | if( pResult==0 ) pResult = p; |
| 105275 | } |
| 105276 | p = whereScanNext(&scan); |
| 105277 | } |
| 105278 | return pResult; |
| 105279 | } |
| 105280 | |
| 105281 | /* Forward reference */ |
| 105282 | static void exprAnalyze(SrcList*, WhereClause*, int); |
| 105283 | |
| 105284 | /* |
| 105285 | ** Call exprAnalyze on all terms in a WHERE clause. |
| 105286 | */ |
| 105287 | static void exprAnalyzeAll( |
| 105288 | SrcList *pTabList, /* the FROM clause */ |
| 105289 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 105290 | ){ |
| @@ -105345,15 +105512,15 @@ | |
| 105512 | static void exprAnalyzeOrTerm( |
| 105513 | SrcList *pSrc, /* the FROM clause */ |
| 105514 | WhereClause *pWC, /* the complete WHERE clause */ |
| 105515 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 105516 | ){ |
| 105517 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 105518 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
| 105519 | sqlite3 *db = pParse->db; /* Database connection */ |
| 105520 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 105521 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
| 105522 | int i; /* Loop counters */ |
| 105523 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 105524 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 105525 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 105526 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| @@ -105368,11 +105535,11 @@ | |
| 105535 | assert( pExpr->op==TK_OR ); |
| 105536 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
| 105537 | if( pOrInfo==0 ) return; |
| 105538 | pTerm->wtFlags |= TERM_ORINFO; |
| 105539 | pOrWc = &pOrInfo->wc; |
| 105540 | whereClauseInit(pOrWc, pWInfo); |
| 105541 | whereSplit(pOrWc, pExpr, TK_OR); |
| 105542 | exprAnalyzeAll(pSrc, pOrWc); |
| 105543 | if( db->mallocFailed ) return; |
| 105544 | assert( pOrWc->nTerm>=2 ); |
| 105545 | |
| @@ -105394,20 +105561,20 @@ | |
| 105561 | Bitmask b = 0; |
| 105562 | pOrTerm->u.pAndInfo = pAndInfo; |
| 105563 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 105564 | pOrTerm->eOperator = WO_AND; |
| 105565 | pAndWC = &pAndInfo->wc; |
| 105566 | whereClauseInit(pAndWC, pWC->pWInfo); |
| 105567 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 105568 | exprAnalyzeAll(pSrc, pAndWC); |
| 105569 | pAndWC->pOuter = pWC; |
| 105570 | testcase( db->mallocFailed ); |
| 105571 | if( !db->mallocFailed ){ |
| 105572 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 105573 | assert( pAndTerm->pExpr ); |
| 105574 | if( allowedOp(pAndTerm->pExpr->op) ){ |
| 105575 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
| 105576 | } |
| 105577 | } |
| 105578 | } |
| 105579 | indexable &= b; |
| 105580 | } |
| @@ -105414,14 +105581,14 @@ | |
| 105581 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 105582 | /* Skip this term for now. We revisit it when we process the |
| 105583 | ** corresponding TERM_VIRTUAL term */ |
| 105584 | }else{ |
| 105585 | Bitmask b; |
| 105586 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
| 105587 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 105588 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
| 105589 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
| 105590 | } |
| 105591 | indexable &= b; |
| 105592 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
| 105593 | chngToIN = 0; |
| 105594 | }else{ |
| @@ -105479,11 +105646,11 @@ | |
| 105646 | /* This is the 2-bit case and we are on the second iteration and |
| 105647 | ** current term is from the first iteration. So skip this term. */ |
| 105648 | assert( j==1 ); |
| 105649 | continue; |
| 105650 | } |
| 105651 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
| 105652 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 105653 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 105654 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 105655 | ** and use its inversion. */ |
| 105656 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| @@ -105498,11 +105665,11 @@ | |
| 105665 | if( i<0 ){ |
| 105666 | /* No candidate table+column was found. This can only occur |
| 105667 | ** on the second iteration */ |
| 105668 | assert( j==1 ); |
| 105669 | assert( IsPowerOfTwo(chngToIN) ); |
| 105670 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
| 105671 | break; |
| 105672 | } |
| 105673 | testcase( j==1 ); |
| 105674 | |
| 105675 | /* We have found a candidate table and column. Check to see if that |
| @@ -105547,11 +105714,11 @@ | |
| 105714 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
| 105715 | assert( pOrTerm->eOperator & WO_EQ ); |
| 105716 | assert( pOrTerm->leftCursor==iCursor ); |
| 105717 | assert( pOrTerm->u.leftColumn==iColumn ); |
| 105718 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
| 105719 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
| 105720 | pLeft = pOrTerm->pExpr->pLeft; |
| 105721 | } |
| 105722 | assert( pLeft!=0 ); |
| 105723 | pDup = sqlite3ExprDup(db, pLeft, 0); |
| 105724 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
| @@ -105596,10 +105763,11 @@ | |
| 105763 | static void exprAnalyze( |
| 105764 | SrcList *pSrc, /* the FROM clause */ |
| 105765 | WhereClause *pWC, /* the WHERE clause */ |
| 105766 | int idxTerm /* Index of the term to be analyzed */ |
| 105767 | ){ |
| 105768 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 105769 | WhereTerm *pTerm; /* The term to be analyzed */ |
| 105770 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
| 105771 | Expr *pExpr; /* The expression to be analyzed */ |
| 105772 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 105773 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
| @@ -105606,18 +105774,18 @@ | |
| 105774 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
| 105775 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 105776 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 105777 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
| 105778 | int op; /* Top-level operator. pExpr->op */ |
| 105779 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 105780 | sqlite3 *db = pParse->db; /* Database connection */ |
| 105781 | |
| 105782 | if( db->mallocFailed ){ |
| 105783 | return; |
| 105784 | } |
| 105785 | pTerm = &pWC->a[idxTerm]; |
| 105786 | pMaskSet = &pWInfo->sMaskSet; |
| 105787 | pExpr = pTerm->pExpr; |
| 105788 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
| 105789 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
| 105790 | op = pExpr->op; |
| 105791 | if( op==TK_IN ){ |
| @@ -105891,15 +106059,12 @@ | |
| 106059 | */ |
| 106060 | pTerm->prereqRight |= extraRight; |
| 106061 | } |
| 106062 | |
| 106063 | /* |
| 106064 | ** This function searches pList for a entry that matches the iCol-th column |
| 106065 | ** of index pIdx. |
| 106066 | ** |
| 106067 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 106068 | ** no expression is found, -1 is returned. |
| 106069 | */ |
| 106070 | static int findIndexCol( |
| @@ -105925,82 +106090,23 @@ | |
| 106090 | } |
| 106091 | } |
| 106092 | |
| 106093 | return -1; |
| 106094 | } |
| 106095 | |
| 106096 | /* |
| 106097 | ** Return true if the DISTINCT expression-list passed as the third argument |
| 106098 | ** is redundant. |
| 106099 | ** |
| 106100 | ** A DISTINCT list is redundant if the database contains some subset of |
| 106101 | ** columns that are unique and non-null. |
| 106102 | */ |
| 106103 | static int isDistinctRedundant( |
| 106104 | Parse *pParse, /* Parsing context */ |
| 106105 | SrcList *pTabList, /* The FROM clause */ |
| 106106 | WhereClause *pWC, /* The WHERE clause */ |
| 106107 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
| 106108 | ){ |
| 106109 | Table *pTab; |
| 106110 | Index *pIdx; |
| 106111 | int i; |
| 106112 | int iBase; |
| @@ -106051,35 +106157,90 @@ | |
| 106157 | } |
| 106158 | } |
| 106159 | |
| 106160 | return 0; |
| 106161 | } |
| 106162 | |
| 106163 | /* |
| 106164 | ** The (an approximate) sum of two WhereCosts. This computation is |
| 106165 | ** not a simple "+" operator because WhereCost is stored as a logarithmic |
| 106166 | ** value. |
| 106167 | ** |
| 106168 | */ |
| 106169 | static WhereCost whereCostAdd(WhereCost a, WhereCost b){ |
| 106170 | static const unsigned char x[] = { |
| 106171 | 10, 10, /* 0,1 */ |
| 106172 | 9, 9, /* 2,3 */ |
| 106173 | 8, 8, /* 4,5 */ |
| 106174 | 7, 7, 7, /* 6,7,8 */ |
| 106175 | 6, 6, 6, /* 9,10,11 */ |
| 106176 | 5, 5, 5, /* 12-14 */ |
| 106177 | 4, 4, 4, 4, /* 15-18 */ |
| 106178 | 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 106179 | 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 106180 | }; |
| 106181 | if( a>=b ){ |
| 106182 | if( a>b+49 ) return a; |
| 106183 | if( a>b+31 ) return a+1; |
| 106184 | return a+x[a-b]; |
| 106185 | }else{ |
| 106186 | if( b>a+49 ) return b; |
| 106187 | if( b>a+31 ) return b+1; |
| 106188 | return b+x[b-a]; |
| 106189 | } |
| 106190 | } |
| 106191 | |
| 106192 | /* |
| 106193 | ** Convert an integer into a WhereCost. In other words, compute a |
| 106194 | ** good approximatation for 10*log2(x). |
| 106195 | */ |
| 106196 | static WhereCost whereCost(tRowcnt x){ |
| 106197 | static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 106198 | WhereCost y = 40; |
| 106199 | if( x<8 ){ |
| 106200 | if( x<2 ) return 0; |
| 106201 | while( x<8 ){ y -= 10; x <<= 1; } |
| 106202 | }else{ |
| 106203 | while( x>255 ){ y += 40; x >>= 4; } |
| 106204 | while( x>15 ){ y += 10; x >>= 1; } |
| 106205 | } |
| 106206 | return a[x&7] + y - 10; |
| 106207 | } |
| 106208 | |
| 106209 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 106210 | /* |
| 106211 | ** Convert a double (as received from xBestIndex of a virtual table) |
| 106212 | ** into a WhereCost. In other words, compute an approximation for |
| 106213 | ** 10*log2(x). |
| 106214 | */ |
| 106215 | static WhereCost whereCostFromDouble(double x){ |
| 106216 | u64 a; |
| 106217 | WhereCost e; |
| 106218 | assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 106219 | if( x<=1 ) return 0; |
| 106220 | if( x<=2000000000 ) return whereCost((tRowcnt)x); |
| 106221 | memcpy(&a, &x, 8); |
| 106222 | e = (a>>52) - 1022; |
| 106223 | return e*10; |
| 106224 | } |
| 106225 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 106226 | |
| 106227 | /* |
| 106228 | ** Estimate the logarithm of the input value to base 2. |
| 106229 | */ |
| 106230 | static WhereCost estLog(WhereCost N){ |
| 106231 | WhereCost x = whereCost(N); |
| 106232 | return x>33 ? x - 33 : 0; |
| 106233 | } |
| 106234 | |
| 106235 | /* |
| 106236 | ** Two routines for printing the content of an sqlite3_index_info |
| 106237 | ** structure. Used for testing and debugging only. If neither |
| 106238 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 106239 | ** are no-ops. |
| 106240 | */ |
| 106241 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
| 106242 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 106243 | int i; |
| 106244 | if( !sqlite3WhereTrace ) return; |
| 106245 | for(i=0; i<p->nConstraint; i++){ |
| 106246 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| @@ -106113,111 +106274,10 @@ | |
| 106274 | #else |
| 106275 | #define TRACE_IDX_INPUTS(A) |
| 106276 | #define TRACE_IDX_OUTPUTS(A) |
| 106277 | #endif |
| 106278 | |
| 106279 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 106280 | /* |
| 106281 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 106282 | ** could be used with an index to access pSrc, assuming an appropriate |
| 106283 | ** index existed. |
| @@ -106229,92 +106289,17 @@ | |
| 106289 | ){ |
| 106290 | char aff; |
| 106291 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
| 106292 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
| 106293 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
| 106294 | if( pTerm->u.leftColumn<0 ) return 0; |
| 106295 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 106296 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 106297 | return 1; |
| 106298 | } |
| 106299 | #endif |
| 106300 | |
| 106301 | |
| 106302 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 106303 | /* |
| 106304 | ** Generate code to construct the Index object for an automatic index |
| 106305 | ** and to set up the WhereLevel object pLevel so that the code generator |
| @@ -106340,12 +106325,14 @@ | |
| 106325 | int regRecord; /* Register holding an index record */ |
| 106326 | int n; /* Column counter */ |
| 106327 | int i; /* Loop counter */ |
| 106328 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
| 106329 | CollSeq *pColl; /* Collating sequence to on a column */ |
| 106330 | WhereLoop *pLoop; /* The Loop object */ |
| 106331 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 106332 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 106333 | const int mxConstraint = 10; /* Maximum number of constraints */ |
| 106334 | |
| 106335 | /* Generate code to skip over the creation and initialization of the |
| 106336 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 106337 | v = pParse->pVdbe; |
| 106338 | assert( v!=0 ); |
| @@ -106354,54 +106341,58 @@ | |
| 106341 | /* Count the number of columns that will be added to the index |
| 106342 | ** and used to match WHERE clause constraints */ |
| 106343 | nColumn = 0; |
| 106344 | pTable = pSrc->pTab; |
| 106345 | pWCEnd = &pWC->a[pWC->nTerm]; |
| 106346 | pLoop = pLevel->pWLoop; |
| 106347 | idxCols = 0; |
| 106348 | for(pTerm=pWC->a; pTerm<pWCEnd && pLoop->nLTerm<mxConstraint; pTerm++){ |
| 106349 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 106350 | int iCol = pTerm->u.leftColumn; |
| 106351 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 106352 | testcase( iCol==BMS ); |
| 106353 | testcase( iCol==BMS-1 ); |
| 106354 | if( (idxCols & cMask)==0 ){ |
| 106355 | if( whereLoopResize(pParse->db, pLoop, nColumn+1) ) return; |
| 106356 | pLoop->aLTerm[nColumn++] = pTerm; |
| 106357 | idxCols |= cMask; |
| 106358 | } |
| 106359 | } |
| 106360 | } |
| 106361 | assert( nColumn>0 ); |
| 106362 | pLoop->u.btree.nEq = pLoop->nLTerm = nColumn; |
| 106363 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
| 106364 | | WHERE_TEMP_INDEX; |
| 106365 | |
| 106366 | /* Count the number of additional columns needed to create a |
| 106367 | ** covering index. A "covering index" is an index that contains all |
| 106368 | ** columns that are needed by the query. With a covering index, the |
| 106369 | ** original table never needs to be accessed. Automatic indices must |
| 106370 | ** be a covering index because the index will not be updated if the |
| 106371 | ** original table changes and the index and table cannot both be used |
| 106372 | ** if they go out of sync. |
| 106373 | */ |
| 106374 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 106375 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
| 106376 | testcase( pTable->nCol==BMS-1 ); |
| 106377 | testcase( pTable->nCol==BMS-2 ); |
| 106378 | for(i=0; i<mxBitCol; i++){ |
| 106379 | if( extraCols & MASKBIT(i) ) nColumn++; |
| 106380 | } |
| 106381 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 106382 | nColumn += pTable->nCol - BMS + 1; |
| 106383 | } |
| 106384 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
| 106385 | |
| 106386 | /* Construct the Index object to describe this index */ |
| 106387 | nByte = sizeof(Index); |
| 106388 | nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 106389 | nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 106390 | nByte += nColumn; /* Index.aSortOrder */ |
| 106391 | pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 106392 | if( pIdx==0 ) return; |
| 106393 | pLoop->u.btree.pIndex = pIdx; |
| 106394 | pIdx->azColl = (char**)&pIdx[1]; |
| 106395 | pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 106396 | pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 106397 | pIdx->zName = "auto-index"; |
| 106398 | pIdx->nColumn = nColumn; |
| @@ -106409,11 +106400,11 @@ | |
| 106400 | n = 0; |
| 106401 | idxCols = 0; |
| 106402 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 106403 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 106404 | int iCol = pTerm->u.leftColumn; |
| 106405 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 106406 | if( (idxCols & cMask)==0 ){ |
| 106407 | Expr *pX = pTerm->pExpr; |
| 106408 | idxCols |= cMask; |
| 106409 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 106410 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| @@ -106420,22 +106411,22 @@ | |
| 106411 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
| 106412 | n++; |
| 106413 | } |
| 106414 | } |
| 106415 | } |
| 106416 | assert( (u32)n==pLoop->u.btree.nEq ); |
| 106417 | |
| 106418 | /* Add additional columns needed to make the automatic index into |
| 106419 | ** a covering index */ |
| 106420 | for(i=0; i<mxBitCol; i++){ |
| 106421 | if( extraCols & MASKBIT(i) ){ |
| 106422 | pIdx->aiColumn[n] = i; |
| 106423 | pIdx->azColl[n] = "BINARY"; |
| 106424 | n++; |
| 106425 | } |
| 106426 | } |
| 106427 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 106428 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 106429 | pIdx->aiColumn[n] = i; |
| 106430 | pIdx->azColl[n] = "BINARY"; |
| 106431 | n++; |
| 106432 | } |
| @@ -106443,10 +106434,11 @@ | |
| 106434 | assert( n==nColumn ); |
| 106435 | |
| 106436 | /* Create the automatic index */ |
| 106437 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 106438 | assert( pLevel->iIdxCur>=0 ); |
| 106439 | pLevel->iIdxCur = pParse->nTab++; |
| 106440 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
| 106441 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
| 106442 | VdbeComment((v, "for %s", pTable->zName)); |
| 106443 | |
| 106444 | /* Fill the automatic index with content */ |
| @@ -106469,26 +106461,25 @@ | |
| 106461 | /* |
| 106462 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 106463 | ** responsibility of the caller to eventually release the structure |
| 106464 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 106465 | */ |
| 106466 | static sqlite3_index_info *allocateIndexInfo( |
| 106467 | Parse *pParse, |
| 106468 | WhereClause *pWC, |
| 106469 | struct SrcList_item *pSrc, |
| 106470 | ExprList *pOrderBy |
| 106471 | ){ |
| 106472 | int i, j; |
| 106473 | int nTerm; |
| 106474 | struct sqlite3_index_constraint *pIdxCons; |
| 106475 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 106476 | struct sqlite3_index_constraint_usage *pUsage; |
| 106477 | WhereTerm *pTerm; |
| 106478 | int nOrderBy; |
| 106479 | sqlite3_index_info *pIdxInfo; |
| 106480 | |
| 106481 | /* Count the number of possible WHERE clause constraints referring |
| 106482 | ** to this virtual table */ |
| 106483 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 106484 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 106485 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| @@ -106520,11 +106511,10 @@ | |
| 106511 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 106512 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 106513 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 106514 | if( pIdxInfo==0 ){ |
| 106515 | sqlite3ErrorMsg(pParse, "out of memory"); |
| 106516 | return 0; |
| 106517 | } |
| 106518 | |
| 106519 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 106520 | ** many fields that are declared "const" to prevent xBestIndex from |
| @@ -106576,12 +106566,12 @@ | |
| 106566 | } |
| 106567 | |
| 106568 | /* |
| 106569 | ** The table object reference passed as the second argument to this function |
| 106570 | ** must represent a virtual table. This function invokes the xBestIndex() |
| 106571 | ** method of the virtual table with the sqlite3_index_info object that |
| 106572 | ** comes in as the 3rd argument to this function. |
| 106573 | ** |
| 106574 | ** If an error occurs, pParse is populated with an error message and a |
| 106575 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 106576 | ** part of the sqlite3_index_info structure is left populated. |
| 106577 | ** |
| @@ -106592,11 +106582,10 @@ | |
| 106582 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
| 106583 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
| 106584 | int i; |
| 106585 | int rc; |
| 106586 | |
| 106587 | TRACE_IDX_INPUTS(p); |
| 106588 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 106589 | TRACE_IDX_OUTPUTS(p); |
| 106590 | |
| 106591 | if( rc!=SQLITE_OK ){ |
| @@ -106618,211 +106607,12 @@ | |
| 106607 | } |
| 106608 | } |
| 106609 | |
| 106610 | return pParse->nErr; |
| 106611 | } |
| 106612 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 106613 | |
| 106614 | |
| 106615 | #ifdef SQLITE_ENABLE_STAT3 |
| 106616 | /* |
| 106617 | ** Estimate the location of a particular key among all keys in an |
| 106618 | ** index. Store the results in aStat as follows: |
| @@ -107060,11 +106850,11 @@ | |
| 106850 | Parse *pParse, /* Parsing & code generating context */ |
| 106851 | Index *p, /* The index containing the range-compared column; "x" */ |
| 106852 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 106853 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 106854 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 106855 | WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */ |
| 106856 | ){ |
| 106857 | int rc = SQLITE_OK; |
| 106858 | |
| 106859 | #ifdef SQLITE_ENABLE_STAT3 |
| 106860 | |
| @@ -107098,29 +106888,35 @@ | |
| 106888 | if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1]; |
| 106889 | } |
| 106890 | sqlite3ValueFree(pRangeVal); |
| 106891 | } |
| 106892 | if( rc==SQLITE_OK ){ |
| 106893 | WhereCost iBase = whereCost(p->aiRowEst[0]); |
| 106894 | if( iUpper>iLower ){ |
| 106895 | iBase -= whereCost(iUpper - iLower); |
| 106896 | } |
| 106897 | *pRangeDiv = iBase; |
| 106898 | WHERETRACE(0x100, ("range scan regions: %u..%u div=%d\n", |
| 106899 | (u32)iLower, (u32)iUpper, *pRangeDiv)); |
| 106900 | return SQLITE_OK; |
| 106901 | } |
| 106902 | } |
| 106903 | #else |
| 106904 | UNUSED_PARAMETER(pParse); |
| 106905 | UNUSED_PARAMETER(p); |
| 106906 | UNUSED_PARAMETER(nEq); |
| 106907 | #endif |
| 106908 | assert( pLower || pUpper ); |
| 106909 | *pRangeDiv = 0; |
| 106910 | /* TUNING: Each inequality constraint reduces the search space 4-fold. |
| 106911 | ** A BETWEEN operator, therefore, reduces the search space 16-fold */ |
| 106912 | if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){ |
| 106913 | *pRangeDiv += 20; assert( 20==whereCost(4) ); |
| 106914 | } |
| 106915 | if( pUpper ){ |
| 106916 | *pRangeDiv += 20; assert( 20==whereCost(4) ); |
| 106917 | } |
| 106918 | return rc; |
| 106919 | } |
| 106920 | |
| 106921 | #ifdef SQLITE_ENABLE_STAT3 |
| 106922 | /* |
| @@ -107142,11 +106938,11 @@ | |
| 106938 | */ |
| 106939 | static int whereEqualScanEst( |
| 106940 | Parse *pParse, /* Parsing & code generating context */ |
| 106941 | Index *p, /* The index whose left-most column is pTerm */ |
| 106942 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 106943 | tRowcnt *pnRow /* Write the revised row estimate here */ |
| 106944 | ){ |
| 106945 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
| 106946 | u8 aff; /* Column affinity */ |
| 106947 | int rc; /* Subfunction return code */ |
| 106948 | tRowcnt a[2]; /* Statistics */ |
| @@ -107161,11 +106957,11 @@ | |
| 106957 | pRhs = sqlite3ValueNew(pParse->db); |
| 106958 | } |
| 106959 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
| 106960 | rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 106961 | if( rc==SQLITE_OK ){ |
| 106962 | WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1])); |
| 106963 | *pnRow = a[1]; |
| 106964 | } |
| 106965 | whereEqualScanEst_cancel: |
| 106966 | sqlite3ValueFree(pRhs); |
| 106967 | return rc; |
| @@ -107191,16 +106987,16 @@ | |
| 106987 | */ |
| 106988 | static int whereInScanEst( |
| 106989 | Parse *pParse, /* Parsing & code generating context */ |
| 106990 | Index *p, /* The index whose left-most column is pTerm */ |
| 106991 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 106992 | tRowcnt *pnRow /* Write the revised row estimate here */ |
| 106993 | ){ |
| 106994 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 106995 | tRowcnt nEst; /* Number of rows for a single term */ |
| 106996 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 106997 | int i; /* Loop counter */ |
| 106998 | |
| 106999 | assert( p->aSample!=0 ); |
| 107000 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 107001 | nEst = p->aiRowEst[0]; |
| 107002 | rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| @@ -107207,888 +107003,15 @@ | |
| 107003 | nRowEst += nEst; |
| 107004 | } |
| 107005 | if( rc==SQLITE_OK ){ |
| 107006 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 107007 | *pnRow = nRowEst; |
| 107008 | WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst)); |
| 107009 | } |
| 107010 | return rc; |
| 107011 | } |
| 107012 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 107013 | |
| 107014 | /* |
| 107015 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 107016 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 107017 | ** or USING clause of that join. |
| @@ -108183,10 +107106,11 @@ | |
| 107106 | static int codeEqualityTerm( |
| 107107 | Parse *pParse, /* The parsing context */ |
| 107108 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
| 107109 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 107110 | int iEq, /* Index of the equality term within this level */ |
| 107111 | int bRev, /* True for reverse-order IN operations */ |
| 107112 | int iTarget /* Attempt to leave results in this register */ |
| 107113 | ){ |
| 107114 | Expr *pX = pTerm->pExpr; |
| 107115 | Vdbe *v = pParse->pVdbe; |
| 107116 | int iReg; /* Register holding results */ |
| @@ -108200,18 +107124,17 @@ | |
| 107124 | #ifndef SQLITE_OMIT_SUBQUERY |
| 107125 | }else{ |
| 107126 | int eType; |
| 107127 | int iTab; |
| 107128 | struct InLoop *pIn; |
| 107129 | WhereLoop *pLoop = pLevel->pWLoop; |
| 107130 | |
| 107131 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 107132 | && pLoop->u.btree.pIndex!=0 |
| 107133 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
| 107134 | ){ |
| 107135 | testcase( iEq==0 ); |
| 107136 | testcase( bRev ); |
| 107137 | bRev = !bRev; |
| 107138 | } |
| 107139 | assert( pX->op==TK_IN ); |
| 107140 | iReg = iTarget; |
| @@ -108220,11 +107143,12 @@ | |
| 107143 | testcase( bRev ); |
| 107144 | bRev = !bRev; |
| 107145 | } |
| 107146 | iTab = pX->iTable; |
| 107147 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 107148 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 107149 | pLoop->wsFlags |= WHERE_IN_ABLE; |
| 107150 | if( pLevel->u.in.nIn==0 ){ |
| 107151 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 107152 | } |
| 107153 | pLevel->u.in.nIn++; |
| 107154 | pLevel->u.in.aInLoop = |
| @@ -108292,31 +107216,35 @@ | |
| 107216 | static int codeAllEqualityTerms( |
| 107217 | Parse *pParse, /* Parsing context */ |
| 107218 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 107219 | WhereClause *pWC, /* The WHERE clause */ |
| 107220 | Bitmask notReady, /* Which parts of FROM have not yet been coded */ |
| 107221 | int bRev, /* Reverse the order of IN operators */ |
| 107222 | int nExtraReg, /* Number of extra registers to allocate */ |
| 107223 | char **pzAff /* OUT: Set to point to affinity string */ |
| 107224 | ){ |
| 107225 | int nEq; /* The number of == or IN constraints to code */ |
| 107226 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 107227 | Index *pIdx; /* The index being used for this loop */ |
| 107228 | WhereTerm *pTerm; /* A single constraint term */ |
| 107229 | WhereLoop *pLoop; /* The WhereLoop object */ |
| 107230 | int j; /* Loop counter */ |
| 107231 | int regBase; /* Base register */ |
| 107232 | int nReg; /* Number of registers to allocate */ |
| 107233 | char *zAff; /* Affinity string to return */ |
| 107234 | |
| 107235 | /* This module is only called on query plans that use an index. */ |
| 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 ); |
| 107241 | |
| 107242 | /* Figure out how many memory cells we will need then allocate them. |
| 107243 | */ |
| 107244 | regBase = pParse->nMem + 1; |
| 107245 | nReg = pLoop->u.btree.nEq + nExtraReg; |
| 107246 | pParse->nMem += nReg; |
| 107247 | |
| 107248 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 107249 | if( !zAff ){ |
| 107250 | pParse->db->mallocFailed = 1; |
| @@ -108325,18 +107253,17 @@ | |
| 107253 | /* Evaluate the equality constraints |
| 107254 | */ |
| 107255 | assert( pIdx->nColumn>=nEq ); |
| 107256 | for(j=0; j<nEq; j++){ |
| 107257 | int r1; |
| 107258 | pTerm = pLoop->aLTerm[j]; |
| 107259 | assert( pTerm!=0 ); |
| 107260 | /* The following true for indices with redundant columns. |
| 107261 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 107262 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
| 107263 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 107264 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
| 107265 | if( r1!=regBase+j ){ |
| 107266 | if( nReg==1 ){ |
| 107267 | sqlite3ReleaseTempReg(pParse, regBase); |
| 107268 | regBase = r1; |
| 107269 | }else{ |
| @@ -108400,20 +107327,20 @@ | |
| 107327 | ** |
| 107328 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 107329 | ** It is the responsibility of the caller to free the buffer when it is |
| 107330 | ** no longer required. |
| 107331 | */ |
| 107332 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 107333 | Index *pIndex = pLoop->u.btree.pIndex; |
| 107334 | int nEq = pLoop->u.btree.nEq; |
| 107335 | int i, j; |
| 107336 | Column *aCol = pTab->aCol; |
| 107337 | int *aiColumn = pIndex->aiColumn; |
| 107338 | StrAccum txt; |
| 107339 | |
| 107340 | if( pIndex==0 ) return 0; |
| 107341 | if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
| 107342 | return 0; |
| 107343 | } |
| 107344 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
| 107345 | txt.db = db; |
| 107346 | sqlite3StrAccumAppend(&txt, " (", 2); |
| @@ -108420,15 +107347,15 @@ | |
| 107347 | for(i=0; i<nEq; i++){ |
| 107348 | explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "="); |
| 107349 | } |
| 107350 | |
| 107351 | j = i; |
| 107352 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
| 107353 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 107354 | explainAppendTerm(&txt, i++, z, ">"); |
| 107355 | } |
| 107356 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
| 107357 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 107358 | explainAppendTerm(&txt, i, z, "<"); |
| 107359 | } |
| 107360 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 107361 | return sqlite3StrAccumFinish(&txt); |
| @@ -108447,24 +107374,26 @@ | |
| 107374 | int iLevel, /* Value for "level" column of output */ |
| 107375 | int iFrom, /* Value for "from" column of output */ |
| 107376 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 107377 | ){ |
| 107378 | if( pParse->explain==2 ){ |
| 107379 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 107380 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 107381 | sqlite3 *db = pParse->db; /* Database handle */ |
| 107382 | char *zMsg; /* Text to add to EQP output */ |
| 107383 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 107384 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 107385 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 107386 | u32 flags; /* Flags that describe this loop */ |
| 107387 | |
| 107388 | pLoop = pLevel->pWLoop; |
| 107389 | flags = pLoop->wsFlags; |
| 107390 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
| 107391 | |
| 107392 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 107393 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 107394 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 107395 | |
| 107396 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
| 107397 | if( pItem->pSelect ){ |
| 107398 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
| 107399 | }else{ |
| @@ -108472,24 +107401,26 @@ | |
| 107401 | } |
| 107402 | |
| 107403 | if( pItem->zAlias ){ |
| 107404 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 107405 | } |
| 107406 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
| 107407 | && pLoop->u.btree.pIndex!=0 |
| 107408 | ){ |
| 107409 | char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
| 107410 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, |
| 107411 | ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""), |
| 107412 | ((flags & WHERE_IDX_ONLY)?"COVERING ":""), |
| 107413 | ((flags & WHERE_TEMP_INDEX)?"":" "), |
| 107414 | ((flags & WHERE_TEMP_INDEX)?"": pLoop->u.btree.pIndex->zName), |
| 107415 | zWhere |
| 107416 | ); |
| 107417 | sqlite3DbFree(db, zWhere); |
| 107418 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
| 107419 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
| 107420 | |
| 107421 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
| 107422 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
| 107423 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
| 107424 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 107425 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 107426 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
| @@ -108497,22 +107428,15 @@ | |
| 107428 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 107429 | } |
| 107430 | } |
| 107431 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 107432 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
| 107433 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
| 107434 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
| 107435 | } |
| 107436 | #endif |
| 107437 | zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
| 107438 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
| 107439 | } |
| 107440 | } |
| 107441 | #else |
| 107442 | # define explainOneScan(u,v,w,x,y,z) |
| @@ -108524,19 +107448,19 @@ | |
| 107448 | ** implementation described by pWInfo. |
| 107449 | */ |
| 107450 | static Bitmask codeOneLoopStart( |
| 107451 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 107452 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 107453 | Bitmask notReady /* Which tables are currently available */ |
| 107454 | ){ |
| 107455 | int j, k; /* Loop counters */ |
| 107456 | int iCur; /* The VDBE cursor for the table */ |
| 107457 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 107458 | int omitTable; /* True if we use the index only */ |
| 107459 | int bRev; /* True if we need to scan in reverse order */ |
| 107460 | WhereLevel *pLevel; /* The where level to be coded */ |
| 107461 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
| 107462 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 107463 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 107464 | Parse *pParse; /* Parsing context */ |
| 107465 | Vdbe *v; /* The prepared stmt under constructions */ |
| 107466 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
| @@ -108546,17 +107470,18 @@ | |
| 107470 | int iReleaseReg = 0; /* Temp register to free before returning */ |
| 107471 | Bitmask newNotReady; /* Return value */ |
| 107472 | |
| 107473 | pParse = pWInfo->pParse; |
| 107474 | v = pParse->pVdbe; |
| 107475 | pWC = &pWInfo->sWC; |
| 107476 | pLevel = &pWInfo->a[iLevel]; |
| 107477 | pLoop = pLevel->pWLoop; |
| 107478 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 107479 | iCur = pTabItem->iCursor; |
| 107480 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 107481 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
| 107482 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
| 107483 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
| 107484 | |
| 107485 | /* Create labels for the "break" and "continue" instructions |
| 107486 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 107487 | ** Jump to cont to go immediately to the next iteration of the |
| @@ -108589,51 +107514,41 @@ | |
| 107514 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 107515 | pLevel->op = OP_Goto; |
| 107516 | }else |
| 107517 | |
| 107518 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 107519 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 107520 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
| 107521 | ** to access the data. |
| 107522 | */ |
| 107523 | int iReg; /* P3 Value for OP_VFilter */ |
| 107524 | int addrNotFound; |
| 107525 | int nConstraint = pLoop->nLTerm; |
| 107526 | |
| 107527 | sqlite3ExprCachePush(pParse); |
| 107528 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 107529 | addrNotFound = pLevel->addrBrk; |
| 107530 | for(j=0; j<nConstraint; j++){ |
| 107531 | int iTarget = iReg+j+2; |
| 107532 | pTerm = pLoop->aLTerm[j]; |
| 107533 | if( pTerm==0 ) continue; |
| 107534 | if( pTerm->eOperator & WO_IN ){ |
| 107535 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 107536 | addrNotFound = pLevel->addrNxt; |
| 107537 | }else{ |
| 107538 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 107539 | } |
| 107540 | } |
| 107541 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| 107542 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
| 107543 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 107544 | pLoop->u.vtab.idxStr, |
| 107545 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 107546 | pLoop->u.vtab.needFree = 0; |
| 107547 | for(j=0; j<nConstraint && j<16; j++){ |
| 107548 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
| 107549 | disableTerm(pLevel, pLoop->aLTerm[j]); |
| 107550 | } |
| 107551 | } |
| 107552 | pLevel->op = OP_VNext; |
| 107553 | pLevel->p1 = iCur; |
| 107554 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| @@ -108640,41 +107555,48 @@ | |
| 107555 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
| 107556 | sqlite3ExprCachePop(pParse, 1); |
| 107557 | }else |
| 107558 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 107559 | |
| 107560 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 107561 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 107562 | ){ |
| 107563 | /* Case 2: We can directly reference a single row using an |
| 107564 | ** equality comparison against the ROWID field. Or |
| 107565 | ** we reference multiple rows using a "rowid IN (...)" |
| 107566 | ** construct. |
| 107567 | */ |
| 107568 | assert( pLoop->u.btree.nEq==1 ); |
| 107569 | iReleaseReg = sqlite3GetTempReg(pParse); |
| 107570 | pTerm = pLoop->aLTerm[0]; |
| 107571 | assert( pTerm!=0 ); |
| 107572 | assert( pTerm->pExpr!=0 ); |
| 107573 | assert( omitTable==0 ); |
| 107574 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 107575 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
| 107576 | addrNxt = pLevel->addrNxt; |
| 107577 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 107578 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
| 107579 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
| 107580 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 107581 | VdbeComment((v, "pk")); |
| 107582 | pLevel->op = OP_Noop; |
| 107583 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 107584 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 107585 | ){ |
| 107586 | /* Case 3: We have an inequality comparison against the ROWID field. |
| 107587 | */ |
| 107588 | int testOp = OP_Noop; |
| 107589 | int start; |
| 107590 | int memEndValue = 0; |
| 107591 | WhereTerm *pStart, *pEnd; |
| 107592 | |
| 107593 | assert( omitTable==0 ); |
| 107594 | j = 0; |
| 107595 | pStart = pEnd = 0; |
| 107596 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 107597 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
| 107598 | if( bRev ){ |
| 107599 | pTerm = pStart; |
| 107600 | pStart = pEnd; |
| 107601 | pEnd = pTerm; |
| 107602 | } |
| @@ -108737,12 +107659,12 @@ | |
| 107659 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
| 107660 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 107661 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 107662 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
| 107663 | } |
| 107664 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 107665 | /* Case 4: A scan using an index. |
| 107666 | ** |
| 107667 | ** The WHERE clause may contain zero or more equality |
| 107668 | ** terms ("==" or "IN" operators) that refer to the N |
| 107669 | ** left-most columns of the index. It may also contain |
| 107670 | ** inequality constraints (>, <, >= or <=) on the indexed |
| @@ -108784,12 +107706,12 @@ | |
| 107706 | static const u8 aEndOp[] = { |
| 107707 | OP_Noop, /* 0: (!end_constraints) */ |
| 107708 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 107709 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 107710 | }; |
| 107711 | int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 107712 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
| 107713 | int regBase; /* Base register holding constraint values */ |
| 107714 | int r1; /* Temp register */ |
| 107715 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 107716 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 107717 | int startEq; /* True if range start uses ==, >= or <= */ |
| @@ -108801,11 +107723,11 @@ | |
| 107723 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 107724 | int op; /* Instruction opcode */ |
| 107725 | char *zStartAff; /* Affinity for start of range constraint */ |
| 107726 | char *zEndAff; /* Affinity for end of range constraint */ |
| 107727 | |
| 107728 | pIdx = pLoop->u.btree.pIndex; |
| 107729 | iIdxCur = pLevel->iIdxCur; |
| 107730 | k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]); |
| 107731 | |
| 107732 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 107733 | ** was passed to this function to implement a "SELECT min(x) ..." |
| @@ -108813,12 +107735,12 @@ | |
| 107735 | ** a single iteration. This means that the first row returned |
| 107736 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 107737 | ** the first one after the nEq equality constraints in the index, |
| 107738 | ** this requires some special handling. |
| 107739 | */ |
| 107740 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 107741 | && (pWInfo->bOBSat!=0) |
| 107742 | && (pIdx->nColumn>nEq) |
| 107743 | ){ |
| 107744 | /* assert( pOrderBy->nExpr==1 ); */ |
| 107745 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 107746 | isMinQuery = 1; |
| @@ -108826,25 +107748,26 @@ | |
| 107748 | } |
| 107749 | |
| 107750 | /* Find any inequality constraint terms for the start and end |
| 107751 | ** of the range. |
| 107752 | */ |
| 107753 | j = nEq; |
| 107754 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
| 107755 | pRangeStart = pLoop->aLTerm[j++]; |
| 107756 | nExtraReg = 1; |
| 107757 | } |
| 107758 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
| 107759 | pRangeEnd = pLoop->aLTerm[j++]; |
| 107760 | nExtraReg = 1; |
| 107761 | } |
| 107762 | |
| 107763 | /* Generate code to evaluate all constraint terms using == or IN |
| 107764 | ** and store the values of those terms in an array of registers |
| 107765 | ** starting at regBase. |
| 107766 | */ |
| 107767 | regBase = codeAllEqualityTerms( |
| 107768 | pParse, pLevel, pWC, notReady, bRev, nExtraReg, &zStartAff |
| 107769 | ); |
| 107770 | zEndAff = sqlite3DbStrDup(pParse->db, zStartAff); |
| 107771 | addrNxt = pLevel->addrNxt; |
| 107772 | |
| 107773 | /* If we are doing a reverse order scan on an ascending index, or |
| @@ -108948,13 +107871,13 @@ | |
| 107871 | /* If there are inequality constraints, check that the value |
| 107872 | ** of the table column that the inequality contrains is not NULL. |
| 107873 | ** If it is, jump to the next iteration of the loop. |
| 107874 | */ |
| 107875 | r1 = sqlite3GetTempReg(pParse); |
| 107876 | testcase( pLoop->wsFlags & WHERE_BTM_LIMIT ); |
| 107877 | testcase( pLoop->wsFlags & WHERE_TOP_LIMIT ); |
| 107878 | if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
| 107879 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 107880 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 107881 | } |
| 107882 | sqlite3ReleaseTempReg(pParse, r1); |
| 107883 | |
| @@ -108969,28 +107892,28 @@ | |
| 107892 | } |
| 107893 | |
| 107894 | /* Record the instruction used to terminate the loop. Disable |
| 107895 | ** WHERE clause terms made redundant by the index range scan. |
| 107896 | */ |
| 107897 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
| 107898 | pLevel->op = OP_Noop; |
| 107899 | }else if( bRev ){ |
| 107900 | pLevel->op = OP_Prev; |
| 107901 | }else{ |
| 107902 | pLevel->op = OP_Next; |
| 107903 | } |
| 107904 | pLevel->p1 = iIdxCur; |
| 107905 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
| 107906 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 107907 | }else{ |
| 107908 | assert( pLevel->p5==0 ); |
| 107909 | } |
| 107910 | }else |
| 107911 | |
| 107912 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
| 107913 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 107914 | /* Case 5: Two or more separately indexed terms connected by OR |
| 107915 | ** |
| 107916 | ** Example: |
| 107917 | ** |
| 107918 | ** CREATE TABLE t1(a,b,c,d); |
| 107919 | ** CREATE INDEX i1 ON t1(a); |
| @@ -109039,11 +107962,11 @@ | |
| 107962 | int iRetInit; /* Address of regReturn init */ |
| 107963 | int untestedTerms = 0; /* Some terms not completely tested */ |
| 107964 | int ii; /* Loop counter */ |
| 107965 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
| 107966 | |
| 107967 | pTerm = pLoop->aLTerm[0]; |
| 107968 | assert( pTerm!=0 ); |
| 107969 | assert( pTerm->eOperator & WO_OR ); |
| 107970 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 107971 | pOrWc = &pTerm->u.pOrInfo->wc; |
| 107972 | pLevel->op = OP_Return; |
| @@ -109080,11 +108003,11 @@ | |
| 108003 | ** over the top of the loop into the body of it. In this case the |
| 108004 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 108005 | ** fall through to the next instruction, just as an OP_Next does if |
| 108006 | ** called on an uninitialized cursor. |
| 108007 | */ |
| 108008 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 108009 | regRowset = ++pParse->nMem; |
| 108010 | regRowid = ++pParse->nMem; |
| 108011 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 108012 | } |
| 108013 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| @@ -109131,15 +108054,15 @@ | |
| 108054 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 108055 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
| 108056 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
| 108057 | assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed ); |
| 108058 | if( pSubWInfo ){ |
| 108059 | WhereLoop *pSubLoop; |
| 108060 | explainOneScan( |
| 108061 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 108062 | ); |
| 108063 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 108064 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 108065 | int r; |
| 108066 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
| 108067 | regRowid, 0); |
| 108068 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| @@ -109164,17 +108087,17 @@ | |
| 108087 | ** processed or the index is the same as that used by all previous |
| 108088 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 108089 | ** pCov to NULL to indicate that no candidate covering index will |
| 108090 | ** be available. |
| 108091 | */ |
| 108092 | pSubLoop = pSubWInfo->a[0].pWLoop; |
| 108093 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
| 108094 | && (pSubLoop->wsFlags & WHERE_TEMP_INDEX)==0 |
| 108095 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
| 108096 | ){ |
| 108097 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
| 108098 | pCov = pSubLoop->u.btree.pIndex; |
| 108099 | }else{ |
| 108100 | pCov = 0; |
| 108101 | } |
| 108102 | |
| 108103 | /* Finish the loop through table entries that match term pOrTerm. */ |
| @@ -109196,23 +108119,22 @@ | |
| 108119 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 108120 | }else |
| 108121 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 108122 | |
| 108123 | { |
| 108124 | /* Case 6: There is no usable index. We must do a complete |
| 108125 | ** scan of the entire table. |
| 108126 | */ |
| 108127 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 108128 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 108129 | assert( bRev==0 || bRev==1 ); |
| 108130 | pLevel->op = aStep[bRev]; |
| 108131 | pLevel->p1 = iCur; |
| 108132 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
| 108133 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 108134 | } |
| 108135 | newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
| 108136 | |
| 108137 | /* Insert code to test every subexpression that can be completely |
| 108138 | ** computed using the current set of tables. |
| 108139 | ** |
| 108140 | ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through |
| @@ -109290,51 +108212,1529 @@ | |
| 108212 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
| 108213 | |
| 108214 | return newNotReady; |
| 108215 | } |
| 108216 | |
| 108217 | #ifdef WHERETRACE_ENABLED |
| 108218 | /* |
| 108219 | ** Print a WhereLoop object for debugging purposes |
| 108220 | */ |
| 108221 | static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){ |
| 108222 | int nb = 1+(pTabList->nSrc+7)/8; |
| 108223 | struct SrcList_item *pItem = pTabList->a + p->iTab; |
| 108224 | Table *pTab = pItem->pTab; |
| 108225 | sqlite3DebugPrintf("%c %2d.%0*llx.%0*llx", p->cId, |
| 108226 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
| 108227 | sqlite3DebugPrintf(" %8s", |
| 108228 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
| 108229 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 108230 | if( p->u.btree.pIndex ){ |
| 108231 | const char *zName = p->u.btree.pIndex->zName; |
| 108232 | if( zName==0 ) zName = "ipk"; |
| 108233 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 108234 | int i = sqlite3Strlen30(zName) - 1; |
| 108235 | while( zName[i]!='_' ) i--; |
| 108236 | zName += i; |
| 108237 | } |
| 108238 | sqlite3DebugPrintf(".%-12s %2d", zName, p->u.btree.nEq); |
| 108239 | }else{ |
| 108240 | sqlite3DebugPrintf("%16s",""); |
| 108241 | } |
| 108242 | }else{ |
| 108243 | char *z; |
| 108244 | if( p->u.vtab.idxStr ){ |
| 108245 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 108246 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
| 108247 | }else{ |
| 108248 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
| 108249 | } |
| 108250 | sqlite3DebugPrintf(" %-15s", z); |
| 108251 | sqlite3_free(z); |
| 108252 | } |
| 108253 | sqlite3DebugPrintf(" fg %05x N %d", p->wsFlags, p->nLTerm); |
| 108254 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 108255 | } |
| 108256 | #endif |
| 108257 | |
| 108258 | /* |
| 108259 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 108260 | ** to whereLoopClear harmlessly. |
| 108261 | */ |
| 108262 | static void whereLoopInit(WhereLoop *p){ |
| 108263 | p->aLTerm = p->aLTermSpace; |
| 108264 | p->nLTerm = 0; |
| 108265 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 108266 | p->wsFlags = 0; |
| 108267 | } |
| 108268 | |
| 108269 | /* |
| 108270 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 108271 | */ |
| 108272 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
| 108273 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_TEMP_INDEX) ){ |
| 108274 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 108275 | sqlite3_free(p->u.vtab.idxStr); |
| 108276 | p->u.vtab.needFree = 0; |
| 108277 | p->u.vtab.idxStr = 0; |
| 108278 | }else if( (p->wsFlags & WHERE_TEMP_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
| 108279 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 108280 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 108281 | p->u.btree.pIndex = 0; |
| 108282 | } |
| 108283 | } |
| 108284 | } |
| 108285 | |
| 108286 | /* |
| 108287 | ** Deallocate internal memory used by a WhereLoop object |
| 108288 | */ |
| 108289 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 108290 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 108291 | whereLoopClearUnion(db, p); |
| 108292 | whereLoopInit(p); |
| 108293 | } |
| 108294 | |
| 108295 | /* |
| 108296 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 108297 | */ |
| 108298 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 108299 | WhereTerm **paNew; |
| 108300 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 108301 | n = (n+7)&~7; |
| 108302 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 108303 | if( paNew==0 ) return SQLITE_NOMEM; |
| 108304 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 108305 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 108306 | p->aLTerm = paNew; |
| 108307 | p->nLSlot = n; |
| 108308 | return SQLITE_OK; |
| 108309 | } |
| 108310 | |
| 108311 | /* |
| 108312 | ** Transfer content from the second pLoop into the first. |
| 108313 | */ |
| 108314 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
| 108315 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ) return SQLITE_NOMEM; |
| 108316 | whereLoopClearUnion(db, pTo); |
| 108317 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 108318 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
| 108319 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 108320 | pFrom->u.vtab.needFree = 0; |
| 108321 | }else if( (pFrom->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 108322 | pFrom->u.btree.pIndex = 0; |
| 108323 | } |
| 108324 | return SQLITE_OK; |
| 108325 | } |
| 108326 | |
| 108327 | /* |
| 108328 | ** Delete a WhereLoop object |
| 108329 | */ |
| 108330 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
| 108331 | whereLoopClear(db, p); |
| 108332 | sqlite3DbFree(db, p); |
| 108333 | } |
| 108334 | |
| 108335 | /* |
| 108336 | ** Free a WhereInfo structure |
| 108337 | */ |
| 108338 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
| 108339 | if( ALWAYS(pWInfo) ){ |
| 108340 | whereClauseClear(&pWInfo->sWC); |
| 108341 | while( pWInfo->pLoops ){ |
| 108342 | WhereLoop *p = pWInfo->pLoops; |
| 108343 | pWInfo->pLoops = p->pNextLoop; |
| 108344 | whereLoopDelete(db, p); |
| 108345 | } |
| 108346 | sqlite3DbFree(db, pWInfo); |
| 108347 | } |
| 108348 | } |
| 108349 | |
| 108350 | /* |
| 108351 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 108352 | ** |
| 108353 | ** An existing WhereLoop entry might be overwritten if the new template |
| 108354 | ** is better and has fewer dependencies. Or the template will be ignored |
| 108355 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 108356 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
| 108357 | ** added based on the template. |
| 108358 | ** |
| 108359 | ** If pBuilder->pBest is not NULL then we only care about the very |
| 108360 | ** best template and that template should be stored in pBuilder->pBest. |
| 108361 | ** If pBuilder->pBest is NULL then a list of the best templates are stored |
| 108362 | ** in pBuilder->pWInfo->pLoops. |
| 108363 | ** |
| 108364 | ** When accumulating multiple loops (when pBuilder->pBest is NULL) we |
| 108365 | ** still might overwrite similar loops with the new template if the |
| 108366 | ** template is better. Loops may be overwritten if the following |
| 108367 | ** conditions are met: |
| 108368 | ** |
| 108369 | ** (1) They have the same iTab. |
| 108370 | ** (2) They have the same iSortIdx. |
| 108371 | ** (3) The template has same or fewer dependencies than the current loop |
| 108372 | ** (4) The template has the same or lower cost than the current loop |
| 108373 | ** (5) The template uses more terms of the same index but has no additional |
| 108374 | ** dependencies |
| 108375 | */ |
| 108376 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
| 108377 | WhereLoop **ppPrev, *p, *pNext = 0; |
| 108378 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 108379 | sqlite3 *db = pWInfo->pParse->db; |
| 108380 | |
| 108381 | /* If pBuilder->pBest is defined, then only keep track of the single |
| 108382 | ** best WhereLoop. pBuilder->pBest->maskSelf==0 indicates that no |
| 108383 | ** prior WhereLoops have been evaluated and that the current pTemplate |
| 108384 | ** is therefore the first and hence the best and should be retained. |
| 108385 | */ |
| 108386 | if( (p = pBuilder->pBest)!=0 ){ |
| 108387 | if( p->maskSelf!=0 ){ |
| 108388 | WhereCost rCost = whereCostAdd(p->rRun,p->rSetup); |
| 108389 | WhereCost rTemplate = whereCostAdd(pTemplate->rRun,pTemplate->rSetup); |
| 108390 | if( rCost < rTemplate ){ |
| 108391 | goto whereLoopInsert_noop; |
| 108392 | } |
| 108393 | if( rCost == rTemplate && p->prereq <= pTemplate->prereq ){ |
| 108394 | goto whereLoopInsert_noop; |
| 108395 | } |
| 108396 | } |
| 108397 | #if WHERETRACE_ENABLED |
| 108398 | if( sqlite3WhereTrace & 0x8 ){ |
| 108399 | sqlite3DebugPrintf(p->maskSelf==0 ? "ins-init: " : "ins-best: "); |
| 108400 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
| 108401 | } |
| 108402 | #endif |
| 108403 | whereLoopXfer(db, p, pTemplate); |
| 108404 | return SQLITE_OK; |
| 108405 | } |
| 108406 | |
| 108407 | /* Search for an existing WhereLoop to overwrite, or which takes |
| 108408 | ** priority over pTemplate. |
| 108409 | */ |
| 108410 | for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
| 108411 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ) continue; |
| 108412 | if( (p->prereq & pTemplate->prereq)==p->prereq |
| 108413 | && p->rSetup<=pTemplate->rSetup |
| 108414 | && p->rRun<=pTemplate->rRun |
| 108415 | ){ |
| 108416 | /* p is equal or better than pTemplate */ |
| 108417 | if( p->nLTerm<pTemplate->nLTerm |
| 108418 | && (p->wsFlags & WHERE_INDEXED)!=0 |
| 108419 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 108420 | && p->u.btree.pIndex==pTemplate->u.btree.pIndex |
| 108421 | && p->prereq==pTemplate->prereq |
| 108422 | ){ |
| 108423 | /* Overwrite an existing WhereLoop with an similar one that uses |
| 108424 | ** more terms of the index */ |
| 108425 | pNext = p->pNextLoop; |
| 108426 | break; |
| 108427 | }else if( p->nOut>pTemplate->nOut |
| 108428 | && p->rSetup==pTemplate->rSetup |
| 108429 | && p->rRun==pTemplate->rRun |
| 108430 | ){ |
| 108431 | /* Overwrite an existing WhereLoop with the same cost but more |
| 108432 | ** outputs */ |
| 108433 | pNext = p->pNextLoop; |
| 108434 | break; |
| 108435 | }else{ |
| 108436 | /* pTemplate is not helpful. |
| 108437 | ** Return without changing or adding anything */ |
| 108438 | goto whereLoopInsert_noop; |
| 108439 | } |
| 108440 | } |
| 108441 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 108442 | && p->rSetup>=pTemplate->rSetup |
| 108443 | && p->rRun>=pTemplate->rRun |
| 108444 | ){ |
| 108445 | /* Overwrite an existing WhereLoop with a better one */ |
| 108446 | pNext = p->pNextLoop; |
| 108447 | break; |
| 108448 | } |
| 108449 | } |
| 108450 | |
| 108451 | /* If we reach this point it means that either p[] should be overwritten |
| 108452 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 108453 | ** WhereLoop and insert it. |
| 108454 | */ |
| 108455 | #if WHERETRACE_ENABLED |
| 108456 | if( sqlite3WhereTrace & 0x8 ){ |
| 108457 | if( p!=0 ){ |
| 108458 | sqlite3DebugPrintf("ins-del: "); |
| 108459 | whereLoopPrint(p, pWInfo->pTabList); |
| 108460 | } |
| 108461 | sqlite3DebugPrintf("ins-new: "); |
| 108462 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
| 108463 | } |
| 108464 | #endif |
| 108465 | if( p==0 ){ |
| 108466 | p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
| 108467 | if( p==0 ) return SQLITE_NOMEM; |
| 108468 | whereLoopInit(p); |
| 108469 | } |
| 108470 | whereLoopXfer(db, p, pTemplate); |
| 108471 | p->pNextLoop = pNext; |
| 108472 | *ppPrev = p; |
| 108473 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 108474 | Index *pIndex = p->u.btree.pIndex; |
| 108475 | if( pIndex && pIndex->tnum==0 ){ |
| 108476 | p->u.btree.pIndex = 0; |
| 108477 | } |
| 108478 | } |
| 108479 | return SQLITE_OK; |
| 108480 | |
| 108481 | /* Jump here if the insert is a no-op */ |
| 108482 | whereLoopInsert_noop: |
| 108483 | #if WHERETRACE_ENABLED |
| 108484 | if( sqlite3WhereTrace & 0x8 ){ |
| 108485 | sqlite3DebugPrintf(pBuilder->pBest ? "ins-skip: " : "ins-noop: "); |
| 108486 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
| 108487 | } |
| 108488 | #endif |
| 108489 | return SQLITE_OK; |
| 108490 | } |
| 108491 | |
| 108492 | /* |
| 108493 | ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex. |
| 108494 | ** Try to match one more. |
| 108495 | ** |
| 108496 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 108497 | ** INTEGER PRIMARY KEY. |
| 108498 | */ |
| 108499 | static int whereLoopAddBtreeIndex( |
| 108500 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 108501 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 108502 | Index *pProbe, /* An index on pSrc */ |
| 108503 | WhereCost nInMul /* log(Number of iterations due to IN) */ |
| 108504 | ){ |
| 108505 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 108506 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 108507 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
| 108508 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 108509 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
| 108510 | int opMask; /* Valid operators for constraints */ |
| 108511 | WhereScan scan; /* Iterator for WHERE terms */ |
| 108512 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 108513 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 108514 | int saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 108515 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 108516 | WhereCost saved_nOut; /* Original value of pNew->nOut */ |
| 108517 | int iCol; /* Index of the column in the table */ |
| 108518 | int rc = SQLITE_OK; /* Return code */ |
| 108519 | WhereCost nRowEst; /* Estimated index selectivity */ |
| 108520 | WhereCost rLogSize; /* Logarithm of table size */ |
| 108521 | WhereTerm *pTop, *pBtm; /* Top and bottom range constraints */ |
| 108522 | |
| 108523 | pNew = pBuilder->pNew; |
| 108524 | if( db->mallocFailed ) return SQLITE_NOMEM; |
| 108525 | |
| 108526 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 108527 | assert( pNew->u.btree.nEq<=pProbe->nColumn ); |
| 108528 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 108529 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 108530 | opMask = WO_LT|WO_LE; |
| 108531 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 108532 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
| 108533 | }else{ |
| 108534 | opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE; |
| 108535 | } |
| 108536 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
| 108537 | |
| 108538 | if( pNew->u.btree.nEq < pProbe->nColumn ){ |
| 108539 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 108540 | nRowEst = whereCost(pProbe->aiRowEst[pNew->u.btree.nEq+1]); |
| 108541 | }else{ |
| 108542 | iCol = -1; |
| 108543 | nRowEst = 0; |
| 108544 | } |
| 108545 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
| 108546 | opMask, pProbe); |
| 108547 | saved_nEq = pNew->u.btree.nEq; |
| 108548 | saved_nLTerm = pNew->nLTerm; |
| 108549 | saved_wsFlags = pNew->wsFlags; |
| 108550 | saved_prereq = pNew->prereq; |
| 108551 | saved_nOut = pNew->nOut; |
| 108552 | pNew->rSetup = 0; |
| 108553 | rLogSize = estLog(whereCost(pProbe->aiRowEst[0])); |
| 108554 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
| 108555 | int nIn = 0; |
| 108556 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 108557 | pNew->wsFlags = saved_wsFlags; |
| 108558 | pNew->u.btree.nEq = saved_nEq; |
| 108559 | pNew->nLTerm = saved_nLTerm; |
| 108560 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 108561 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 108562 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
| 108563 | pNew->rRun = rLogSize; /* Baseline cost is log2(N). Adjustments below */ |
| 108564 | if( pTerm->eOperator & WO_IN ){ |
| 108565 | Expr *pExpr = pTerm->pExpr; |
| 108566 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 108567 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 108568 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
| 108569 | nIn = 46; assert( 46==whereCost(25) ); |
| 108570 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 108571 | /* "x IN (value, value, ...)" */ |
| 108572 | nIn = whereCost(pExpr->x.pList->nExpr); |
| 108573 | } |
| 108574 | pNew->rRun += nIn; |
| 108575 | pNew->u.btree.nEq++; |
| 108576 | pNew->nOut = nRowEst + nInMul + nIn; |
| 108577 | }else if( pTerm->eOperator & (WO_EQ) ){ |
| 108578 | assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 |
| 108579 | || nInMul==0 ); |
| 108580 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
| 108581 | if( iCol<0 |
| 108582 | || (pProbe->onError!=OE_None && nInMul==0 |
| 108583 | && pNew->u.btree.nEq==pProbe->nColumn-1) |
| 108584 | ){ |
| 108585 | testcase( pNew->wsFlags & WHERE_COLUMN_IN ); |
| 108586 | pNew->wsFlags |= WHERE_ONEROW; |
| 108587 | } |
| 108588 | pNew->u.btree.nEq++; |
| 108589 | pNew->nOut = nRowEst + nInMul; |
| 108590 | }else if( pTerm->eOperator & (WO_ISNULL) ){ |
| 108591 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 108592 | pNew->u.btree.nEq++; |
| 108593 | /* TUNING: IS NULL selects 2 rows */ |
| 108594 | nIn = 10; assert( 10==whereCost(2) ); |
| 108595 | pNew->nOut = nRowEst + nInMul + nIn; |
| 108596 | }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ |
| 108597 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
| 108598 | pBtm = pTerm; |
| 108599 | pTop = 0; |
| 108600 | }else if( pTerm->eOperator & (WO_LT|WO_LE) ){ |
| 108601 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
| 108602 | pTop = pTerm; |
| 108603 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
| 108604 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
| 108605 | } |
| 108606 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 108607 | /* Adjust nOut and rRun for STAT3 range values */ |
| 108608 | WhereCost rDiv; |
| 108609 | whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq, |
| 108610 | pBtm, pTop, &rDiv); |
| 108611 | pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10; |
| 108612 | } |
| 108613 | #ifdef SQLITE_ENABLE_STAT3 |
| 108614 | if( pNew->u.btree.nEq==1 && pProbe->nSample ){ |
| 108615 | tRowcnt nOut = 0; |
| 108616 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ |
| 108617 | rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut); |
| 108618 | }else if( (pTerm->eOperator & WO_IN) |
| 108619 | && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){ |
| 108620 | rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut); |
| 108621 | } |
| 108622 | pNew->nOut = whereCost(nOut); |
| 108623 | } |
| 108624 | #endif |
| 108625 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
| 108626 | /* Each row involves a step of the index, then a binary search of |
| 108627 | ** the main table */ |
| 108628 | pNew->rRun = whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10); |
| 108629 | } |
| 108630 | /* Step cost for each output row */ |
| 108631 | pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut); |
| 108632 | /* TBD: Adjust nOut for additional constraints */ |
| 108633 | rc = whereLoopInsert(pBuilder, pNew); |
| 108634 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
| 108635 | && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0)) |
| 108636 | ){ |
| 108637 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
| 108638 | } |
| 108639 | } |
| 108640 | pNew->prereq = saved_prereq; |
| 108641 | pNew->u.btree.nEq = saved_nEq; |
| 108642 | pNew->wsFlags = saved_wsFlags; |
| 108643 | pNew->nOut = saved_nOut; |
| 108644 | pNew->nLTerm = saved_nLTerm; |
| 108645 | return rc; |
| 108646 | } |
| 108647 | |
| 108648 | /* |
| 108649 | ** Return True if it is possible that pIndex might be useful in |
| 108650 | ** implementing the ORDER BY clause in pBuilder. |
| 108651 | ** |
| 108652 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 108653 | ** if there is no way for pIndex to be useful in implementing that |
| 108654 | ** ORDER BY clause. |
| 108655 | */ |
| 108656 | static int indexMightHelpWithOrderBy( |
| 108657 | WhereLoopBuilder *pBuilder, |
| 108658 | Index *pIndex, |
| 108659 | int iCursor |
| 108660 | ){ |
| 108661 | ExprList *pOB; |
| 108662 | int ii, jj; |
| 108663 | |
| 108664 | if( pIndex->bUnordered ) return 0; |
| 108665 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
| 108666 | for(ii=0; ii<pOB->nExpr; ii++){ |
| 108667 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
| 108668 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 108669 | if( pExpr->iTable==iCursor ){ |
| 108670 | for(jj=0; jj<pIndex->nColumn; jj++){ |
| 108671 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 108672 | } |
| 108673 | } |
| 108674 | } |
| 108675 | return 0; |
| 108676 | } |
| 108677 | |
| 108678 | /* |
| 108679 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 108680 | ** the table is used by an index. Only the first 63 columns are considered. |
| 108681 | */ |
| 108682 | static Bitmask columnsInIndex(Index *pIdx){ |
| 108683 | Bitmask m = 0; |
| 108684 | int j; |
| 108685 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 108686 | int x = pIdx->aiColumn[j]; |
| 108687 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 108688 | } |
| 108689 | return m; |
| 108690 | } |
| 108691 | |
| 108692 | |
| 108693 | /* |
| 108694 | ** Add all WhereLoop objects a single table of the join were the table |
| 108695 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 108696 | ** a b-tree table, not a virtual table. |
| 108697 | */ |
| 108698 | static int whereLoopAddBtree( |
| 108699 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 108700 | Bitmask mExtra /* Extra prerequesites for using this table */ |
| 108701 | ){ |
| 108702 | WhereInfo *pWInfo; /* WHERE analysis context */ |
| 108703 | Index *pProbe; /* An index we are evaluating */ |
| 108704 | Index sPk; /* A fake index object for the primary key */ |
| 108705 | tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 108706 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 108707 | SrcList *pTabList; /* The FROM clause */ |
| 108708 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
| 108709 | WhereLoop *pNew; /* Template WhereLoop object */ |
| 108710 | int rc = SQLITE_OK; /* Return code */ |
| 108711 | int iSortIdx = 1; /* Index number */ |
| 108712 | int b; /* A boolean value */ |
| 108713 | WhereCost rSize; /* number of rows in the table */ |
| 108714 | WhereCost rLogSize; /* Logarithm of the number of rows in the table */ |
| 108715 | |
| 108716 | pNew = pBuilder->pNew; |
| 108717 | pWInfo = pBuilder->pWInfo; |
| 108718 | pTabList = pWInfo->pTabList; |
| 108719 | pSrc = pTabList->a + pNew->iTab; |
| 108720 | assert( !IsVirtual(pSrc->pTab) ); |
| 108721 | |
| 108722 | if( pSrc->pIndex ){ |
| 108723 | /* An INDEXED BY clause specifies a particular index to use */ |
| 108724 | pProbe = pSrc->pIndex; |
| 108725 | }else{ |
| 108726 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 108727 | ** variable sPk to represent the rowid primary key index. Make this |
| 108728 | ** fake index the first in a chain of Index objects with all of the real |
| 108729 | ** indices to follow */ |
| 108730 | Index *pFirst; /* First of real indices on the table */ |
| 108731 | memset(&sPk, 0, sizeof(Index)); |
| 108732 | sPk.nColumn = 1; |
| 108733 | sPk.aiColumn = &aiColumnPk; |
| 108734 | sPk.aiRowEst = aiRowEstPk; |
| 108735 | sPk.onError = OE_Replace; |
| 108736 | sPk.pTable = pSrc->pTab; |
| 108737 | aiRowEstPk[0] = pSrc->pTab->nRowEst; |
| 108738 | aiRowEstPk[1] = 1; |
| 108739 | pFirst = pSrc->pTab->pIndex; |
| 108740 | if( pSrc->notIndexed==0 ){ |
| 108741 | /* The real indices of the table are only considered if the |
| 108742 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 108743 | sPk.pNext = pFirst; |
| 108744 | } |
| 108745 | pProbe = &sPk; |
| 108746 | } |
| 108747 | rSize = whereCost(pSrc->pTab->nRowEst); |
| 108748 | rLogSize = estLog(rSize); |
| 108749 | |
| 108750 | /* Automatic indexes */ |
| 108751 | if( !pBuilder->pBest |
| 108752 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 108753 | && pSrc->pIndex==0 |
| 108754 | && !pSrc->viaCoroutine |
| 108755 | && !pSrc->notIndexed |
| 108756 | && !pSrc->isCorrelated |
| 108757 | ){ |
| 108758 | /* Generate auto-index WhereLoops */ |
| 108759 | WhereClause *pWC = pBuilder->pWC; |
| 108760 | WhereTerm *pTerm; |
| 108761 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 108762 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| 108763 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 108764 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 108765 | pNew->u.btree.nEq = 1; |
| 108766 | pNew->u.btree.pIndex = 0; |
| 108767 | pNew->nLTerm = 1; |
| 108768 | pNew->aLTerm[0] = pTerm; |
| 108769 | /* TUNING: One-time cost for computing the automatic index is |
| 108770 | ** approximately 6*N*log2(N) where N is the number of rows in |
| 108771 | ** the table being indexed. */ |
| 108772 | pNew->rSetup = rLogSize + rSize + 26; assert( 26==whereCost(6) ); |
| 108773 | /* TUNING: Each index lookup yields 10 rows in the table */ |
| 108774 | pNew->nOut = 33; assert( 33==whereCost(10) ); |
| 108775 | pNew->rRun = whereCostAdd(rLogSize,pNew->nOut); |
| 108776 | pNew->wsFlags = WHERE_TEMP_INDEX; |
| 108777 | pNew->prereq = mExtra | pTerm->prereqRight; |
| 108778 | rc = whereLoopInsert(pBuilder, pNew); |
| 108779 | } |
| 108780 | } |
| 108781 | } |
| 108782 | |
| 108783 | /* Loop over all indices |
| 108784 | */ |
| 108785 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
| 108786 | pNew->u.btree.nEq = 0; |
| 108787 | pNew->nLTerm = 0; |
| 108788 | pNew->iSortIdx = 0; |
| 108789 | pNew->rSetup = 0; |
| 108790 | pNew->prereq = mExtra; |
| 108791 | pNew->u.btree.pIndex = pProbe; |
| 108792 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
| 108793 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 108794 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
| 108795 | if( pProbe->tnum<=0 ){ |
| 108796 | /* Integer primary key index */ |
| 108797 | pNew->wsFlags = WHERE_IPK; |
| 108798 | |
| 108799 | /* Full table scan */ |
| 108800 | pNew->iSortIdx = b ? iSortIdx : 0; |
| 108801 | pNew->nOut = rSize; |
| 108802 | /* TUNING: Cost of full table scan is 3*(N + log2(N)). |
| 108803 | ** + The extra 3 factor is to encourage the use of indexed lookups |
| 108804 | ** over full scans. A smaller constant 2 is used for covering |
| 108805 | ** index scans so that a covering index scan will be favored over |
| 108806 | ** a table scan. */ |
| 108807 | pNew->rRun = whereCostAdd(rSize,rLogSize) + 16; |
| 108808 | rc = whereLoopInsert(pBuilder, pNew); |
| 108809 | if( rc ) break; |
| 108810 | }else{ |
| 108811 | Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe); |
| 108812 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 108813 | |
| 108814 | /* Full scan via index */ |
| 108815 | if( b |
| 108816 | || ( m==0 |
| 108817 | && pProbe->bUnordered==0 |
| 108818 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 108819 | && sqlite3GlobalConfig.bUseCis |
| 108820 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 108821 | ) |
| 108822 | ){ |
| 108823 | pNew->iSortIdx = b ? iSortIdx : 0; |
| 108824 | pNew->nOut = rSize; |
| 108825 | if( m==0 ){ |
| 108826 | /* TUNING: Cost of a covering index scan is 2*(N + log2(N)). |
| 108827 | ** + The extra 2 factor is to encourage the use of indexed lookups |
| 108828 | ** over index scans. A table scan uses a factor of 3 so that |
| 108829 | ** index scans are favored over table scans. |
| 108830 | ** + If this covering index might also help satisfy the ORDER BY |
| 108831 | ** clause, then the cost is fudged down slightly so that this |
| 108832 | ** index is favored above other indices that have no hope of |
| 108833 | ** helping with the ORDER BY. */ |
| 108834 | pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b; |
| 108835 | }else{ |
| 108836 | assert( b!=0 ); |
| 108837 | /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N) |
| 108838 | ** which we will simplify to just N*log2(N) */ |
| 108839 | pNew->rRun = rSize + rLogSize; |
| 108840 | } |
| 108841 | rc = whereLoopInsert(pBuilder, pNew); |
| 108842 | if( rc ) break; |
| 108843 | } |
| 108844 | } |
| 108845 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
| 108846 | |
| 108847 | /* If there was an INDEXED BY clause, then only that one index is |
| 108848 | ** considered. */ |
| 108849 | if( pSrc->pIndex ) break; |
| 108850 | } |
| 108851 | return rc; |
| 108852 | } |
| 108853 | |
| 108854 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 108855 | /* |
| 108856 | ** Add all WhereLoop objects for a table of the join identified by |
| 108857 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
| 108858 | */ |
| 108859 | static int whereLoopAddVirtual( |
| 108860 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 108861 | Bitmask mExtra /* Extra prerequesites for using this table */ |
| 108862 | ){ |
| 108863 | WhereInfo *pWInfo; /* WHERE analysis context */ |
| 108864 | Parse *pParse; /* The parsing context */ |
| 108865 | WhereClause *pWC; /* The WHERE clause */ |
| 108866 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 108867 | Table *pTab; |
| 108868 | sqlite3 *db; |
| 108869 | sqlite3_index_info *pIdxInfo; |
| 108870 | struct sqlite3_index_constraint *pIdxCons; |
| 108871 | struct sqlite3_index_constraint_usage *pUsage; |
| 108872 | WhereTerm *pTerm; |
| 108873 | int i, j; |
| 108874 | int iTerm, mxTerm; |
| 108875 | int nConstraint; |
| 108876 | int seenIn = 0; /* True if an IN operator is seen */ |
| 108877 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 108878 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 108879 | WhereLoop *pNew; |
| 108880 | int rc = SQLITE_OK; |
| 108881 | |
| 108882 | pWInfo = pBuilder->pWInfo; |
| 108883 | pParse = pWInfo->pParse; |
| 108884 | db = pParse->db; |
| 108885 | pWC = pBuilder->pWC; |
| 108886 | pNew = pBuilder->pNew; |
| 108887 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
| 108888 | pTab = pSrc->pTab; |
| 108889 | assert( IsVirtual(pTab) ); |
| 108890 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
| 108891 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
| 108892 | pNew->prereq = 0; |
| 108893 | pNew->rSetup = 0; |
| 108894 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
| 108895 | pNew->nLTerm = 0; |
| 108896 | pNew->u.vtab.needFree = 0; |
| 108897 | pUsage = pIdxInfo->aConstraintUsage; |
| 108898 | nConstraint = pIdxInfo->nConstraint; |
| 108899 | if( whereLoopResize(db, pNew, nConstraint) ) return SQLITE_NOMEM; |
| 108900 | |
| 108901 | for(iPhase=0; iPhase<=3; iPhase++){ |
| 108902 | if( !seenIn && (iPhase&1)!=0 ){ |
| 108903 | iPhase++; |
| 108904 | if( iPhase>3 ) break; |
| 108905 | } |
| 108906 | if( !seenVar && iPhase>1 ) break; |
| 108907 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 108908 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 108909 | j = pIdxCons->iTermOffset; |
| 108910 | pTerm = &pWC->a[j]; |
| 108911 | switch( iPhase ){ |
| 108912 | case 0: /* Constants without IN operator */ |
| 108913 | pIdxCons->usable = 0; |
| 108914 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 108915 | seenIn = 1; |
| 108916 | }else if( pTerm->prereqRight!=0 ){ |
| 108917 | seenVar = 1; |
| 108918 | }else{ |
| 108919 | pIdxCons->usable = 1; |
| 108920 | } |
| 108921 | break; |
| 108922 | case 1: /* Constants with IN operators */ |
| 108923 | assert( seenIn ); |
| 108924 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 108925 | break; |
| 108926 | case 2: /* Variables without IN */ |
| 108927 | assert( seenVar ); |
| 108928 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 108929 | break; |
| 108930 | default: /* Variables with IN */ |
| 108931 | assert( seenVar && seenIn ); |
| 108932 | pIdxCons->usable = 1; |
| 108933 | break; |
| 108934 | } |
| 108935 | } |
| 108936 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 108937 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 108938 | pIdxInfo->idxStr = 0; |
| 108939 | pIdxInfo->idxNum = 0; |
| 108940 | pIdxInfo->needToFreeIdxStr = 0; |
| 108941 | pIdxInfo->orderByConsumed = 0; |
| 108942 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
| 108943 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 108944 | if( rc ) goto whereLoopAddVtab_exit; |
| 108945 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 108946 | pNew->prereq = 0; |
| 108947 | mxTerm = -1; |
| 108948 | assert( pNew->nLSlot>=nConstraint ); |
| 108949 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
| 108950 | pNew->u.vtab.omitMask = 0; |
| 108951 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 108952 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 108953 | j = pIdxCons->iTermOffset; |
| 108954 | if( iTerm>=nConstraint |
| 108955 | || j<0 |
| 108956 | || j>=pWC->nTerm |
| 108957 | || pNew->aLTerm[iTerm]!=0 |
| 108958 | ){ |
| 108959 | rc = SQLITE_ERROR; |
| 108960 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 108961 | goto whereLoopAddVtab_exit; |
| 108962 | } |
| 108963 | pTerm = &pWC->a[j]; |
| 108964 | pNew->prereq |= pTerm->prereqRight; |
| 108965 | assert( iTerm<pNew->nLSlot ); |
| 108966 | pNew->aLTerm[iTerm] = pTerm; |
| 108967 | if( iTerm>mxTerm ) mxTerm = iTerm; |
| 108968 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
| 108969 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 108970 | if( pUsage[i].omit==0 ){ |
| 108971 | /* Do not attempt to use an IN constraint if the virtual table |
| 108972 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 108973 | ** If we do attempt to use such a constraint, some rows might be |
| 108974 | ** repeated in the output. */ |
| 108975 | break; |
| 108976 | } |
| 108977 | /* A virtual table that is constrained by an IN clause may not |
| 108978 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 108979 | ** is not necessarily related to the order of output terms and |
| 108980 | ** (2) Multiple outputs from a single IN value will not merge |
| 108981 | ** together. */ |
| 108982 | pIdxInfo->orderByConsumed = 0; |
| 108983 | } |
| 108984 | } |
| 108985 | } |
| 108986 | if( i>=nConstraint ){ |
| 108987 | pNew->nLTerm = mxTerm+1; |
| 108988 | assert( pNew->nLTerm<=pNew->nLSlot ); |
| 108989 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 108990 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 108991 | pIdxInfo->needToFreeIdxStr = 0; |
| 108992 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
| 108993 | pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) |
| 108994 | && pIdxInfo->orderByConsumed); |
| 108995 | pNew->rSetup = 0; |
| 108996 | pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost); |
| 108997 | /* TUNING: Every virtual table query returns 25 rows */ |
| 108998 | pNew->nOut = 46; assert( 46==whereCost(25) ); |
| 108999 | whereLoopInsert(pBuilder, pNew); |
| 109000 | if( pNew->u.vtab.needFree ){ |
| 109001 | sqlite3_free(pNew->u.vtab.idxStr); |
| 109002 | pNew->u.vtab.needFree = 0; |
| 109003 | } |
| 109004 | } |
| 109005 | } |
| 109006 | |
| 109007 | whereLoopAddVtab_exit: |
| 109008 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 109009 | sqlite3DbFree(db, pIdxInfo); |
| 109010 | return rc; |
| 109011 | } |
| 109012 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 109013 | |
| 109014 | /* |
| 109015 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 109016 | ** btrees or virtual tables. |
| 109017 | */ |
| 109018 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
| 109019 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 109020 | WhereClause *pWC; |
| 109021 | WhereLoop *pNew; |
| 109022 | WhereTerm *pTerm, *pWCEnd; |
| 109023 | int rc = SQLITE_OK; |
| 109024 | int iCur; |
| 109025 | WhereClause tempWC; |
| 109026 | WhereLoopBuilder sSubBuild; |
| 109027 | WhereLoop sBest; |
| 109028 | struct SrcList_item *pItem; |
| 109029 | |
| 109030 | pWC = pBuilder->pWC; |
| 109031 | if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
| 109032 | pWCEnd = pWC->a + pWC->nTerm; |
| 109033 | pNew = pBuilder->pNew; |
| 109034 | |
| 109035 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 109036 | if( (pTerm->eOperator & WO_OR)!=0 |
| 109037 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 109038 | ){ |
| 109039 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 109040 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 109041 | WhereTerm *pOrTerm; |
| 109042 | WhereCost rTotal = 0; |
| 109043 | WhereCost nRow = 0; |
| 109044 | Bitmask prereq = mExtra; |
| 109045 | |
| 109046 | whereLoopInit(&sBest); |
| 109047 | pItem = pWInfo->pTabList->a + pNew->iTab; |
| 109048 | iCur = pItem->iCursor; |
| 109049 | sSubBuild = *pBuilder; |
| 109050 | sSubBuild.pOrderBy = 0; |
| 109051 | sSubBuild.pBest = &sBest; |
| 109052 | |
| 109053 | for(pOrTerm=pOrWC->a; rc==SQLITE_OK && pOrTerm<pOrWCEnd; pOrTerm++){ |
| 109054 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 109055 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 109056 | }else if( pOrTerm->leftCursor==iCur ){ |
| 109057 | tempWC.pWInfo = pWC->pWInfo; |
| 109058 | tempWC.pOuter = pWC; |
| 109059 | tempWC.op = TK_AND; |
| 109060 | tempWC.nTerm = 1; |
| 109061 | tempWC.a = pOrTerm; |
| 109062 | sSubBuild.pWC = &tempWC; |
| 109063 | }else{ |
| 109064 | continue; |
| 109065 | } |
| 109066 | sBest.maskSelf = 0; |
| 109067 | sBest.rSetup = 0; |
| 109068 | sBest.rRun = 0; |
| 109069 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 109070 | if( IsVirtual(pItem->pTab) ){ |
| 109071 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
| 109072 | }else |
| 109073 | #endif |
| 109074 | { |
| 109075 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 109076 | } |
| 109077 | if( sBest.maskSelf==0 ) break; |
| 109078 | assert( sBest.rSetup==0 ); |
| 109079 | rTotal = whereCostAdd(rTotal, sBest.rRun); |
| 109080 | nRow = whereCostAdd(nRow, sBest.nOut); |
| 109081 | prereq |= sBest.prereq; |
| 109082 | } |
| 109083 | assert( pNew->nLSlot>=1 ); |
| 109084 | if( sBest.maskSelf ){ |
| 109085 | pNew->nLTerm = 1; |
| 109086 | pNew->aLTerm[0] = pTerm; |
| 109087 | pNew->wsFlags = WHERE_MULTI_OR; |
| 109088 | pNew->rSetup = 0; |
| 109089 | pNew->rRun = rTotal; |
| 109090 | pNew->nOut = nRow; |
| 109091 | pNew->prereq = prereq; |
| 109092 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 109093 | rc = whereLoopInsert(pBuilder, pNew); |
| 109094 | } |
| 109095 | whereLoopClear(pWInfo->pParse->db, &sBest); |
| 109096 | } |
| 109097 | } |
| 109098 | return rc; |
| 109099 | } |
| 109100 | |
| 109101 | /* |
| 109102 | ** Add all WhereLoop objects for all tables |
| 109103 | */ |
| 109104 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
| 109105 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 109106 | Bitmask mExtra = 0; |
| 109107 | Bitmask mPrior = 0; |
| 109108 | int iTab; |
| 109109 | SrcList *pTabList = pWInfo->pTabList; |
| 109110 | struct SrcList_item *pItem; |
| 109111 | sqlite3 *db = pWInfo->pParse->db; |
| 109112 | int nTabList = pWInfo->nLevel; |
| 109113 | int rc = SQLITE_OK; |
| 109114 | u8 priorJoinType = 0; |
| 109115 | WhereLoop *pNew; |
| 109116 | |
| 109117 | /* Loop over the tables in the join, from left to right */ |
| 109118 | pNew = pBuilder->pNew; |
| 109119 | whereLoopInit(pNew); |
| 109120 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
| 109121 | pNew->iTab = iTab; |
| 109122 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
| 109123 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
| 109124 | mExtra = mPrior; |
| 109125 | } |
| 109126 | priorJoinType = pItem->jointype; |
| 109127 | if( IsVirtual(pItem->pTab) ){ |
| 109128 | rc = whereLoopAddVirtual(pBuilder, mExtra); |
| 109129 | }else{ |
| 109130 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 109131 | } |
| 109132 | if( rc==SQLITE_OK ){ |
| 109133 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 109134 | } |
| 109135 | mPrior |= pNew->maskSelf; |
| 109136 | if( rc || db->mallocFailed ) break; |
| 109137 | } |
| 109138 | whereLoopClear(db, pNew); |
| 109139 | return rc; |
| 109140 | } |
| 109141 | |
| 109142 | /* |
| 109143 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
| 109144 | ** parameters) to see if it outputs rows in the requested ORDER BY |
| 109145 | ** (or GROUP BY) without requiring a separate source operation. Return: |
| 109146 | ** |
| 109147 | ** 0: ORDER BY is not satisfied. Sorting required |
| 109148 | ** 1: ORDER BY is satisfied. Omit sorting |
| 109149 | ** -1: Unknown at this time |
| 109150 | ** |
| 109151 | */ |
| 109152 | static int wherePathSatisfiesOrderBy( |
| 109153 | WhereInfo *pWInfo, /* The WHERE clause */ |
| 109154 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
| 109155 | WherePath *pPath, /* The WherePath to check */ |
| 109156 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 109157 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
| 109158 | u8 isLastLoop, /* True if pLast is the inner-most loop */ |
| 109159 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
| 109160 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
| 109161 | ){ |
| 109162 | u8 revSet; /* True if rev is known */ |
| 109163 | u8 rev; /* Composite sort order */ |
| 109164 | u8 revIdx; /* Index sort order */ |
| 109165 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 109166 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 109167 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
| 109168 | u16 nColumn; /* Number of columns in pIndex */ |
| 109169 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 109170 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 109171 | int i, j; /* Loop counters */ |
| 109172 | int iCur; /* Cursor number for current WhereLoop */ |
| 109173 | int iColumn; /* A column number within table iCur */ |
| 109174 | WhereLoop *pLoop; /* Current WhereLoop being processed. */ |
| 109175 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 109176 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 109177 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 109178 | Index *pIndex; /* The index associated with pLoop */ |
| 109179 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 109180 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 109181 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
| 109182 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
| 109183 | Bitmask ready; /* Mask of inner loops */ |
| 109184 | |
| 109185 | /* |
| 109186 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 109187 | ** row of output. A WhereLoop is one-row if all of the following are true: |
| 109188 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 109189 | ** (b) The index is unique |
| 109190 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 109191 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
| 109192 | ** |
| 109193 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 109194 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 109195 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 109196 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 109197 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 109198 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 109199 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 109200 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 109201 | ** |
| 109202 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 109203 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 109204 | ** automatically order-distinct. |
| 109205 | */ |
| 109206 | |
| 109207 | assert( pOrderBy!=0 ); |
| 109208 | |
| 109209 | /* Sortability of virtual tables is determined by the xBestIndex method |
| 109210 | ** of the virtual table itself */ |
| 109211 | if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ |
| 109212 | testcase( nLoop>0 ); /* True when outer loops are one-row and match |
| 109213 | ** no ORDER BY terms */ |
| 109214 | return pLast->u.vtab.isOrdered; |
| 109215 | } |
| 109216 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
| 109217 | |
| 109218 | nOrderBy = pOrderBy->nExpr; |
| 109219 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 109220 | isOrderDistinct = 1; |
| 109221 | obDone = MASKBIT(nOrderBy)-1; |
| 109222 | orderDistinctMask = 0; |
| 109223 | ready = 0; |
| 109224 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
| 109225 | if( iLoop>0 ) ready |= pLoop->maskSelf; |
| 109226 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
| 109227 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 109228 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
| 109229 | |
| 109230 | /* Mark off any ORDER BY term X that is a column in the table of |
| 109231 | ** the current loop for which there is term in the WHERE |
| 109232 | ** clause of the form X IS NULL or X=? that reference only outer |
| 109233 | ** loops. |
| 109234 | */ |
| 109235 | for(i=0; i<nOrderBy; i++){ |
| 109236 | if( MASKBIT(i) & obSat ) continue; |
| 109237 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 109238 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 109239 | if( pOBExpr->iTable!=iCur ) continue; |
| 109240 | pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 109241 | ~ready, WO_EQ|WO_ISNULL, 0); |
| 109242 | if( pTerm==0 ) continue; |
| 109243 | if( pOBExpr->iColumn>=0 ){ |
| 109244 | const char *z1, *z2; |
| 109245 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 109246 | if( !pColl ) pColl = db->pDfltColl; |
| 109247 | z1 = pColl->zName; |
| 109248 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 109249 | if( !pColl ) pColl = db->pDfltColl; |
| 109250 | z2 = pColl->zName; |
| 109251 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 109252 | } |
| 109253 | obSat |= MASKBIT(i); |
| 109254 | } |
| 109255 | |
| 109256 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 109257 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 109258 | pIndex = 0; |
| 109259 | nColumn = 0; |
| 109260 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
| 109261 | return 0; |
| 109262 | }else{ |
| 109263 | nColumn = pIndex->nColumn; |
| 109264 | isOrderDistinct = pIndex->onError!=OE_None; |
| 109265 | } |
| 109266 | |
| 109267 | /* For every term of the index that is constrained by == or IS NULL, |
| 109268 | ** mark off corresponding ORDER BY terms wherever they occur |
| 109269 | ** in the ORDER BY clause. |
| 109270 | */ |
| 109271 | for(i=0; i<pLoop->u.btree.nEq; i++){ |
| 109272 | pTerm = pLoop->aLTerm[i]; |
| 109273 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))==0 ) continue; |
| 109274 | iColumn = pTerm->u.leftColumn; |
| 109275 | for(j=0; j<nOrderBy; j++){ |
| 109276 | if( MASKBIT(j) & obSat ) continue; |
| 109277 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[j].pExpr); |
| 109278 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 109279 | if( pOBExpr->iTable!=iCur ) continue; |
| 109280 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 109281 | if( iColumn>=0 ){ |
| 109282 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[j].pExpr); |
| 109283 | if( !pColl ) pColl = db->pDfltColl; |
| 109284 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[i])!=0 ) continue; |
| 109285 | } |
| 109286 | obSat |= MASKBIT(j); |
| 109287 | } |
| 109288 | if( obSat==obDone ) return 1; |
| 109289 | } |
| 109290 | |
| 109291 | /* Loop through all columns of the index and deal with the ones |
| 109292 | ** that are not constrained by == or IN. |
| 109293 | */ |
| 109294 | rev = revSet = 0; |
| 109295 | distinctColumns = 0; |
| 109296 | for(j=0; j<=nColumn; j++){ |
| 109297 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 109298 | |
| 109299 | /* Skip over == and IS NULL terms */ |
| 109300 | if( j<pLoop->u.btree.nEq |
| 109301 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
| 109302 | ){ |
| 109303 | if( i & WO_ISNULL ) isOrderDistinct = 0; |
| 109304 | continue; |
| 109305 | } |
| 109306 | |
| 109307 | /* Get the column number in the table (iColumn) and sort order |
| 109308 | ** (revIdx) for the j-th column of the index. |
| 109309 | */ |
| 109310 | if( j<nColumn ){ |
| 109311 | /* Normal index columns */ |
| 109312 | iColumn = pIndex->aiColumn[j]; |
| 109313 | revIdx = pIndex->aSortOrder[j]; |
| 109314 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
| 109315 | }else{ |
| 109316 | /* The ROWID column at the end */ |
| 109317 | iColumn = -1; |
| 109318 | revIdx = 0; |
| 109319 | } |
| 109320 | |
| 109321 | /* An unconstrained column that might be NULL means that this |
| 109322 | ** WhereLoop is not well-ordered |
| 109323 | */ |
| 109324 | if( isOrderDistinct |
| 109325 | && iColumn>=0 |
| 109326 | && j>=pLoop->u.btree.nEq |
| 109327 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 109328 | ){ |
| 109329 | isOrderDistinct = 0; |
| 109330 | } |
| 109331 | |
| 109332 | /* Find the ORDER BY term that corresponds to the j-th column |
| 109333 | ** of the index and and mark that ORDER BY term off |
| 109334 | */ |
| 109335 | bOnce = 1; |
| 109336 | isMatch = 0; |
| 109337 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 109338 | if( MASKBIT(i) & obSat ) continue; |
| 109339 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 109340 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
| 109341 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 109342 | if( pOBExpr->iTable!=iCur ) continue; |
| 109343 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 109344 | if( iColumn>=0 ){ |
| 109345 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 109346 | if( !pColl ) pColl = db->pDfltColl; |
| 109347 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 109348 | } |
| 109349 | isMatch = 1; |
| 109350 | break; |
| 109351 | } |
| 109352 | if( isMatch ){ |
| 109353 | if( iColumn<0 ) distinctColumns = 1; |
| 109354 | obSat |= MASKBIT(i); |
| 109355 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 109356 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 109357 | ** Sort order is irrelevant for a GROUP BY clause. */ |
| 109358 | if( revSet ){ |
| 109359 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; |
| 109360 | }else{ |
| 109361 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 109362 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 109363 | revSet = 1; |
| 109364 | } |
| 109365 | } |
| 109366 | }else{ |
| 109367 | /* No match found */ |
| 109368 | if( j==0 || j<nColumn ) isOrderDistinct = 0; |
| 109369 | break; |
| 109370 | } |
| 109371 | } /* end Loop over all index columns */ |
| 109372 | if( distinctColumns ) isOrderDistinct = 1; |
| 109373 | } /* end-if not one-row */ |
| 109374 | |
| 109375 | /* Mark off any other ORDER BY terms that reference pLoop */ |
| 109376 | if( isOrderDistinct ){ |
| 109377 | orderDistinctMask |= pLoop->maskSelf; |
| 109378 | for(i=0; i<nOrderBy; i++){ |
| 109379 | Expr *p; |
| 109380 | if( MASKBIT(i) & obSat ) continue; |
| 109381 | p = pOrderBy->a[i].pExpr; |
| 109382 | if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){ |
| 109383 | obSat |= MASKBIT(i); |
| 109384 | } |
| 109385 | } |
| 109386 | } |
| 109387 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
| 109388 | if( obSat==obDone ) return 1; |
| 109389 | if( !isOrderDistinct ) return 0; |
| 109390 | if( isLastLoop ) return 1; |
| 109391 | return -1; |
| 109392 | } |
| 109393 | |
| 109394 | #ifdef WHERETRACE_ENABLED |
| 109395 | /* For debugging use only: */ |
| 109396 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 109397 | static char zName[65]; |
| 109398 | int i; |
| 109399 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 109400 | if( pLast ) zName[i++] = pLast->cId; |
| 109401 | zName[i] = 0; |
| 109402 | return zName; |
| 109403 | } |
| 109404 | #endif |
| 109405 | |
| 109406 | |
| 109407 | /* |
| 109408 | ** Given the list of WhereLoop objects on pWInfo->pLoops, this routine |
| 109409 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 109410 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 109411 | ** |
| 109412 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 109413 | ** error occurs. |
| 109414 | */ |
| 109415 | static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){ |
| 109416 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
| 109417 | int nLoop; /* Number of terms in the join */ |
| 109418 | Parse *pParse; /* Parsing context */ |
| 109419 | sqlite3 *db; /* The database connection */ |
| 109420 | int iLoop; /* Loop counter over the terms of the join */ |
| 109421 | int ii, jj; /* Loop counters */ |
| 109422 | WhereCost rCost; /* Cost of a path */ |
| 109423 | WhereCost mxCost; /* Maximum cost of a set of paths */ |
| 109424 | WhereCost rSortCost; /* Cost to do a sort */ |
| 109425 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 109426 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 109427 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 109428 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 109429 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 109430 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 109431 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 109432 | char *pSpace; /* Temporary memory used by this routine */ |
| 109433 | |
| 109434 | pParse = pWInfo->pParse; |
| 109435 | db = pParse->db; |
| 109436 | nLoop = pWInfo->nLevel; |
| 109437 | /* TUNING: For simple queries, only the best path is tracked. |
| 109438 | ** For 2-way joins, the 5 best paths are followed. |
| 109439 | ** For joins of 3 or more tables, track the 10 best paths */ |
| 109440 | mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10); |
| 109441 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
| 109442 | WHERETRACE(0x002, ("---- begin solver\n")); |
| 109443 | |
| 109444 | /* Allocate and initialize space for aTo and aFrom */ |
| 109445 | ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 109446 | pSpace = sqlite3DbMallocRaw(db, ii); |
| 109447 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 109448 | aTo = (WherePath*)pSpace; |
| 109449 | aFrom = aTo+mxChoice; |
| 109450 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 109451 | pX = (WhereLoop**)(aFrom+mxChoice); |
| 109452 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
| 109453 | pFrom->aLoop = pX; |
| 109454 | } |
| 109455 | |
| 109456 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 109457 | ** |
| 109458 | ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 109459 | ** of computing an automatic index is not paid back within the first 25 |
| 109460 | ** rows, then do not use the automatic index. */ |
| 109461 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==whereCost(25) ); |
| 109462 | nFrom = 1; |
| 109463 | |
| 109464 | /* Precompute the cost of sorting the final result set, if the caller |
| 109465 | ** to sqlite3WhereBegin() was concerned about sorting */ |
| 109466 | rSortCost = 0; |
| 109467 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
| 109468 | aFrom[0].isOrderedValid = 1; |
| 109469 | }else{ |
| 109470 | /* TUNING: Estimated cost of sorting is N*log2(N) where N is the |
| 109471 | ** number of output rows. */ |
| 109472 | rSortCost = nRowEst + estLog(nRowEst); |
| 109473 | WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost)); |
| 109474 | } |
| 109475 | |
| 109476 | /* Compute successively longer WherePaths using the previous generation |
| 109477 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 109478 | ** best paths at each generation */ |
| 109479 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 109480 | nTo = 0; |
| 109481 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 109482 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 109483 | Bitmask maskNew; |
| 109484 | Bitmask revMask = 0; |
| 109485 | u8 isOrderedValid = pFrom->isOrderedValid; |
| 109486 | u8 isOrdered = pFrom->isOrdered; |
| 109487 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 109488 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
| 109489 | /* At this point, pWLoop is a candidate to be the next loop. |
| 109490 | ** Compute its cost */ |
| 109491 | rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 109492 | rCost = whereCostAdd(rCost, pFrom->rCost); |
| 109493 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
| 109494 | if( !isOrderedValid ){ |
| 109495 | switch( wherePathSatisfiesOrderBy(pWInfo, |
| 109496 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
| 109497 | iLoop, iLoop==nLoop-1, pWLoop, &revMask) ){ |
| 109498 | case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ |
| 109499 | isOrdered = 1; |
| 109500 | isOrderedValid = 1; |
| 109501 | break; |
| 109502 | case 0: /* No. pFrom+pWLoop will require a separate sort */ |
| 109503 | isOrdered = 0; |
| 109504 | isOrderedValid = 1; |
| 109505 | rCost = whereCostAdd(rCost, rSortCost); |
| 109506 | break; |
| 109507 | default: /* Cannot tell yet. Try again on the next iteration */ |
| 109508 | break; |
| 109509 | } |
| 109510 | }else{ |
| 109511 | revMask = pFrom->revLoop; |
| 109512 | } |
| 109513 | /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 109514 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
| 109515 | if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){ |
| 109516 | break; |
| 109517 | } |
| 109518 | } |
| 109519 | if( jj>=nTo ){ |
| 109520 | if( nTo>=mxChoice && rCost>=mxCost ){ |
| 109521 | #ifdef WHERETRACE_ENABLED |
| 109522 | if( sqlite3WhereTrace&0x4 ){ |
| 109523 | sqlite3DebugPrintf("Skip %s cost=%3d order=%c\n", |
| 109524 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 109525 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 109526 | } |
| 109527 | #endif |
| 109528 | continue; |
| 109529 | } |
| 109530 | /* Add a new Path to the aTo[] set */ |
| 109531 | if( nTo<mxChoice ){ |
| 109532 | /* Increase the size of the aTo set by one */ |
| 109533 | jj = nTo++; |
| 109534 | }else{ |
| 109535 | /* New path replaces the prior worst to keep count below mxChoice */ |
| 109536 | for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); } |
| 109537 | } |
| 109538 | pTo = &aTo[jj]; |
| 109539 | #ifdef WHERETRACE_ENABLED |
| 109540 | if( sqlite3WhereTrace&0x4 ){ |
| 109541 | sqlite3DebugPrintf("New %s cost=%-3d order=%c\n", |
| 109542 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 109543 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 109544 | } |
| 109545 | #endif |
| 109546 | }else{ |
| 109547 | if( pTo->rCost<=rCost ){ |
| 109548 | #ifdef WHERETRACE_ENABLED |
| 109549 | if( sqlite3WhereTrace&0x4 ){ |
| 109550 | sqlite3DebugPrintf( |
| 109551 | "Skip %s cost=%-3d order=%c", |
| 109552 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 109553 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 109554 | sqlite3DebugPrintf(" vs %s cost=%-3d order=%c\n", |
| 109555 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 109556 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 109557 | } |
| 109558 | #endif |
| 109559 | continue; |
| 109560 | } |
| 109561 | /* A new and better score for a previously created equivalent path */ |
| 109562 | #ifdef WHERETRACE_ENABLED |
| 109563 | if( sqlite3WhereTrace&0x4 ){ |
| 109564 | sqlite3DebugPrintf( |
| 109565 | "Update %s cost=%-3d order=%c", |
| 109566 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 109567 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 109568 | sqlite3DebugPrintf(" was %s cost=%-3d order=%c\n", |
| 109569 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 109570 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 109571 | } |
| 109572 | #endif |
| 109573 | } |
| 109574 | /* pWLoop is a winner. Add it to the set of best so far */ |
| 109575 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
| 109576 | pTo->revLoop = revMask; |
| 109577 | pTo->nRow = pFrom->nRow + pWLoop->nOut; |
| 109578 | pTo->rCost = rCost; |
| 109579 | pTo->isOrderedValid = isOrderedValid; |
| 109580 | pTo->isOrdered = isOrdered; |
| 109581 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 109582 | pTo->aLoop[iLoop] = pWLoop; |
| 109583 | if( nTo>=mxChoice ){ |
| 109584 | mxCost = aTo[0].rCost; |
| 109585 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
| 109586 | if( pTo->rCost>mxCost ) mxCost = pTo->rCost; |
| 109587 | } |
| 109588 | } |
| 109589 | } |
| 109590 | } |
| 109591 | |
| 109592 | #ifdef WHERETRACE_ENABLED |
| 109593 | if( sqlite3WhereTrace>=2 ){ |
| 109594 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
| 109595 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
| 109596 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
| 109597 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 109598 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 109599 | if( pTo->isOrderedValid && pTo->isOrdered ){ |
| 109600 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 109601 | }else{ |
| 109602 | sqlite3DebugPrintf("\n"); |
| 109603 | } |
| 109604 | } |
| 109605 | } |
| 109606 | #endif |
| 109607 | |
| 109608 | /* Swap the roles of aFrom and aTo for the next generation */ |
| 109609 | pFrom = aTo; |
| 109610 | aTo = aFrom; |
| 109611 | aFrom = pFrom; |
| 109612 | nFrom = nTo; |
| 109613 | } |
| 109614 | |
| 109615 | if( nFrom==0 ){ |
| 109616 | sqlite3ErrorMsg(pParse, "no query solution"); |
| 109617 | sqlite3DbFree(db, pSpace); |
| 109618 | return SQLITE_ERROR; |
| 109619 | } |
| 109620 | |
| 109621 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
| 109622 | pFrom = aFrom; |
| 109623 | for(ii=1; ii<nFrom; ii++){ |
| 109624 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 109625 | } |
| 109626 | assert( pWInfo->nLevel==nLoop ); |
| 109627 | /* Load the lowest cost path into pWInfo */ |
| 109628 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 109629 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 109630 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
| 109631 | pLevel->iFrom = pWLoop->iTab; |
| 109632 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
| 109633 | } |
| 109634 | if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 109635 | && pWInfo->pDistinct |
| 109636 | && nRowEst |
| 109637 | ){ |
| 109638 | Bitmask notUsed; |
| 109639 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinct, pFrom, |
| 109640 | WHERE_DISTINCTBY, nLoop-1, 1, pFrom->aLoop[nLoop-1], ¬Used); |
| 109641 | if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 109642 | } |
| 109643 | if( pFrom->isOrdered ){ |
| 109644 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 109645 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 109646 | }else{ |
| 109647 | pWInfo->bOBSat = 1; |
| 109648 | pWInfo->revMask = pFrom->revLoop; |
| 109649 | } |
| 109650 | } |
| 109651 | pWInfo->nRowOut = pFrom->nRow; |
| 109652 | |
| 109653 | /* Free temporary memory and return success */ |
| 109654 | sqlite3DbFree(db, pSpace); |
| 109655 | return SQLITE_OK; |
| 109656 | } |
| 109657 | |
| 109658 | /* |
| 109659 | ** Most queries use only a single table (they are not joins) and have |
| 109660 | ** simple == constraints against indexed fields. This routine attempts |
| 109661 | ** to plan those simple cases using much less ceremony than the |
| 109662 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 109663 | ** times for the common case. |
| 109664 | ** |
| 109665 | ** Return non-zero on success, if this query can be handled by this |
| 109666 | ** no-frills query planner. Return zero if this query needs the |
| 109667 | ** general-purpose query planner. |
| 109668 | */ |
| 109669 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
| 109670 | WhereInfo *pWInfo; |
| 109671 | struct SrcList_item *pItem; |
| 109672 | WhereClause *pWC; |
| 109673 | WhereTerm *pTerm; |
| 109674 | WhereLoop *pLoop; |
| 109675 | int iCur; |
| 109676 | int j; |
| 109677 | Table *pTab; |
| 109678 | Index *pIdx; |
| 109679 | |
| 109680 | pWInfo = pBuilder->pWInfo; |
| 109681 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
| 109682 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 109683 | pItem = pWInfo->pTabList->a; |
| 109684 | pTab = pItem->pTab; |
| 109685 | if( IsVirtual(pTab) ) return 0; |
| 109686 | if( pItem->zIndex ) return 0; |
| 109687 | iCur = pItem->iCursor; |
| 109688 | pWC = &pWInfo->sWC; |
| 109689 | pLoop = pBuilder->pNew; |
| 109690 | pLoop->wsFlags = 0; |
| 109691 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
| 109692 | if( pTerm ){ |
| 109693 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 109694 | pLoop->aLTerm[0] = pTerm; |
| 109695 | pLoop->nLTerm = 1; |
| 109696 | pLoop->u.btree.nEq = 1; |
| 109697 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 109698 | pLoop->rRun = 33; /* 33==whereCost(10) */ |
| 109699 | }else{ |
| 109700 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 109701 | if( pIdx->onError==OE_None ) continue; |
| 109702 | for(j=0; j<pIdx->nColumn; j++){ |
| 109703 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
| 109704 | if( pTerm==0 ) break; |
| 109705 | whereLoopResize(pWInfo->pParse->db, pLoop, j); |
| 109706 | pLoop->aLTerm[j] = pTerm; |
| 109707 | } |
| 109708 | if( j!=pIdx->nColumn ) continue; |
| 109709 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
| 109710 | if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
| 109711 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 109712 | } |
| 109713 | pLoop->nLTerm = j; |
| 109714 | pLoop->u.btree.nEq = j; |
| 109715 | pLoop->u.btree.pIndex = pIdx; |
| 109716 | /* TUNING: Cost of a unique index lookup is 15 */ |
| 109717 | pLoop->rRun = 39; /* 39==whereCost(15) */ |
| 109718 | break; |
| 109719 | } |
| 109720 | } |
| 109721 | if( pLoop->wsFlags ){ |
| 109722 | pLoop->nOut = (WhereCost)1; |
| 109723 | pWInfo->a[0].pWLoop = pLoop; |
| 109724 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 109725 | pWInfo->a[0].iTabCur = iCur; |
| 109726 | pWInfo->nRowOut = 1; |
| 109727 | if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1; |
| 109728 | if( pWInfo->pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 109729 | #ifdef SQLITE_DEBUG |
| 109730 | pLoop->cId = '0'; |
| 109731 | #endif |
| 109732 | return 1; |
| 109733 | } |
| 109734 | return 0; |
| 109735 | } |
| 109736 | |
| 109737 | /* |
| 109738 | ** Generate the beginning of the loop used for WHERE clause processing. |
| 109739 | ** The return value is a pointer to an opaque structure that contains |
| 109740 | ** information needed to terminate the loop. Later, the calling routine |
| @@ -109410,19 +109810,10 @@ | |
| 109810 | ** ORDER BY CLAUSE PROCESSING |
| 109811 | ** |
| 109812 | ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
| 109813 | ** if there is one. If there is no ORDER BY clause or if this routine |
| 109814 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
| 109815 | */ |
| 109816 | SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( |
| 109817 | Parse *pParse, /* The parser context */ |
| 109818 | SrcList *pTabList, /* A list of all tables to be scanned */ |
| 109819 | Expr *pWhere, /* The WHERE clause */ |
| @@ -109434,22 +109825,21 @@ | |
| 109825 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| 109826 | int nTabList; /* Number of elements in pTabList */ |
| 109827 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 109828 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 109829 | Bitmask notReady; /* Cursors that are not yet positioned */ |
| 109830 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
| 109831 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
| 109832 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
| 109833 | int ii; /* Loop counter */ |
| 109834 | sqlite3 *db; /* Database connection */ |
| 109835 | int rc; /* Return code */ |
| 109836 | |
| 109837 | |
| 109838 | /* Variable initialization */ |
| 109839 | memset(&sWLB, 0, sizeof(sWLB)); |
| 109840 | sWLB.pOrderBy = pOrderBy; |
| 109841 | |
| 109842 | /* The number of tables in the FROM clause is limited by the number of |
| 109843 | ** bits in a Bitmask |
| 109844 | */ |
| 109845 | testcase( pTabList->nSrc==BMS ); |
| @@ -109472,49 +109862,59 @@ | |
| 109862 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 109863 | ** some architectures. Hence the ROUND8() below. |
| 109864 | */ |
| 109865 | db = pParse->db; |
| 109866 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
| 109867 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
| 109868 | if( db->mallocFailed ){ |
| 109869 | sqlite3DbFree(db, pWInfo); |
| 109870 | pWInfo = 0; |
| 109871 | goto whereBeginError; |
| 109872 | } |
| 109873 | pWInfo->nLevel = nTabList; |
| 109874 | pWInfo->pParse = pParse; |
| 109875 | pWInfo->pTabList = pTabList; |
| 109876 | pWInfo->pOrderBy = pOrderBy; |
| 109877 | pWInfo->pDistinct = pDistinct; |
| 109878 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
| 109879 | pWInfo->wctrlFlags = wctrlFlags; |
| 109880 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 109881 | pMaskSet = &pWInfo->sMaskSet; |
| 109882 | sWLB.pWInfo = pWInfo; |
| 109883 | sWLB.pWC = &pWInfo->sWC; |
| 109884 | sWLB.pNew = (WhereLoop*)&pWInfo->a[nTabList]; |
| 109885 | whereLoopInit(sWLB.pNew); |
| 109886 | #ifdef SQLITE_DEBUG |
| 109887 | sWLB.pNew->cId = '*'; |
| 109888 | #endif |
| 109889 | |
| 109890 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 109891 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 109892 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0; |
| 109893 | |
| 109894 | /* Split the WHERE clause into separate subexpressions where each |
| 109895 | ** subexpression is separated by an AND operator. |
| 109896 | */ |
| 109897 | initMaskSet(pMaskSet); |
| 109898 | whereClauseInit(&pWInfo->sWC, pWInfo); |
| 109899 | sqlite3ExprCodeConstants(pParse, pWhere); |
| 109900 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
| 109901 | |
| 109902 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 109903 | ** expression and either jump over all of the code or fall thru. |
| 109904 | */ |
| 109905 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
| 109906 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
| 109907 | pWhere = 0; |
| 109908 | } |
| 109909 | |
| 109910 | /* Special case: No FROM clause |
| 109911 | */ |
| 109912 | if( nTabList==0 ){ |
| 109913 | if( pOrderBy ) pWInfo->bOBSat = 1; |
| 109914 | if( pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 109915 | } |
| 109916 | |
| 109917 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 109918 | ** |
| 109919 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 109920 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| @@ -109547,332 +109947,149 @@ | |
| 109947 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 109948 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 109949 | ** want to analyze these virtual terms, so start analyzing at the end |
| 109950 | ** and work forward so that the added virtual terms are never processed. |
| 109951 | */ |
| 109952 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
| 109953 | if( db->mallocFailed ){ |
| 109954 | goto whereBeginError; |
| 109955 | } |
| 109956 | |
| 109957 | /* If the ORDER BY (or GROUP BY) clause contains references to general |
| 109958 | ** expressions, then we won't be able to satisfy it using indices, so |
| 109959 | ** go ahead and disable it now. |
| 109960 | */ |
| 109961 | if( pOrderBy && pDistinct ){ |
| 109962 | for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 109963 | Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr); |
| 109964 | if( pExpr->op!=TK_COLUMN ){ |
| 109965 | pWInfo->pOrderBy = pOrderBy = 0; |
| 109966 | break; |
| 109967 | }else if( pExpr->iColumn<0 ){ |
| 109968 | break; |
| 109969 | } |
| 109970 | } |
| 109971 | } |
| 109972 | |
| 109973 | /* Check if the DISTINCT qualifier, if there is one, is redundant. |
| 109974 | ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to |
| 109975 | ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT. |
| 109976 | */ |
| 109977 | if( pDistinct ){ |
| 109978 | if( isDistinctRedundant(pParse,pTabList,&pWInfo->sWC,pDistinct) ){ |
| 109979 | pDistinct = 0; |
| 109980 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 109981 | }else if( pOrderBy==0 ){ |
| 109982 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
| 109983 | pWInfo->pOrderBy = pDistinct; |
| 109984 | } |
| 109985 | } |
| 109986 | |
| 109987 | /* Construct the WhereLoop objects */ |
| 109988 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
| 109989 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
| 109990 | rc = whereLoopAddAll(&sWLB); |
| 109991 | if( rc ) goto whereBeginError; |
| 109992 | |
| 109993 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
| 109994 | #ifdef WHERETRACE_ENABLED |
| 109995 | if( sqlite3WhereTrace ){ |
| 109996 | WhereLoop *p; |
| 109997 | int i = 0; |
| 109998 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 109999 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
| 110000 | for(p=pWInfo->pLoops; p; p=p->pNextLoop){ |
| 110001 | p->cId = zLabel[(i++)%sizeof(zLabel)]; |
| 110002 | whereLoopPrint(p, pTabList); |
| 110003 | } |
| 110004 | } |
| 110005 | #endif |
| 110006 | |
| 110007 | wherePathSolver(pWInfo, 0); |
| 110008 | if( db->mallocFailed ) goto whereBeginError; |
| 110009 | if( pWInfo->pOrderBy ){ |
| 110010 | wherePathSolver(pWInfo, pWInfo->nRowOut); |
| 110011 | if( db->mallocFailed ) goto whereBeginError; |
| 110012 | } |
| 110013 | } |
| 110014 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
| 110015 | pWInfo->revMask = (Bitmask)(-1); |
| 110016 | } |
| 110017 | if( pParse->nErr || db->mallocFailed ){ |
| 110018 | goto whereBeginError; |
| 110019 | } |
| 110020 | #ifdef WHERETRACE_ENABLED |
| 110021 | if( sqlite3WhereTrace ){ |
| 110022 | int ii; |
| 110023 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 110024 | if( pWInfo->bOBSat ){ |
| 110025 | sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask); |
| 110026 | } |
| 110027 | switch( pWInfo->eDistinct ){ |
| 110028 | case WHERE_DISTINCT_UNIQUE: { |
| 110029 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 110030 | break; |
| 110031 | } |
| 110032 | case WHERE_DISTINCT_ORDERED: { |
| 110033 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 110034 | break; |
| 110035 | } |
| 110036 | case WHERE_DISTINCT_UNORDERED: { |
| 110037 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 110038 | break; |
| 110039 | } |
| 110040 | } |
| 110041 | sqlite3DebugPrintf("\n"); |
| 110042 | for(ii=0; ii<nTabList; ii++){ |
| 110043 | whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList); |
| 110044 | } |
| 110045 | } |
| 110046 | #endif |
| 110047 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
| 110048 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
| 110049 | |
| 110050 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 110051 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 110052 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 110053 | ** the statement to update a single row. |
| 110054 | */ |
| 110055 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
| 110056 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 110057 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
| 110058 | pWInfo->okOnePass = 1; |
| 110059 | pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 110060 | } |
| 110061 | |
| 110062 | /* Open all tables in the pTabList and any indices selected for |
| 110063 | ** searching those tables. |
| 110064 | */ |
| 110065 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 110066 | notReady = ~(Bitmask)0; |
| 110067 | pWInfo->nRowOut = (WhereCost)1; |
| 110068 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
| 110069 | Table *pTab; /* Table to open */ |
| 110070 | int iDb; /* Index of database containing table/index */ |
| 110071 | struct SrcList_item *pTabItem; |
| 110072 | WhereLoop *pLoop; |
| 110073 | |
| 110074 | pTabItem = &pTabList->a[pLevel->iFrom]; |
| 110075 | pTab = pTabItem->pTab; |
| 110076 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 110077 | pLoop = pLevel->pWLoop; |
| 110078 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
| 110079 | /* Do nothing */ |
| 110080 | }else |
| 110081 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 110082 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 110083 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 110084 | int iCur = pTabItem->iCursor; |
| 110085 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| 110086 | }else if( IsVirtual(pTab) ){ |
| 110087 | /* noop */ |
| 110088 | }else |
| 110089 | #endif |
| 110090 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 110091 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
| 110092 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 110093 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 110094 | testcase( pTab->nCol==BMS-1 ); |
| 110095 | testcase( pTab->nCol==BMS ); |
| @@ -109886,26 +110103,27 @@ | |
| 110103 | } |
| 110104 | }else{ |
| 110105 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 110106 | } |
| 110107 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 110108 | if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 110109 | constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel); |
| 110110 | }else |
| 110111 | #endif |
| 110112 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 110113 | Index *pIx = pLoop->u.btree.pIndex; |
| 110114 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
| 110115 | /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */ |
| 110116 | int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++; |
| 110117 | assert( pIx->pSchema==pTab->pSchema ); |
| 110118 | assert( iIndexCur>=0 ); |
| 110119 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
| 110120 | (char*)pKey, P4_KEYINFO_HANDOFF); |
| 110121 | VdbeComment((v, "%s", pIx->zName)); |
| 110122 | } |
| 110123 | sqlite3CodeVerifySchema(pParse, iDb); |
| 110124 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
| 110125 | } |
| 110126 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 110127 | if( db->mallocFailed ) goto whereBeginError; |
| 110128 | |
| 110129 | /* Generate the code to do the search. Each iteration of the for |
| @@ -109914,70 +110132,15 @@ | |
| 110132 | */ |
| 110133 | notReady = ~(Bitmask)0; |
| 110134 | for(ii=0; ii<nTabList; ii++){ |
| 110135 | pLevel = &pWInfo->a[ii]; |
| 110136 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
| 110137 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 110138 | pWInfo->iContinue = pLevel->addrCont; |
| 110139 | } |
| 110140 | |
| 110141 | /* Done. */ |
| 110142 | return pWInfo; |
| 110143 | |
| 110144 | /* Jump here if malloc fails */ |
| 110145 | whereBeginError: |
| 110146 | if( pWInfo ){ |
| @@ -109994,24 +110157,26 @@ | |
| 110157 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ |
| 110158 | Parse *pParse = pWInfo->pParse; |
| 110159 | Vdbe *v = pParse->pVdbe; |
| 110160 | int i; |
| 110161 | WhereLevel *pLevel; |
| 110162 | WhereLoop *pLoop; |
| 110163 | SrcList *pTabList = pWInfo->pTabList; |
| 110164 | sqlite3 *db = pParse->db; |
| 110165 | |
| 110166 | /* Generate loop termination code. |
| 110167 | */ |
| 110168 | sqlite3ExprCacheClear(pParse); |
| 110169 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
| 110170 | pLevel = &pWInfo->a[i]; |
| 110171 | pLoop = pLevel->pWLoop; |
| 110172 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
| 110173 | if( pLevel->op!=OP_Noop ){ |
| 110174 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
| 110175 | sqlite3VdbeChangeP5(v, pLevel->p5); |
| 110176 | } |
| 110177 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
| 110178 | struct InLoop *pIn; |
| 110179 | int j; |
| 110180 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
| 110181 | for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
| 110182 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| @@ -110022,16 +110187,16 @@ | |
| 110187 | } |
| 110188 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
| 110189 | if( pLevel->iLeftJoin ){ |
| 110190 | int addr; |
| 110191 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); |
| 110192 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 110193 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 110194 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
| 110195 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 110196 | } |
| 110197 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 110198 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
| 110199 | } |
| 110200 | if( pLevel->op==OP_Return ){ |
| 110201 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 110202 | }else{ |
| @@ -110052,19 +110217,20 @@ | |
| 110217 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
| 110218 | Index *pIdx = 0; |
| 110219 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
| 110220 | Table *pTab = pTabItem->pTab; |
| 110221 | assert( pTab!=0 ); |
| 110222 | pLoop = pLevel->pWLoop; |
| 110223 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 110224 | && pTab->pSelect==0 |
| 110225 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
| 110226 | ){ |
| 110227 | int ws = pLoop->wsFlags; |
| 110228 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
| 110229 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 110230 | } |
| 110231 | if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_TEMP_INDEX))==0 ){ |
| 110232 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 110233 | } |
| 110234 | } |
| 110235 | |
| 110236 | /* If this scan uses an index, make code substitutions to read data |
| @@ -110078,16 +110244,16 @@ | |
| 110244 | ** sqlite3WhereEnd will have created code that references the table |
| 110245 | ** directly. This loop scans all that code looking for opcodes |
| 110246 | ** that reference the table and converts them into opcodes that |
| 110247 | ** reference the index. |
| 110248 | */ |
| 110249 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 110250 | pIdx = pLoop->u.btree.pIndex; |
| 110251 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 110252 | pIdx = pLevel->u.pCovidx; |
| 110253 | } |
| 110254 | if( pIdx && !db->mallocFailed ){ |
| 110255 | int k, j, last; |
| 110256 | VdbeOp *pOp; |
| 110257 | |
| 110258 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 110259 | last = sqlite3VdbeCurrentAddr(v); |
| @@ -110099,12 +110265,11 @@ | |
| 110265 | pOp->p2 = j; |
| 110266 | pOp->p1 = pLevel->iIdxCur; |
| 110267 | break; |
| 110268 | } |
| 110269 | } |
| 110270 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn ); |
| 110271 | }else if( pOp->opcode==OP_Rowid ){ |
| 110272 | pOp->p1 = pLevel->iIdxCur; |
| 110273 | pOp->opcode = OP_IdxRowid; |
| 110274 | } |
| 110275 | } |
| @@ -115480,11 +115645,11 @@ | |
| 115645 | } |
| 115646 | |
| 115647 | /* |
| 115648 | ** Another built-in collating sequence: NOCASE. |
| 115649 | ** |
| 115650 | ** This collating sequence is intended to be used for "case independent |
| 115651 | ** comparison". SQLite's knowledge of upper and lower case equivalents |
| 115652 | ** extends only to the 26 characters used in the English language. |
| 115653 | ** |
| 115654 | ** At the moment there is only a UTF-8 implementation. |
| 115655 | */ |
| @@ -115627,16 +115792,10 @@ | |
| 115792 | "statements or unfinished backups"); |
| 115793 | sqlite3_mutex_leave(db->mutex); |
| 115794 | return SQLITE_BUSY; |
| 115795 | } |
| 115796 | |
| 115797 | #ifdef SQLITE_ENABLE_SQLLOG |
| 115798 | if( sqlite3GlobalConfig.xSqllog ){ |
| 115799 | /* Closing the handle. Fourth parameter is passed the value 2. */ |
| 115800 | sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2); |
| 115801 | } |
| @@ -115686,10 +115845,16 @@ | |
| 115845 | /* If we reach this point, it means that the database connection has |
| 115846 | ** closed all sqlite3_stmt and sqlite3_backup objects and has been |
| 115847 | ** passed to sqlite3_close (meaning that it is a zombie). Therefore, |
| 115848 | ** go ahead and free all resources. |
| 115849 | */ |
| 115850 | |
| 115851 | /* If a transaction is open, roll it back. This also ensures that if |
| 115852 | ** any database schemas have been modified by an uncommitted transaction |
| 115853 | ** they are reset. And that the required b-tree mutex is held to make |
| 115854 | ** the pager rollback and schema reset an atomic operation. */ |
| 115855 | sqlite3RollbackAll(db, SQLITE_OK); |
| 115856 | |
| 115857 | /* Free any outstanding Savepoint structures. */ |
| 115858 | sqlite3CloseSavepoints(db); |
| 115859 | |
| 115860 | /* Close all database connections */ |
| @@ -115787,19 +115952,26 @@ | |
| 115952 | SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){ |
| 115953 | int i; |
| 115954 | int inTrans = 0; |
| 115955 | assert( sqlite3_mutex_held(db->mutex) ); |
| 115956 | sqlite3BeginBenignMalloc(); |
| 115957 | |
| 115958 | /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). |
| 115959 | ** This is important in case the transaction being rolled back has |
| 115960 | ** modified the database schema. If the b-tree mutexes are not taken |
| 115961 | ** here, then another shared-cache connection might sneak in between |
| 115962 | ** the database rollback and schema reset, which can cause false |
| 115963 | ** corruption reports in some cases. */ |
| 115964 | sqlite3BtreeEnterAll(db); |
| 115965 | |
| 115966 | for(i=0; i<db->nDb; i++){ |
| 115967 | Btree *p = db->aDb[i].pBt; |
| 115968 | if( p ){ |
| 115969 | if( sqlite3BtreeIsInTrans(p) ){ |
| 115970 | inTrans = 1; |
| 115971 | } |
| 115972 | sqlite3BtreeRollback(p, tripCode); |
| 115973 | } |
| 115974 | } |
| 115975 | sqlite3VtabRollback(db); |
| 115976 | sqlite3EndBenignMalloc(); |
| 115977 | |
| @@ -117562,12 +117734,10 @@ | |
| 117734 | /* |
| 117735 | ** Test to see whether or not the database connection is in autocommit |
| 117736 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 117737 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 117738 | ** by the next COMMIT or ROLLBACK. |
| 117739 | */ |
| 117740 | SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 117741 | return db->autoCommit; |
| 117742 | } |
| 117743 | |
| @@ -119051,10 +119221,22 @@ | |
| 119221 | |
| 119222 | #endif /* _FTS3_HASH_H_ */ |
| 119223 | |
| 119224 | /************** End of fts3_hash.h *******************************************/ |
| 119225 | /************** Continuing where we left off in fts3Int.h ********************/ |
| 119226 | |
| 119227 | /* |
| 119228 | ** This constant determines the maximum depth of an FTS expression tree |
| 119229 | ** that the library will create and use. FTS uses recursion to perform |
| 119230 | ** various operations on the query tree, so the disadvantage of a large |
| 119231 | ** limit is that it may allow very large queries to use large amounts |
| 119232 | ** of stack space (perhaps causing a stack overflow). |
| 119233 | */ |
| 119234 | #ifndef SQLITE_FTS3_MAX_EXPR_DEPTH |
| 119235 | # define SQLITE_FTS3_MAX_EXPR_DEPTH 12 |
| 119236 | #endif |
| 119237 | |
| 119238 | |
| 119239 | /* |
| 119240 | ** This constant controls how often segments are merged. Once there are |
| 119241 | ** FTS3_MERGE_COUNT segments of level N, they are merged into a single |
| 119242 | ** segment of level N+1. |
| @@ -120709,11 +120891,11 @@ | |
| 120891 | /* By default use a full table scan. This is an expensive option, |
| 120892 | ** so search through the constraints to see if a more efficient |
| 120893 | ** strategy is possible. |
| 120894 | */ |
| 120895 | pInfo->idxNum = FTS3_FULLSCAN_SEARCH; |
| 120896 | pInfo->estimatedCost = 5000000; |
| 120897 | for(i=0; i<pInfo->nConstraint; i++){ |
| 120898 | struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i]; |
| 120899 | if( pCons->usable==0 ) continue; |
| 120900 | |
| 120901 | /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */ |
| @@ -122270,15 +122452,11 @@ | |
| 122452 | ); |
| 122453 | if( rc!=SQLITE_OK ){ |
| 122454 | return rc; |
| 122455 | } |
| 122456 | |
| 122457 | rc = fts3EvalStart(pCsr); |
| 122458 | sqlite3Fts3SegmentsClose(p); |
| 122459 | if( rc!=SQLITE_OK ) return rc; |
| 122460 | pCsr->pNextId = pCsr->aDoclist; |
| 122461 | pCsr->iPrevId = 0; |
| 122462 | } |
| @@ -126129,30 +126307,30 @@ | |
| 126307 | int iDefaultCol, /* Default column to query */ |
| 126308 | const char *z, int n, /* Text of MATCH query */ |
| 126309 | Fts3Expr **ppExpr, /* OUT: Parsed query structure */ |
| 126310 | char **pzErr /* OUT: Error message (sqlite3_malloc) */ |
| 126311 | ){ |
| 126312 | int rc = fts3ExprParseUnbalanced( |
| 126313 | pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr |
| 126314 | ); |
| 126315 | |
| 126316 | /* Rebalance the expression. And check that its depth does not exceed |
| 126317 | ** SQLITE_FTS3_MAX_EXPR_DEPTH. */ |
| 126318 | if( rc==SQLITE_OK && *ppExpr ){ |
| 126319 | rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); |
| 126320 | if( rc==SQLITE_OK ){ |
| 126321 | rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH); |
| 126322 | } |
| 126323 | } |
| 126324 | |
| 126325 | if( rc!=SQLITE_OK ){ |
| 126326 | sqlite3Fts3ExprFree(*ppExpr); |
| 126327 | *ppExpr = 0; |
| 126328 | if( rc==SQLITE_TOOBIG ){ |
| 126329 | *pzErr = sqlite3_mprintf( |
| 126330 | "FTS expression tree is too large (maximum depth %d)", |
| 126331 | SQLITE_FTS3_MAX_EXPR_DEPTH |
| 126332 | ); |
| 126333 | rc = SQLITE_ERROR; |
| 126334 | }else if( rc==SQLITE_ERROR ){ |
| 126335 | *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z); |
| 126336 | } |
| @@ -129110,41 +129288,34 @@ | |
| 129288 | *pRC = rc; |
| 129289 | } |
| 129290 | |
| 129291 | |
| 129292 | /* |
| 129293 | ** This function ensures that the caller has obtained an exclusive |
| 129294 | ** shared-cache table-lock on the %_segdir table. This is required before |
| 129295 | ** writing data to the fts3 table. If this lock is not acquired first, then |
| 129296 | ** the caller may end up attempting to take this lock as part of committing |
| 129297 | ** a transaction, causing SQLite to return SQLITE_LOCKED or |
| 129298 | ** LOCKED_SHAREDCACHEto a COMMIT command. |
| 129299 | ** |
| 129300 | ** It is best to avoid this because if FTS3 returns any error when |
| 129301 | ** committing a transaction, the whole transaction will be rolled back. |
| 129302 | ** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. |
| 129303 | ** It can still happen if the user locks the underlying tables directly |
| 129304 | ** instead of accessing them via FTS. |
| 129305 | */ |
| 129306 | static int fts3Writelock(Fts3Table *p){ |
| 129307 | int rc = SQLITE_OK; |
| 129308 | |
| 129309 | if( p->nPendingData==0 ){ |
| 129310 | sqlite3_stmt *pStmt; |
| 129311 | rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0); |
| 129312 | if( rc==SQLITE_OK ){ |
| 129313 | sqlite3_bind_null(pStmt, 1); |
| 129314 | sqlite3_step(pStmt); |
| 129315 | rc = sqlite3_reset(pStmt); |
| 129316 | } |
| 129317 | } |
| 129318 | |
| 129319 | return rc; |
| 129320 | } |
| 129321 | |
| @@ -133918,10 +134089,13 @@ | |
| 134089 | goto update_out; |
| 134090 | } |
| 134091 | aSzIns = &aSzDel[p->nColumn+1]; |
| 134092 | memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2); |
| 134093 | |
| 134094 | rc = fts3Writelock(p); |
| 134095 | if( rc!=SQLITE_OK ) goto update_out; |
| 134096 | |
| 134097 | /* If this is an INSERT operation, or an UPDATE that modifies the rowid |
| 134098 | ** value, then this operation requires constraint handling. |
| 134099 | ** |
| 134100 | ** If the on-conflict mode is REPLACE, this means that the existing row |
| 134101 | ** should be deleted from the database before inserting the new row. Or, |
| @@ -136051,32 +136225,31 @@ | |
| 136225 | 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004, |
| 136226 | 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002, |
| 136227 | 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803, |
| 136228 | 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07, |
| 136229 | 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02, |
| 136230 | 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802, |
| 136231 | 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013, |
| 136232 | 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06, |
| 136233 | 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003, |
| 136234 | 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01, |
| 136235 | 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403, |
| 136236 | 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009, |
| 136237 | 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003, |
| 136238 | 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003, |
| 136239 | 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E, |
| 136240 | 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046, |
| 136241 | 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401, |
| 136242 | 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401, |
| 136243 | 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F, |
| 136244 | 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C, |
| 136245 | 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002, |
| 136246 | 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025, |
| 136247 | 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6, |
| 136248 | 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46, |
| 136249 | 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060, |
| 136250 | 0x380400F0, |
| 136251 | }; |
| 136252 | static const unsigned int aAscii[4] = { |
| 136253 | 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001, |
| 136254 | }; |
| 136255 | |
| @@ -139705,11 +139878,11 @@ | |
| 139878 | ** operator) using the ICU uregex_XX() APIs. |
| 139879 | ** |
| 139880 | ** * Implementations of the SQL scalar upper() and lower() functions |
| 139881 | ** for case mapping. |
| 139882 | ** |
| 139883 | ** * Integration of ICU and SQLite collation sequences. |
| 139884 | ** |
| 139885 | ** * An implementation of the LIKE operator that uses ICU to |
| 139886 | ** provide case-independent matching. |
| 139887 | */ |
| 139888 | |
| 139889 |
+11
-1
| --- src/sqlite3.h | ||
| +++ src/sqlite3.h | ||
| @@ -107,11 +107,11 @@ | ||
| 107 | 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | 109 | */ |
| 110 | 110 | #define SQLITE_VERSION "3.7.17" |
| 111 | 111 | #define SQLITE_VERSION_NUMBER 3007017 |
| 112 | -#define SQLITE_SOURCE_ID "2013-05-15 18:34:17 00231fb0127960d700de3549e34e82f8ec1b5819" | |
| 112 | +#define SQLITE_SOURCE_ID "2013-06-14 02:51:48 b920bb70bb009b7c54e7667544c9810c5ee25e19" | |
| 113 | 113 | |
| 114 | 114 | /* |
| 115 | 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | 117 | ** |
| @@ -4516,10 +4516,15 @@ | ||
| 4516 | 4516 | */ |
| 4517 | 4517 | SQLITE_API int sqlite3_key( |
| 4518 | 4518 | sqlite3 *db, /* Database to be rekeyed */ |
| 4519 | 4519 | const void *pKey, int nKey /* The key */ |
| 4520 | 4520 | ); |
| 4521 | +SQLITE_API int sqlite3_key_v2( | |
| 4522 | + sqlite3 *db, /* Database to be rekeyed */ | |
| 4523 | + const char *zDbName, /* Name of the database */ | |
| 4524 | + const void *pKey, int nKey /* The key */ | |
| 4525 | +); | |
| 4521 | 4526 | |
| 4522 | 4527 | /* |
| 4523 | 4528 | ** Change the key on an open database. If the current database is not |
| 4524 | 4529 | ** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the |
| 4525 | 4530 | ** database is decrypted. |
| @@ -4529,10 +4534,15 @@ | ||
| 4529 | 4534 | */ |
| 4530 | 4535 | SQLITE_API int sqlite3_rekey( |
| 4531 | 4536 | sqlite3 *db, /* Database to be rekeyed */ |
| 4532 | 4537 | const void *pKey, int nKey /* The new key */ |
| 4533 | 4538 | ); |
| 4539 | +SQLITE_API int sqlite3_rekey_v2( | |
| 4540 | + sqlite3 *db, /* Database to be rekeyed */ | |
| 4541 | + const char *zDbName, /* Name of the database */ | |
| 4542 | + const void *pKey, int nKey /* The new key */ | |
| 4543 | +); | |
| 4534 | 4544 | |
| 4535 | 4545 | /* |
| 4536 | 4546 | ** Specify the activation key for a SEE database. Unless |
| 4537 | 4547 | ** activated, none of the SEE routines will work. |
| 4538 | 4548 | */ |
| 4539 | 4549 |
| --- src/sqlite3.h | |
| +++ src/sqlite3.h | |
| @@ -107,11 +107,11 @@ | |
| 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | */ |
| 110 | #define SQLITE_VERSION "3.7.17" |
| 111 | #define SQLITE_VERSION_NUMBER 3007017 |
| 112 | #define SQLITE_SOURCE_ID "2013-05-15 18:34:17 00231fb0127960d700de3549e34e82f8ec1b5819" |
| 113 | |
| 114 | /* |
| 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | ** |
| @@ -4516,10 +4516,15 @@ | |
| 4516 | */ |
| 4517 | SQLITE_API int sqlite3_key( |
| 4518 | sqlite3 *db, /* Database to be rekeyed */ |
| 4519 | const void *pKey, int nKey /* The key */ |
| 4520 | ); |
| 4521 | |
| 4522 | /* |
| 4523 | ** Change the key on an open database. If the current database is not |
| 4524 | ** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the |
| 4525 | ** database is decrypted. |
| @@ -4529,10 +4534,15 @@ | |
| 4529 | */ |
| 4530 | SQLITE_API int sqlite3_rekey( |
| 4531 | sqlite3 *db, /* Database to be rekeyed */ |
| 4532 | const void *pKey, int nKey /* The new key */ |
| 4533 | ); |
| 4534 | |
| 4535 | /* |
| 4536 | ** Specify the activation key for a SEE database. Unless |
| 4537 | ** activated, none of the SEE routines will work. |
| 4538 | */ |
| 4539 |
| --- src/sqlite3.h | |
| +++ src/sqlite3.h | |
| @@ -107,11 +107,11 @@ | |
| 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | */ |
| 110 | #define SQLITE_VERSION "3.7.17" |
| 111 | #define SQLITE_VERSION_NUMBER 3007017 |
| 112 | #define SQLITE_SOURCE_ID "2013-06-14 02:51:48 b920bb70bb009b7c54e7667544c9810c5ee25e19" |
| 113 | |
| 114 | /* |
| 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | ** |
| @@ -4516,10 +4516,15 @@ | |
| 4516 | */ |
| 4517 | SQLITE_API int sqlite3_key( |
| 4518 | sqlite3 *db, /* Database to be rekeyed */ |
| 4519 | const void *pKey, int nKey /* The key */ |
| 4520 | ); |
| 4521 | SQLITE_API int sqlite3_key_v2( |
| 4522 | sqlite3 *db, /* Database to be rekeyed */ |
| 4523 | const char *zDbName, /* Name of the database */ |
| 4524 | const void *pKey, int nKey /* The key */ |
| 4525 | ); |
| 4526 | |
| 4527 | /* |
| 4528 | ** Change the key on an open database. If the current database is not |
| 4529 | ** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the |
| 4530 | ** database is decrypted. |
| @@ -4529,10 +4534,15 @@ | |
| 4534 | */ |
| 4535 | SQLITE_API int sqlite3_rekey( |
| 4536 | sqlite3 *db, /* Database to be rekeyed */ |
| 4537 | const void *pKey, int nKey /* The new key */ |
| 4538 | ); |
| 4539 | SQLITE_API int sqlite3_rekey_v2( |
| 4540 | sqlite3 *db, /* Database to be rekeyed */ |
| 4541 | const char *zDbName, /* Name of the database */ |
| 4542 | const void *pKey, int nKey /* The new key */ |
| 4543 | ); |
| 4544 | |
| 4545 | /* |
| 4546 | ** Specify the activation key for a SEE database. Unless |
| 4547 | ** activated, none of the SEE routines will work. |
| 4548 | */ |
| 4549 |