drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This module contains C code that generates VDBE code used to process |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 13 | ** the WHERE clause of SQL statements. This module is responsible for |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 14 | ** generating the code that loops through a table looking for applicable |
| 15 | ** rows. Indices are selected and used to speed the search when doing |
| 16 | ** so is applicable. Because this module is responsible for selecting |
| 17 | ** indices, you might also think of this module as the "query optimizer". |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
| 20 | |
drh | 7924f3e | 2011-02-09 03:04:27 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 23 | ** Trace output macros |
| 24 | */ |
| 25 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 26 | /***/ int sqlite3WhereTrace = 0; |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 27 | #endif |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 28 | #if defined(SQLITE_DEBUG) \ |
| 29 | && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 30 | # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 31 | # define WHERETRACE_ENABLED 1 |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 32 | #else |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 33 | # define WHERETRACE(K,X) |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 36 | /* Forward references |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 37 | */ |
| 38 | typedef struct WhereClause WhereClause; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 39 | typedef struct WhereMaskSet WhereMaskSet; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 40 | typedef struct WhereOrInfo WhereOrInfo; |
| 41 | typedef struct WhereAndInfo WhereAndInfo; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 42 | typedef struct WhereLevel WhereLevel; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 43 | typedef struct WhereLoop WhereLoop; |
| 44 | typedef struct WherePath WherePath; |
| 45 | typedef struct WhereTerm WhereTerm; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 46 | typedef struct WhereLoopBuilder WhereLoopBuilder; |
| 47 | typedef struct WhereScan WhereScan; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 48 | typedef struct WhereOrCost WhereOrCost; |
| 49 | typedef struct WhereOrSet WhereOrSet; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 52 | ** This object contains information needed to implement a single nested |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 53 | ** loop in WHERE clause. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 54 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 55 | ** Contrast this object with WhereLoop. This object describes the |
| 56 | ** implementation of the loop. WhereLoop describes the algorithm. |
| 57 | ** This object contains a pointer to the WhereLoop algorithm as one of |
| 58 | ** its elements. |
| 59 | ** |
| 60 | ** The WhereInfo object contains a single instance of this object for |
| 61 | ** each term in the FROM clause (which is to say, for each of the |
| 62 | ** nested loops as implemented). The order of WhereLevel objects determines |
| 63 | ** the loop nested order, with WhereInfo.a[0] being the outer loop and |
| 64 | ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 65 | */ |
| 66 | struct WhereLevel { |
| 67 | int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 68 | int iTabCur; /* The VDBE cursor used to access the table */ |
| 69 | int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 70 | int addrBrk; /* Jump here to break out of the loop */ |
| 71 | int addrNxt; /* Jump here to start the next IN combination */ |
| 72 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 73 | int addrFirst; /* First instruction of interior of the loop */ |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 74 | int addrBody; /* Beginning of the body of this loop */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 75 | u8 iFrom; /* Which entry in the FROM clause */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 76 | u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ |
| 77 | int p1, p2; /* Operands of the opcode used to ends the loop */ |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 78 | union { /* Information that depends on pWLoop->wsFlags */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 79 | struct { |
| 80 | int nIn; /* Number of entries in aInLoop[] */ |
| 81 | struct InLoop { |
| 82 | int iCur; /* The VDBE cursor used by this IN operator */ |
| 83 | int addrInTop; /* Top of the IN loop */ |
| 84 | u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 85 | } *aInLoop; /* Information about each nested IN operator */ |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 86 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 87 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 88 | } u; |
| 89 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 90 | Bitmask notReady; /* FROM entries not usable at this level */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 94 | ** Each instance of this object represents an algorithm for evaluating one |
| 95 | ** term of a join. Every term of the FROM clause will have at least |
| 96 | ** one corresponding WhereLoop object (unless INDEXED BY constraints |
| 97 | ** prevent a query solution - which is an error) and many terms of the |
| 98 | ** FROM clause will have multiple WhereLoop objects, each describing a |
| 99 | ** potential way of implementing that FROM-clause term, together with |
| 100 | ** dependencies and cost estimates for using the chosen algorithm. |
| 101 | ** |
| 102 | ** Query planning consists of building up a collection of these WhereLoop |
| 103 | ** objects, then computing a particular sequence of WhereLoop objects, with |
| 104 | ** one WhereLoop object per FROM clause term, that satisfy all dependencies |
| 105 | ** and that minimize the overall cost. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 106 | */ |
| 107 | struct WhereLoop { |
| 108 | Bitmask prereq; /* Bitmask of other loops that must run first */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 109 | Bitmask maskSelf; /* Bitmask identifying table iTab */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 110 | #ifdef SQLITE_DEBUG |
| 111 | char cId; /* Symbolic ID of this loop for debugging use */ |
| 112 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 113 | u8 iTab; /* Position in FROM clause of table for this loop */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 114 | u8 iSortIdx; /* Sorting index number. 0==None */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 115 | LogEst rSetup; /* One-time setup cost (ex: create transient index) */ |
| 116 | LogEst rRun; /* Cost of running each loop */ |
| 117 | LogEst nOut; /* Estimated number of output rows */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 118 | union { |
| 119 | struct { /* Information for internal btree tables */ |
| 120 | int nEq; /* Number of equality constraints */ |
| 121 | Index *pIndex; /* Index used, or NULL */ |
| 122 | } btree; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 123 | struct { /* Information for virtual tables */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 124 | int idxNum; /* Index number */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 125 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 126 | u8 isOrdered; /* True if satisfies ORDER BY */ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 127 | u16 omitMask; /* Terms that may be omitted */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 128 | char *idxStr; /* Index identifier string */ |
| 129 | } vtab; |
| 130 | } u; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 131 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 132 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 133 | /**** whereLoopXfer() copies fields above ***********************/ |
| 134 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 135 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 136 | WhereTerm **aLTerm; /* WhereTerms used */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 137 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 138 | WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 141 | /* This object holds the prerequisites and the cost of running a |
| 142 | ** subquery on one operand of an OR operator in the WHERE clause. |
| 143 | ** See WhereOrSet for additional information |
| 144 | */ |
| 145 | struct WhereOrCost { |
| 146 | Bitmask prereq; /* Prerequisites */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 147 | LogEst rRun; /* Cost of running this subquery */ |
| 148 | LogEst nOut; /* Number of outputs for this subquery */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /* The WhereOrSet object holds a set of possible WhereOrCosts that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 152 | ** correspond to the subquery(s) of OR-clause processing. Only the |
| 153 | ** best N_OR_COST elements are retained. |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 154 | */ |
| 155 | #define N_OR_COST 3 |
| 156 | struct WhereOrSet { |
| 157 | u16 n; /* Number of valid a[] entries */ |
| 158 | WhereOrCost a[N_OR_COST]; /* Set of best costs */ |
| 159 | }; |
| 160 | |
| 161 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 162 | /* Forward declaration of methods */ |
| 163 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 164 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 165 | /* |
| 166 | ** Each instance of this object holds a sequence of WhereLoop objects |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 167 | ** that implement some or all of a query plan. |
| 168 | ** |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 169 | ** Think of each WhereLoop object as a node in a graph with arcs |
drh | e56dd3a | 2013-08-30 17:50:35 +0000 | [diff] [blame] | 170 | ** showing dependencies and costs for travelling between nodes. (That is |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 171 | ** not a completely accurate description because WhereLoop costs are a |
drh | e56dd3a | 2013-08-30 17:50:35 +0000 | [diff] [blame] | 172 | ** vector, not a scalar, and because dependencies are many-to-one, not |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 173 | ** one-to-one as are graph nodes. But it is a useful visualization aid.) |
| 174 | ** Then a WherePath object is a path through the graph that visits some |
| 175 | ** or all of the WhereLoop objects once. |
| 176 | ** |
| 177 | ** The "solver" works by creating the N best WherePath objects of length |
| 178 | ** 1. Then using those as a basis to compute the N best WherePath objects |
| 179 | ** of length 2. And so forth until the length of WherePaths equals the |
| 180 | ** number of nodes in the FROM clause. The best (lowest cost) WherePath |
| 181 | ** at the end is the choosen query plan. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 182 | */ |
| 183 | struct WherePath { |
| 184 | Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 185 | Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 186 | LogEst nRow; /* Estimated number of rows generated by this path */ |
| 187 | LogEst rCost; /* Total cost of this path */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 188 | u8 isOrdered; /* True if this path satisfies ORDER BY */ |
| 189 | u8 isOrderedValid; /* True if the isOrdered field is valid */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 190 | WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 191 | }; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 192 | |
| 193 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 194 | ** The query generator uses an array of instances of this structure to |
| 195 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 196 | ** clause subexpression is separated from the others by AND operators, |
| 197 | ** usually, or sometimes subexpressions separated by OR. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 198 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 199 | ** All WhereTerms are collected into a single WhereClause structure. |
| 200 | ** The following identity holds: |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 201 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 202 | ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 203 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 204 | ** When a term is of the form: |
| 205 | ** |
| 206 | ** X <op> <expr> |
| 207 | ** |
| 208 | ** where X is a column name and <op> is one of certain operators, |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 209 | ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the |
| 210 | ** cursor number and column number for X. WhereTerm.eOperator records |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 211 | ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 212 | ** use of a bitmask encoding for the operator allows us to search |
| 213 | ** quickly for terms that match any of several different operators. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 214 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 215 | ** A WhereTerm might also be two or more subterms connected by OR: |
| 216 | ** |
| 217 | ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... |
| 218 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 219 | ** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 220 | ** and the WhereTerm.u.pOrInfo field points to auxiliary information that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 221 | ** is collected about the OR clause. |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 222 | ** |
| 223 | ** If a term in the WHERE clause does not match either of the two previous |
| 224 | ** categories, then eOperator==0. The WhereTerm.pExpr field is still set |
| 225 | ** to the original subexpression content and wtFlags is set up appropriately |
| 226 | ** but no other fields in the WhereTerm object are meaningful. |
| 227 | ** |
| 228 | ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 229 | ** but they do so indirectly. A single WhereMaskSet structure translates |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 230 | ** cursor number into bits and the translated bit is stored in the prereq |
| 231 | ** fields. The translation is used in order to maximize the number of |
| 232 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 233 | ** spread out over the non-negative integers. For example, the cursor |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 234 | ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 235 | ** translates these sparse cursor numbers into consecutive integers |
| 236 | ** beginning with 0 in order to make the best possible use of the available |
| 237 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 238 | ** would be mapped into integers 0 through 7. |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 239 | ** |
| 240 | ** The number of terms in a join is limited by the number of bits |
| 241 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 242 | ** is only able to process joins with 64 or fewer tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 243 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 244 | struct WhereTerm { |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 245 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 246 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 247 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 248 | union { |
| 249 | int leftColumn; /* Column number of X in "X <op> <expr>" */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 250 | WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 251 | WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 252 | } u; |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 253 | LogEst truthProb; /* Probability of truth for this expression */ |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 254 | u16 eOperator; /* A WO_xx value describing <op> */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 255 | u8 wtFlags; /* TERM_xxx bit flags. See below */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 256 | u8 nChild; /* Number of children that must disable us */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 257 | WhereClause *pWC; /* The clause this term is part of */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 258 | Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 259 | Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | /* |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 263 | ** Allowed values of WhereTerm.wtFlags |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 264 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 265 | #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 266 | #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
| 267 | #define TERM_CODED 0x04 /* This term is already coded */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 268 | #define TERM_COPIED 0x08 /* Has a child */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 269 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 270 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 271 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 272 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 273 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 274 | #else |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 275 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 276 | #endif |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 277 | |
| 278 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 279 | ** An instance of the WhereScan object is used as an iterator for locating |
| 280 | ** terms in the WHERE clause that are useful to the query planner. |
| 281 | */ |
| 282 | struct WhereScan { |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 283 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 284 | WhereClause *pWC; /* WhereClause currently being scanned */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 285 | char *zCollName; /* Required collating sequence, if not NULL */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 286 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 287 | unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 288 | unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 289 | u32 opMask; /* Acceptable operators */ |
| 290 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 291 | int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ |
| 292 | }; |
| 293 | |
| 294 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 295 | ** An instance of the following structure holds all information about a |
| 296 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 297 | ** |
| 298 | ** Explanation of pOuter: For a WHERE clause of the form |
| 299 | ** |
| 300 | ** a AND ((b AND c) OR (d AND e)) AND f |
| 301 | ** |
| 302 | ** There are separate WhereClause objects for the whole clause and for |
| 303 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 304 | ** subclauses points to the WhereClause object for the whole clause. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 305 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 306 | struct WhereClause { |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 307 | WhereInfo *pWInfo; /* WHERE clause processing context */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 308 | WhereClause *pOuter; /* Outer conjunction */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 309 | u8 op; /* Split operator. TK_AND or TK_OR */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 310 | int nTerm; /* Number of terms */ |
| 311 | int nSlot; /* Number of entries in a[] */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 312 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 313 | #if defined(SQLITE_SMALL_STACK) |
| 314 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| 315 | #else |
| 316 | WhereTerm aStatic[8]; /* Initial static space for a[] */ |
| 317 | #endif |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 321 | ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to |
| 322 | ** a dynamically allocated instance of the following structure. |
| 323 | */ |
| 324 | struct WhereOrInfo { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 325 | WhereClause wc; /* Decomposition into subterms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 326 | Bitmask indexable; /* Bitmask of all indexable tables in the clause */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | /* |
| 330 | ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to |
| 331 | ** a dynamically allocated instance of the following structure. |
| 332 | */ |
| 333 | struct WhereAndInfo { |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 334 | WhereClause wc; /* The subexpression broken out */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 338 | ** An instance of the following structure keeps track of a mapping |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 339 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 340 | ** |
| 341 | ** The VDBE cursor numbers are small integers contained in |
| 342 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 343 | ** clause, the cursor numbers might not begin with 0 and they might |
| 344 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 345 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 346 | ** from the sparse cursor numbers into consecutive integers beginning |
| 347 | ** with 0. |
| 348 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 349 | ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 350 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 351 | ** |
| 352 | ** For example, if the WHERE clause expression used these VDBE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 353 | ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 354 | ** would map those cursor numbers into bits 0 through 5. |
| 355 | ** |
| 356 | ** Note that the mapping is not necessarily ordered. In the example |
| 357 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 358 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 359 | ** does not really matter. What is important is that sparse cursor |
| 360 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 361 | ** no gaps. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 362 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 363 | struct WhereMaskSet { |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 364 | int n; /* Number of assigned cursor values */ |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 365 | int ix[BMS]; /* Cursor assigned to each bit */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 368 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 369 | ** This object is a convenience wrapper holding all information needed |
| 370 | ** to construct WhereLoop objects for a particular query. |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 371 | */ |
| 372 | struct WhereLoopBuilder { |
| 373 | WhereInfo *pWInfo; /* Information about this WHERE */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 374 | WhereClause *pWC; /* WHERE clause terms */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 375 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 376 | WhereLoop *pNew; /* Template WhereLoop */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 377 | WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 378 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 379 | UnpackedRecord *pRec; /* Probe for stat4 (if required) */ |
| 380 | int nRecValid; /* Number of valid fields currently in pRec */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 381 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | /* |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 385 | ** The WHERE clause processing routine has two halves. The |
| 386 | ** first part does the start of the WHERE loop and the second |
| 387 | ** half does the tail of the WHERE loop. An instance of |
| 388 | ** this structure is returned by the first half and passed |
| 389 | ** into the second half to give some continuity. |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 390 | ** |
| 391 | ** An instance of this object holds the complete state of the query |
| 392 | ** planner. |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 393 | */ |
| 394 | struct WhereInfo { |
| 395 | Parse *pParse; /* Parsing and code generating context */ |
| 396 | SrcList *pTabList; /* List of tables in the join */ |
| 397 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 398 | ExprList *pResultSet; /* Result set. DISTINCT operates on these */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 399 | WhereLoop *pLoops; /* List of all WhereLoop objects */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 400 | Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 401 | LogEst nRowOut; /* Estimated number of output rows */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 402 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 403 | u8 bOBSat; /* ORDER BY satisfied by indices */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 404 | u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ |
| 405 | u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
| 406 | u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 407 | u8 nLevel; /* Number of nested loop */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 408 | int iTop; /* The very beginning of the WHERE loop */ |
| 409 | int iContinue; /* Jump here to continue with next record */ |
| 410 | int iBreak; /* Jump here to break out of the loop */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 411 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 412 | int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 413 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 414 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 415 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 416 | }; |
| 417 | |
| 418 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 419 | ** Bitmasks for the operators on WhereTerm objects. These are all |
| 420 | ** operators that are of interest to the query planner. An |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 421 | ** OR-ed combination of these values can be used when searching for |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 422 | ** particular WhereTerms within a WhereClause. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 423 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 424 | #define WO_IN 0x001 |
| 425 | #define WO_EQ 0x002 |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 426 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 427 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 428 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 429 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 430 | #define WO_MATCH 0x040 |
| 431 | #define WO_ISNULL 0x080 |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 432 | #define WO_OR 0x100 /* Two or more OR-connected terms */ |
| 433 | #define WO_AND 0x200 /* Two or more AND-connected terms */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 434 | #define WO_EQUIV 0x400 /* Of the form A==B, both columns */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 435 | #define WO_NOOP 0x800 /* This term does not restrict search space */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 436 | |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 437 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 438 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 439 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 440 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 441 | ** These are definitions of bits in the WhereLoop.wsFlags field. |
| 442 | ** The particular combination of bits in each WhereLoop help to |
| 443 | ** determine the algorithm that WhereLoop represents. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 444 | */ |
drh | be4fe3a | 2013-07-01 10:38:35 +0000 | [diff] [blame] | 445 | #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 446 | #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 447 | #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 448 | #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 449 | #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 450 | #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 451 | #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 452 | #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 453 | #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 454 | #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 455 | #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 456 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 457 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 458 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 459 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 460 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 461 | |
| 462 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 463 | ** Return the estimated number of output rows from a WHERE clause |
| 464 | */ |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 465 | u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 466 | return sqlite3LogEstToInt(pWInfo->nRowOut); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* |
| 470 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 471 | ** WHERE clause returns outputs for DISTINCT processing. |
| 472 | */ |
| 473 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 474 | return pWInfo->eDistinct; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 479 | ** Return FALSE if the output needs to be sorted. |
| 480 | */ |
| 481 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 482 | return pWInfo->bOBSat!=0; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* |
| 486 | ** Return the VDBE address or label to jump to in order to continue |
| 487 | ** immediately with the next row of a WHERE clause. |
| 488 | */ |
| 489 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 490 | return pWInfo->iContinue; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | ** Return the VDBE address or label to jump to in order to break |
| 495 | ** out of a WHERE loop. |
| 496 | */ |
| 497 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 498 | return pWInfo->iBreak; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 503 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 504 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 505 | ** |
| 506 | ** If the ONEPASS optimization is used (if this routine returns true) |
| 507 | ** then also write the indices of open cursors used by ONEPASS |
| 508 | ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data |
| 509 | ** table and iaCur[1] gets the cursor used by an auxiliary index. |
| 510 | ** Either value may be -1, indicating that cursor is not used. |
| 511 | ** Any cursors returned will have been opened for writing. |
| 512 | ** |
| 513 | ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is |
| 514 | ** unable to use the ONEPASS optimization. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 515 | */ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 516 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ |
| 517 | memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 518 | return pWInfo->okOnePass; |
| 519 | } |
| 520 | |
| 521 | /* |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 522 | ** Move the content of pSrc into pDest |
| 523 | */ |
| 524 | static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 525 | pDest->n = pSrc->n; |
| 526 | memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 531 | ** |
| 532 | ** The new entry might overwrite an existing entry, or it might be |
| 533 | ** appended, or it might be discarded. Do whatever is the right thing |
| 534 | ** so that pSet keeps the N_OR_COST best entries seen so far. |
| 535 | */ |
| 536 | static int whereOrInsert( |
| 537 | WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 538 | Bitmask prereq, /* Prerequisites of the new entry */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 539 | LogEst rRun, /* Run-cost of the new entry */ |
| 540 | LogEst nOut /* Number of outputs for the new entry */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 541 | ){ |
| 542 | u16 i; |
| 543 | WhereOrCost *p; |
| 544 | for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 545 | if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 546 | goto whereOrInsert_done; |
| 547 | } |
| 548 | if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 549 | return 0; |
| 550 | } |
| 551 | } |
| 552 | if( pSet->n<N_OR_COST ){ |
| 553 | p = &pSet->a[pSet->n++]; |
| 554 | p->nOut = nOut; |
| 555 | }else{ |
| 556 | p = pSet->a; |
| 557 | for(i=1; i<pSet->n; i++){ |
| 558 | if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 559 | } |
| 560 | if( p->rRun<=rRun ) return 0; |
| 561 | } |
| 562 | whereOrInsert_done: |
| 563 | p->prereq = prereq; |
| 564 | p->rRun = rRun; |
| 565 | if( p->nOut>nOut ) p->nOut = nOut; |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 570 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 571 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 572 | static void whereClauseInit( |
| 573 | WhereClause *pWC, /* The WhereClause to be initialized */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 574 | WhereInfo *pWInfo /* The WHERE processing context */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 575 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 576 | pWC->pWInfo = pWInfo; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 577 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 578 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 579 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 580 | pWC->a = pWC->aStatic; |
| 581 | } |
| 582 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 583 | /* Forward reference */ |
| 584 | static void whereClauseClear(WhereClause*); |
| 585 | |
| 586 | /* |
| 587 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 588 | */ |
| 589 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 590 | whereClauseClear(&p->wc); |
| 591 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
| 595 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 596 | */ |
| 597 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 598 | whereClauseClear(&p->wc); |
| 599 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 600 | } |
| 601 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 602 | /* |
| 603 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 604 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 605 | */ |
| 606 | static void whereClauseClear(WhereClause *pWC){ |
| 607 | int i; |
| 608 | WhereTerm *a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 609 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 610 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 611 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 612 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 613 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 614 | if( a->wtFlags & TERM_ORINFO ){ |
| 615 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 616 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 617 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 618 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 619 | } |
| 620 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 621 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| 625 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 626 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 627 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 628 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 629 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 630 | ** allocation error. The memory allocation failure will be recorded in |
| 631 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 632 | ** |
| 633 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 634 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 635 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 636 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 637 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 638 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 639 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 640 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 641 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 642 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 643 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 644 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 645 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 646 | int idx; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 647 | testcase( wtFlags & TERM_VIRTUAL ); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 648 | if( pWC->nTerm>=pWC->nSlot ){ |
| 649 | WhereTerm *pOld = pWC->a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 650 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 651 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 652 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 653 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 654 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 655 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 656 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 657 | return 0; |
| 658 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 659 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 660 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 661 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 662 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 663 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 664 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 665 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 666 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 667 | pTerm->truthProb = sqlite3LogEst(p->iTable) - 99; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 668 | }else{ |
| 669 | pTerm->truthProb = -1; |
| 670 | } |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 671 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 672 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 673 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 674 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 675 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 676 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 677 | |
| 678 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 679 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 680 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 681 | ** operator specified in the op parameter. The WhereClause structure |
| 682 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 683 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 684 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 685 | ** \________/ \_______________/ \________________/ |
| 686 | ** slot[0] slot[1] slot[2] |
| 687 | ** |
| 688 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 689 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 690 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 691 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 692 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 693 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 694 | */ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 695 | static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 696 | pWC->op = op; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 697 | if( pExpr==0 ) return; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 698 | if( pExpr->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 699 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 700 | }else{ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 701 | whereSplit(pWC, pExpr->pLeft, op); |
| 702 | whereSplit(pWC, pExpr->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 703 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 707 | ** Initialize a WhereMaskSet object |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 708 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 709 | #define initMaskSet(P) (P)->n=0 |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 710 | |
| 711 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 712 | ** Return the bitmask for the given cursor number. Return 0 if |
| 713 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 714 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 715 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 716 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 717 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 718 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 719 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 720 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 721 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 722 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 727 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 728 | ** |
| 729 | ** There is one cursor per table in the FROM clause. The number of |
| 730 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 731 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 732 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 733 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 734 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 735 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 736 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 740 | ** These routines walk (recursively) an expression tree and generate |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 741 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 742 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 743 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 744 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 745 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 746 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 747 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 748 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 749 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 750 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 751 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 752 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 753 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 754 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 755 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 756 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 757 | }else{ |
| 758 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 759 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 760 | return mask; |
| 761 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 762 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 763 | int i; |
| 764 | Bitmask mask = 0; |
| 765 | if( pList ){ |
| 766 | for(i=0; i<pList->nExpr; i++){ |
| 767 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 770 | return mask; |
| 771 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 772 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 773 | Bitmask mask = 0; |
| 774 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 775 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 776 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 777 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 778 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 779 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 780 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 781 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 782 | int i; |
| 783 | for(i=0; i<pSrc->nSrc; i++){ |
| 784 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 785 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 786 | } |
| 787 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 788 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 789 | } |
| 790 | return mask; |
| 791 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 792 | |
| 793 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 794 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 795 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 796 | ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 797 | */ |
| 798 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 799 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 800 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 801 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 802 | assert( TK_GE==TK_EQ+4 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 803 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /* |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 807 | ** Swap two objects of type TYPE. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 808 | */ |
| 809 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 810 | |
| 811 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 812 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 813 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 814 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 815 | ** If left/right precedence rules come into play when determining the |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 816 | ** collating sequence, then COLLATE operators are adjusted to ensure |
| 817 | ** that the collating sequence does not change. For example: |
| 818 | ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 819 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 820 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 821 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 822 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 823 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 824 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 825 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 826 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 827 | if( expRight==expLeft ){ |
| 828 | /* Either X and Y both have COLLATE operator or neither do */ |
| 829 | if( expRight ){ |
| 830 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 831 | ** used by clearing the EP_Collate flag from Y. */ |
| 832 | pExpr->pRight->flags &= ~EP_Collate; |
| 833 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 834 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 835 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 836 | ** it to be searched first. */ |
| 837 | pExpr->pLeft->flags |= EP_Collate; |
| 838 | } |
| 839 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 840 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 841 | if( pExpr->op>=TK_GT ){ |
| 842 | assert( TK_LT==TK_GT+2 ); |
| 843 | assert( TK_GE==TK_LE+2 ); |
| 844 | assert( TK_GT>TK_EQ ); |
| 845 | assert( TK_GT<TK_LE ); |
| 846 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 847 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 848 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 852 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 853 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 854 | static u16 operatorMask(int op){ |
| 855 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 856 | assert( allowedOp(op) ); |
| 857 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 858 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 859 | }else if( op==TK_ISNULL ){ |
| 860 | c = WO_ISNULL; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 861 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 862 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 863 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 864 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 865 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 866 | assert( op!=TK_IN || c==WO_IN ); |
| 867 | assert( op!=TK_EQ || c==WO_EQ ); |
| 868 | assert( op!=TK_LT || c==WO_LT ); |
| 869 | assert( op!=TK_LE || c==WO_LE ); |
| 870 | assert( op!=TK_GT || c==WO_GT ); |
| 871 | assert( op!=TK_GE || c==WO_GE ); |
| 872 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 876 | ** Advance to the next WhereTerm that matches according to the criteria |
| 877 | ** established when the pScan object was initialized by whereScanInit(). |
| 878 | ** Return NULL if there are no more matching WhereTerms. |
| 879 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 880 | static WhereTerm *whereScanNext(WhereScan *pScan){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 881 | int iCur; /* The cursor on the LHS of the term */ |
| 882 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 883 | Expr *pX; /* An expression being tested */ |
| 884 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 885 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 886 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 887 | |
| 888 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 889 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 890 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 891 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 892 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | e1a086e | 2013-10-28 20:15:56 +0000 | [diff] [blame] | 893 | if( pTerm->leftCursor==iCur |
| 894 | && pTerm->u.leftColumn==iColumn |
| 895 | && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 896 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 897 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 898 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 899 | ){ |
| 900 | int j; |
| 901 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 902 | assert( pX->op==TK_COLUMN ); |
| 903 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 904 | if( pScan->aEquiv[j]==pX->iTable |
| 905 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | if( j==pScan->nEquiv ){ |
| 910 | pScan->aEquiv[j] = pX->iTable; |
| 911 | pScan->aEquiv[j+1] = pX->iColumn; |
| 912 | pScan->nEquiv += 2; |
| 913 | } |
| 914 | } |
| 915 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 916 | /* Verify the affinity and collating sequence match */ |
| 917 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 918 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 919 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 920 | pX = pTerm->pExpr; |
| 921 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 922 | continue; |
| 923 | } |
| 924 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 925 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 926 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 927 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 928 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 929 | continue; |
| 930 | } |
| 931 | } |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 932 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 933 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 934 | && pX->iTable==pScan->aEquiv[0] |
| 935 | && pX->iColumn==pScan->aEquiv[1] |
| 936 | ){ |
| 937 | continue; |
| 938 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 939 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 940 | return pTerm; |
| 941 | } |
| 942 | } |
| 943 | } |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 944 | pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 945 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 946 | } |
| 947 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 948 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 949 | pScan->iEquiv += 2; |
| 950 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 956 | ** first match. Return NULL if there are no matches. |
| 957 | ** |
| 958 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 959 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 960 | ** iCur. The <op> must be one of the operators described by opMask. |
| 961 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 962 | ** If the search is for X and the WHERE clause contains terms of the |
| 963 | ** form X=Y then this routine might also return terms of the form |
| 964 | ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 965 | ** but is enough to handle most commonly occurring SQL statements. |
| 966 | ** |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 967 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 968 | ** index pIdx. |
| 969 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 970 | static WhereTerm *whereScanInit( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 971 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 972 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 973 | int iCur, /* Cursor to scan for */ |
| 974 | int iColumn, /* Column to scan for */ |
| 975 | u32 opMask, /* Operator(s) to scan for */ |
| 976 | Index *pIdx /* Must be compatible with this index */ |
| 977 | ){ |
| 978 | int j; |
| 979 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 980 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 981 | pScan->pOrigWC = pWC; |
| 982 | pScan->pWC = pWC; |
| 983 | if( pIdx && iColumn>=0 ){ |
| 984 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 985 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 986 | if( NEVER(j>=pIdx->nKeyCol) ) return 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 987 | } |
| 988 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 989 | }else{ |
| 990 | pScan->idxaff = 0; |
| 991 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 992 | } |
| 993 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 994 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 995 | pScan->aEquiv[0] = iCur; |
| 996 | pScan->aEquiv[1] = iColumn; |
| 997 | pScan->nEquiv = 2; |
| 998 | pScan->iEquiv = 2; |
| 999 | return whereScanNext(pScan); |
| 1000 | } |
| 1001 | |
| 1002 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1003 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 1004 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 1005 | ** the WO_xx operator codes specified by the op parameter. |
| 1006 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 1007 | ** |
| 1008 | ** The term returned might by Y=<expr> if there is another constraint in |
| 1009 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 1010 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 1011 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 1012 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 1013 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 1014 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 1015 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 1016 | ** and ... and A9=A10 and A10=<expr>. |
| 1017 | ** |
| 1018 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 1019 | ** then try for the one with no dependencies on <expr> - in other words where |
| 1020 | ** <expr> is a constant expression of some kind. Only return entries of |
| 1021 | ** the form "X <op> Y" where Y is a column in another table if no terms of |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 1022 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 1023 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1024 | */ |
| 1025 | static WhereTerm *findTerm( |
| 1026 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 1027 | int iCur, /* Cursor number of LHS */ |
| 1028 | int iColumn, /* Column number of LHS */ |
| 1029 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 1030 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1031 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 1032 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1033 | WhereTerm *pResult = 0; |
| 1034 | WhereTerm *p; |
| 1035 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1036 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1037 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 1038 | while( p ){ |
| 1039 | if( (p->prereqRight & notReady)==0 ){ |
| 1040 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 1041 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1042 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1043 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1044 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1045 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1046 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1047 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1050 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1051 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1052 | |
| 1053 | /* |
| 1054 | ** Call exprAnalyze on all terms in a WHERE clause. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1055 | */ |
| 1056 | static void exprAnalyzeAll( |
| 1057 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1058 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 1059 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1060 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1061 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1062 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1066 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1067 | /* |
| 1068 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 1069 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 1070 | ** so and false if not. |
| 1071 | ** |
| 1072 | ** In order for the operator to be optimizible, the RHS must be a string |
| 1073 | ** literal that does not begin with a wildcard. |
| 1074 | */ |
| 1075 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1076 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1077 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1078 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1079 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 1080 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1081 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1082 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 1083 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 1084 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 1085 | int c; /* One character in z[] */ |
| 1086 | int cnt; /* Number of non-wildcard prefix characters */ |
| 1087 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 1088 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1089 | sqlite3_value *pVal = 0; |
| 1090 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1091 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1092 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1095 | #ifdef SQLITE_EBCDIC |
| 1096 | if( *pnoCase ) return 0; |
| 1097 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1098 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1099 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 1100 | if( pLeft->op!=TK_COLUMN |
| 1101 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 1102 | || IsVirtual(pLeft->pTab) |
| 1103 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1104 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 1105 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1106 | return 0; |
| 1107 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1108 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1109 | |
| 1110 | pRight = pList->a[0].pExpr; |
| 1111 | op = pRight->op; |
| 1112 | if( op==TK_REGISTER ){ |
| 1113 | op = pRight->op2; |
| 1114 | } |
| 1115 | if( op==TK_VARIABLE ){ |
| 1116 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 1117 | int iCol = pRight->iColumn; |
drh | cf0fd4a | 2013-08-01 12:21:58 +0000 | [diff] [blame] | 1118 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1119 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 1120 | z = (char *)sqlite3_value_text(pVal); |
| 1121 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1122 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1123 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 1124 | }else if( op==TK_STRING ){ |
| 1125 | z = pRight->u.zToken; |
| 1126 | } |
| 1127 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1128 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1129 | while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ |
drh | 24fb627 | 2009-05-01 21:13:36 +0000 | [diff] [blame] | 1130 | cnt++; |
| 1131 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1132 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1133 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1134 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1135 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 1136 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 1137 | *ppPrefix = pPrefix; |
| 1138 | if( op==TK_VARIABLE ){ |
| 1139 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1140 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1141 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 1142 | /* If the rhs of the LIKE expression is a variable, and the current |
| 1143 | ** value of the variable means there is no need to invoke the LIKE |
| 1144 | ** function, then no OP_Variable will be added to the program. |
| 1145 | ** This causes problems for the sqlite3_bind_parameter_name() |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1146 | ** API. To workaround them, add a dummy OP_Variable here. |
| 1147 | */ |
| 1148 | int r1 = sqlite3GetTempReg(pParse); |
| 1149 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1150 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1151 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | }else{ |
| 1155 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1156 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1157 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1158 | |
| 1159 | sqlite3ValueFree(pVal); |
| 1160 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1161 | } |
| 1162 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 1163 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1164 | |
| 1165 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1166 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1167 | ** Check to see if the given expression is of the form |
| 1168 | ** |
| 1169 | ** column MATCH expr |
| 1170 | ** |
| 1171 | ** If it is then return TRUE. If not, return FALSE. |
| 1172 | */ |
| 1173 | static int isMatchOfColumn( |
| 1174 | Expr *pExpr /* Test this expression */ |
| 1175 | ){ |
| 1176 | ExprList *pList; |
| 1177 | |
| 1178 | if( pExpr->op!=TK_FUNCTION ){ |
| 1179 | return 0; |
| 1180 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1181 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1182 | return 0; |
| 1183 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1184 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1185 | if( pList->nExpr!=2 ){ |
| 1186 | return 0; |
| 1187 | } |
| 1188 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 1189 | return 0; |
| 1190 | } |
| 1191 | return 1; |
| 1192 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1193 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1194 | |
| 1195 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1196 | ** If the pBase expression originated in the ON or USING clause of |
| 1197 | ** a join, then transfer the appropriate markings over to derived. |
| 1198 | */ |
| 1199 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1200 | if( pDerived ){ |
| 1201 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 1202 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 1203 | } |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1206 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 1207 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1208 | ** Analyze a term that consists of two or more OR-connected |
| 1209 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1210 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1211 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 1212 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1213 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1214 | ** This routine analyzes terms such as the middle term in the above example. |
| 1215 | ** A WhereOrTerm object is computed and attached to the term under |
| 1216 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1217 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1218 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 1219 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1220 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1221 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 1222 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1223 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1224 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1225 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 1226 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 1227 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 1228 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 1229 | ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6) |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1230 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1231 | ** CASE 1: |
| 1232 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1233 | ** If all subterms are of the form T.C=expr for some single column of C and |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1234 | ** a single table T (as shown in example B above) then create a new virtual |
| 1235 | ** term that is an equivalent IN expression. In other words, if the term |
| 1236 | ** being analyzed is: |
| 1237 | ** |
| 1238 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 1239 | ** |
| 1240 | ** then create a new virtual term like this: |
| 1241 | ** |
| 1242 | ** x IN (expr1,expr2,expr3) |
| 1243 | ** |
| 1244 | ** CASE 2: |
| 1245 | ** |
| 1246 | ** If all subterms are indexable by a single table T, then set |
| 1247 | ** |
| 1248 | ** WhereTerm.eOperator = WO_OR |
| 1249 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 1250 | ** |
| 1251 | ** A subterm is "indexable" if it is of the form |
| 1252 | ** "T.C <op> <expr>" where C is any column of table T and |
| 1253 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 1254 | ** A subterm is also indexable if it is an AND of two or more |
| 1255 | ** subsubterms at least one of which is indexable. Indexable AND |
| 1256 | ** subterms have their eOperator set to WO_AND and they have |
| 1257 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 1258 | ** |
| 1259 | ** From another point of view, "indexable" means that the subterm could |
| 1260 | ** potentially be used with an index if an appropriate index exists. |
| 1261 | ** This analysis does not consider whether or not the index exists; that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 1262 | ** is decided elsewhere. This analysis only looks at whether subterms |
| 1263 | ** appropriate for indexing exist. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1264 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 1265 | ** All examples A through E above satisfy case 2. But if a term |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1266 | ** also statisfies case 1 (such as B) we know that the optimizer will |
| 1267 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 1268 | ** satisfied. |
| 1269 | ** |
| 1270 | ** It might be the case that multiple tables are indexable. For example, |
| 1271 | ** (E) above is indexable on tables P, Q, and R. |
| 1272 | ** |
| 1273 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 1274 | ** separate indices to find rowids for each subterm and composing |
| 1275 | ** the union of all rowids using a RowSet object. This is similar |
| 1276 | ** to "bitmap indices" in other database engines. |
| 1277 | ** |
| 1278 | ** OTHERWISE: |
| 1279 | ** |
| 1280 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 1281 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1282 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1283 | static void exprAnalyzeOrTerm( |
| 1284 | SrcList *pSrc, /* the FROM clause */ |
| 1285 | WhereClause *pWC, /* the complete WHERE clause */ |
| 1286 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 1287 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1288 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 1289 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1290 | sqlite3 *db = pParse->db; /* Database connection */ |
| 1291 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 1292 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1293 | int i; /* Loop counters */ |
| 1294 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 1295 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 1296 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 1297 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 1298 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1299 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1300 | /* |
| 1301 | ** Break the OR clause into its separate subterms. The subterms are |
| 1302 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 1303 | ** object that is attached to the original OR clause term. |
| 1304 | */ |
| 1305 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 1306 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 1307 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1308 | if( pOrInfo==0 ) return; |
| 1309 | pTerm->wtFlags |= TERM_ORINFO; |
| 1310 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1311 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1312 | whereSplit(pOrWc, pExpr, TK_OR); |
| 1313 | exprAnalyzeAll(pSrc, pOrWc); |
| 1314 | if( db->mallocFailed ) return; |
| 1315 | assert( pOrWc->nTerm>=2 ); |
| 1316 | |
| 1317 | /* |
| 1318 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 1319 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 1320 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1321 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1322 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 1323 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1324 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1325 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1326 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1327 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 1328 | if( pAndInfo ){ |
| 1329 | WhereClause *pAndWC; |
| 1330 | WhereTerm *pAndTerm; |
| 1331 | int j; |
| 1332 | Bitmask b = 0; |
| 1333 | pOrTerm->u.pAndInfo = pAndInfo; |
| 1334 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 1335 | pOrTerm->eOperator = WO_AND; |
| 1336 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1337 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1338 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 1339 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 1340 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 1341 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1342 | if( !db->mallocFailed ){ |
| 1343 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 1344 | assert( pAndTerm->pExpr ); |
| 1345 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1346 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1347 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | indexable &= b; |
| 1351 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1352 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 1353 | /* Skip this term for now. We revisit it when we process the |
| 1354 | ** corresponding TERM_VIRTUAL term */ |
| 1355 | }else{ |
| 1356 | Bitmask b; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1357 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1358 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 1359 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1360 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1361 | } |
| 1362 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1363 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1364 | chngToIN = 0; |
| 1365 | }else{ |
| 1366 | chngToIN &= b; |
| 1367 | } |
| 1368 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1369 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1370 | |
| 1371 | /* |
| 1372 | ** Record the set of tables that satisfy case 2. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1373 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1374 | */ |
| 1375 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1376 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1377 | |
| 1378 | /* |
| 1379 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 1380 | ** we have to do some additional checking to see if case 1 really |
| 1381 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1382 | ** |
| 1383 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 1384 | ** that there is no possibility of transforming the OR clause into an |
| 1385 | ** IN operator because one or more terms in the OR clause contain |
| 1386 | ** something other than == on a column in the single table. The 1-bit |
| 1387 | ** case means that every term of the OR clause is of the form |
| 1388 | ** "table.column=expr" for some single table. The one bit that is set |
| 1389 | ** will correspond to the common table. We still need to check to make |
| 1390 | ** sure the same column is used on all terms. The 2-bit case is when |
| 1391 | ** the all terms are of the form "table1.column=table2.column". It |
| 1392 | ** might be possible to form an IN operator with either table1.column |
| 1393 | ** or table2.column as the LHS if either is common to every term of |
| 1394 | ** the OR clause. |
| 1395 | ** |
| 1396 | ** Note that terms of the form "table.column1=table.column2" (the |
| 1397 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1398 | */ |
| 1399 | if( chngToIN ){ |
| 1400 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 1401 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 1402 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1403 | int j = 0; /* Loop counter */ |
| 1404 | |
| 1405 | /* Search for a table and column that appears on one side or the |
| 1406 | ** other of the == operator in every subterm. That table and column |
| 1407 | ** will be recorded in iCursor and iColumn. There might not be any |
| 1408 | ** such table and column. Set okToChngToIN if an appropriate table |
| 1409 | ** and column is found but leave okToChngToIN false if not found. |
| 1410 | */ |
| 1411 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 1412 | pOrTerm = pOrWc->a; |
| 1413 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1414 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1415 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1416 | if( pOrTerm->leftCursor==iCursor ){ |
| 1417 | /* This is the 2-bit case and we are on the second iteration and |
| 1418 | ** current term is from the first iteration. So skip this term. */ |
| 1419 | assert( j==1 ); |
| 1420 | continue; |
| 1421 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1422 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1423 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 1424 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 1425 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 1426 | ** and use its inversion. */ |
| 1427 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 1428 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 1429 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 1430 | continue; |
| 1431 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1432 | iColumn = pOrTerm->u.leftColumn; |
| 1433 | iCursor = pOrTerm->leftCursor; |
| 1434 | break; |
| 1435 | } |
| 1436 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1437 | /* No candidate table+column was found. This can only occur |
| 1438 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1439 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1440 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1441 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1442 | break; |
| 1443 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1444 | testcase( j==1 ); |
| 1445 | |
| 1446 | /* We have found a candidate table and column. Check to see if that |
| 1447 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1448 | okToChngToIN = 1; |
| 1449 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1450 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1451 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1452 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1453 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1454 | okToChngToIN = 0; |
| 1455 | }else{ |
| 1456 | int affLeft, affRight; |
| 1457 | /* If the right-hand side is also a column, then the affinities |
| 1458 | ** of both right and left sides must be such that no type |
| 1459 | ** conversions are required on the right. (Ticket #2249) |
| 1460 | */ |
| 1461 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1462 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1463 | if( affRight!=0 && affRight!=affLeft ){ |
| 1464 | okToChngToIN = 0; |
| 1465 | }else{ |
| 1466 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1473 | ** case 1. In that case, construct a new virtual term that is |
| 1474 | ** pTerm converted into an IN operator. |
| 1475 | */ |
| 1476 | if( okToChngToIN ){ |
| 1477 | Expr *pDup; /* A transient duplicate expression */ |
| 1478 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1479 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1480 | Expr *pNew; /* The complete IN operator */ |
| 1481 | |
| 1482 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1483 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1484 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1485 | assert( pOrTerm->leftCursor==iCursor ); |
| 1486 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1487 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1488 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1489 | pLeft = pOrTerm->pExpr->pLeft; |
| 1490 | } |
| 1491 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1492 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1493 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1494 | if( pNew ){ |
| 1495 | int idxNew; |
| 1496 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1497 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1498 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1499 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1500 | testcase( idxNew==0 ); |
| 1501 | exprAnalyze(pSrc, pWC, idxNew); |
| 1502 | pTerm = &pWC->a[idxTerm]; |
| 1503 | pWC->a[idxNew].iParent = idxTerm; |
| 1504 | pTerm->nChild = 1; |
| 1505 | }else{ |
| 1506 | sqlite3ExprListDelete(db, pList); |
| 1507 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1508 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1509 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1510 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1511 | } |
| 1512 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1513 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1514 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1515 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1516 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1517 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1518 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1519 | ** |
| 1520 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1521 | ** to the standard form of "X <op> <expr>". |
| 1522 | ** |
| 1523 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1524 | ** columns, then the original expression is unchanged and a new virtual |
| 1525 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1526 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1527 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1528 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1529 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1530 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1531 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1532 | static void exprAnalyze( |
| 1533 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1534 | WhereClause *pWC, /* the WHERE clause */ |
| 1535 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1536 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1537 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1538 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1539 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1540 | Expr *pExpr; /* The expression to be analyzed */ |
| 1541 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1542 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1543 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1544 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1545 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1546 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1547 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1548 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1549 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1550 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1551 | if( db->mallocFailed ){ |
| 1552 | return; |
| 1553 | } |
| 1554 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1555 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1556 | pExpr = pTerm->pExpr; |
| 1557 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1558 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1559 | op = pExpr->op; |
| 1560 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1561 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1562 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1563 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1564 | }else{ |
| 1565 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1566 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1567 | }else if( op==TK_ISNULL ){ |
| 1568 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1569 | }else{ |
| 1570 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1571 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1572 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1573 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1574 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1575 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1576 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1577 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1578 | } |
| 1579 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1580 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1581 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1582 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1583 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1584 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1585 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1586 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1587 | if( pLeft->op==TK_COLUMN ){ |
| 1588 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1589 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1590 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1591 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1592 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1593 | WhereTerm *pNew; |
| 1594 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1595 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1596 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1597 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1598 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1599 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1600 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1601 | return; |
| 1602 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1603 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1604 | if( idxNew==0 ) return; |
| 1605 | pNew = &pWC->a[idxNew]; |
| 1606 | pNew->iParent = idxTerm; |
| 1607 | pTerm = &pWC->a[idxTerm]; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1608 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1609 | pTerm->wtFlags |= TERM_COPIED; |
drh | eb5bc92 | 2013-01-17 16:43:33 +0000 | [diff] [blame] | 1610 | if( pExpr->op==TK_EQ |
| 1611 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1612 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 1613 | ){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1614 | pTerm->eOperator |= WO_EQUIV; |
| 1615 | eExtraOp = WO_EQUIV; |
| 1616 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1617 | }else{ |
| 1618 | pDup = pExpr; |
| 1619 | pNew = pTerm; |
| 1620 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1621 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1622 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1623 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1624 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1625 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1626 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1627 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1628 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1629 | } |
| 1630 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1631 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1632 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1633 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1634 | ** that define the range that the BETWEEN implements. For example: |
| 1635 | ** |
| 1636 | ** a BETWEEN b AND c |
| 1637 | ** |
| 1638 | ** is converted into: |
| 1639 | ** |
| 1640 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1641 | ** |
| 1642 | ** The two new terms are added onto the end of the WhereClause object. |
| 1643 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1644 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1645 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1646 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1647 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1648 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1649 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1650 | int i; |
| 1651 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1652 | assert( pList!=0 ); |
| 1653 | assert( pList->nExpr==2 ); |
| 1654 | for(i=0; i<2; i++){ |
| 1655 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1656 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1657 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1658 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1659 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1660 | transferJoinMarkings(pNewExpr, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1661 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1662 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1663 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1664 | pTerm = &pWC->a[idxTerm]; |
| 1665 | pWC->a[idxNew].iParent = idxTerm; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1666 | } |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1667 | pTerm->nChild = 2; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1668 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1669 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1670 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1671 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1672 | /* Analyze a term that is composed of two or more subterms connected by |
| 1673 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1674 | */ |
| 1675 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1676 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1677 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1678 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1679 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1680 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1681 | |
| 1682 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1683 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1684 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1685 | ** |
| 1686 | ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 1687 | ** |
| 1688 | ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 1689 | ** |
| 1690 | ** The last character of the prefix "abc" is incremented to form the |
shane | 7bc71e5 | 2008-05-28 18:01:44 +0000 | [diff] [blame] | 1691 | ** termination condition "abd". |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1692 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1693 | if( pWC->op==TK_AND |
| 1694 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1695 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1696 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1697 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1698 | Expr *pNewExpr1; |
| 1699 | Expr *pNewExpr2; |
| 1700 | int idxNew1; |
| 1701 | int idxNew2; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1702 | Token sCollSeqName; /* Name of collating sequence */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1703 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1704 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1705 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1706 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1707 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1708 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1709 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1710 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1711 | /* The point is to increment the last character before the first |
| 1712 | ** wildcard. But if we increment '@', that will push it into the |
| 1713 | ** alphabetic range where case conversions will mess up the |
| 1714 | ** inequality. To avoid this, make sure to also run the full |
| 1715 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1716 | */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 1717 | if( c=='A'-1 ) isComplete = 0; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1718 | c = sqlite3UpperToLower[c]; |
| 1719 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1720 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1721 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1722 | sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 1723 | sCollSeqName.n = 6; |
| 1724 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1725 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1726 | sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1727 | pStr1, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1728 | transferJoinMarkings(pNewExpr1, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1729 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1730 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1731 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1732 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1733 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1734 | sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1735 | pStr2, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1736 | transferJoinMarkings(pNewExpr2, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1737 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1738 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1739 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1740 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1741 | if( isComplete ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1742 | pWC->a[idxNew1].iParent = idxTerm; |
| 1743 | pWC->a[idxNew2].iParent = idxTerm; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1744 | pTerm->nChild = 2; |
| 1745 | } |
| 1746 | } |
| 1747 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1748 | |
| 1749 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1750 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1751 | ** current expression is of the form: column MATCH expr. |
| 1752 | ** This information is used by the xBestIndex methods of |
| 1753 | ** virtual tables. The native query optimizer does not attempt |
| 1754 | ** to do anything with MATCH functions. |
| 1755 | */ |
| 1756 | if( isMatchOfColumn(pExpr) ){ |
| 1757 | int idxNew; |
| 1758 | Expr *pRight, *pLeft; |
| 1759 | WhereTerm *pNewTerm; |
| 1760 | Bitmask prereqColumn, prereqExpr; |
| 1761 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1762 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1763 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1764 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1765 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1766 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1767 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1768 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1769 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1770 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1771 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1772 | pNewTerm = &pWC->a[idxNew]; |
| 1773 | pNewTerm->prereqRight = prereqExpr; |
| 1774 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1775 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1776 | pNewTerm->eOperator = WO_MATCH; |
| 1777 | pNewTerm->iParent = idxTerm; |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1778 | pTerm = &pWC->a[idxTerm]; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1779 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1780 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1781 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1782 | } |
| 1783 | } |
| 1784 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1785 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1786 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1787 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1788 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1789 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1790 | ** virtual term of that form. |
| 1791 | ** |
| 1792 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 1793 | ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 1794 | ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 1795 | ** the start of the loop will prevent any results from being returned. |
| 1796 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1797 | if( pExpr->op==TK_NOTNULL |
| 1798 | && pExpr->pLeft->op==TK_COLUMN |
| 1799 | && pExpr->pLeft->iColumn>=0 |
drh | 40aa936 | 2013-06-28 17:29:25 +0000 | [diff] [blame] | 1800 | && OptimizationEnabled(db, SQLITE_Stat3) |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1801 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1802 | Expr *pNewExpr; |
| 1803 | Expr *pLeft = pExpr->pLeft; |
| 1804 | int idxNew; |
| 1805 | WhereTerm *pNewTerm; |
| 1806 | |
| 1807 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1808 | sqlite3ExprDup(db, pLeft, 0), |
| 1809 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1810 | |
| 1811 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1812 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1813 | if( idxNew ){ |
| 1814 | pNewTerm = &pWC->a[idxNew]; |
| 1815 | pNewTerm->prereqRight = 0; |
| 1816 | pNewTerm->leftCursor = pLeft->iTable; |
| 1817 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1818 | pNewTerm->eOperator = WO_GT; |
| 1819 | pNewTerm->iParent = idxTerm; |
| 1820 | pTerm = &pWC->a[idxTerm]; |
| 1821 | pTerm->nChild = 1; |
| 1822 | pTerm->wtFlags |= TERM_COPIED; |
| 1823 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1824 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1825 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1826 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1827 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1828 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1829 | ** an index for tables to the left of the join. |
| 1830 | */ |
| 1831 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1834 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1835 | ** This function searches pList for a entry that matches the iCol-th column |
| 1836 | ** of index pIdx. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1837 | ** |
| 1838 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1839 | ** no expression is found, -1 is returned. |
| 1840 | */ |
| 1841 | static int findIndexCol( |
| 1842 | Parse *pParse, /* Parse context */ |
| 1843 | ExprList *pList, /* Expression list to search */ |
| 1844 | int iBase, /* Cursor for table associated with pIdx */ |
| 1845 | Index *pIdx, /* Index to match column of */ |
| 1846 | int iCol /* Column of index to match */ |
| 1847 | ){ |
| 1848 | int i; |
| 1849 | const char *zColl = pIdx->azColl[iCol]; |
| 1850 | |
| 1851 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1852 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1853 | if( p->op==TK_COLUMN |
| 1854 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1855 | && p->iTable==iBase |
| 1856 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1857 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1858 | if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1859 | return i; |
| 1860 | } |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | return -1; |
| 1865 | } |
| 1866 | |
| 1867 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1868 | ** Return true if the DISTINCT expression-list passed as the third argument |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1869 | ** is redundant. |
| 1870 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1871 | ** A DISTINCT list is redundant if the database contains some subset of |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1872 | ** columns that are unique and non-null. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1873 | */ |
| 1874 | static int isDistinctRedundant( |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1875 | Parse *pParse, /* Parsing context */ |
| 1876 | SrcList *pTabList, /* The FROM clause */ |
| 1877 | WhereClause *pWC, /* The WHERE clause */ |
| 1878 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1879 | ){ |
| 1880 | Table *pTab; |
| 1881 | Index *pIdx; |
| 1882 | int i; |
| 1883 | int iBase; |
| 1884 | |
| 1885 | /* If there is more than one table or sub-select in the FROM clause of |
| 1886 | ** this query, then it will not be possible to show that the DISTINCT |
| 1887 | ** clause is redundant. */ |
| 1888 | if( pTabList->nSrc!=1 ) return 0; |
| 1889 | iBase = pTabList->a[0].iCursor; |
| 1890 | pTab = pTabList->a[0].pTab; |
| 1891 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1892 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1893 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1894 | ** current SELECT is a correlated sub-query. |
| 1895 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1896 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1897 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1898 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* Loop through all indices on the table, checking each to see if it makes |
| 1902 | ** the DISTINCT qualifier redundant. It does so if: |
| 1903 | ** |
| 1904 | ** 1. The index is itself UNIQUE, and |
| 1905 | ** |
| 1906 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1907 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1908 | ** where X is a constant value. The collation sequences of the |
| 1909 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1910 | ** |
| 1911 | ** 3. All of those index columns for which the WHERE clause does not |
| 1912 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1913 | */ |
| 1914 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1915 | if( pIdx->onError==OE_None ) continue; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1916 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 1917 | i16 iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1918 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1919 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1920 | if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){ |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1921 | break; |
| 1922 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1925 | if( i==pIdx->nKeyCol ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1926 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1927 | return 1; |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | return 0; |
| 1932 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1933 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1934 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1935 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1936 | ** Estimate the logarithm of the input value to base 2. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1937 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 1938 | static LogEst estLog(LogEst N){ |
| 1939 | LogEst x = sqlite3LogEst(N); |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 1940 | return x>33 ? x - 33 : 0; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1943 | /* |
| 1944 | ** Two routines for printing the content of an sqlite3_index_info |
| 1945 | ** structure. Used for testing and debugging only. If neither |
| 1946 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 1947 | ** are no-ops. |
| 1948 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 1949 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1950 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 1951 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1952 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1953 | for(i=0; i<p->nConstraint; i++){ |
| 1954 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 1955 | i, |
| 1956 | p->aConstraint[i].iColumn, |
| 1957 | p->aConstraint[i].iTermOffset, |
| 1958 | p->aConstraint[i].op, |
| 1959 | p->aConstraint[i].usable); |
| 1960 | } |
| 1961 | for(i=0; i<p->nOrderBy; i++){ |
| 1962 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 1963 | i, |
| 1964 | p->aOrderBy[i].iColumn, |
| 1965 | p->aOrderBy[i].desc); |
| 1966 | } |
| 1967 | } |
| 1968 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 1969 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1970 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1971 | for(i=0; i<p->nConstraint; i++){ |
| 1972 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 1973 | i, |
| 1974 | p->aConstraintUsage[i].argvIndex, |
| 1975 | p->aConstraintUsage[i].omit); |
| 1976 | } |
| 1977 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 1978 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 1979 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 1980 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
| 1981 | } |
| 1982 | #else |
| 1983 | #define TRACE_IDX_INPUTS(A) |
| 1984 | #define TRACE_IDX_OUTPUTS(A) |
| 1985 | #endif |
| 1986 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1987 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1988 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1989 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 1990 | ** could be used with an index to access pSrc, assuming an appropriate |
| 1991 | ** index existed. |
| 1992 | */ |
| 1993 | static int termCanDriveIndex( |
| 1994 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 1995 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 1996 | Bitmask notReady /* Tables in outer loops of the join */ |
| 1997 | ){ |
| 1998 | char aff; |
| 1999 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2000 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2001 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 2002 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2003 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 2004 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 2005 | return 1; |
| 2006 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2007 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2008 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2009 | |
| 2010 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2011 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2012 | ** Generate code to construct the Index object for an automatic index |
| 2013 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 2014 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2015 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2016 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2017 | Parse *pParse, /* The parsing context */ |
| 2018 | WhereClause *pWC, /* The WHERE clause */ |
| 2019 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 2020 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 2021 | WhereLevel *pLevel /* Write new index here */ |
| 2022 | ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2023 | int nKeyCol; /* Number of columns in the constructed index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2024 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 2025 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2026 | Index *pIdx; /* Object describing the transient index */ |
| 2027 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2028 | int addrInit; /* Address of the initialization bypass jump */ |
| 2029 | Table *pTable; /* The table being indexed */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2030 | int addrTop; /* Top of the index fill loop */ |
| 2031 | int regRecord; /* Register holding an index record */ |
| 2032 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2033 | int i; /* Loop counter */ |
| 2034 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2035 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2036 | WhereLoop *pLoop; /* The Loop object */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 2037 | char *zNotUsed; /* Extra space on the end of pIdx */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2038 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 2039 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 2040 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2041 | |
| 2042 | /* Generate code to skip over the creation and initialization of the |
| 2043 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 2044 | v = pParse->pVdbe; |
| 2045 | assert( v!=0 ); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2046 | addrInit = sqlite3CodeOnce(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2047 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2048 | /* Count the number of columns that will be added to the index |
| 2049 | ** and used to match WHERE clause constraints */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2050 | nKeyCol = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2051 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2052 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2053 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2054 | idxCols = 0; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 2055 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2056 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 2057 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2058 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2059 | testcase( iCol==BMS ); |
| 2060 | testcase( iCol==BMS-1 ); |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 2061 | if( !sentWarning ){ |
| 2062 | sqlite3_log(SQLITE_WARNING_AUTOINDEX, |
| 2063 | "automatic index on %s(%s)", pTable->zName, |
| 2064 | pTable->aCol[iCol].zName); |
| 2065 | sentWarning = 1; |
| 2066 | } |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2067 | if( (idxCols & cMask)==0 ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2068 | if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return; |
| 2069 | pLoop->aLTerm[nKeyCol++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2070 | idxCols |= cMask; |
| 2071 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2072 | } |
| 2073 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2074 | assert( nKeyCol>0 ); |
| 2075 | pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 2076 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 2077 | | WHERE_AUTO_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2078 | |
| 2079 | /* Count the number of additional columns needed to create a |
| 2080 | ** covering index. A "covering index" is an index that contains all |
| 2081 | ** columns that are needed by the query. With a covering index, the |
| 2082 | ** original table never needs to be accessed. Automatic indices must |
| 2083 | ** be a covering index because the index will not be updated if the |
| 2084 | ** original table changes and the index and table cannot both be used |
| 2085 | ** if they go out of sync. |
| 2086 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2087 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2088 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2089 | testcase( pTable->nCol==BMS-1 ); |
| 2090 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2091 | for(i=0; i<mxBitCol; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2092 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2093 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2094 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2095 | nKeyCol += pTable->nCol - BMS + 1; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2096 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2097 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2098 | |
| 2099 | /* Construct the Index object to describe this index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2100 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2101 | if( pIdx==0 ) return; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2102 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2103 | pIdx->zName = "auto-index"; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2104 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2105 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2106 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2107 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2108 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2109 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2110 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 2111 | testcase( iCol==BMS-1 ); |
| 2112 | testcase( iCol==BMS ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2113 | if( (idxCols & cMask)==0 ){ |
| 2114 | Expr *pX = pTerm->pExpr; |
| 2115 | idxCols |= cMask; |
| 2116 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 2117 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 6f2e6c0 | 2011-02-17 13:33:15 +0000 | [diff] [blame] | 2118 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2119 | n++; |
| 2120 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2121 | } |
| 2122 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2123 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2124 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2125 | /* Add additional columns needed to make the automatic index into |
| 2126 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2127 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2128 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2129 | pIdx->aiColumn[n] = i; |
| 2130 | pIdx->azColl[n] = "BINARY"; |
| 2131 | n++; |
| 2132 | } |
| 2133 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2134 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2135 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 2136 | pIdx->aiColumn[n] = i; |
| 2137 | pIdx->azColl[n] = "BINARY"; |
| 2138 | n++; |
| 2139 | } |
| 2140 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2141 | assert( n==nKeyCol ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 2142 | pIdx->aiColumn[n] = -1; |
| 2143 | pIdx->azColl[n] = "BINARY"; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2144 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2145 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2146 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 2147 | pLevel->iIdxCur = pParse->nTab++; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 2148 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 2149 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2150 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2151 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2152 | /* Fill the automatic index with content */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2153 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); |
| 2154 | regRecord = sqlite3GetTempReg(pParse); |
drh | 9eade08 | 2013-10-24 14:16:10 +0000 | [diff] [blame] | 2155 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2156 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 2157 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2158 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2159 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2160 | sqlite3VdbeJumpHere(v, addrTop); |
| 2161 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 2162 | |
| 2163 | /* Jump here when skipping the initialization */ |
| 2164 | sqlite3VdbeJumpHere(v, addrInit); |
| 2165 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2166 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2167 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 2168 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2169 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2170 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 2171 | ** responsibility of the caller to eventually release the structure |
| 2172 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 2173 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2174 | static sqlite3_index_info *allocateIndexInfo( |
| 2175 | Parse *pParse, |
| 2176 | WhereClause *pWC, |
| 2177 | struct SrcList_item *pSrc, |
| 2178 | ExprList *pOrderBy |
| 2179 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2180 | int i, j; |
| 2181 | int nTerm; |
| 2182 | struct sqlite3_index_constraint *pIdxCons; |
| 2183 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 2184 | struct sqlite3_index_constraint_usage *pUsage; |
| 2185 | WhereTerm *pTerm; |
| 2186 | int nOrderBy; |
| 2187 | sqlite3_index_info *pIdxInfo; |
| 2188 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2189 | /* Count the number of possible WHERE clause constraints referring |
| 2190 | ** to this virtual table */ |
| 2191 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 2192 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2193 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2194 | testcase( pTerm->eOperator & WO_IN ); |
| 2195 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2196 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2197 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2198 | nTerm++; |
| 2199 | } |
| 2200 | |
| 2201 | /* If the ORDER BY clause contains only columns in the current |
| 2202 | ** virtual table then allocate space for the aOrderBy part of |
| 2203 | ** the sqlite3_index_info structure. |
| 2204 | */ |
| 2205 | nOrderBy = 0; |
| 2206 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2207 | int n = pOrderBy->nExpr; |
| 2208 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2209 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2210 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 2211 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2212 | if( i==n){ |
| 2213 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | /* Allocate the sqlite3_index_info structure |
| 2218 | */ |
| 2219 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 2220 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 2221 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 2222 | if( pIdxInfo==0 ){ |
| 2223 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2224 | return 0; |
| 2225 | } |
| 2226 | |
| 2227 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 2228 | ** many fields that are declared "const" to prevent xBestIndex from |
| 2229 | ** changing them. We have to do some funky casting in order to |
| 2230 | ** initialize those fields. |
| 2231 | */ |
| 2232 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 2233 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 2234 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 2235 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 2236 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 2237 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 2238 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 2239 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 2240 | pUsage; |
| 2241 | |
| 2242 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2243 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2244 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2245 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2246 | testcase( pTerm->eOperator & WO_IN ); |
| 2247 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2248 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2249 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2250 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 2251 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2252 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2253 | if( op==WO_IN ) op = WO_EQ; |
| 2254 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2255 | /* The direct assignment in the previous line is possible only because |
| 2256 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 2257 | ** following asserts verify this fact. */ |
| 2258 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 2259 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 2260 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 2261 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 2262 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 2263 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2264 | assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2265 | j++; |
| 2266 | } |
| 2267 | for(i=0; i<nOrderBy; i++){ |
| 2268 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2269 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 2270 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 2271 | } |
| 2272 | |
| 2273 | return pIdxInfo; |
| 2274 | } |
| 2275 | |
| 2276 | /* |
| 2277 | ** The table object reference passed as the second argument to this function |
| 2278 | ** must represent a virtual table. This function invokes the xBestIndex() |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2279 | ** method of the virtual table with the sqlite3_index_info object that |
| 2280 | ** comes in as the 3rd argument to this function. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2281 | ** |
| 2282 | ** If an error occurs, pParse is populated with an error message and a |
| 2283 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 2284 | ** part of the sqlite3_index_info structure is left populated. |
| 2285 | ** |
| 2286 | ** Whether or not an error is returned, it is the responsibility of the |
| 2287 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 2288 | ** that this is required. |
| 2289 | */ |
| 2290 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 2291 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2292 | int i; |
| 2293 | int rc; |
| 2294 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2295 | TRACE_IDX_INPUTS(p); |
| 2296 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 2297 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2298 | |
| 2299 | if( rc!=SQLITE_OK ){ |
| 2300 | if( rc==SQLITE_NOMEM ){ |
| 2301 | pParse->db->mallocFailed = 1; |
| 2302 | }else if( !pVtab->zErrMsg ){ |
| 2303 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 2304 | }else{ |
| 2305 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 2306 | } |
| 2307 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2308 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2309 | pVtab->zErrMsg = 0; |
| 2310 | |
| 2311 | for(i=0; i<p->nConstraint; i++){ |
| 2312 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 2313 | sqlite3ErrorMsg(pParse, |
| 2314 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | return pParse->nErr; |
| 2319 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2320 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2321 | |
| 2322 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2323 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2324 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2325 | ** Estimate the location of a particular key among all keys in an |
| 2326 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2327 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2328 | ** aStat[0] Est. number of rows less than pVal |
| 2329 | ** aStat[1] Est. number of rows equal to pVal |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2330 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2331 | ** Return SQLITE_OK on success. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2332 | */ |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2333 | static void whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2334 | Parse *pParse, /* Database connection */ |
| 2335 | Index *pIdx, /* Index to consider domain of */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2336 | UnpackedRecord *pRec, /* Vector of values to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2337 | int roundUp, /* Round up if true. Round down if false */ |
| 2338 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2339 | ){ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 2340 | IndexSample *aSample = pIdx->aSample; |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 2341 | int iCol; /* Index of required stats in anEq[] etc. */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2342 | int iMin = 0; /* Smallest sample not yet tested */ |
| 2343 | int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */ |
| 2344 | int iTest; /* Next sample to test */ |
| 2345 | int res; /* Result of comparison operation */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2346 | |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 2347 | #ifndef SQLITE_DEBUG |
| 2348 | UNUSED_PARAMETER( pParse ); |
| 2349 | #endif |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 2350 | assert( pRec!=0 || pParse->db->mallocFailed ); |
| 2351 | if( pRec==0 ) return; |
| 2352 | iCol = pRec->nField - 1; |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2353 | assert( pIdx->nSample>0 ); |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 2354 | assert( pRec->nField>0 && iCol<pIdx->nSampleCol ); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2355 | do{ |
| 2356 | iTest = (iMin+i)/2; |
| 2357 | res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec); |
| 2358 | if( res<0 ){ |
| 2359 | iMin = iTest+1; |
| 2360 | }else{ |
| 2361 | i = iTest; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2362 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2363 | }while( res && iMin<i ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2364 | |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2365 | #ifdef SQLITE_DEBUG |
| 2366 | /* The following assert statements check that the binary search code |
| 2367 | ** above found the right answer. This block serves no purpose other |
| 2368 | ** than to invoke the asserts. */ |
| 2369 | if( res==0 ){ |
| 2370 | /* If (res==0) is true, then sample $i must be equal to pRec */ |
| 2371 | assert( i<pIdx->nSample ); |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 2372 | assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 2373 | || pParse->db->mallocFailed ); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2374 | }else{ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2375 | /* Otherwise, pRec must be smaller than sample $i and larger than |
| 2376 | ** sample ($i-1). */ |
| 2377 | assert( i==pIdx->nSample |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 2378 | || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 2379 | || pParse->db->mallocFailed ); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2380 | assert( i==0 |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 2381 | || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 2382 | || pParse->db->mallocFailed ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2383 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2384 | #endif /* ifdef SQLITE_DEBUG */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2385 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2386 | /* At this point, aSample[i] is the first sample that is greater than |
| 2387 | ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2388 | ** than pVal. If aSample[i]==pVal, then res==0. |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2389 | */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2390 | if( res==0 ){ |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2391 | aStat[0] = aSample[i].anLt[iCol]; |
| 2392 | aStat[1] = aSample[i].anEq[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2393 | }else{ |
| 2394 | tRowcnt iLower, iUpper, iGap; |
| 2395 | if( i==0 ){ |
| 2396 | iLower = 0; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2397 | iUpper = aSample[0].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2398 | }else{ |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2399 | iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol]; |
| 2400 | iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2401 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2402 | aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2403 | if( iLower>=iUpper ){ |
| 2404 | iGap = 0; |
| 2405 | }else{ |
| 2406 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2407 | } |
| 2408 | if( roundUp ){ |
| 2409 | iGap = (iGap*2)/3; |
| 2410 | }else{ |
| 2411 | iGap = iGap/3; |
| 2412 | } |
| 2413 | aStat[0] = iLower + iGap; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2414 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2415 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2416 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2417 | |
| 2418 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2419 | ** This function is used to estimate the number of rows that will be visited |
| 2420 | ** by scanning an index for a range of values. The range may have an upper |
| 2421 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2422 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2423 | ** example, assuming that index p is on t1(a): |
| 2424 | ** |
| 2425 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2426 | ** |_____| |_____| |
| 2427 | ** | | |
| 2428 | ** pLower pUpper |
| 2429 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2430 | ** If either of the upper or lower bound is not present, then NULL is passed in |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2431 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2432 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2433 | ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index |
| 2434 | ** column subject to the range constraint. Or, equivalently, the number of |
| 2435 | ** equality constraints optimized by the proposed index scan. For example, |
| 2436 | ** assuming index p is on t1(a, b), and the SQL query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2437 | ** |
| 2438 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2439 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2440 | ** then nEq is set to 1 (as the range restricted column, b, is the second |
| 2441 | ** left-most column of the index). Or, if the query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2442 | ** |
| 2443 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2444 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2445 | ** then nEq is set to 0. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2446 | ** |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2447 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2448 | ** number of rows that the index scan is expected to visit without |
| 2449 | ** considering the range constraints. If nEq is 0, this is the number of |
| 2450 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
| 2451 | ** to account for the range contraints pLower and pUpper. |
| 2452 | ** |
| 2453 | ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be |
| 2454 | ** used, each range inequality reduces the search space by a factor of 4. |
| 2455 | ** Hence a pair of constraints (x>? AND x<?) reduces the expected number of |
| 2456 | ** rows visited by a factor of 16. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2457 | */ |
| 2458 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2459 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2460 | WhereLoopBuilder *pBuilder, |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2461 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2462 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2463 | WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2464 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2465 | int rc = SQLITE_OK; |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2466 | int nOut = pLoop->nOut; |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2467 | LogEst nNew; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2468 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2469 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2470 | Index *p = pLoop->u.btree.pIndex; |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 2471 | int nEq = pLoop->u.btree.nEq; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2472 | |
drh | 74dade2 | 2013-09-04 18:14:53 +0000 | [diff] [blame] | 2473 | if( p->nSample>0 |
| 2474 | && nEq==pBuilder->nRecValid |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 2475 | && nEq<p->nSampleCol |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2476 | && OptimizationEnabled(pParse->db, SQLITE_Stat3) |
| 2477 | ){ |
| 2478 | UnpackedRecord *pRec = pBuilder->pRec; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2479 | tRowcnt a[2]; |
dan | 575ab2f | 2013-09-02 07:16:40 +0000 | [diff] [blame] | 2480 | u8 aff; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2481 | |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2482 | /* Variable iLower will be set to the estimate of the number of rows in |
| 2483 | ** the index that are less than the lower bound of the range query. The |
| 2484 | ** lower bound being the concatenation of $P and $L, where $P is the |
| 2485 | ** key-prefix formed by the nEq values matched against the nEq left-most |
| 2486 | ** columns of the index, and $L is the value in pLower. |
| 2487 | ** |
| 2488 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 2489 | ** is not a simple variable or literal value), the lower bound of the |
| 2490 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 2491 | ** if $L is available, whereKeyStats() is called for both ($P) and |
| 2492 | ** ($P:$L) and the larger of the two returned values used. |
| 2493 | ** |
| 2494 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 2495 | ** less than the upper bound of the range query. Where the upper bound |
| 2496 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 2497 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 2498 | */ |
| 2499 | tRowcnt iLower; |
| 2500 | tRowcnt iUpper; |
| 2501 | |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2502 | if( nEq==p->nKeyCol ){ |
mistachkin | c2cfb51 | 2013-09-04 04:04:08 +0000 | [diff] [blame] | 2503 | aff = SQLITE_AFF_INTEGER; |
| 2504 | }else{ |
| 2505 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
| 2506 | } |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2507 | /* Determine iLower and iUpper using ($P) only. */ |
| 2508 | if( nEq==0 ){ |
| 2509 | iLower = 0; |
| 2510 | iUpper = p->aiRowEst[0]; |
| 2511 | }else{ |
| 2512 | /* Note: this call could be optimized away - since the same values must |
| 2513 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 2514 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2515 | iLower = a[0]; |
| 2516 | iUpper = a[0] + a[1]; |
| 2517 | } |
| 2518 | |
| 2519 | /* If possible, improve on the iLower estimate using ($P:$L). */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2520 | if( pLower ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2521 | int bOk; /* True if value is extracted from pExpr */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2522 | Expr *pExpr = pLower->pExpr->pRight; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2523 | assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2524 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2525 | if( rc==SQLITE_OK && bOk ){ |
| 2526 | tRowcnt iNew; |
| 2527 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2528 | iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0); |
| 2529 | if( iNew>iLower ) iLower = iNew; |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2530 | nOut--; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2531 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2532 | } |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2533 | |
| 2534 | /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 2535 | if( pUpper ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2536 | int bOk; /* True if value is extracted from pExpr */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2537 | Expr *pExpr = pUpper->pExpr->pRight; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2538 | assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2539 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2540 | if( rc==SQLITE_OK && bOk ){ |
| 2541 | tRowcnt iNew; |
| 2542 | whereKeyStats(pParse, p, pRec, 1, a); |
| 2543 | iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0); |
| 2544 | if( iNew<iUpper ) iUpper = iNew; |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2545 | nOut--; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2546 | } |
| 2547 | } |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2548 | |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2549 | pBuilder->pRec = pRec; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2550 | if( rc==SQLITE_OK ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2551 | if( iUpper>iLower ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2552 | nNew = sqlite3LogEst(iUpper - iLower); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2553 | }else{ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2554 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2555 | } |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2556 | if( nNew<nOut ){ |
| 2557 | nOut = nNew; |
| 2558 | } |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2559 | pLoop->nOut = (LogEst)nOut; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2560 | WHERETRACE(0x10, ("range scan regions: %u..%u est=%d\n", |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2561 | (u32)iLower, (u32)iUpper, nOut)); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2562 | return SQLITE_OK; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2563 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2564 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2565 | #else |
| 2566 | UNUSED_PARAMETER(pParse); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2567 | UNUSED_PARAMETER(pBuilder); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2568 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2569 | assert( pLower || pUpper ); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2570 | /* TUNING: Each inequality constraint reduces the search space 4-fold. |
| 2571 | ** A BETWEEN operator, therefore, reduces the search space 16-fold */ |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2572 | nNew = nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2573 | if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2574 | nNew -= 20; assert( 20==sqlite3LogEst(4) ); |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2575 | nOut--; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2576 | } |
| 2577 | if( pUpper ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2578 | nNew -= 20; assert( 20==sqlite3LogEst(4) ); |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2579 | nOut--; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2580 | } |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2581 | if( nNew<10 ) nNew = 10; |
| 2582 | if( nNew<nOut ) nOut = nNew; |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2583 | pLoop->nOut = (LogEst)nOut; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2584 | return rc; |
| 2585 | } |
| 2586 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2587 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2588 | /* |
| 2589 | ** Estimate the number of rows that will be returned based on |
| 2590 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2591 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2592 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2593 | ** for that index. When pExpr==NULL that means the constraint is |
| 2594 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2595 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2596 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2597 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2598 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2599 | ** |
| 2600 | ** This routine can fail if it is unable to load a collating sequence |
| 2601 | ** required for string comparison, or if unable to allocate memory |
| 2602 | ** for a UTF conversion required for comparison. The error is stored |
| 2603 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2604 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2605 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2606 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2607 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2608 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2609 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2610 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2611 | Index *p = pBuilder->pNew->u.btree.pIndex; |
| 2612 | int nEq = pBuilder->pNew->u.btree.nEq; |
| 2613 | UnpackedRecord *pRec = pBuilder->pRec; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2614 | u8 aff; /* Column affinity */ |
| 2615 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2616 | tRowcnt a[2]; /* Statistics */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2617 | int bOk; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2618 | |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2619 | assert( nEq>=1 ); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2620 | assert( nEq<=(p->nKeyCol+1) ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2621 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2622 | assert( p->nSample>0 ); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2623 | assert( pBuilder->nRecValid<nEq ); |
| 2624 | |
| 2625 | /* If values are not available for all fields of the index to the left |
| 2626 | ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ |
| 2627 | if( pBuilder->nRecValid<(nEq-1) ){ |
| 2628 | return SQLITE_NOTFOUND; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2629 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2630 | |
dan | dd6e1f1 | 2013-08-10 19:08:30 +0000 | [diff] [blame] | 2631 | /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() |
| 2632 | ** below would return the same value. */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2633 | if( nEq>p->nKeyCol ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2634 | *pnRow = 1; |
| 2635 | return SQLITE_OK; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2636 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2637 | |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2638 | aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity; |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2639 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk); |
| 2640 | pBuilder->pRec = pRec; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2641 | if( rc!=SQLITE_OK ) return rc; |
| 2642 | if( bOk==0 ) return SQLITE_NOTFOUND; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2643 | pBuilder->nRecValid = nEq; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2644 | |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2645 | whereKeyStats(pParse, p, pRec, 0, a); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2646 | WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1])); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2647 | *pnRow = a[1]; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2648 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2649 | return rc; |
| 2650 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2651 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2652 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2653 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2654 | /* |
| 2655 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2656 | ** an IN constraint where the right-hand side of the IN operator |
| 2657 | ** is a list of values. Example: |
| 2658 | ** |
| 2659 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2660 | ** |
| 2661 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2662 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2663 | ** non-zero. |
| 2664 | ** |
| 2665 | ** This routine can fail if it is unable to load a collating sequence |
| 2666 | ** required for string comparison, or if unable to allocate memory |
| 2667 | ** for a UTF conversion required for comparison. The error is stored |
| 2668 | ** in the pParse structure. |
| 2669 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2670 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2671 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2672 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2673 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2674 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2675 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2676 | Index *p = pBuilder->pNew->u.btree.pIndex; |
| 2677 | int nRecValid = pBuilder->nRecValid; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2678 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2679 | tRowcnt nEst; /* Number of rows for a single term */ |
| 2680 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 2681 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2682 | |
| 2683 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2684 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 2685 | nEst = p->aiRowEst[0]; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2686 | rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2687 | nRowEst += nEst; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2688 | pBuilder->nRecValid = nRecValid; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2689 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2690 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2691 | if( rc==SQLITE_OK ){ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2692 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 2693 | *pnRow = nRowEst; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2694 | WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst)); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2695 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2696 | assert( pBuilder->nRecValid==nRecValid ); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2697 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2698 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2699 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2700 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2701 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2702 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2703 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2704 | ** or USING clause of that join. |
| 2705 | ** |
| 2706 | ** Consider the term t2.z='ok' in the following queries: |
| 2707 | ** |
| 2708 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2709 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2710 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2711 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2712 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2713 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2714 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2715 | ** |
| 2716 | ** Disabling a term causes that term to not be tested in the inner loop |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 2717 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2718 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2719 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2720 | ** but joins might run a little slower. The trick is to disable as much |
| 2721 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2722 | ** the wrong answer. See ticket #813. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2723 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2724 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 2725 | if( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2726 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2727 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 2728 | && (pLevel->notReady & pTerm->prereqAll)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2729 | ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 2730 | pTerm->wtFlags |= TERM_CODED; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 2731 | if( pTerm->iParent>=0 ){ |
| 2732 | WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 2733 | if( (--pOther->nChild)==0 ){ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 2734 | disableTerm(pLevel, pOther); |
| 2735 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2736 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2741 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2742 | ** to the n registers starting at base. |
| 2743 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2744 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2745 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2746 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2747 | ** |
| 2748 | ** This routine makes its own copy of zAff so that the caller is free |
| 2749 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2750 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2751 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2752 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2753 | if( zAff==0 ){ |
| 2754 | assert( pParse->db->mallocFailed ); |
| 2755 | return; |
| 2756 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2757 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2758 | |
| 2759 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2760 | ** and end of the affinity string. |
| 2761 | */ |
| 2762 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2763 | n--; |
| 2764 | base++; |
| 2765 | zAff++; |
| 2766 | } |
| 2767 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2768 | n--; |
| 2769 | } |
| 2770 | |
| 2771 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2772 | if( n>0 ){ |
| 2773 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2774 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2775 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2776 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2779 | |
| 2780 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2781 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2782 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2783 | ** coded. |
| 2784 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2785 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2786 | ** |
| 2787 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2788 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2789 | ** this routine sets up a loop that will iterate over all values of X. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2790 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2791 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2792 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2793 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2794 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2795 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2796 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2797 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2798 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2799 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2800 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2801 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2802 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2803 | assert( iTarget>0 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2804 | if( pX->op==TK_EQ ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2805 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2806 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2807 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2808 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2809 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2810 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2811 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2812 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2813 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2814 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2815 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2816 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2817 | && pLoop->u.btree.pIndex!=0 |
| 2818 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2819 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2820 | testcase( iEq==0 ); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2821 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2822 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2823 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2824 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2825 | iReg = iTarget; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2826 | eType = sqlite3FindInIndex(pParse, pX, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2827 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2828 | testcase( bRev ); |
| 2829 | bRev = !bRev; |
| 2830 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2831 | iTab = pX->iTable; |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2832 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2833 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2834 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2835 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2836 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2837 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2838 | pLevel->u.in.nIn++; |
| 2839 | pLevel->u.in.aInLoop = |
| 2840 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2841 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2842 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2843 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2844 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2845 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2846 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2847 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2848 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2849 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2850 | } |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2851 | pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2852 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2853 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2854 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2855 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2856 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2857 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2858 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2859 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2862 | /* |
| 2863 | ** Generate code that will evaluate all == and IN constraints for an |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2864 | ** index. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2865 | ** |
| 2866 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2867 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2868 | ** The index has as many as three equality constraints, but in this |
| 2869 | ** example, the third "c" value is an inequality. So only two |
| 2870 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2871 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2872 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2873 | ** |
| 2874 | ** In the example above nEq==2. But this subroutine works for any value |
| 2875 | ** of nEq including 0. If nEq==0, this routine is nearly a no-op. |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2876 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2877 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2878 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2879 | ** This routine always allocates at least one memory cell and returns |
| 2880 | ** the index of that memory cell. The code that |
| 2881 | ** calls this routine will use that memory cell to store the termination |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2882 | ** key value of the loop. If one or more IN operators appear, then |
| 2883 | ** this routine allocates an additional nEq memory cells for internal |
| 2884 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2885 | ** |
| 2886 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2887 | ** copy of the column affinity string of the index allocated using |
| 2888 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2889 | ** with equality constraints that use NONE affinity are set to |
| 2890 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2891 | ** |
| 2892 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2893 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2894 | ** |
| 2895 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2896 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2897 | ** no conversion should be attempted before using a t2.b value as part of |
| 2898 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2899 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2900 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2901 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2902 | Parse *pParse, /* Parsing context */ |
| 2903 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2904 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2905 | int nExtraReg, /* Number of extra registers to allocate */ |
| 2906 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2907 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2908 | int nEq; /* The number of == or IN constraints to code */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2909 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 2910 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2911 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2912 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2913 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2914 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2915 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2916 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2917 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2918 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2919 | pLoop = pLevel->pWLoop; |
| 2920 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 2921 | nEq = pLoop->u.btree.nEq; |
| 2922 | pIdx = pLoop->u.btree.pIndex; |
| 2923 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2924 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2925 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2926 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2927 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2928 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2929 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2930 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2931 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 2932 | if( !zAff ){ |
| 2933 | pParse->db->mallocFailed = 1; |
| 2934 | } |
| 2935 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2936 | /* Evaluate the equality constraints |
| 2937 | */ |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 2938 | assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
drh | c49de5d | 2007-01-19 01:06:01 +0000 | [diff] [blame] | 2939 | for(j=0; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2940 | int r1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2941 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2942 | assert( pTerm!=0 ); |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2943 | /* The following true for indices with redundant columns. |
| 2944 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 2945 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 2946 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2947 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2948 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2949 | if( nReg==1 ){ |
| 2950 | sqlite3ReleaseTempReg(pParse, regBase); |
| 2951 | regBase = r1; |
| 2952 | }else{ |
| 2953 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 2954 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2955 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 2956 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 2957 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2958 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2959 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 2f2855b | 2009-11-18 01:25:26 +0000 | [diff] [blame] | 2960 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2961 | if( zAff ){ |
| 2962 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 2963 | zAff[j] = SQLITE_AFF_NONE; |
| 2964 | } |
| 2965 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 2966 | zAff[j] = SQLITE_AFF_NONE; |
| 2967 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2968 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2969 | } |
| 2970 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2971 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2972 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2975 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2976 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2977 | ** This routine is a helper for explainIndexRange() below |
| 2978 | ** |
| 2979 | ** pStr holds the text of an expression that we are building up one term |
| 2980 | ** at a time. This routine adds a new term to the end of the expression. |
| 2981 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 2982 | ** terms only. |
| 2983 | */ |
| 2984 | static void explainAppendTerm( |
| 2985 | StrAccum *pStr, /* The text expression being built */ |
| 2986 | int iTerm, /* Index of this term. First is zero */ |
| 2987 | const char *zColumn, /* Name of the column */ |
| 2988 | const char *zOp /* Name of the operator */ |
| 2989 | ){ |
| 2990 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 2991 | sqlite3StrAccumAppend(pStr, zColumn, -1); |
| 2992 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 2993 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 2994 | } |
| 2995 | |
| 2996 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2997 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 2998 | ** function returns a pointer to a string buffer containing a description |
| 2999 | ** of the subset of table rows scanned by the strategy in the form of an |
| 3000 | ** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 3001 | ** |
| 3002 | ** For example, if the query: |
| 3003 | ** |
| 3004 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 3005 | ** |
| 3006 | ** is run and there is an index on (a, b), then this function returns a |
| 3007 | ** string similar to: |
| 3008 | ** |
| 3009 | ** "a=? AND b>?" |
| 3010 | ** |
| 3011 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 3012 | ** It is the responsibility of the caller to free the buffer when it is |
| 3013 | ** no longer required. |
| 3014 | */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3015 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 3016 | Index *pIndex = pLoop->u.btree.pIndex; |
| 3017 | int nEq = pLoop->u.btree.nEq; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3018 | int i, j; |
| 3019 | Column *aCol = pTab->aCol; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3020 | i16 *aiColumn = pIndex->aiColumn; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3021 | StrAccum txt; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3022 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3023 | if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3024 | return 0; |
| 3025 | } |
| 3026 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
drh | 03b6df1 | 2010-11-15 16:29:30 +0000 | [diff] [blame] | 3027 | txt.db = db; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3028 | sqlite3StrAccumAppend(&txt, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3029 | for(i=0; i<nEq; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3030 | char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName; |
dan | e934e63 | 2013-08-20 17:14:57 +0000 | [diff] [blame] | 3031 | explainAppendTerm(&txt, i, z, "="); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3034 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3035 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3036 | char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName; |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3037 | explainAppendTerm(&txt, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3038 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3039 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3040 | char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName; |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3041 | explainAppendTerm(&txt, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3042 | } |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3043 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 3044 | return sqlite3StrAccumFinish(&txt); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3047 | /* |
| 3048 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 3049 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 3050 | ** record is added to the output to describe the table scan strategy in |
| 3051 | ** pLevel. |
| 3052 | */ |
| 3053 | static void explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3054 | Parse *pParse, /* Parse context */ |
| 3055 | SrcList *pTabList, /* Table list this loop refers to */ |
| 3056 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 3057 | int iLevel, /* Value for "level" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3058 | int iFrom, /* Value for "from" column of output */ |
| 3059 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3060 | ){ |
| 3061 | if( pParse->explain==2 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3062 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3063 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 3064 | sqlite3 *db = pParse->db; /* Database handle */ |
| 3065 | char *zMsg; /* Text to add to EQP output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3066 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3067 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3068 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 3069 | u32 flags; /* Flags that describe this loop */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3070 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3071 | pLoop = pLevel->pWLoop; |
| 3072 | flags = pLoop->wsFlags; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3073 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3074 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3075 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 3076 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 3077 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3078 | |
| 3079 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3080 | if( pItem->pSelect ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3081 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3082 | }else{ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3083 | zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3086 | if( pItem->zAlias ){ |
| 3087 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 3088 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3089 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3090 | && ALWAYS(pLoop->u.btree.pIndex!=0) |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3091 | ){ |
| 3092 | char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3093 | zMsg = sqlite3MAppendf(db, zMsg, |
| 3094 | ((flags & WHERE_AUTO_INDEX) ? |
| 3095 | "%s USING AUTOMATIC %sINDEX%.0s%s" : |
| 3096 | "%s USING %sINDEX %s%s"), |
| 3097 | zMsg, ((flags & WHERE_IDX_ONLY) ? "COVERING " : ""), |
| 3098 | pLoop->u.btree.pIndex->zName, zWhere); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3099 | sqlite3DbFree(db, zWhere); |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 3100 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3101 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3102 | |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 3103 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3104 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 3105 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3106 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 3107 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 3108 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3109 | }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3110 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 3111 | } |
| 3112 | } |
| 3113 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3114 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3115 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3116 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3117 | } |
| 3118 | #endif |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3119 | zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3120 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | #else |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3124 | # define explainOneScan(u,v,w,x,y,z) |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3125 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 3126 | |
| 3127 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3128 | /* |
| 3129 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 3130 | ** implementation described by pWInfo. |
| 3131 | */ |
| 3132 | static Bitmask codeOneLoopStart( |
| 3133 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 3134 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3135 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3136 | ){ |
| 3137 | int j, k; /* Loop counters */ |
| 3138 | int iCur; /* The VDBE cursor for the table */ |
| 3139 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 3140 | int omitTable; /* True if we use the index only */ |
| 3141 | int bRev; /* True if we need to scan in reverse order */ |
| 3142 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3143 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3144 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 3145 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 3146 | Parse *pParse; /* Parsing context */ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3147 | sqlite3 *db; /* Database connection */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3148 | Vdbe *v; /* The prepared stmt under constructions */ |
| 3149 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3150 | int addrBrk; /* Jump here to break out of the loop */ |
| 3151 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3152 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 3153 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3154 | |
| 3155 | pParse = pWInfo->pParse; |
| 3156 | v = pParse->pVdbe; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3157 | pWC = &pWInfo->sWC; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3158 | db = pParse->db; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3159 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3160 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3161 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 3162 | iCur = pTabItem->iCursor; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3163 | pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3164 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 3165 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3166 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 3167 | VdbeNoopComment((v, "Begin WHERE-Loop %d: %s", iLevel,pTabItem->pTab->zName)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3168 | |
| 3169 | /* Create labels for the "break" and "continue" instructions |
| 3170 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 3171 | ** Jump to cont to go immediately to the next iteration of the |
| 3172 | ** loop. |
| 3173 | ** |
| 3174 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 3175 | ** means to continue with the next IN value combination. When |
| 3176 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 3177 | ** is the same as "addrBrk". |
| 3178 | */ |
| 3179 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 3180 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 3181 | |
| 3182 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 3183 | ** initialize a memory cell that records if this table matches any |
| 3184 | ** row of the left table of the join. |
| 3185 | */ |
| 3186 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 3187 | pLevel->iLeftJoin = ++pParse->nMem; |
| 3188 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 3189 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 3190 | } |
| 3191 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 3192 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 3193 | if( pTabItem->viaCoroutine ){ |
| 3194 | int regYield = pTabItem->regReturn; |
| 3195 | sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield); |
| 3196 | pLevel->p2 = sqlite3VdbeAddOp1(v, OP_Yield, regYield); |
| 3197 | VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName)); |
| 3198 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 3199 | pLevel->op = OP_Goto; |
| 3200 | }else |
| 3201 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3202 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3203 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 3204 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3205 | ** to access the data. |
| 3206 | */ |
| 3207 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3208 | int addrNotFound; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3209 | int nConstraint = pLoop->nLTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3210 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3211 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3212 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3213 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3214 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 3215 | int iTarget = iReg+j+2; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3216 | pTerm = pLoop->aLTerm[j]; |
drh | 95ed68d | 2013-06-12 17:55:50 +0000 | [diff] [blame] | 3217 | if( pTerm==0 ) continue; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3218 | if( pTerm->eOperator & WO_IN ){ |
| 3219 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 3220 | addrNotFound = pLevel->addrNxt; |
| 3221 | }else{ |
| 3222 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 3223 | } |
| 3224 | } |
| 3225 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 3226 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3227 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 3228 | pLoop->u.vtab.idxStr, |
| 3229 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 3230 | pLoop->u.vtab.needFree = 0; |
| 3231 | for(j=0; j<nConstraint && j<16; j++){ |
| 3232 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3233 | disableTerm(pLevel, pLoop->aLTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3234 | } |
| 3235 | } |
| 3236 | pLevel->op = OP_VNext; |
| 3237 | pLevel->p1 = iCur; |
| 3238 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3239 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3240 | sqlite3ExprCachePop(pParse, 1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3241 | }else |
| 3242 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3243 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3244 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3245 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 3246 | ){ |
| 3247 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3248 | ** equality comparison against the ROWID field. Or |
| 3249 | ** we reference multiple rows using a "rowid IN (...)" |
| 3250 | ** construct. |
| 3251 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3252 | assert( pLoop->u.btree.nEq==1 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3253 | iReleaseReg = sqlite3GetTempReg(pParse); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3254 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3255 | assert( pTerm!=0 ); |
| 3256 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3257 | assert( omitTable==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3258 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3259 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3260 | addrNxt = pLevel->addrNxt; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3261 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 3262 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 3263 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3264 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3265 | VdbeComment((v, "pk")); |
| 3266 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3267 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3268 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 3269 | ){ |
| 3270 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3271 | */ |
| 3272 | int testOp = OP_Noop; |
| 3273 | int start; |
| 3274 | int memEndValue = 0; |
| 3275 | WhereTerm *pStart, *pEnd; |
| 3276 | |
| 3277 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3278 | j = 0; |
| 3279 | pStart = pEnd = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3280 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 3281 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3282 | assert( pStart!=0 || pEnd!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3283 | if( bRev ){ |
| 3284 | pTerm = pStart; |
| 3285 | pStart = pEnd; |
| 3286 | pEnd = pTerm; |
| 3287 | } |
| 3288 | if( pStart ){ |
| 3289 | Expr *pX; /* The expression that defines the start bound */ |
| 3290 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 3291 | |
| 3292 | /* The following constant maps TK_xx codes into corresponding |
| 3293 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 3294 | */ |
| 3295 | const u8 aMoveOp[] = { |
| 3296 | /* TK_GT */ OP_SeekGt, |
| 3297 | /* TK_LE */ OP_SeekLe, |
| 3298 | /* TK_LT */ OP_SeekLt, |
| 3299 | /* TK_GE */ OP_SeekGe |
| 3300 | }; |
| 3301 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 3302 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 3303 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 3304 | |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3305 | assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3306 | testcase( pStart->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3307 | pX = pStart->pExpr; |
| 3308 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3309 | testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3310 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 3311 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| 3312 | VdbeComment((v, "pk")); |
| 3313 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 3314 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 3315 | disableTerm(pLevel, pStart); |
| 3316 | }else{ |
| 3317 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 3318 | } |
| 3319 | if( pEnd ){ |
| 3320 | Expr *pX; |
| 3321 | pX = pEnd->pExpr; |
| 3322 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3323 | assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 3324 | testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3325 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3326 | memEndValue = ++pParse->nMem; |
| 3327 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 3328 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 3329 | testOp = bRev ? OP_Le : OP_Ge; |
| 3330 | }else{ |
| 3331 | testOp = bRev ? OP_Lt : OP_Gt; |
| 3332 | } |
| 3333 | disableTerm(pLevel, pEnd); |
| 3334 | } |
| 3335 | start = sqlite3VdbeCurrentAddr(v); |
| 3336 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 3337 | pLevel->p1 = iCur; |
| 3338 | pLevel->p2 = start; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3339 | assert( pLevel->p5==0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3340 | if( testOp!=OP_Noop ){ |
| 3341 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3342 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3343 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3344 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 3345 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3346 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3347 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3348 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3349 | ** |
| 3350 | ** The WHERE clause may contain zero or more equality |
| 3351 | ** terms ("==" or "IN" operators) that refer to the N |
| 3352 | ** left-most columns of the index. It may also contain |
| 3353 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3354 | ** column that immediately follows the N equalities. Only |
| 3355 | ** the right-most column can be an inequality - the rest must |
| 3356 | ** use the "==" and "IN" operators. For example, if the |
| 3357 | ** index is on (x,y,z), then the following clauses are all |
| 3358 | ** optimized: |
| 3359 | ** |
| 3360 | ** x=5 |
| 3361 | ** x=5 AND y=10 |
| 3362 | ** x=5 AND y<10 |
| 3363 | ** x=5 AND y>5 AND y<10 |
| 3364 | ** x=5 AND y=5 AND z<=10 |
| 3365 | ** |
| 3366 | ** The z<10 term of the following cannot be used, only |
| 3367 | ** the x=5 term: |
| 3368 | ** |
| 3369 | ** x=5 AND z<10 |
| 3370 | ** |
| 3371 | ** N may be zero if there are inequality constraints. |
| 3372 | ** If there are no inequality constraints, then N is at |
| 3373 | ** least one. |
| 3374 | ** |
| 3375 | ** This case is also used when there are no WHERE clause |
| 3376 | ** constraints but an index is selected anyway, in order |
| 3377 | ** to force the output order to conform to an ORDER BY. |
| 3378 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3379 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3380 | 0, |
| 3381 | 0, |
| 3382 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3383 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 3384 | OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3385 | OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ |
| 3386 | OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ |
| 3387 | OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ |
| 3388 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3389 | static const u8 aEndOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3390 | OP_Noop, /* 0: (!end_constraints) */ |
| 3391 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 3392 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 3393 | }; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3394 | int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 3395 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3396 | int regBase; /* Base register holding constraint values */ |
| 3397 | int r1; /* Temp register */ |
| 3398 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3399 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3400 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3401 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3402 | int start_constraints; /* Start of range is constrained */ |
| 3403 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3404 | Index *pIdx; /* The index we will be using */ |
| 3405 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3406 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3407 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3408 | char *zStartAff; /* Affinity for start of range constraint */ |
| 3409 | char *zEndAff; /* Affinity for end of range constraint */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3410 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3411 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3412 | iIdxCur = pLevel->iIdxCur; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3413 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3414 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3415 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3416 | ** query, then the caller will only allow the loop to run for |
| 3417 | ** a single iteration. This means that the first row returned |
| 3418 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3419 | ** the first one after the nEq equality constraints in the index, |
| 3420 | ** this requires some special handling. |
| 3421 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3422 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 3423 | && (pWInfo->bOBSat!=0) |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3424 | && (pIdx->nKeyCol>nEq) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3425 | ){ |
| 3426 | /* assert( pOrderBy->nExpr==1 ); */ |
| 3427 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 3428 | isMinQuery = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3429 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | /* Find any inequality constraint terms for the start and end |
| 3433 | ** of the range. |
| 3434 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3435 | j = nEq; |
| 3436 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3437 | pRangeStart = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3438 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3439 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3440 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3441 | pRangeEnd = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3442 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3445 | /* Generate code to evaluate all constraint terms using == or IN |
| 3446 | ** and store the values of those terms in an array of registers |
| 3447 | ** starting at regBase. |
| 3448 | */ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 3449 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3450 | zEndAff = sqlite3DbStrDup(db, zStartAff); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3451 | addrNxt = pLevel->addrNxt; |
| 3452 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3453 | /* If we are doing a reverse order scan on an ascending index, or |
| 3454 | ** a forward order scan on a descending index, interchange the |
| 3455 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3456 | */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3457 | if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3458 | || (bRev && pIdx->nKeyCol==nEq) |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3459 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3460 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 3461 | } |
| 3462 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3463 | testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 3464 | testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 3465 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 3466 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3467 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3468 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3469 | start_constraints = pRangeStart || nEq>0; |
| 3470 | |
| 3471 | /* Seek the index cursor to the start of the range. */ |
| 3472 | nConstraint = nEq; |
| 3473 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3474 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3475 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3476 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){ |
| 3477 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3478 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3479 | if( zStartAff ){ |
| 3480 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3481 | /* Since the comparison is to be performed with no conversions |
| 3482 | ** applied to the operands, set the affinity to apply to pRight to |
| 3483 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3484 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3485 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3486 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3487 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3488 | } |
| 3489 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3490 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3491 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3492 | }else if( isMinQuery ){ |
| 3493 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3494 | nConstraint++; |
| 3495 | startEq = 0; |
| 3496 | start_constraints = 1; |
| 3497 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3498 | codeApplyAffinity(pParse, regBase, nConstraint, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3499 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3500 | assert( op!=0 ); |
| 3501 | testcase( op==OP_Rewind ); |
| 3502 | testcase( op==OP_Last ); |
| 3503 | testcase( op==OP_SeekGt ); |
| 3504 | testcase( op==OP_SeekGe ); |
| 3505 | testcase( op==OP_SeekLe ); |
| 3506 | testcase( op==OP_SeekLt ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3507 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3508 | |
| 3509 | /* Load the value for the inequality constraint at the end of the |
| 3510 | ** range (if any). |
| 3511 | */ |
| 3512 | nConstraint = nEq; |
| 3513 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3514 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3515 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3516 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3517 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ |
| 3518 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3519 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3520 | if( zEndAff ){ |
| 3521 | if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3522 | /* Since the comparison is to be performed with no conversions |
| 3523 | ** applied to the operands, set the affinity to apply to pRight to |
| 3524 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3525 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3526 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3527 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ |
| 3528 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3529 | } |
| 3530 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3531 | codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3532 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3533 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3534 | } |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3535 | sqlite3DbFree(db, zStartAff); |
| 3536 | sqlite3DbFree(db, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3537 | |
| 3538 | /* Top of the loop body */ |
| 3539 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3540 | |
| 3541 | /* Check if the index cursor is past the end of the range. */ |
| 3542 | op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)]; |
| 3543 | testcase( op==OP_Noop ); |
| 3544 | testcase( op==OP_IdxGE ); |
| 3545 | testcase( op==OP_IdxLT ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3546 | if( op!=OP_Noop ){ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3547 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3548 | sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0); |
| 3549 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3550 | |
| 3551 | /* If there are inequality constraints, check that the value |
| 3552 | ** of the table column that the inequality contrains is not NULL. |
| 3553 | ** If it is, jump to the next iteration of the loop. |
| 3554 | */ |
| 3555 | r1 = sqlite3GetTempReg(pParse); |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 3556 | testcase( pLoop->wsFlags & WHERE_BTM_LIMIT ); |
| 3557 | testcase( pLoop->wsFlags & WHERE_TOP_LIMIT ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3558 | if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3559 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 3560 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 3561 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3562 | sqlite3ReleaseTempReg(pParse, r1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3563 | |
| 3564 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3565 | disableTerm(pLevel, pRangeStart); |
| 3566 | disableTerm(pLevel, pRangeEnd); |
drh | 85c1c55 | 2013-10-24 00:18:18 +0000 | [diff] [blame] | 3567 | if( omitTable ){ |
| 3568 | /* pIdx is a covering index. No need to access the main table. */ |
| 3569 | }else if( HasRowid(pIdx->pTable) ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3570 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3571 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3572 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3573 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | 85c1c55 | 2013-10-24 00:18:18 +0000 | [diff] [blame] | 3574 | }else{ |
| 3575 | Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 3576 | iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 3577 | for(j=0; j<pPk->nKeyCol; j++){ |
| 3578 | k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); |
| 3579 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); |
| 3580 | } |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 3581 | sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, |
| 3582 | iRowidReg, pPk->nKeyCol); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3583 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3584 | |
| 3585 | /* Record the instruction used to terminate the loop. Disable |
| 3586 | ** WHERE clause terms made redundant by the index range scan. |
| 3587 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3588 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3589 | pLevel->op = OP_Noop; |
| 3590 | }else if( bRev ){ |
| 3591 | pLevel->op = OP_Prev; |
| 3592 | }else{ |
| 3593 | pLevel->op = OP_Next; |
| 3594 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3595 | pLevel->p1 = iIdxCur; |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3596 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3597 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3598 | }else{ |
| 3599 | assert( pLevel->p5==0 ); |
| 3600 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3601 | }else |
| 3602 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3603 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3604 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3605 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3606 | ** |
| 3607 | ** Example: |
| 3608 | ** |
| 3609 | ** CREATE TABLE t1(a,b,c,d); |
| 3610 | ** CREATE INDEX i1 ON t1(a); |
| 3611 | ** CREATE INDEX i2 ON t1(b); |
| 3612 | ** CREATE INDEX i3 ON t1(c); |
| 3613 | ** |
| 3614 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3615 | ** |
| 3616 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3617 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3618 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3619 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3620 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3621 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3622 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3623 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3624 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3625 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3626 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3627 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3628 | ** Gosub 2 A |
| 3629 | ** sqlite3WhereEnd() |
| 3630 | ** |
| 3631 | ** Following the above, code to terminate the loop. Label A, the target |
| 3632 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3633 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3634 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3635 | ** Goto B # The loop is finished. |
| 3636 | ** |
| 3637 | ** A: <loop body> # Return data, whatever. |
| 3638 | ** |
| 3639 | ** Return 2 # Jump back to the Gosub |
| 3640 | ** |
| 3641 | ** B: <after the loop> |
| 3642 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3643 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3644 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3645 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3646 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3647 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3648 | |
| 3649 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3650 | int regRowset = 0; /* Register for RowSet object */ |
| 3651 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3652 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3653 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3654 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3655 | int ii; /* Loop counter */ |
| 3656 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3657 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3658 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3659 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3660 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3661 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3662 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3663 | pLevel->op = OP_Return; |
| 3664 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3665 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3666 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3667 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3668 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3669 | */ |
| 3670 | if( pWInfo->nLevel>1 ){ |
| 3671 | int nNotReady; /* The number of notReady tables */ |
| 3672 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3673 | nNotReady = pWInfo->nLevel - iLevel - 1; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3674 | pOrTab = sqlite3StackAllocRaw(db, |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3675 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3676 | if( pOrTab==0 ) return notReady; |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 3677 | pOrTab->nAlloc = (u8)(nNotReady + 1); |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3678 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3679 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3680 | origSrc = pWInfo->pTabList->a; |
| 3681 | for(k=1; k<=nNotReady; k++){ |
| 3682 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3683 | } |
| 3684 | }else{ |
| 3685 | pOrTab = pWInfo->pTabList; |
| 3686 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3687 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3688 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 3689 | ** equivalent to an empty rowset. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3690 | ** |
| 3691 | ** Also initialize regReturn to contain the address of the instruction |
| 3692 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3693 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3694 | ** over the top of the loop into the body of it. In this case the |
| 3695 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3696 | ** fall through to the next instruction, just as an OP_Next does if |
| 3697 | ** called on an uninitialized cursor. |
| 3698 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3699 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3700 | regRowset = ++pParse->nMem; |
| 3701 | regRowid = ++pParse->nMem; |
| 3702 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3703 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3704 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3705 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3706 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3707 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3708 | ** That way, terms in y that are factored into the disjunction will |
| 3709 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3710 | ** |
| 3711 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3712 | ** the "interesting" terms of z - terms that did not originate in the |
| 3713 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3714 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3715 | ** |
| 3716 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3717 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3718 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3719 | */ |
| 3720 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3721 | int iTerm; |
| 3722 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3723 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 3724 | if( &pWC->a[iTerm] == pTerm ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3725 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 3726 | if( pWC->a[iTerm].wtFlags & (TERM_ORINFO) ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3727 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3728 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 3729 | pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3730 | } |
| 3731 | if( pAndExpr ){ |
| 3732 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3733 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3736 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3737 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3738 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3739 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3740 | Expr *pOrExpr = pOrTerm->pExpr; |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3741 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3742 | pAndExpr->pLeft = pOrExpr; |
| 3743 | pOrExpr = pAndExpr; |
| 3744 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3745 | /* Loop through table entries that match term pOrTerm. */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3746 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 3747 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3748 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3749 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3750 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3751 | WhereLoop *pSubLoop; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3752 | explainOneScan( |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3753 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3754 | ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3755 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3756 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3757 | int r; |
| 3758 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3759 | regRowid, 0); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3760 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| 3761 | sqlite3VdbeCurrentAddr(v)+2, r, iSet); |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3762 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3763 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 3764 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3765 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 3766 | ** contained one or more AND term from a notReady table. The |
| 3767 | ** terms from the notReady table could not be tested and will |
| 3768 | ** need to be tested later. |
| 3769 | */ |
| 3770 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 3771 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3772 | /* If all of the OR-connected terms are optimized using the same |
| 3773 | ** index, and the index is opened using the same cursor number |
| 3774 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 3775 | ** be possible to use that index as a covering index. |
| 3776 | ** |
| 3777 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 3778 | ** uses an index, and this is either the first OR-connected term |
| 3779 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3780 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 3781 | ** pCov to NULL to indicate that no candidate covering index will |
| 3782 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3783 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3784 | pSubLoop = pSubWInfo->a[0].pWLoop; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3785 | assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3786 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3787 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3788 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3789 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
drh | 907717f | 2013-06-04 18:03:22 +0000 | [diff] [blame] | 3790 | pCov = pSubLoop->u.btree.pIndex; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3791 | }else{ |
| 3792 | pCov = 0; |
| 3793 | } |
| 3794 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3795 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 3796 | sqlite3WhereEnd(pSubWInfo); |
| 3797 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3798 | } |
| 3799 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 3800 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 3801 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3802 | if( pAndExpr ){ |
| 3803 | pAndExpr->pLeft = 0; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3804 | sqlite3ExprDelete(db, pAndExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3805 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3806 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3807 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 3808 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 3809 | |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3810 | if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3811 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3812 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3813 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3814 | |
| 3815 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3816 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3817 | ** scan of the entire table. |
| 3818 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3819 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 3820 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 3821 | assert( bRev==0 || bRev==1 ); |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3822 | pLevel->op = aStep[bRev]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3823 | pLevel->p1 = iCur; |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3824 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3825 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3826 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3827 | |
| 3828 | /* Insert code to test every subexpression that can be completely |
| 3829 | ** computed using the current set of tables. |
| 3830 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3831 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3832 | Expr *pE; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3833 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3834 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3835 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3836 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3837 | testcase( pWInfo->untestedTerms==0 |
| 3838 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 3839 | pWInfo->untestedTerms = 1; |
| 3840 | continue; |
| 3841 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3842 | pE = pTerm->pExpr; |
| 3843 | assert( pE!=0 ); |
| 3844 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 3845 | continue; |
| 3846 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3847 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3848 | pTerm->wtFlags |= TERM_CODED; |
| 3849 | } |
| 3850 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3851 | /* Insert code to test for implied constraints based on transitivity |
| 3852 | ** of the "==" operator. |
| 3853 | ** |
| 3854 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 3855 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 3856 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 3857 | ** the implied "t1.a=123" constraint. |
| 3858 | */ |
| 3859 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3860 | Expr *pE, *pEAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3861 | WhereTerm *pAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3862 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 3863 | if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue; |
| 3864 | if( pTerm->leftCursor!=iCur ) continue; |
drh | cdc2e43 | 2013-07-01 17:27:19 +0000 | [diff] [blame] | 3865 | if( pLevel->iLeftJoin ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3866 | pE = pTerm->pExpr; |
| 3867 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3868 | assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3869 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 3870 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 3871 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3872 | testcase( pAlt->eOperator & WO_EQ ); |
| 3873 | testcase( pAlt->eOperator & WO_IN ); |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3874 | VdbeNoopComment((v, "begin transitive constraint")); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3875 | pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 3876 | if( pEAlt ){ |
| 3877 | *pEAlt = *pAlt->pExpr; |
| 3878 | pEAlt->pLeft = pE->pLeft; |
| 3879 | sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| 3880 | sqlite3StackFree(db, pEAlt); |
| 3881 | } |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3884 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 3885 | ** at least one row of the right table has matched the left table. |
| 3886 | */ |
| 3887 | if( pLevel->iLeftJoin ){ |
| 3888 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 3889 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 3890 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3891 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3892 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3893 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3894 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3895 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3896 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 3897 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3898 | continue; |
| 3899 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3900 | assert( pTerm->pExpr ); |
| 3901 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 3902 | pTerm->wtFlags |= TERM_CODED; |
| 3903 | } |
| 3904 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3905 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3906 | |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3907 | return pLevel->notReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 3910 | #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 3911 | /* |
| 3912 | ** Generate "Explanation" text for a WhereTerm. |
| 3913 | */ |
| 3914 | static void whereExplainTerm(Vdbe *v, WhereTerm *pTerm){ |
| 3915 | char zType[4]; |
| 3916 | memcpy(zType, "...", 4); |
| 3917 | if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 3918 | if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 3919 | if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 3920 | sqlite3ExplainPrintf(v, "%s ", zType); |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 3921 | sqlite3ExplainExpr(v, pTerm->pExpr); |
| 3922 | } |
| 3923 | #endif /* WHERETRACE_ENABLED && SQLITE_ENABLE_TREE_EXPLAIN */ |
| 3924 | |
| 3925 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3926 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3927 | /* |
| 3928 | ** Print a WhereLoop object for debugging purposes |
| 3929 | */ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3930 | static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ |
| 3931 | WhereInfo *pWInfo = pWC->pWInfo; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3932 | int nb = 1+(pWInfo->pTabList->nSrc+7)/8; |
| 3933 | struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3934 | Table *pTab = pItem->pTab; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3935 | sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 3936 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3937 | sqlite3DebugPrintf(" %12s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3938 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3939 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3940 | const char *zName; |
| 3941 | if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3942 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 3943 | int i = sqlite3Strlen30(zName) - 1; |
| 3944 | while( zName[i]!='_' ) i--; |
| 3945 | zName += i; |
| 3946 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3947 | sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3948 | }else{ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3949 | sqlite3DebugPrintf("%20s",""); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3950 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3951 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3952 | char *z; |
| 3953 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3954 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 3955 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3956 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3957 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3958 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3959 | sqlite3DebugPrintf(" %-19s", z); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3960 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3961 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3962 | sqlite3DebugPrintf(" f %04x N %d", p->wsFlags, p->nLTerm); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3963 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3964 | #ifdef SQLITE_ENABLE_TREE_EXPLAIN |
| 3965 | /* If the 0x100 bit of wheretracing is set, then show all of the constraint |
| 3966 | ** expressions in the WhereLoop.aLTerm[] array. |
| 3967 | */ |
| 3968 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ /* WHERETRACE 0x100 */ |
| 3969 | int i; |
| 3970 | Vdbe *v = pWInfo->pParse->pVdbe; |
| 3971 | sqlite3ExplainBegin(v); |
| 3972 | for(i=0; i<p->nLTerm; i++){ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3973 | WhereTerm *pTerm = p->aLTerm[i]; |
drh | 7afc8b0 | 2013-10-28 22:33:36 +0000 | [diff] [blame] | 3974 | sqlite3ExplainPrintf(v, " (%d) #%-2d ", i+1, (int)(pTerm-pWC->a)); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3975 | sqlite3ExplainPush(v); |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 3976 | whereExplainTerm(v, pTerm); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3977 | sqlite3ExplainPop(v); |
| 3978 | sqlite3ExplainNL(v); |
| 3979 | } |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3980 | sqlite3ExplainFinish(v); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 3981 | sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v)); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 3982 | } |
| 3983 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3984 | } |
| 3985 | #endif |
| 3986 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3987 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3988 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 3989 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3990 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3991 | static void whereLoopInit(WhereLoop *p){ |
| 3992 | p->aLTerm = p->aLTermSpace; |
| 3993 | p->nLTerm = 0; |
| 3994 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 3995 | p->wsFlags = 0; |
| 3996 | } |
| 3997 | |
| 3998 | /* |
| 3999 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 4000 | */ |
| 4001 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4002 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4003 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 4004 | sqlite3_free(p->u.vtab.idxStr); |
| 4005 | p->u.vtab.needFree = 0; |
| 4006 | p->u.vtab.idxStr = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4007 | }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4008 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 4009 | sqlite3KeyInfoUnref(p->u.btree.pIndex->pKeyInfo); |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4010 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 4011 | p->u.btree.pIndex = 0; |
| 4012 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4013 | } |
| 4014 | } |
| 4015 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4016 | /* |
| 4017 | ** Deallocate internal memory used by a WhereLoop object |
| 4018 | */ |
| 4019 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 4020 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 4021 | whereLoopClearUnion(db, p); |
| 4022 | whereLoopInit(p); |
| 4023 | } |
| 4024 | |
| 4025 | /* |
| 4026 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 4027 | */ |
| 4028 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 4029 | WhereTerm **paNew; |
| 4030 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 4031 | n = (n+7)&~7; |
| 4032 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 4033 | if( paNew==0 ) return SQLITE_NOMEM; |
| 4034 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 4035 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 4036 | p->aLTerm = paNew; |
| 4037 | p->nLSlot = n; |
| 4038 | return SQLITE_OK; |
| 4039 | } |
| 4040 | |
| 4041 | /* |
| 4042 | ** Transfer content from the second pLoop into the first. |
| 4043 | */ |
| 4044 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4045 | whereLoopClearUnion(db, pTo); |
drh | 0d31dc3 | 2013-09-06 00:40:59 +0000 | [diff] [blame] | 4046 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ |
| 4047 | memset(&pTo->u, 0, sizeof(pTo->u)); |
| 4048 | return SQLITE_NOMEM; |
| 4049 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4050 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 4051 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4052 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 4053 | pFrom->u.vtab.needFree = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4054 | }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4055 | pFrom->u.btree.pIndex = 0; |
| 4056 | } |
| 4057 | return SQLITE_OK; |
| 4058 | } |
| 4059 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4060 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4061 | ** Delete a WhereLoop object |
| 4062 | */ |
| 4063 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4064 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4065 | sqlite3DbFree(db, p); |
| 4066 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 4067 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4068 | /* |
| 4069 | ** Free a WhereInfo structure |
| 4070 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 4071 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 4072 | if( ALWAYS(pWInfo) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4073 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4074 | while( pWInfo->pLoops ){ |
| 4075 | WhereLoop *p = pWInfo->pLoops; |
| 4076 | pWInfo->pLoops = p->pNextLoop; |
| 4077 | whereLoopDelete(db, p); |
| 4078 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4079 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4083 | /* |
| 4084 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 4085 | ** |
| 4086 | ** An existing WhereLoop entry might be overwritten if the new template |
| 4087 | ** is better and has fewer dependencies. Or the template will be ignored |
| 4088 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 4089 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4090 | ** added based on the template. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4091 | ** |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4092 | ** If pBuilder->pOrSet is not NULL then we only care about only the |
| 4093 | ** prerequisites and rRun and nOut costs of the N best loops. That |
| 4094 | ** information is gathered in the pBuilder->pOrSet object. This special |
| 4095 | ** processing mode is used only for OR clause processing. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4096 | ** |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4097 | ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4098 | ** still might overwrite similar loops with the new template if the |
| 4099 | ** template is better. Loops may be overwritten if the following |
| 4100 | ** conditions are met: |
| 4101 | ** |
| 4102 | ** (1) They have the same iTab. |
| 4103 | ** (2) They have the same iSortIdx. |
| 4104 | ** (3) The template has same or fewer dependencies than the current loop |
| 4105 | ** (4) The template has the same or lower cost than the current loop |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4106 | ** (5) The template uses more terms of the same index but has no additional |
| 4107 | ** dependencies |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4108 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4109 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4110 | WhereLoop **ppPrev, *p, *pNext = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4111 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4112 | sqlite3 *db = pWInfo->pParse->db; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4113 | |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4114 | /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 4115 | ** and prereqs. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4116 | */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4117 | if( pBuilder->pOrSet!=0 ){ |
| 4118 | #if WHERETRACE_ENABLED |
| 4119 | u16 n = pBuilder->pOrSet->n; |
| 4120 | int x = |
| 4121 | #endif |
| 4122 | whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, |
| 4123 | pTemplate->nOut); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4124 | #if WHERETRACE_ENABLED /* 0x8 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4125 | if( sqlite3WhereTrace & 0x8 ){ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4126 | sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4127 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4128 | } |
| 4129 | #endif |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4130 | return SQLITE_OK; |
| 4131 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4132 | |
| 4133 | /* Search for an existing WhereLoop to overwrite, or which takes |
| 4134 | ** priority over pTemplate. |
| 4135 | */ |
| 4136 | for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4137 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 4138 | /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 4139 | ** then those WhereLoops need to be considered separately. Neither is |
| 4140 | ** a candidate to replace the other. */ |
| 4141 | continue; |
| 4142 | } |
| 4143 | /* In the current implementation, the rSetup value is either zero |
| 4144 | ** or the cost of building an automatic index (NlogN) and the NlogN |
| 4145 | ** is the same for compatible WhereLoops. */ |
| 4146 | assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 4147 | || p->rSetup==pTemplate->rSetup ); |
| 4148 | |
| 4149 | /* whereLoopAddBtree() always generates and inserts the automatic index |
| 4150 | ** case first. Hence compatible candidate WhereLoops never have a larger |
| 4151 | ** rSetup. Call this SETUP-INVARIANT */ |
| 4152 | assert( p->rSetup>=pTemplate->rSetup ); |
| 4153 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4154 | if( (p->prereq & pTemplate->prereq)==p->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4155 | && p->rSetup<=pTemplate->rSetup |
| 4156 | && p->rRun<=pTemplate->rRun |
drh | f46af73 | 2013-08-30 17:35:44 +0000 | [diff] [blame] | 4157 | && p->nOut<=pTemplate->nOut |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4158 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4159 | /* This branch taken when p is equal or better than pTemplate in |
drh | e56dd3a | 2013-08-30 17:50:35 +0000 | [diff] [blame] | 4160 | ** all of (1) dependencies (2) setup-cost, (3) run-cost, and |
drh | f46af73 | 2013-08-30 17:35:44 +0000 | [diff] [blame] | 4161 | ** (4) number of output rows. */ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4162 | assert( p->rSetup==pTemplate->rSetup ); |
drh | 05db3c7 | 2013-09-02 20:22:18 +0000 | [diff] [blame] | 4163 | if( p->prereq==pTemplate->prereq |
| 4164 | && p->nLTerm<pTemplate->nLTerm |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 4165 | && (p->wsFlags & pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 4166 | && (p->u.btree.pIndex==pTemplate->u.btree.pIndex |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 4167 | || pTemplate->rRun+p->nLTerm<=p->rRun+pTemplate->nLTerm) |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4168 | ){ |
| 4169 | /* Overwrite an existing WhereLoop with an similar one that uses |
| 4170 | ** more terms of the index */ |
| 4171 | pNext = p->pNextLoop; |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4172 | break; |
| 4173 | }else{ |
| 4174 | /* pTemplate is not helpful. |
| 4175 | ** Return without changing or adding anything */ |
| 4176 | goto whereLoopInsert_noop; |
| 4177 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4178 | } |
| 4179 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4180 | && p->rRun>=pTemplate->rRun |
drh | f46af73 | 2013-08-30 17:35:44 +0000 | [diff] [blame] | 4181 | && p->nOut>=pTemplate->nOut |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4182 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4183 | /* Overwrite an existing WhereLoop with a better one: one that is |
drh | e56dd3a | 2013-08-30 17:50:35 +0000 | [diff] [blame] | 4184 | ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost |
drh | f46af73 | 2013-08-30 17:35:44 +0000 | [diff] [blame] | 4185 | ** or (4) number of output rows, and is no worse in any of those |
| 4186 | ** categories. */ |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 4187 | assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4188 | pNext = p->pNextLoop; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4189 | break; |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | /* If we reach this point it means that either p[] should be overwritten |
| 4194 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 4195 | ** WhereLoop and insert it. |
| 4196 | */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4197 | #if WHERETRACE_ENABLED /* 0x8 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4198 | if( sqlite3WhereTrace & 0x8 ){ |
| 4199 | if( p!=0 ){ |
| 4200 | sqlite3DebugPrintf("ins-del: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4201 | whereLoopPrint(p, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4202 | } |
| 4203 | sqlite3DebugPrintf("ins-new: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4204 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4205 | } |
| 4206 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4207 | if( p==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4208 | p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4209 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4210 | whereLoopInit(p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4211 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4212 | whereLoopXfer(db, p, pTemplate); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4213 | p->pNextLoop = pNext; |
| 4214 | *ppPrev = p; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4215 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4216 | Index *pIndex = p->u.btree.pIndex; |
| 4217 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4218 | p->u.btree.pIndex = 0; |
| 4219 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4220 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4221 | return SQLITE_OK; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4222 | |
| 4223 | /* Jump here if the insert is a no-op */ |
| 4224 | whereLoopInsert_noop: |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4225 | #if WHERETRACE_ENABLED /* 0x8 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4226 | if( sqlite3WhereTrace & 0x8 ){ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4227 | sqlite3DebugPrintf("ins-noop: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4228 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4229 | } |
| 4230 | #endif |
| 4231 | return SQLITE_OK; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | /* |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4235 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 4236 | ** WHERE clause that reference the loop but which are not used by an |
| 4237 | ** index. |
| 4238 | ** |
| 4239 | ** In the current implementation, the first extra WHERE clause term reduces |
| 4240 | ** the number of output rows by a factor of 10 and each additional term |
| 4241 | ** reduces the number of output rows by sqrt(2). |
| 4242 | */ |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 4243 | static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4244 | WhereTerm *pTerm, *pX; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4245 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4246 | int i, j; |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 4247 | |
| 4248 | if( !OptimizationEnabled(pWC->pWInfo->pParse->db, SQLITE_AdjustOutEst) ){ |
| 4249 | return; |
| 4250 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4251 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4252 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4253 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 4254 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4255 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| 4256 | pX = pLoop->aLTerm[j]; |
| 4257 | if( pX==pTerm ) break; |
| 4258 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 4259 | } |
| 4260 | if( j<0 ) pLoop->nOut += pTerm->truthProb; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4261 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | /* |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4265 | ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex. |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4266 | ** Try to match one more. |
| 4267 | ** |
| 4268 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 4269 | ** INTEGER PRIMARY KEY. |
| 4270 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4271 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4272 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 4273 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 4274 | Index *pProbe, /* An index on pSrc */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4275 | LogEst nInMul /* log(Number of iterations due to IN) */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4276 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4277 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 4278 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 4279 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4280 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 4281 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4282 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4283 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4284 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 4285 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 4286 | int saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 4287 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4288 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4289 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4290 | int rc = SQLITE_OK; /* Return code */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4291 | LogEst nRowEst; /* Estimated index selectivity */ |
| 4292 | LogEst rLogSize; /* Logarithm of table size */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4293 | WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4294 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4295 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4296 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4297 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4298 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4299 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 4300 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 4301 | opMask = WO_LT|WO_LE; |
| 4302 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 4303 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4304 | }else{ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4305 | opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4306 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4307 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4308 | |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4309 | assert( pNew->u.btree.nEq<=pProbe->nKeyCol ); |
| 4310 | if( pNew->u.btree.nEq < pProbe->nKeyCol ){ |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4311 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4312 | nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]); |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4313 | if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4314 | }else{ |
| 4315 | iCol = -1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4316 | nRowEst = 0; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4317 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4318 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4319 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4320 | saved_nEq = pNew->u.btree.nEq; |
| 4321 | saved_nLTerm = pNew->nLTerm; |
| 4322 | saved_wsFlags = pNew->wsFlags; |
| 4323 | saved_prereq = pNew->prereq; |
| 4324 | saved_nOut = pNew->nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4325 | pNew->rSetup = 0; |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4326 | rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0])); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4327 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4328 | int nIn = 0; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4329 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4330 | int nRecValid = pBuilder->nRecValid; |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 4331 | #endif |
dan | 8bff07a | 2013-08-29 14:56:14 +0000 | [diff] [blame] | 4332 | if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) |
| 4333 | && (iCol<0 || pSrc->pTab->aCol[iCol].notNull) |
| 4334 | ){ |
| 4335 | continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 4336 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4337 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 4338 | |
dan | ad45ed7 | 2013-08-08 12:21:32 +0000 | [diff] [blame] | 4339 | assert( pNew->nOut==saved_nOut ); |
| 4340 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4341 | pNew->wsFlags = saved_wsFlags; |
| 4342 | pNew->u.btree.nEq = saved_nEq; |
| 4343 | pNew->nLTerm = saved_nLTerm; |
| 4344 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4345 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 4346 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4347 | pNew->rRun = rLogSize; /* Baseline cost is log2(N). Adjustments below */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4348 | if( pTerm->eOperator & WO_IN ){ |
| 4349 | Expr *pExpr = pTerm->pExpr; |
| 4350 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4351 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4352 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4353 | nIn = 46; assert( 46==sqlite3LogEst(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4354 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4355 | /* "x IN (value, value, ...)" */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4356 | nIn = sqlite3LogEst(pExpr->x.pList->nExpr); |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4357 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4358 | pNew->rRun += nIn; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4359 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4360 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4361 | }else if( pTerm->eOperator & (WO_EQ) ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4362 | assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4363 | || nInMul==0 ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4364 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4365 | if( iCol<0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4366 | || (pProbe->onError!=OE_None && nInMul==0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4367 | && pNew->u.btree.nEq==pProbe->nKeyCol-1) |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4368 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4369 | assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4370 | pNew->wsFlags |= WHERE_ONEROW; |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4371 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4372 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4373 | pNew->nOut = nRowEst + nInMul; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4374 | }else if( pTerm->eOperator & (WO_ISNULL) ){ |
| 4375 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 4376 | pNew->u.btree.nEq++; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4377 | /* TUNING: IS NULL selects 2 rows */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4378 | nIn = 10; assert( 10==sqlite3LogEst(2) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4379 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4380 | }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4381 | testcase( pTerm->eOperator & WO_GT ); |
| 4382 | testcase( pTerm->eOperator & WO_GE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4383 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4384 | pBtm = pTerm; |
| 4385 | pTop = 0; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4386 | }else{ |
| 4387 | assert( pTerm->eOperator & (WO_LT|WO_LE) ); |
| 4388 | testcase( pTerm->eOperator & WO_LT ); |
| 4389 | testcase( pTerm->eOperator & WO_LE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4390 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4391 | pTop = pTerm; |
| 4392 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4393 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4394 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4395 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4396 | /* Adjust nOut and rRun for STAT3 range values */ |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 4397 | assert( pNew->nOut==saved_nOut ); |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 4398 | whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4399 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4400 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 4401 | if( nInMul==0 |
| 4402 | && pProbe->nSample |
| 4403 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 4404 | && OptimizationEnabled(db, SQLITE_Stat3) |
| 4405 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4406 | Expr *pExpr = pTerm->pExpr; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4407 | tRowcnt nOut = 0; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4408 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 4409 | testcase( pTerm->eOperator & WO_EQ ); |
| 4410 | testcase( pTerm->eOperator & WO_ISNULL ); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4411 | rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4412 | }else if( (pTerm->eOperator & WO_IN) |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4413 | && !ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 4414 | rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4415 | } |
drh | 8284633 | 2013-08-01 17:21:26 +0000 | [diff] [blame] | 4416 | assert( nOut==0 || rc==SQLITE_OK ); |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 4417 | if( nOut ){ |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 4418 | pNew->nOut = sqlite3LogEst(nOut); |
| 4419 | if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut; |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 4420 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4421 | } |
| 4422 | #endif |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4423 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4424 | /* Each row involves a step of the index, then a binary search of |
| 4425 | ** the main table */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4426 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4427 | } |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4428 | /* Step cost for each output row */ |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4429 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut); |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 4430 | whereLoopOutputAdjust(pBuilder->pWC, pNew); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4431 | rc = whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4432 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4433 | && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0)) |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4434 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4435 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4436 | } |
dan | ad45ed7 | 2013-08-08 12:21:32 +0000 | [diff] [blame] | 4437 | pNew->nOut = saved_nOut; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4438 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4439 | pBuilder->nRecValid = nRecValid; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4440 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4441 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4442 | pNew->prereq = saved_prereq; |
| 4443 | pNew->u.btree.nEq = saved_nEq; |
| 4444 | pNew->wsFlags = saved_wsFlags; |
| 4445 | pNew->nOut = saved_nOut; |
| 4446 | pNew->nLTerm = saved_nLTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4447 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4451 | ** Return True if it is possible that pIndex might be useful in |
| 4452 | ** implementing the ORDER BY clause in pBuilder. |
| 4453 | ** |
| 4454 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 4455 | ** if there is no way for pIndex to be useful in implementing that |
| 4456 | ** ORDER BY clause. |
| 4457 | */ |
| 4458 | static int indexMightHelpWithOrderBy( |
| 4459 | WhereLoopBuilder *pBuilder, |
| 4460 | Index *pIndex, |
| 4461 | int iCursor |
| 4462 | ){ |
| 4463 | ExprList *pOB; |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4464 | int ii, jj; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4465 | |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4466 | if( pIndex->bUnordered ) return 0; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4467 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4468 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 4469 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4470 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 4471 | if( pExpr->iTable==iCursor ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4472 | for(jj=0; jj<pIndex->nKeyCol; jj++){ |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4473 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 4474 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4475 | } |
| 4476 | } |
| 4477 | return 0; |
| 4478 | } |
| 4479 | |
| 4480 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4481 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 4482 | ** the table is used by an index. Only the first 63 columns are considered. |
| 4483 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4484 | static Bitmask columnsInIndex(Index *pIdx){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4485 | Bitmask m = 0; |
| 4486 | int j; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4487 | for(j=pIdx->nColumn-1; j>=0; j--){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4488 | int x = pIdx->aiColumn[j]; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4489 | if( x>=0 ){ |
| 4490 | testcase( x==BMS-1 ); |
| 4491 | testcase( x==BMS-2 ); |
| 4492 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 4493 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4494 | } |
| 4495 | return m; |
| 4496 | } |
| 4497 | |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4498 | /* Check to see if a partial index with pPartIndexWhere can be used |
| 4499 | ** in the current query. Return true if it can be and false if not. |
| 4500 | */ |
| 4501 | static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ |
| 4502 | int i; |
| 4503 | WhereTerm *pTerm; |
| 4504 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 4505 | if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1; |
| 4506 | } |
| 4507 | return 0; |
| 4508 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4509 | |
| 4510 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 4511 | ** Add all WhereLoop objects for a single table of the join where the table |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4512 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 4513 | ** a b-tree table, not a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4514 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4515 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4516 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4517 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4518 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4519 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4520 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4521 | Index sPk; /* A fake index object for the primary key */ |
| 4522 | tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4523 | i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4524 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4525 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4526 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4527 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4528 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4529 | int b; /* A boolean value */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4530 | LogEst rSize; /* number of rows in the table */ |
| 4531 | LogEst rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4532 | WhereClause *pWC; /* The parsed WHERE clause */ |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4533 | Table *pTab; /* Table being queried */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4534 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4535 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4536 | pWInfo = pBuilder->pWInfo; |
| 4537 | pTabList = pWInfo->pTabList; |
| 4538 | pSrc = pTabList->a + pNew->iTab; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4539 | pTab = pSrc->pTab; |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4540 | pWC = pBuilder->pWC; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4541 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4542 | |
| 4543 | if( pSrc->pIndex ){ |
| 4544 | /* An INDEXED BY clause specifies a particular index to use */ |
| 4545 | pProbe = pSrc->pIndex; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4546 | }else if( !HasRowid(pTab) ){ |
| 4547 | pProbe = pTab->pIndex; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4548 | }else{ |
| 4549 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 4550 | ** variable sPk to represent the rowid primary key index. Make this |
| 4551 | ** fake index the first in a chain of Index objects with all of the real |
| 4552 | ** indices to follow */ |
| 4553 | Index *pFirst; /* First of real indices on the table */ |
| 4554 | memset(&sPk, 0, sizeof(Index)); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4555 | sPk.nKeyCol = 1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4556 | sPk.aiColumn = &aiColumnPk; |
| 4557 | sPk.aiRowEst = aiRowEstPk; |
| 4558 | sPk.onError = OE_Replace; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4559 | sPk.pTable = pTab; |
| 4560 | aiRowEstPk[0] = pTab->nRowEst; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4561 | aiRowEstPk[1] = 1; |
| 4562 | pFirst = pSrc->pTab->pIndex; |
| 4563 | if( pSrc->notIndexed==0 ){ |
| 4564 | /* The real indices of the table are only considered if the |
| 4565 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 4566 | sPk.pNext = pFirst; |
| 4567 | } |
| 4568 | pProbe = &sPk; |
| 4569 | } |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 4570 | rSize = sqlite3LogEst(pTab->nRowEst); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4571 | rLogSize = estLog(rSize); |
| 4572 | |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 4573 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4574 | /* Automatic indexes */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4575 | if( !pBuilder->pOrSet |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 4576 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 4577 | && pSrc->pIndex==0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4578 | && !pSrc->viaCoroutine |
| 4579 | && !pSrc->notIndexed |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4580 | && HasRowid(pTab) |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4581 | && !pSrc->isCorrelated |
| 4582 | ){ |
| 4583 | /* Generate auto-index WhereLoops */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4584 | WhereTerm *pTerm; |
| 4585 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 4586 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4587 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4588 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 4589 | pNew->u.btree.nEq = 1; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4590 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4591 | pNew->nLTerm = 1; |
| 4592 | pNew->aLTerm[0] = pTerm; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4593 | /* TUNING: One-time cost for computing the automatic index is |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4594 | ** approximately 7*N*log2(N) where N is the number of rows in |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4595 | ** the table being indexed. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4596 | pNew->rSetup = rLogSize + rSize + 28; assert( 28==sqlite3LogEst(7) ); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4597 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 4598 | ** is more than the usual guess of 10 rows, since we have no way |
| 4599 | ** of knowning how selective the index will ultimately be. It would |
| 4600 | ** not be unreasonable to make this value much larger. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4601 | pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4602 | pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4603 | pNew->wsFlags = WHERE_AUTO_INDEX; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4604 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4605 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4606 | } |
| 4607 | } |
| 4608 | } |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 4609 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4610 | |
| 4611 | /* Loop over all indices |
| 4612 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4613 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4614 | if( pProbe->pPartIdxWhere!=0 |
| 4615 | && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){ |
| 4616 | continue; /* Partial index inappropriate for this query */ |
| 4617 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4618 | pNew->u.btree.nEq = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4619 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4620 | pNew->iSortIdx = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4621 | pNew->rSetup = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4622 | pNew->prereq = mExtra; |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4623 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4624 | pNew->u.btree.pIndex = pProbe; |
| 4625 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4626 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 4627 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4628 | if( pProbe->tnum<=0 ){ |
| 4629 | /* Integer primary key index */ |
| 4630 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4631 | |
| 4632 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4633 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4634 | /* TUNING: Cost of full table scan is 3*(N + log2(N)). |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4635 | ** + The extra 3 factor is to encourage the use of indexed lookups |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 4636 | ** over full scans. FIXME */ |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4637 | pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16; |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 4638 | whereLoopOutputAdjust(pWC, pNew); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4639 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4640 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4641 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4642 | }else{ |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 4643 | Bitmask m; |
| 4644 | if( pProbe->isCovering ){ |
| 4645 | pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 4646 | m = 0; |
| 4647 | }else{ |
| 4648 | m = pSrc->colUsed & ~columnsInIndex(pProbe); |
| 4649 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 4650 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4651 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4652 | /* Full scan via index */ |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4653 | if( b |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 4654 | || !HasRowid(pTab) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4655 | || ( m==0 |
| 4656 | && pProbe->bUnordered==0 |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 4657 | && (pProbe->szIdxRow<pTab->szTabRow) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4658 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 4659 | && sqlite3GlobalConfig.bUseCis |
| 4660 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 4661 | ) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4662 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4663 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4664 | if( m==0 ){ |
drh | d9e3cad | 2013-10-04 02:36:19 +0000 | [diff] [blame] | 4665 | /* TUNING: Cost of a covering index scan is K*(N + log2(N)). |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4666 | ** + The extra factor K of between 1.1 and 3.0 that depends |
| 4667 | ** on the relative sizes of the table and the index. K |
| 4668 | ** is smaller for smaller indices, thus favoring them. |
drh | d9e3cad | 2013-10-04 02:36:19 +0000 | [diff] [blame] | 4669 | */ |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4670 | pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 1 + |
| 4671 | (15*pProbe->szIdxRow)/pTab->szTabRow; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4672 | }else{ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4673 | /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N) |
| 4674 | ** which we will simplify to just N*log2(N) */ |
| 4675 | pNew->rRun = rSize + rLogSize; |
| 4676 | } |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 4677 | whereLoopOutputAdjust(pWC, pNew); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4678 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4679 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4680 | if( rc ) break; |
| 4681 | } |
| 4682 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4683 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4684 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4685 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 4686 | sqlite3Stat4ProbeFree(pBuilder->pRec); |
| 4687 | pBuilder->nRecValid = 0; |
| 4688 | pBuilder->pRec = 0; |
dan | ddc2d6e | 2013-08-06 20:15:06 +0000 | [diff] [blame] | 4689 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4690 | |
| 4691 | /* If there was an INDEXED BY clause, then only that one index is |
| 4692 | ** considered. */ |
| 4693 | if( pSrc->pIndex ) break; |
| 4694 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4695 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4698 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4699 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4700 | ** Add all WhereLoop objects for a table of the join identified by |
| 4701 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4702 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4703 | static int whereLoopAddVirtual( |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4704 | WhereLoopBuilder *pBuilder /* WHERE clause information */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4705 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4706 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4707 | Parse *pParse; /* The parsing context */ |
| 4708 | WhereClause *pWC; /* The WHERE clause */ |
| 4709 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 4710 | Table *pTab; |
| 4711 | sqlite3 *db; |
| 4712 | sqlite3_index_info *pIdxInfo; |
| 4713 | struct sqlite3_index_constraint *pIdxCons; |
| 4714 | struct sqlite3_index_constraint_usage *pUsage; |
| 4715 | WhereTerm *pTerm; |
| 4716 | int i, j; |
| 4717 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4718 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4719 | int seenIn = 0; /* True if an IN operator is seen */ |
| 4720 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 4721 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 4722 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4723 | int rc = SQLITE_OK; |
| 4724 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4725 | pWInfo = pBuilder->pWInfo; |
| 4726 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4727 | db = pParse->db; |
| 4728 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4729 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4730 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4731 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4732 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4733 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4734 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4735 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4736 | pNew->rSetup = 0; |
| 4737 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4738 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4739 | pNew->u.vtab.needFree = 0; |
| 4740 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4741 | nConstraint = pIdxInfo->nConstraint; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4742 | if( whereLoopResize(db, pNew, nConstraint) ){ |
| 4743 | sqlite3DbFree(db, pIdxInfo); |
| 4744 | return SQLITE_NOMEM; |
| 4745 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4746 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4747 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4748 | if( !seenIn && (iPhase&1)!=0 ){ |
| 4749 | iPhase++; |
| 4750 | if( iPhase>3 ) break; |
| 4751 | } |
| 4752 | if( !seenVar && iPhase>1 ) break; |
| 4753 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4754 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4755 | j = pIdxCons->iTermOffset; |
| 4756 | pTerm = &pWC->a[j]; |
| 4757 | switch( iPhase ){ |
| 4758 | case 0: /* Constants without IN operator */ |
| 4759 | pIdxCons->usable = 0; |
| 4760 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4761 | seenIn = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4762 | } |
| 4763 | if( pTerm->prereqRight!=0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4764 | seenVar = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4765 | }else if( (pTerm->eOperator & WO_IN)==0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4766 | pIdxCons->usable = 1; |
| 4767 | } |
| 4768 | break; |
| 4769 | case 1: /* Constants with IN operators */ |
| 4770 | assert( seenIn ); |
| 4771 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 4772 | break; |
| 4773 | case 2: /* Variables without IN */ |
| 4774 | assert( seenVar ); |
| 4775 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 4776 | break; |
| 4777 | default: /* Variables with IN */ |
| 4778 | assert( seenVar && seenIn ); |
| 4779 | pIdxCons->usable = 1; |
| 4780 | break; |
| 4781 | } |
| 4782 | } |
| 4783 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 4784 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4785 | pIdxInfo->idxStr = 0; |
| 4786 | pIdxInfo->idxNum = 0; |
| 4787 | pIdxInfo->needToFreeIdxStr = 0; |
| 4788 | pIdxInfo->orderByConsumed = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4789 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4790 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 4791 | if( rc ) goto whereLoopAddVtab_exit; |
| 4792 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4793 | pNew->prereq = 0; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4794 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4795 | assert( pNew->nLSlot>=nConstraint ); |
| 4796 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4797 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4798 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4799 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 4800 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4801 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4802 | || j<0 |
| 4803 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4804 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4805 | ){ |
| 4806 | rc = SQLITE_ERROR; |
| 4807 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4808 | goto whereLoopAddVtab_exit; |
| 4809 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4810 | testcase( iTerm==nConstraint-1 ); |
| 4811 | testcase( j==0 ); |
| 4812 | testcase( j==pWC->nTerm-1 ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4813 | pTerm = &pWC->a[j]; |
| 4814 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4815 | assert( iTerm<pNew->nLSlot ); |
| 4816 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4817 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4818 | testcase( iTerm==15 ); |
| 4819 | testcase( iTerm==16 ); |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4820 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4821 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4822 | if( pUsage[i].omit==0 ){ |
| 4823 | /* Do not attempt to use an IN constraint if the virtual table |
| 4824 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4825 | ** If we do attempt to use such a constraint, some rows might be |
| 4826 | ** repeated in the output. */ |
| 4827 | break; |
| 4828 | } |
| 4829 | /* A virtual table that is constrained by an IN clause may not |
| 4830 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4831 | ** is not necessarily related to the order of output terms and |
| 4832 | ** (2) Multiple outputs from a single IN value will not merge |
| 4833 | ** together. */ |
| 4834 | pIdxInfo->orderByConsumed = 0; |
| 4835 | } |
| 4836 | } |
| 4837 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4838 | if( i>=nConstraint ){ |
| 4839 | pNew->nLTerm = mxTerm+1; |
| 4840 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4841 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4842 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 4843 | pIdxInfo->needToFreeIdxStr = 0; |
| 4844 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 3b1d808 | 2013-06-03 16:56:37 +0000 | [diff] [blame] | 4845 | pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) |
| 4846 | && pIdxInfo->orderByConsumed); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4847 | pNew->rSetup = 0; |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 4848 | pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4849 | /* TUNING: Every virtual table query returns 25 rows */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4850 | pNew->nOut = 46; assert( 46==sqlite3LogEst(25) ); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4851 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4852 | if( pNew->u.vtab.needFree ){ |
| 4853 | sqlite3_free(pNew->u.vtab.idxStr); |
| 4854 | pNew->u.vtab.needFree = 0; |
| 4855 | } |
| 4856 | } |
| 4857 | } |
| 4858 | |
| 4859 | whereLoopAddVtab_exit: |
| 4860 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4861 | sqlite3DbFree(db, pIdxInfo); |
| 4862 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4863 | } |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4864 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4865 | |
| 4866 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4867 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 4868 | ** btrees or virtual tables. |
| 4869 | */ |
| 4870 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4871 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4872 | WhereClause *pWC; |
| 4873 | WhereLoop *pNew; |
| 4874 | WhereTerm *pTerm, *pWCEnd; |
| 4875 | int rc = SQLITE_OK; |
| 4876 | int iCur; |
| 4877 | WhereClause tempWC; |
| 4878 | WhereLoopBuilder sSubBuild; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4879 | WhereOrSet sSum, sCur, sPrev; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4880 | struct SrcList_item *pItem; |
| 4881 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4882 | pWC = pBuilder->pWC; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4883 | if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4884 | pWCEnd = pWC->a + pWC->nTerm; |
| 4885 | pNew = pBuilder->pNew; |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 4886 | memset(&sSum, 0, sizeof(sSum)); |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 4887 | pItem = pWInfo->pTabList->a + pNew->iTab; |
drh | d4ddae9 | 2013-11-06 12:05:57 +0000 | [diff] [blame] | 4888 | if( !HasRowid(pItem->pTab) ) return SQLITE_OK; |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 4889 | iCur = pItem->iCursor; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4890 | |
| 4891 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 4892 | if( (pTerm->eOperator & WO_OR)!=0 |
| 4893 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 4894 | ){ |
| 4895 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 4896 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 4897 | WhereTerm *pOrTerm; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4898 | int once = 1; |
| 4899 | int i, j; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4900 | |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4901 | sSubBuild = *pBuilder; |
| 4902 | sSubBuild.pOrderBy = 0; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4903 | sSubBuild.pOrSet = &sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4904 | |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4905 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4906 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4907 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 4908 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4909 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4910 | tempWC.pOuter = pWC; |
| 4911 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4912 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4913 | tempWC.a = pOrTerm; |
| 4914 | sSubBuild.pWC = &tempWC; |
| 4915 | }else{ |
| 4916 | continue; |
| 4917 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4918 | sCur.n = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4919 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4920 | if( IsVirtual(pItem->pTab) ){ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4921 | rc = whereLoopAddVirtual(&sSubBuild); |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4922 | for(i=0; i<sCur.n; i++) sCur.a[i].prereq |= mExtra; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4923 | }else |
| 4924 | #endif |
| 4925 | { |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4926 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 4927 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4928 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 4929 | if( sCur.n==0 ){ |
| 4930 | sSum.n = 0; |
| 4931 | break; |
| 4932 | }else if( once ){ |
| 4933 | whereOrMove(&sSum, &sCur); |
| 4934 | once = 0; |
| 4935 | }else{ |
| 4936 | whereOrMove(&sPrev, &sSum); |
| 4937 | sSum.n = 0; |
| 4938 | for(i=0; i<sPrev.n; i++){ |
| 4939 | for(j=0; j<sCur.n; j++){ |
| 4940 | whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4941 | sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun), |
| 4942 | sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4943 | } |
| 4944 | } |
| 4945 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4946 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4947 | pNew->nLTerm = 1; |
| 4948 | pNew->aLTerm[0] = pTerm; |
| 4949 | pNew->wsFlags = WHERE_MULTI_OR; |
| 4950 | pNew->rSetup = 0; |
| 4951 | pNew->iSortIdx = 0; |
| 4952 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 4953 | for(i=0; rc==SQLITE_OK && i<sSum.n; i++){ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4954 | /* TUNING: Multiple by 3.5 for the secondary table lookup */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4955 | pNew->rRun = sSum.a[i].rRun + 18; |
| 4956 | pNew->nOut = sSum.a[i].nOut; |
| 4957 | pNew->prereq = sSum.a[i].prereq; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4958 | rc = whereLoopInsert(pBuilder, pNew); |
| 4959 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4960 | } |
| 4961 | } |
| 4962 | return rc; |
| 4963 | } |
| 4964 | |
| 4965 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4966 | ** Add all WhereLoop objects for all tables |
| 4967 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4968 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4969 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4970 | Bitmask mExtra = 0; |
| 4971 | Bitmask mPrior = 0; |
| 4972 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4973 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4974 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4975 | sqlite3 *db = pWInfo->pParse->db; |
| 4976 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4977 | int rc = SQLITE_OK; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4978 | u8 priorJoinType = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4979 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4980 | |
| 4981 | /* Loop over the tables in the join, from left to right */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4982 | pNew = pBuilder->pNew; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4983 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4984 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4985 | pNew->iTab = iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4986 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4987 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4988 | mExtra = mPrior; |
| 4989 | } |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4990 | priorJoinType = pItem->jointype; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4991 | if( IsVirtual(pItem->pTab) ){ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4992 | rc = whereLoopAddVirtual(pBuilder); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4993 | }else{ |
| 4994 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 4995 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4996 | if( rc==SQLITE_OK ){ |
| 4997 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 4998 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4999 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5000 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5001 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 5002 | whereLoopClear(db, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5003 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5006 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5007 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5008 | ** parameters) to see if it outputs rows in the requested ORDER BY |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5009 | ** (or GROUP BY) without requiring a separate sort operation. Return: |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5010 | ** |
| 5011 | ** 0: ORDER BY is not satisfied. Sorting required |
| 5012 | ** 1: ORDER BY is satisfied. Omit sorting |
| 5013 | ** -1: Unknown at this time |
| 5014 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5015 | ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as |
| 5016 | ** strict. With GROUP BY and DISTINCT the only requirement is that |
| 5017 | ** equivalent rows appear immediately adjacent to one another. GROUP BY |
| 5018 | ** and DISTINT do not require rows to appear in any particular order as long |
| 5019 | ** as equivelent rows are grouped together. Thus for GROUP BY and DISTINCT |
| 5020 | ** the pOrderBy terms can be matched in any order. With ORDER BY, the |
| 5021 | ** pOrderBy terms must be matched in strict left-to-right order. |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5022 | */ |
| 5023 | static int wherePathSatisfiesOrderBy( |
| 5024 | WhereInfo *pWInfo, /* The WHERE clause */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5025 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5026 | WherePath *pPath, /* The WherePath to check */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5027 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 5028 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5029 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5030 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5031 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5032 | u8 revSet; /* True if rev is known */ |
| 5033 | u8 rev; /* Composite sort order */ |
| 5034 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5035 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 5036 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 5037 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5038 | u16 nKeyCol; /* Number of key columns in pIndex */ |
| 5039 | u16 nColumn; /* Total number of ordered columns in the index */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5040 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 5041 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 5042 | int i, j; /* Loop counters */ |
| 5043 | int iCur; /* Cursor number for current WhereLoop */ |
| 5044 | int iColumn; /* A column number within table iCur */ |
drh | e8ae583 | 2013-06-19 13:32:46 +0000 | [diff] [blame] | 5045 | WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5046 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 5047 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 5048 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 5049 | Index *pIndex; /* The index associated with pLoop */ |
| 5050 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 5051 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 5052 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5053 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5054 | Bitmask ready; /* Mask of inner loops */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5055 | |
| 5056 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5057 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 5058 | ** row of output. A WhereLoop is one-row if all of the following are true: |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5059 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 5060 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5061 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 5062 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5063 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5064 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 5065 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 5066 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 5067 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 5068 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 5069 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 5070 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 5071 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 5072 | ** |
| 5073 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 5074 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 5075 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5076 | */ |
| 5077 | |
| 5078 | assert( pOrderBy!=0 ); |
| 5079 | |
| 5080 | /* Sortability of virtual tables is determined by the xBestIndex method |
| 5081 | ** of the virtual table itself */ |
| 5082 | if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5083 | testcase( nLoop>0 ); /* True when outer loops are one-row and match |
| 5084 | ** no ORDER BY terms */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5085 | return pLast->u.vtab.isOrdered; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5086 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5087 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5088 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5089 | nOrderBy = pOrderBy->nExpr; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5090 | testcase( nOrderBy==BMS-1 ); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5091 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 5092 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5093 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5094 | orderDistinctMask = 0; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5095 | ready = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5096 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5097 | if( iLoop>0 ) ready |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5098 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5099 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5100 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5101 | |
| 5102 | /* Mark off any ORDER BY term X that is a column in the table of |
| 5103 | ** the current loop for which there is term in the WHERE |
| 5104 | ** clause of the form X IS NULL or X=? that reference only outer |
| 5105 | ** loops. |
| 5106 | */ |
| 5107 | for(i=0; i<nOrderBy; i++){ |
| 5108 | if( MASKBIT(i) & obSat ) continue; |
| 5109 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 5110 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 5111 | if( pOBExpr->iTable!=iCur ) continue; |
| 5112 | pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 5113 | ~ready, WO_EQ|WO_ISNULL, 0); |
| 5114 | if( pTerm==0 ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5115 | if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5116 | const char *z1, *z2; |
| 5117 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5118 | if( !pColl ) pColl = db->pDfltColl; |
| 5119 | z1 = pColl->zName; |
| 5120 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 5121 | if( !pColl ) pColl = db->pDfltColl; |
| 5122 | z2 = pColl->zName; |
| 5123 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 5124 | } |
| 5125 | obSat |= MASKBIT(i); |
| 5126 | } |
| 5127 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5128 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 5129 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 5130 | pIndex = 0; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5131 | nKeyCol = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5132 | nColumn = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5133 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5134 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5135 | }else{ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5136 | nKeyCol = pIndex->nKeyCol; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5137 | nColumn = pIndex->nColumn; |
| 5138 | assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) ); |
| 5139 | assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable)); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5140 | isOrderDistinct = pIndex->onError!=OE_None; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5141 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5142 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5143 | /* Loop through all columns of the index and deal with the ones |
| 5144 | ** that are not constrained by == or IN. |
| 5145 | */ |
| 5146 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5147 | distinctColumns = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5148 | for(j=0; j<nColumn; j++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5149 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 5150 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5151 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5152 | if( j<pLoop->u.btree.nEq |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5153 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5154 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5155 | if( i & WO_ISNULL ){ |
| 5156 | testcase( isOrderDistinct ); |
| 5157 | isOrderDistinct = 0; |
| 5158 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5159 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5162 | /* Get the column number in the table (iColumn) and sort order |
| 5163 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5164 | */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5165 | if( pIndex ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5166 | iColumn = pIndex->aiColumn[j]; |
| 5167 | revIdx = pIndex->aSortOrder[j]; |
| 5168 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5169 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5170 | iColumn = -1; |
| 5171 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5172 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5173 | |
| 5174 | /* An unconstrained column that might be NULL means that this |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5175 | ** WhereLoop is not well-ordered |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5176 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5177 | if( isOrderDistinct |
| 5178 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5179 | && j>=pLoop->u.btree.nEq |
| 5180 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 5181 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5182 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | /* Find the ORDER BY term that corresponds to the j-th column |
| 5186 | ** of the index and and mark that ORDER BY term off |
| 5187 | */ |
| 5188 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5189 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5190 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 5191 | if( MASKBIT(i) & obSat ) continue; |
| 5192 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5193 | testcase( wctrlFlags & WHERE_GROUPBY ); |
| 5194 | testcase( wctrlFlags & WHERE_DISTINCTBY ); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5195 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5196 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5197 | if( pOBExpr->iTable!=iCur ) continue; |
| 5198 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 5199 | if( iColumn>=0 ){ |
| 5200 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5201 | if( !pColl ) pColl = db->pDfltColl; |
| 5202 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 5203 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5204 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5205 | break; |
| 5206 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5207 | if( isMatch ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5208 | if( iColumn<0 ){ |
| 5209 | testcase( distinctColumns==0 ); |
| 5210 | distinctColumns = 1; |
| 5211 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5212 | obSat |= MASKBIT(i); |
| 5213 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5214 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 5215 | ** Sort order is irrelevant for a GROUP BY clause. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5216 | if( revSet ){ |
| 5217 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; |
| 5218 | }else{ |
| 5219 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 5220 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 5221 | revSet = 1; |
| 5222 | } |
| 5223 | } |
| 5224 | }else{ |
| 5225 | /* No match found */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5226 | if( j==0 || j<nKeyCol ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5227 | testcase( isOrderDistinct!=0 ); |
| 5228 | isOrderDistinct = 0; |
| 5229 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5230 | break; |
| 5231 | } |
| 5232 | } /* end Loop over all index columns */ |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5233 | if( distinctColumns ){ |
| 5234 | testcase( isOrderDistinct==0 ); |
| 5235 | isOrderDistinct = 1; |
| 5236 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5237 | } /* end-if not one-row */ |
| 5238 | |
| 5239 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5240 | if( isOrderDistinct ){ |
| 5241 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5242 | for(i=0; i<nOrderBy; i++){ |
| 5243 | Expr *p; |
| 5244 | if( MASKBIT(i) & obSat ) continue; |
| 5245 | p = pOrderBy->a[i].pExpr; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5246 | if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5247 | obSat |= MASKBIT(i); |
| 5248 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 5249 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5250 | } |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5251 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5252 | if( obSat==obDone ) return 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5253 | if( !isOrderDistinct ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5254 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5257 | #ifdef WHERETRACE_ENABLED |
| 5258 | /* For debugging use only: */ |
| 5259 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 5260 | static char zName[65]; |
| 5261 | int i; |
| 5262 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 5263 | if( pLast ) zName[i++] = pLast->cId; |
| 5264 | zName[i] = 0; |
| 5265 | return zName; |
| 5266 | } |
| 5267 | #endif |
| 5268 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5269 | |
| 5270 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 5271 | ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5272 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 5273 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 5274 | ** |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5275 | ** Assume that the total number of output rows that will need to be sorted |
| 5276 | ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 5277 | ** costs if nRowEst==0. |
| 5278 | ** |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5279 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 5280 | ** error occurs. |
| 5281 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5282 | static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5283 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5284 | int nLoop; /* Number of terms in the join */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5285 | Parse *pParse; /* Parsing context */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5286 | sqlite3 *db; /* The database connection */ |
| 5287 | int iLoop; /* Loop counter over the terms of the join */ |
| 5288 | int ii, jj; /* Loop counters */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5289 | int mxI = 0; /* Index of next entry to replace */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5290 | LogEst rCost; /* Cost of a path */ |
| 5291 | LogEst nOut; /* Number of outputs */ |
| 5292 | LogEst mxCost = 0; /* Maximum cost of a set of paths */ |
| 5293 | LogEst mxOut = 0; /* Maximum nOut value on the set of paths */ |
| 5294 | LogEst rSortCost; /* Cost to do a sort */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5295 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 5296 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 5297 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 5298 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 5299 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 5300 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 5301 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 5302 | char *pSpace; /* Temporary memory used by this routine */ |
| 5303 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5304 | pParse = pWInfo->pParse; |
| 5305 | db = pParse->db; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5306 | nLoop = pWInfo->nLevel; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5307 | /* TUNING: For simple queries, only the best path is tracked. |
| 5308 | ** For 2-way joins, the 5 best paths are followed. |
| 5309 | ** For joins of 3 or more tables, track the 10 best paths */ |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5310 | mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5311 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5312 | WHERETRACE(0x002, ("---- begin solver\n")); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5313 | |
| 5314 | /* Allocate and initialize space for aTo and aFrom */ |
| 5315 | ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 5316 | pSpace = sqlite3DbMallocRaw(db, ii); |
| 5317 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 5318 | aTo = (WherePath*)pSpace; |
| 5319 | aFrom = aTo+mxChoice; |
| 5320 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 5321 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5322 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5323 | pFrom->aLoop = pX; |
| 5324 | } |
| 5325 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5326 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 5327 | ** |
| 5328 | ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 5329 | ** of computing an automatic index is not paid back within the first 25 |
| 5330 | ** rows, then do not use the automatic index. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5331 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5332 | nFrom = 1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5333 | |
| 5334 | /* Precompute the cost of sorting the final result set, if the caller |
| 5335 | ** to sqlite3WhereBegin() was concerned about sorting */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5336 | rSortCost = 0; |
| 5337 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5338 | aFrom[0].isOrderedValid = 1; |
| 5339 | }else{ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 5340 | /* TUNING: Estimated cost of sorting is 48*N*log2(N) where N is the |
| 5341 | ** number of output rows. The 48 is the expected size of a row to sort. |
| 5342 | ** FIXME: compute a better estimate of the 48 multiplier based on the |
| 5343 | ** result set expressions. */ |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 5344 | rSortCost = nRowEst + estLog(nRowEst); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5345 | WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost)); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | /* Compute successively longer WherePaths using the previous generation |
| 5349 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 5350 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5351 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 5352 | nTo = 0; |
| 5353 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 5354 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 5355 | Bitmask maskNew; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5356 | Bitmask revMask = 0; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5357 | u8 isOrderedValid = pFrom->isOrderedValid; |
| 5358 | u8 isOrdered = pFrom->isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5359 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 5360 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5361 | /* At this point, pWLoop is a candidate to be the next loop. |
| 5362 | ** Compute its cost */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5363 | rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 5364 | rCost = sqlite3LogEstAdd(rCost, pFrom->rCost); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5365 | nOut = pFrom->nRow + pWLoop->nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5366 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5367 | if( !isOrderedValid ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5368 | switch( wherePathSatisfiesOrderBy(pWInfo, |
| 5369 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5370 | iLoop, pWLoop, &revMask) ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5371 | case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ |
| 5372 | isOrdered = 1; |
| 5373 | isOrderedValid = 1; |
| 5374 | break; |
| 5375 | case 0: /* No. pFrom+pWLoop will require a separate sort */ |
| 5376 | isOrdered = 0; |
| 5377 | isOrderedValid = 1; |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5378 | rCost = sqlite3LogEstAdd(rCost, rSortCost); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5379 | break; |
| 5380 | default: /* Cannot tell yet. Try again on the next iteration */ |
| 5381 | break; |
| 5382 | } |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 5383 | }else{ |
| 5384 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5385 | } |
| 5386 | /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 5387 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5388 | if( pTo->maskLoop==maskNew |
| 5389 | && pTo->isOrderedValid==isOrderedValid |
| 5390 | && ((pTo->rCost<=rCost && pTo->nRow<=nOut) || |
| 5391 | (pTo->rCost>=rCost && pTo->nRow>=nOut)) |
| 5392 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5393 | testcase( jj==nTo-1 ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5394 | break; |
| 5395 | } |
| 5396 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5397 | if( jj>=nTo ){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 5398 | if( nTo>=mxChoice && rCost>=mxCost ){ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5399 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5400 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5401 | sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", |
| 5402 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5403 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5404 | } |
| 5405 | #endif |
| 5406 | continue; |
| 5407 | } |
| 5408 | /* Add a new Path to the aTo[] set */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5409 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5410 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5411 | jj = nTo++; |
| 5412 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5413 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5414 | jj = mxI; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5415 | } |
| 5416 | pTo = &aTo[jj]; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5417 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5418 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5419 | sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n", |
| 5420 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5421 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5422 | } |
| 5423 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5424 | }else{ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5425 | if( pTo->rCost<=rCost && pTo->nRow<=nOut ){ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5426 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5427 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5428 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5429 | "Skip %s cost=%-3d,%3d order=%c", |
| 5430 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5431 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5432 | sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n", |
| 5433 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5434 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5435 | } |
| 5436 | #endif |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5437 | testcase( pTo->rCost==rCost ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5438 | continue; |
| 5439 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5440 | testcase( pTo->rCost==rCost+1 ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5441 | /* A new and better score for a previously created equivalent path */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5442 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5443 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5444 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5445 | "Update %s cost=%-3d,%3d order=%c", |
| 5446 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5447 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5448 | sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n", |
| 5449 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5450 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5451 | } |
| 5452 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5453 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5454 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5455 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5456 | pTo->revLoop = revMask; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5457 | pTo->nRow = nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5458 | pTo->rCost = rCost; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5459 | pTo->isOrderedValid = isOrderedValid; |
| 5460 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5461 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 5462 | pTo->aLoop[iLoop] = pWLoop; |
| 5463 | if( nTo>=mxChoice ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5464 | mxI = 0; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5465 | mxCost = aTo[0].rCost; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5466 | mxOut = aTo[0].nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5467 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 5468 | if( pTo->rCost>mxCost || (pTo->rCost==mxCost && pTo->nRow>mxOut) ){ |
| 5469 | mxCost = pTo->rCost; |
| 5470 | mxOut = pTo->nRow; |
| 5471 | mxI = jj; |
| 5472 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5473 | } |
| 5474 | } |
| 5475 | } |
| 5476 | } |
| 5477 | |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5478 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5479 | if( sqlite3WhereTrace>=2 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5480 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5481 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5482 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5483 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5484 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5485 | if( pTo->isOrderedValid && pTo->isOrdered ){ |
| 5486 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 5487 | }else{ |
| 5488 | sqlite3DebugPrintf("\n"); |
| 5489 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5490 | } |
| 5491 | } |
| 5492 | #endif |
| 5493 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5494 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5495 | pFrom = aTo; |
| 5496 | aTo = aFrom; |
| 5497 | aFrom = pFrom; |
| 5498 | nFrom = nTo; |
| 5499 | } |
| 5500 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5501 | if( nFrom==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5502 | sqlite3ErrorMsg(pParse, "no query solution"); |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5503 | sqlite3DbFree(db, pSpace); |
| 5504 | return SQLITE_ERROR; |
| 5505 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5506 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5507 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5508 | pFrom = aFrom; |
| 5509 | for(ii=1; ii<nFrom; ii++){ |
| 5510 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 5511 | } |
| 5512 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5513 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5514 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5515 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 5516 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5517 | pLevel->iFrom = pWLoop->iTab; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5518 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5519 | } |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5520 | if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 5521 | && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 5522 | && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5523 | && nRowEst |
| 5524 | ){ |
| 5525 | Bitmask notUsed; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5526 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5527 | WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5528 | if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5529 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5530 | if( pFrom->isOrdered ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5531 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 5532 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5533 | }else{ |
| 5534 | pWInfo->bOBSat = 1; |
| 5535 | pWInfo->revMask = pFrom->revLoop; |
| 5536 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5537 | } |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5538 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5539 | |
| 5540 | /* Free temporary memory and return success */ |
| 5541 | sqlite3DbFree(db, pSpace); |
| 5542 | return SQLITE_OK; |
| 5543 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 5544 | |
| 5545 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5546 | ** Most queries use only a single table (they are not joins) and have |
| 5547 | ** simple == constraints against indexed fields. This routine attempts |
| 5548 | ** to plan those simple cases using much less ceremony than the |
| 5549 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 5550 | ** times for the common case. |
| 5551 | ** |
| 5552 | ** Return non-zero on success, if this query can be handled by this |
| 5553 | ** no-frills query planner. Return zero if this query needs the |
| 5554 | ** general-purpose query planner. |
| 5555 | */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5556 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5557 | WhereInfo *pWInfo; |
| 5558 | struct SrcList_item *pItem; |
| 5559 | WhereClause *pWC; |
| 5560 | WhereTerm *pTerm; |
| 5561 | WhereLoop *pLoop; |
| 5562 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5563 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5564 | Table *pTab; |
| 5565 | Index *pIdx; |
| 5566 | |
| 5567 | pWInfo = pBuilder->pWInfo; |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 5568 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5569 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 5570 | pItem = pWInfo->pTabList->a; |
| 5571 | pTab = pItem->pTab; |
| 5572 | if( IsVirtual(pTab) ) return 0; |
| 5573 | if( pItem->zIndex ) return 0; |
| 5574 | iCur = pItem->iCursor; |
| 5575 | pWC = &pWInfo->sWC; |
| 5576 | pLoop = pBuilder->pNew; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5577 | pLoop->wsFlags = 0; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5578 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5579 | if( pTerm ){ |
| 5580 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 5581 | pLoop->aLTerm[0] = pTerm; |
| 5582 | pLoop->nLTerm = 1; |
| 5583 | pLoop->u.btree.nEq = 1; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5584 | /* TUNING: Cost of a rowid lookup is 10 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5585 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5586 | }else{ |
| 5587 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 5588 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 5589 | assert( ArraySize(pLoop->aLTermSpace)==4 ); |
| 5590 | if( pIdx->onError==OE_None |
| 5591 | || pIdx->pPartIdxWhere!=0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5592 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 5593 | ) continue; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5594 | for(j=0; j<pIdx->nKeyCol; j++){ |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5595 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5596 | if( pTerm==0 ) break; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5597 | pLoop->aLTerm[j] = pTerm; |
| 5598 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5599 | if( j!=pIdx->nKeyCol ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5600 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 5601 | if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5602 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 5603 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5604 | pLoop->nLTerm = j; |
| 5605 | pLoop->u.btree.nEq = j; |
| 5606 | pLoop->u.btree.pIndex = pIdx; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5607 | /* TUNING: Cost of a unique index lookup is 15 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5608 | pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5609 | break; |
| 5610 | } |
| 5611 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5612 | if( pLoop->wsFlags ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5613 | pLoop->nOut = (LogEst)1; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5614 | pWInfo->a[0].pWLoop = pLoop; |
| 5615 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 5616 | pWInfo->a[0].iTabCur = iCur; |
| 5617 | pWInfo->nRowOut = 1; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5618 | if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5619 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5620 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5621 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5622 | #ifdef SQLITE_DEBUG |
| 5623 | pLoop->cId = '0'; |
| 5624 | #endif |
| 5625 | return 1; |
| 5626 | } |
| 5627 | return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5631 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 5632 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5633 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5634 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5635 | ** in order to complete the WHERE clause processing. |
| 5636 | ** |
| 5637 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5638 | ** |
| 5639 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5640 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5641 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5642 | ** example, if the SQL is this: |
| 5643 | ** |
| 5644 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5645 | ** |
| 5646 | ** Then the code generated is conceptually like the following: |
| 5647 | ** |
| 5648 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5649 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5650 | ** foreach row3 in t3 do / |
| 5651 | ** ... |
| 5652 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5653 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5654 | ** end / |
| 5655 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5656 | ** Note that the loops might not be nested in the order in which they |
| 5657 | ** appear in the FROM clause if a different order is better able to make |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 5658 | ** use of indices. Note also that when the IN operator appears in |
| 5659 | ** the WHERE clause, it might result in additional nested loops for |
| 5660 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5661 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5662 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5663 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5664 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5665 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5666 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5667 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5668 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5669 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5670 | ** data from the various tables of the loop. |
| 5671 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5672 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5673 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5674 | ** the tables have indices and there are terms in the WHERE clause that |
| 5675 | ** refer to those indices, a complete table scan can be avoided and the |
| 5676 | ** code will run much faster. Most of the work of this routine is checking |
| 5677 | ** to see if there are indices that can be used to speed up the loop. |
| 5678 | ** |
| 5679 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 5680 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 5681 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 5682 | ** loops are evaluated and if false a jump is made around all subsequent |
| 5683 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 5684 | ** most loop) |
| 5685 | ** |
| 5686 | ** OUTER JOINS |
| 5687 | ** |
| 5688 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 5689 | ** |
| 5690 | ** foreach row1 in t1 do |
| 5691 | ** flag = 0 |
| 5692 | ** foreach row2 in t2 do |
| 5693 | ** start: |
| 5694 | ** ... |
| 5695 | ** flag = 1 |
| 5696 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5697 | ** if flag==0 then |
| 5698 | ** move the row2 cursor to a null row |
| 5699 | ** goto start |
| 5700 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5701 | ** end |
| 5702 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5703 | ** ORDER BY CLAUSE PROCESSING |
| 5704 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5705 | ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause |
| 5706 | ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5707 | ** if there is one. If there is no ORDER BY clause or if this routine |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5708 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 5709 | ** |
| 5710 | ** The iIdxCur parameter is the cursor number of an index. If |
| 5711 | ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index |
| 5712 | ** to use for OR clause processing. The WHERE clause should use this |
| 5713 | ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is |
| 5714 | ** the first cursor in an array of cursors for all indices. iIdxCur should |
| 5715 | ** be used to compute the appropriate cursor depending on which index is |
| 5716 | ** used. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5717 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5718 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5719 | Parse *pParse, /* The parser context */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5720 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5721 | Expr *pWhere, /* The WHERE clause */ |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5722 | ExprList *pOrderBy, /* An ORDER BY clause, or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5723 | ExprList *pResultSet, /* Result set of the query */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 5724 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 5725 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5726 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5727 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5728 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5729 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 5730 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5731 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5732 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5733 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5734 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5735 | WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5736 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5737 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5738 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5739 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5740 | |
| 5741 | /* Variable initialization */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5742 | db = pParse->db; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5743 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5744 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5745 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5746 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 5747 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 5748 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 5749 | wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 5750 | } |
| 5751 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5752 | /* The number of tables in the FROM clause is limited by the number of |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5753 | ** bits in a Bitmask |
| 5754 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5755 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5756 | if( pTabList->nSrc>BMS ){ |
| 5757 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5758 | return 0; |
| 5759 | } |
| 5760 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5761 | /* This function normally generates a nested loop for all tables in |
| 5762 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 5763 | ** only generate code for the first table in pTabList and assume that |
| 5764 | ** any cursors associated with subsequent tables are uninitialized. |
| 5765 | */ |
| 5766 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 5767 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5768 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5769 | ** return value. A single allocation is used to store the WhereInfo |
| 5770 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 5771 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 5772 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 5773 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5774 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5775 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5776 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5777 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5778 | sqlite3DbFree(db, pWInfo); |
| 5779 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5780 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5781 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 5782 | pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5783 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5784 | pWInfo->pParse = pParse; |
| 5785 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5786 | pWInfo->pOrderBy = pOrderBy; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5787 | pWInfo->pResultSet = pResultSet; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5788 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5789 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5790 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5791 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5792 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5793 | sWLB.pWC = &pWInfo->sWC; |
drh | 1ac87e1 | 2013-07-18 14:50:56 +0000 | [diff] [blame] | 5794 | sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); |
| 5795 | assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5796 | whereLoopInit(sWLB.pNew); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5797 | #ifdef SQLITE_DEBUG |
| 5798 | sWLB.pNew->cId = '*'; |
| 5799 | #endif |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5800 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5801 | /* Split the WHERE clause into separate subexpressions where each |
| 5802 | ** subexpression is separated by an AND operator. |
| 5803 | */ |
| 5804 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5805 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5806 | sqlite3ExprCodeConstants(pParse, pWhere); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 5807 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
drh | 5e128b2 | 2013-07-09 03:04:32 +0000 | [diff] [blame] | 5808 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5809 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5810 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 5811 | ** expression and either jump over all of the code or fall thru. |
| 5812 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5813 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 5814 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 5815 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5816 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5817 | |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5818 | /* Special case: No FROM clause |
| 5819 | */ |
| 5820 | if( nTabList==0 ){ |
| 5821 | if( pOrderBy ) pWInfo->bOBSat = 1; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5822 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5823 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5824 | } |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5825 | } |
| 5826 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5827 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 5828 | ** |
| 5829 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 5830 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 5831 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 5832 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 5833 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 5834 | ** of the join. Subtracting one from the right table bitmask gives a |
| 5835 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 5836 | ** for all tables to the left of a left join is important. Ticket #3015. |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 5837 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5838 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 5839 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 5840 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 5841 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5842 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5843 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5844 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5845 | } |
| 5846 | #ifndef NDEBUG |
| 5847 | { |
| 5848 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5849 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5850 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5851 | assert( (m-1)==toTheLeft ); |
| 5852 | toTheLeft |= m; |
| 5853 | } |
| 5854 | } |
| 5855 | #endif |
| 5856 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5857 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 5858 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 5859 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 5860 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5861 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5862 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5863 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5864 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 5865 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5866 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5867 | /* If the ORDER BY (or GROUP BY) clause contains references to general |
| 5868 | ** expressions, then we won't be able to satisfy it using indices, so |
| 5869 | ** go ahead and disable it now. |
| 5870 | */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5871 | if( pOrderBy && (wctrlFlags & WHERE_WANT_DISTINCT)!=0 ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5872 | for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 5873 | Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr); |
| 5874 | if( pExpr->op!=TK_COLUMN ){ |
| 5875 | pWInfo->pOrderBy = pOrderBy = 0; |
| 5876 | break; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5877 | }else if( pExpr->iColumn<0 ){ |
| 5878 | break; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5879 | } |
| 5880 | } |
| 5881 | } |
| 5882 | |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5883 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5884 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ |
| 5885 | /* The DISTINCT marking is pointless. Ignore it. */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5886 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5887 | }else if( pOrderBy==0 ){ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5888 | /* Try to ORDER BY the result set to make distinct processing easier */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5889 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5890 | pWInfo->pOrderBy = pResultSet; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5891 | } |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5894 | /* Construct the WhereLoop objects */ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5895 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 5896 | /* Display all terms of the WHERE clause */ |
| 5897 | #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 5898 | if( sqlite3WhereTrace & 0x100 ){ |
| 5899 | int i; |
| 5900 | Vdbe *v = pParse->pVdbe; |
| 5901 | sqlite3ExplainBegin(v); |
| 5902 | for(i=0; i<sWLB.pWC->nTerm; i++){ |
drh | 7afc8b0 | 2013-10-28 22:33:36 +0000 | [diff] [blame] | 5903 | sqlite3ExplainPrintf(v, "#%-2d ", i); |
drh | f4e9cb0 | 2013-10-28 19:59:59 +0000 | [diff] [blame] | 5904 | sqlite3ExplainPush(v); |
| 5905 | whereExplainTerm(v, &sWLB.pWC->a[i]); |
| 5906 | sqlite3ExplainPop(v); |
| 5907 | sqlite3ExplainNL(v); |
| 5908 | } |
| 5909 | sqlite3ExplainFinish(v); |
| 5910 | sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v)); |
| 5911 | } |
| 5912 | #endif |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5913 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5914 | rc = whereLoopAddAll(&sWLB); |
| 5915 | if( rc ) goto whereBeginError; |
| 5916 | |
| 5917 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5918 | #ifdef WHERETRACE_ENABLED /* !=0 */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5919 | if( sqlite3WhereTrace ){ |
| 5920 | WhereLoop *p; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5921 | int i; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5922 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 5923 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5924 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 5925 | p->cId = zLabel[i%sizeof(zLabel)]; |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 5926 | whereLoopPrint(p, sWLB.pWC); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5927 | } |
| 5928 | } |
| 5929 | #endif |
| 5930 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5931 | wherePathSolver(pWInfo, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5932 | if( db->mallocFailed ) goto whereBeginError; |
| 5933 | if( pWInfo->pOrderBy ){ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5934 | wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5935 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5936 | } |
| 5937 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5938 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 5939 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5940 | } |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5941 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5942 | goto whereBeginError; |
| 5943 | } |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 5944 | #ifdef WHERETRACE_ENABLED /* !=0 */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5945 | if( sqlite3WhereTrace ){ |
| 5946 | int ii; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5947 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 5948 | if( pWInfo->bOBSat ){ |
| 5949 | sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5950 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5951 | switch( pWInfo->eDistinct ){ |
| 5952 | case WHERE_DISTINCT_UNIQUE: { |
| 5953 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 5954 | break; |
| 5955 | } |
| 5956 | case WHERE_DISTINCT_ORDERED: { |
| 5957 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 5958 | break; |
| 5959 | } |
| 5960 | case WHERE_DISTINCT_UNORDERED: { |
| 5961 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 5962 | break; |
| 5963 | } |
| 5964 | } |
| 5965 | sqlite3DebugPrintf("\n"); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5966 | for(ii=0; ii<pWInfo->nLevel; ii++){ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 5967 | whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5968 | } |
| 5969 | } |
| 5970 | #endif |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5971 | /* Attempt to omit tables from the join that do not effect the result */ |
drh | 1031bd9 | 2013-06-22 15:44:26 +0000 | [diff] [blame] | 5972 | if( pWInfo->nLevel>=2 |
| 5973 | && pResultSet!=0 |
| 5974 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 5975 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5976 | Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet); |
drh | 67a5ec7 | 2013-09-03 14:03:47 +0000 | [diff] [blame] | 5977 | if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5978 | while( pWInfo->nLevel>=2 ){ |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 5979 | WhereTerm *pTerm, *pEnd; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5980 | pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5981 | if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break; |
| 5982 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 5983 | && (pLoop->wsFlags & WHERE_ONEROW)==0 |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5984 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5985 | break; |
| 5986 | } |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5987 | if( (tabUsed & pLoop->maskSelf)!=0 ) break; |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 5988 | pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; |
| 5989 | for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){ |
| 5990 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 |
| 5991 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 5992 | ){ |
| 5993 | break; |
| 5994 | } |
| 5995 | } |
| 5996 | if( pTerm<pEnd ) break; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5997 | WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 5998 | pWInfo->nLevel--; |
| 5999 | nTabList--; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6000 | } |
| 6001 | } |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6002 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 6003 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 6004 | |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6005 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 6006 | ** to use a one-pass algorithm, determine if this is appropriate. |
drh | 24b7fe9 | 2013-09-30 19:33:06 +0000 | [diff] [blame] | 6007 | ** The one-pass algorithm only works if the WHERE clause constrains |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6008 | ** the statement to update a single row. |
| 6009 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 6010 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6011 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 6012 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6013 | pWInfo->okOnePass = 1; |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 6014 | if( HasRowid(pTabList->a[0].pTab) ){ |
| 6015 | pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 6016 | } |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6017 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 6018 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6019 | /* Open all tables in the pTabList and any indices selected for |
| 6020 | ** searching those tables. |
| 6021 | */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6022 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6023 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6024 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6025 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6026 | struct SrcList_item *pTabItem; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6027 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6028 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6029 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6030 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6031 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 6032 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 6033 | /* Do nothing */ |
| 6034 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6035 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6036 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6037 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 6038 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6039 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 6040 | }else if( IsVirtual(pTab) ){ |
| 6041 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6042 | }else |
| 6043 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6044 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6045 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 6046 | int op = OP_OpenRead; |
| 6047 | if( pWInfo->okOnePass ){ |
| 6048 | op = OP_OpenWrite; |
| 6049 | pWInfo->aiCurOnePass[0] = pTabItem->iCursor; |
| 6050 | }; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6051 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 6052 | assert( pTabItem->iCursor==pLevel->iTabCur ); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 6053 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); |
| 6054 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 6055 | if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6056 | Bitmask b = pTabItem->colUsed; |
| 6057 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6058 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 6059 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 6060 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6061 | assert( n<=pTab->nCol ); |
| 6062 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6063 | }else{ |
| 6064 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6065 | } |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 6066 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6067 | Index *pIx = pLoop->u.btree.pIndex; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 6068 | int iIndexCur; |
| 6069 | int op = OP_OpenRead; |
| 6070 | if( pWInfo->okOnePass && iIdxCur ){ |
| 6071 | Index *pJ = pTabItem->pTab->pIndex; |
| 6072 | iIndexCur = iIdxCur; |
| 6073 | assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); |
| 6074 | while( ALWAYS(pJ) && pJ!=pIx ){ |
| 6075 | iIndexCur++; |
| 6076 | pJ = pJ->pNext; |
| 6077 | } |
| 6078 | op = OP_OpenWrite; |
| 6079 | pWInfo->aiCurOnePass[1] = iIndexCur; |
| 6080 | }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){ |
| 6081 | iIndexCur = iIdxCur; |
| 6082 | }else{ |
| 6083 | iIndexCur = pParse->nTab++; |
| 6084 | } |
| 6085 | pLevel->iIdxCur = iIndexCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6086 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 6087 | assert( iIndexCur>=0 ); |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 6088 | sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 6089 | sqlite3VdbeSetP4KeyInfo(pParse, pIx); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 6090 | VdbeComment((v, "%s", pIx->zName)); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6091 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6092 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6093 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6094 | } |
| 6095 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 6096 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6097 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6098 | /* Generate the code to do the search. Each iteration of the for |
| 6099 | ** loop below generates code for a single nested loop of the VM |
| 6100 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6101 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 6102 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6103 | for(ii=0; ii<nTabList; ii++){ |
| 6104 | pLevel = &pWInfo->a[ii]; |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6105 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 6106 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 6107 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 6108 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 6109 | if( db->mallocFailed ) goto whereBeginError; |
| 6110 | } |
| 6111 | #endif |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6112 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6113 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6114 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 6115 | pWInfo->iContinue = pLevel->addrCont; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6116 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 6117 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 6118 | /* Done. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6119 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6120 | |
| 6121 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6122 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6123 | if( pWInfo ){ |
| 6124 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6125 | whereInfoFree(db, pWInfo); |
| 6126 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6127 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6128 | } |
| 6129 | |
| 6130 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6131 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6132 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6133 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6134 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6135 | Parse *pParse = pWInfo->pParse; |
| 6136 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6137 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6138 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6139 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 6140 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6141 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6142 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6143 | /* Generate loop termination code. |
| 6144 | */ |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 6145 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6146 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6147 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6148 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6149 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6150 | if( pLevel->op!=OP_Noop ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6151 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 6152 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6153 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6154 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 6155 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6156 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6157 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6158 | for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6159 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 6160 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6161 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6162 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6163 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 6164 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6165 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6166 | if( pLevel->iLeftJoin ){ |
| 6167 | int addr; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 6168 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6169 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 6170 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 6171 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 6172 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 6173 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 6174 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 6175 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 6176 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 6177 | if( pLevel->op==OP_Return ){ |
| 6178 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 6179 | }else{ |
| 6180 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 6181 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 6182 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6183 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 6184 | VdbeNoopComment((v, "End WHERE-Loop %d: %s", i, |
| 6185 | pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6186 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6187 | |
| 6188 | /* The "break" point is here, just past the end of the outer loop. |
| 6189 | ** Set it. |
| 6190 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6191 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6192 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6193 | assert( pWInfo->nLevel<=pTabList->nSrc ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6194 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6195 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6196 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6197 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 6198 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6199 | pLoop = pLevel->pWLoop; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 6200 | |
| 6201 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
| 6202 | ** Except, do not close cursors that will be reused by the OR optimization |
| 6203 | ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors |
| 6204 | ** created for the ONEPASS optimization. |
| 6205 | */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6206 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 6207 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6208 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6209 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6210 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6211 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6212 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 6213 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame^] | 6214 | if( (ws & WHERE_INDEXED)!=0 |
| 6215 | && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 |
| 6216 | && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1] |
| 6217 | ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6218 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 6219 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6220 | } |
| 6221 | |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 6222 | /* If this scan uses an index, make VDBE code substitutions to read data |
| 6223 | ** from the index instead of from the table where possible. In some cases |
| 6224 | ** this optimization prevents the table from ever being read, which can |
| 6225 | ** yield a significant performance boost. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6226 | ** |
| 6227 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 6228 | ** sqlite3WhereEnd will have created code that references the table |
| 6229 | ** directly. This loop scans all that code looking for opcodes |
| 6230 | ** that reference the table and converts them into opcodes that |
| 6231 | ** reference the index. |
| 6232 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6233 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 6234 | pIdx = pLoop->u.btree.pIndex; |
| 6235 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 6236 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6237 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6238 | if( pIdx && !db->mallocFailed ){ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 6239 | int k, last; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6240 | VdbeOp *pOp; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6241 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6242 | last = sqlite3VdbeCurrentAddr(v); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6243 | k = pLevel->addrBody; |
| 6244 | pOp = sqlite3VdbeGetOp(v, k); |
| 6245 | for(; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6246 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 6247 | if( pOp->opcode==OP_Column ){ |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 6248 | int x = pOp->p2; |
| 6249 | Table *pTab = pIdx->pTable; |
| 6250 | if( !HasRowid(pTab) ){ |
| 6251 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 6252 | x = pPk->aiColumn[x]; |
| 6253 | } |
| 6254 | x = sqlite3ColumnOfIndex(pIdx, x); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 6255 | if( x>=0 ){ |
| 6256 | pOp->p2 = x; |
| 6257 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6258 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 6259 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6260 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6261 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6262 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6263 | } |
| 6264 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6265 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6266 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6267 | |
| 6268 | /* Final cleanup |
| 6269 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 6270 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6271 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6272 | return; |
| 6273 | } |