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 | /* |
| 52 | ** Cost X is tracked as 10*log2(X) stored in a 16-bit integer. The |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 53 | ** maximum cost for ordinary tables is 64*(2**63) which becomes 6900. |
| 54 | ** (Virtual tables can return a larger cost, but let's assume they do not.) |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 55 | ** So all costs can be stored in a 16-bit integer without risk |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 56 | ** of overflow. |
| 57 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 58 | ** Costs are estimates, so no effort is made to compute 10*log2(X) exactly. |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 59 | ** Instead, a close estimate is used. Any value of X=1 is stored as 0. |
| 60 | ** X=2 is 10. X=3 is 16. X=1000 is 99. etc. Negative values are allowed. |
| 61 | ** A WhereCost of -10 means 0.5. WhereCost of -20 means 0.25. And so forth. |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 62 | ** |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 63 | ** The tool/wherecosttest.c source file implements a command-line program |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 64 | ** that will convert WhereCosts to integers, convert integers to WhereCosts |
| 65 | ** and do addition and multiplication on WhereCost values. The wherecosttest |
| 66 | ** command-line program is a useful utility to have around when working with |
| 67 | ** this module. |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 68 | */ |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 69 | typedef short int WhereCost; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 70 | |
| 71 | /* |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 72 | ** This object contains information needed to implement a single nested |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 73 | ** loop in WHERE clause. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 74 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 75 | ** Contrast this object with WhereLoop. This object describes the |
| 76 | ** implementation of the loop. WhereLoop describes the algorithm. |
| 77 | ** This object contains a pointer to the WhereLoop algorithm as one of |
| 78 | ** its elements. |
| 79 | ** |
| 80 | ** The WhereInfo object contains a single instance of this object for |
| 81 | ** each term in the FROM clause (which is to say, for each of the |
| 82 | ** nested loops as implemented). The order of WhereLevel objects determines |
| 83 | ** the loop nested order, with WhereInfo.a[0] being the outer loop and |
| 84 | ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 85 | */ |
| 86 | struct WhereLevel { |
| 87 | int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 88 | int iTabCur; /* The VDBE cursor used to access the table */ |
| 89 | int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 90 | int addrBrk; /* Jump here to break out of the loop */ |
| 91 | int addrNxt; /* Jump here to start the next IN combination */ |
| 92 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 93 | int addrFirst; /* First instruction of interior of the loop */ |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 94 | int addrBody; /* Beginning of the body of this loop */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 95 | u8 iFrom; /* Which entry in the FROM clause */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 96 | u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ |
| 97 | int p1, p2; /* Operands of the opcode used to ends the loop */ |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 98 | union { /* Information that depends on pWLoop->wsFlags */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 99 | struct { |
| 100 | int nIn; /* Number of entries in aInLoop[] */ |
| 101 | struct InLoop { |
| 102 | int iCur; /* The VDBE cursor used by this IN operator */ |
| 103 | int addrInTop; /* Top of the IN loop */ |
| 104 | u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 105 | } *aInLoop; /* Information about each nested IN operator */ |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 106 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 107 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 108 | } u; |
| 109 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 110 | }; |
| 111 | |
| 112 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 113 | ** Each instance of this object represents an algorithm for evaluating one |
| 114 | ** term of a join. Every term of the FROM clause will have at least |
| 115 | ** one corresponding WhereLoop object (unless INDEXED BY constraints |
| 116 | ** prevent a query solution - which is an error) and many terms of the |
| 117 | ** FROM clause will have multiple WhereLoop objects, each describing a |
| 118 | ** potential way of implementing that FROM-clause term, together with |
| 119 | ** dependencies and cost estimates for using the chosen algorithm. |
| 120 | ** |
| 121 | ** Query planning consists of building up a collection of these WhereLoop |
| 122 | ** objects, then computing a particular sequence of WhereLoop objects, with |
| 123 | ** one WhereLoop object per FROM clause term, that satisfy all dependencies |
| 124 | ** and that minimize the overall cost. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 125 | */ |
| 126 | struct WhereLoop { |
| 127 | Bitmask prereq; /* Bitmask of other loops that must run first */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 128 | Bitmask maskSelf; /* Bitmask identifying table iTab */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 129 | #ifdef SQLITE_DEBUG |
| 130 | char cId; /* Symbolic ID of this loop for debugging use */ |
| 131 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 132 | u8 iTab; /* Position in FROM clause of table for this loop */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 133 | u8 iSortIdx; /* Sorting index number. 0==None */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 134 | WhereCost rSetup; /* One-time setup cost (ex: create transient index) */ |
| 135 | WhereCost rRun; /* Cost of running each loop */ |
| 136 | WhereCost nOut; /* Estimated number of output rows */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 137 | union { |
| 138 | struct { /* Information for internal btree tables */ |
| 139 | int nEq; /* Number of equality constraints */ |
| 140 | Index *pIndex; /* Index used, or NULL */ |
| 141 | } btree; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 142 | struct { /* Information for virtual tables */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 143 | int idxNum; /* Index number */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 144 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 145 | u8 isOrdered; /* True if satisfies ORDER BY */ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 146 | u16 omitMask; /* Terms that may be omitted */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 147 | char *idxStr; /* Index identifier string */ |
| 148 | } vtab; |
| 149 | } u; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 150 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 151 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 152 | /**** whereLoopXfer() copies fields above ***********************/ |
| 153 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 154 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 155 | WhereTerm **aLTerm; /* WhereTerms used */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 156 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 157 | WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 160 | /* This object holds the prerequisites and the cost of running a |
| 161 | ** subquery on one operand of an OR operator in the WHERE clause. |
| 162 | ** See WhereOrSet for additional information |
| 163 | */ |
| 164 | struct WhereOrCost { |
| 165 | Bitmask prereq; /* Prerequisites */ |
| 166 | WhereCost rRun; /* Cost of running this subquery */ |
| 167 | WhereCost nOut; /* Number of outputs for this subquery */ |
| 168 | }; |
| 169 | |
| 170 | /* The WhereOrSet object holds a set of possible WhereOrCosts that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 171 | ** correspond to the subquery(s) of OR-clause processing. Only the |
| 172 | ** best N_OR_COST elements are retained. |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 173 | */ |
| 174 | #define N_OR_COST 3 |
| 175 | struct WhereOrSet { |
| 176 | u16 n; /* Number of valid a[] entries */ |
| 177 | WhereOrCost a[N_OR_COST]; /* Set of best costs */ |
| 178 | }; |
| 179 | |
| 180 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 181 | /* Forward declaration of methods */ |
| 182 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 183 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 184 | /* |
| 185 | ** Each instance of this object holds a sequence of WhereLoop objects |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 186 | ** that implement some or all of a query plan. |
| 187 | ** |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 188 | ** Think of each WhereLoop object as a node in a graph with arcs |
drh | e56dd3a | 2013-08-30 17:50:35 +0000 | [diff] [blame] | 189 | ** showing dependencies and costs for travelling between nodes. (That is |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 190 | ** not a completely accurate description because WhereLoop costs are a |
drh | e56dd3a | 2013-08-30 17:50:35 +0000 | [diff] [blame] | 191 | ** vector, not a scalar, and because dependencies are many-to-one, not |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 192 | ** one-to-one as are graph nodes. But it is a useful visualization aid.) |
| 193 | ** Then a WherePath object is a path through the graph that visits some |
| 194 | ** or all of the WhereLoop objects once. |
| 195 | ** |
| 196 | ** The "solver" works by creating the N best WherePath objects of length |
| 197 | ** 1. Then using those as a basis to compute the N best WherePath objects |
| 198 | ** of length 2. And so forth until the length of WherePaths equals the |
| 199 | ** number of nodes in the FROM clause. The best (lowest cost) WherePath |
| 200 | ** at the end is the choosen query plan. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 201 | */ |
| 202 | struct WherePath { |
| 203 | Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 204 | Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 205 | WhereCost nRow; /* Estimated number of rows generated by this path */ |
| 206 | WhereCost rCost; /* Total cost of this path */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 207 | u8 isOrdered; /* True if this path satisfies ORDER BY */ |
| 208 | u8 isOrderedValid; /* True if the isOrdered field is valid */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 209 | WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 210 | }; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 211 | |
| 212 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 213 | ** The query generator uses an array of instances of this structure to |
| 214 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 215 | ** clause subexpression is separated from the others by AND operators, |
| 216 | ** usually, or sometimes subexpressions separated by OR. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 217 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 218 | ** All WhereTerms are collected into a single WhereClause structure. |
| 219 | ** The following identity holds: |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 220 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 221 | ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 222 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 223 | ** When a term is of the form: |
| 224 | ** |
| 225 | ** X <op> <expr> |
| 226 | ** |
| 227 | ** where X is a column name and <op> is one of certain operators, |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 228 | ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the |
| 229 | ** cursor number and column number for X. WhereTerm.eOperator records |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 230 | ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 231 | ** use of a bitmask encoding for the operator allows us to search |
| 232 | ** quickly for terms that match any of several different operators. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 233 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 234 | ** A WhereTerm might also be two or more subterms connected by OR: |
| 235 | ** |
| 236 | ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... |
| 237 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 238 | ** 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] | 239 | ** and the WhereTerm.u.pOrInfo field points to auxiliary information that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 240 | ** is collected about the OR clause. |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 241 | ** |
| 242 | ** If a term in the WHERE clause does not match either of the two previous |
| 243 | ** categories, then eOperator==0. The WhereTerm.pExpr field is still set |
| 244 | ** to the original subexpression content and wtFlags is set up appropriately |
| 245 | ** but no other fields in the WhereTerm object are meaningful. |
| 246 | ** |
| 247 | ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 248 | ** but they do so indirectly. A single WhereMaskSet structure translates |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 249 | ** cursor number into bits and the translated bit is stored in the prereq |
| 250 | ** fields. The translation is used in order to maximize the number of |
| 251 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 252 | ** spread out over the non-negative integers. For example, the cursor |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 253 | ** 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] | 254 | ** translates these sparse cursor numbers into consecutive integers |
| 255 | ** beginning with 0 in order to make the best possible use of the available |
| 256 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 257 | ** would be mapped into integers 0 through 7. |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 258 | ** |
| 259 | ** The number of terms in a join is limited by the number of bits |
| 260 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 261 | ** is only able to process joins with 64 or fewer tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 262 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 263 | struct WhereTerm { |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 264 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 265 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 266 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 267 | union { |
| 268 | int leftColumn; /* Column number of X in "X <op> <expr>" */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 269 | WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 270 | WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 271 | } u; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 272 | WhereCost truthProb; /* Probability of truth for this expression */ |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 273 | u16 eOperator; /* A WO_xx value describing <op> */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 274 | u8 wtFlags; /* TERM_xxx bit flags. See below */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 275 | u8 nChild; /* Number of children that must disable us */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 276 | WhereClause *pWC; /* The clause this term is part of */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 277 | Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 278 | Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | /* |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 282 | ** Allowed values of WhereTerm.wtFlags |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 283 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 284 | #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 285 | #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
| 286 | #define TERM_CODED 0x04 /* This term is already coded */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 287 | #define TERM_COPIED 0x08 /* Has a child */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 288 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 289 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 290 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 291 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 292 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 293 | #else |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 294 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 295 | #endif |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 296 | |
| 297 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 298 | ** An instance of the WhereScan object is used as an iterator for locating |
| 299 | ** terms in the WHERE clause that are useful to the query planner. |
| 300 | */ |
| 301 | struct WhereScan { |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 302 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 303 | WhereClause *pWC; /* WhereClause currently being scanned */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 304 | char *zCollName; /* Required collating sequence, if not NULL */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 305 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 306 | unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 307 | unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 308 | u32 opMask; /* Acceptable operators */ |
| 309 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 310 | int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ |
| 311 | }; |
| 312 | |
| 313 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 314 | ** An instance of the following structure holds all information about a |
| 315 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 316 | ** |
| 317 | ** Explanation of pOuter: For a WHERE clause of the form |
| 318 | ** |
| 319 | ** a AND ((b AND c) OR (d AND e)) AND f |
| 320 | ** |
| 321 | ** There are separate WhereClause objects for the whole clause and for |
| 322 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 323 | ** subclauses points to the WhereClause object for the whole clause. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 324 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 325 | struct WhereClause { |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 326 | WhereInfo *pWInfo; /* WHERE clause processing context */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 327 | WhereClause *pOuter; /* Outer conjunction */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 328 | u8 op; /* Split operator. TK_AND or TK_OR */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 329 | int nTerm; /* Number of terms */ |
| 330 | int nSlot; /* Number of entries in a[] */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 331 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 332 | #if defined(SQLITE_SMALL_STACK) |
| 333 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| 334 | #else |
| 335 | WhereTerm aStatic[8]; /* Initial static space for a[] */ |
| 336 | #endif |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 340 | ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to |
| 341 | ** a dynamically allocated instance of the following structure. |
| 342 | */ |
| 343 | struct WhereOrInfo { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 344 | WhereClause wc; /* Decomposition into subterms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 345 | Bitmask indexable; /* Bitmask of all indexable tables in the clause */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | /* |
| 349 | ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to |
| 350 | ** a dynamically allocated instance of the following structure. |
| 351 | */ |
| 352 | struct WhereAndInfo { |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 353 | WhereClause wc; /* The subexpression broken out */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 357 | ** An instance of the following structure keeps track of a mapping |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 358 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 359 | ** |
| 360 | ** The VDBE cursor numbers are small integers contained in |
| 361 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 362 | ** clause, the cursor numbers might not begin with 0 and they might |
| 363 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 364 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 365 | ** from the sparse cursor numbers into consecutive integers beginning |
| 366 | ** with 0. |
| 367 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 368 | ** 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] | 369 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 370 | ** |
| 371 | ** For example, if the WHERE clause expression used these VDBE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 372 | ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 373 | ** would map those cursor numbers into bits 0 through 5. |
| 374 | ** |
| 375 | ** Note that the mapping is not necessarily ordered. In the example |
| 376 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 377 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 378 | ** does not really matter. What is important is that sparse cursor |
| 379 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 380 | ** no gaps. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 381 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 382 | struct WhereMaskSet { |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 383 | int n; /* Number of assigned cursor values */ |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 384 | int ix[BMS]; /* Cursor assigned to each bit */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 387 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 388 | ** This object is a convenience wrapper holding all information needed |
| 389 | ** to construct WhereLoop objects for a particular query. |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 390 | */ |
| 391 | struct WhereLoopBuilder { |
| 392 | WhereInfo *pWInfo; /* Information about this WHERE */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 393 | WhereClause *pWC; /* WHERE clause terms */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 394 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 395 | WhereLoop *pNew; /* Template WhereLoop */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 396 | WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 397 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 398 | UnpackedRecord *pRec; /* Probe for stat4 (if required) */ |
| 399 | int nRecValid; /* Number of valid fields currently in pRec */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 400 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | /* |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 404 | ** The WHERE clause processing routine has two halves. The |
| 405 | ** first part does the start of the WHERE loop and the second |
| 406 | ** half does the tail of the WHERE loop. An instance of |
| 407 | ** this structure is returned by the first half and passed |
| 408 | ** into the second half to give some continuity. |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 409 | ** |
| 410 | ** An instance of this object holds the complete state of the query |
| 411 | ** planner. |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 412 | */ |
| 413 | struct WhereInfo { |
| 414 | Parse *pParse; /* Parsing and code generating context */ |
| 415 | SrcList *pTabList; /* List of tables in the join */ |
| 416 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 417 | ExprList *pResultSet; /* Result set. DISTINCT operates on these */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 418 | WhereLoop *pLoops; /* List of all WhereLoop objects */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 419 | Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 420 | WhereCost nRowOut; /* Estimated number of output rows */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 421 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 422 | u8 bOBSat; /* ORDER BY satisfied by indices */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 423 | u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ |
| 424 | u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
| 425 | u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 426 | u8 nLevel; /* Number of nested loop */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 427 | int iTop; /* The very beginning of the WHERE loop */ |
| 428 | int iContinue; /* Jump here to continue with next record */ |
| 429 | int iBreak; /* Jump here to break out of the loop */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 430 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 431 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 432 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 433 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 434 | }; |
| 435 | |
| 436 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 437 | ** Bitmasks for the operators on WhereTerm objects. These are all |
| 438 | ** operators that are of interest to the query planner. An |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 439 | ** OR-ed combination of these values can be used when searching for |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 440 | ** particular WhereTerms within a WhereClause. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 441 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 442 | #define WO_IN 0x001 |
| 443 | #define WO_EQ 0x002 |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 444 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 445 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 446 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 447 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 448 | #define WO_MATCH 0x040 |
| 449 | #define WO_ISNULL 0x080 |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 450 | #define WO_OR 0x100 /* Two or more OR-connected terms */ |
| 451 | #define WO_AND 0x200 /* Two or more AND-connected terms */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 452 | #define WO_EQUIV 0x400 /* Of the form A==B, both columns */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 453 | #define WO_NOOP 0x800 /* This term does not restrict search space */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 454 | |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 455 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 456 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 457 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 458 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 459 | ** These are definitions of bits in the WhereLoop.wsFlags field. |
| 460 | ** The particular combination of bits in each WhereLoop help to |
| 461 | ** determine the algorithm that WhereLoop represents. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 462 | */ |
drh | be4fe3a | 2013-07-01 10:38:35 +0000 | [diff] [blame] | 463 | #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 464 | #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 465 | #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 466 | #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 467 | #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 468 | #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 469 | #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 470 | #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 471 | #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 472 | #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 473 | #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 474 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 475 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 476 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 477 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 478 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 479 | |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 480 | |
| 481 | /* Convert a WhereCost value (10 times log2(X)) into its integer value X. |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 482 | ** A rough approximation is used. The value returned is not exact. |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 483 | */ |
| 484 | static u64 whereCostToInt(WhereCost x){ |
| 485 | u64 n; |
drh | 12ffbc7 | 2013-06-13 14:51:53 +0000 | [diff] [blame] | 486 | if( x<10 ) return 1; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 487 | n = x%10; |
| 488 | x /= 10; |
| 489 | if( n>=5 ) n -= 2; |
| 490 | else if( n>=1 ) n -= 1; |
| 491 | if( x>=3 ) return (n+8)<<(x-3); |
| 492 | return (n+8)>>(3-x); |
| 493 | } |
| 494 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 495 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 496 | ** Return the estimated number of output rows from a WHERE clause |
| 497 | */ |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 498 | u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 499 | return whereCostToInt(pWInfo->nRowOut); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* |
| 503 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 504 | ** WHERE clause returns outputs for DISTINCT processing. |
| 505 | */ |
| 506 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 507 | return pWInfo->eDistinct; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 512 | ** Return FALSE if the output needs to be sorted. |
| 513 | */ |
| 514 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 515 | return pWInfo->bOBSat!=0; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* |
| 519 | ** Return the VDBE address or label to jump to in order to continue |
| 520 | ** immediately with the next row of a WHERE clause. |
| 521 | */ |
| 522 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 523 | return pWInfo->iContinue; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | ** Return the VDBE address or label to jump to in order to break |
| 528 | ** out of a WHERE loop. |
| 529 | */ |
| 530 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 531 | return pWInfo->iBreak; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 536 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 537 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
| 538 | */ |
| 539 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo){ |
| 540 | return pWInfo->okOnePass; |
| 541 | } |
| 542 | |
| 543 | /* |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 544 | ** Move the content of pSrc into pDest |
| 545 | */ |
| 546 | static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 547 | pDest->n = pSrc->n; |
| 548 | memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 553 | ** |
| 554 | ** The new entry might overwrite an existing entry, or it might be |
| 555 | ** appended, or it might be discarded. Do whatever is the right thing |
| 556 | ** so that pSet keeps the N_OR_COST best entries seen so far. |
| 557 | */ |
| 558 | static int whereOrInsert( |
| 559 | WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 560 | Bitmask prereq, /* Prerequisites of the new entry */ |
| 561 | WhereCost rRun, /* Run-cost of the new entry */ |
| 562 | WhereCost nOut /* Number of outputs for the new entry */ |
| 563 | ){ |
| 564 | u16 i; |
| 565 | WhereOrCost *p; |
| 566 | for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 567 | if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 568 | goto whereOrInsert_done; |
| 569 | } |
| 570 | if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 571 | return 0; |
| 572 | } |
| 573 | } |
| 574 | if( pSet->n<N_OR_COST ){ |
| 575 | p = &pSet->a[pSet->n++]; |
| 576 | p->nOut = nOut; |
| 577 | }else{ |
| 578 | p = pSet->a; |
| 579 | for(i=1; i<pSet->n; i++){ |
| 580 | if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 581 | } |
| 582 | if( p->rRun<=rRun ) return 0; |
| 583 | } |
| 584 | whereOrInsert_done: |
| 585 | p->prereq = prereq; |
| 586 | p->rRun = rRun; |
| 587 | if( p->nOut>nOut ) p->nOut = nOut; |
| 588 | return 1; |
| 589 | } |
| 590 | |
| 591 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 592 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 593 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 594 | static void whereClauseInit( |
| 595 | WhereClause *pWC, /* The WhereClause to be initialized */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 596 | WhereInfo *pWInfo /* The WHERE processing context */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 597 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 598 | pWC->pWInfo = pWInfo; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 599 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 600 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 601 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 602 | pWC->a = pWC->aStatic; |
| 603 | } |
| 604 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 605 | /* Forward reference */ |
| 606 | static void whereClauseClear(WhereClause*); |
| 607 | |
| 608 | /* |
| 609 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 610 | */ |
| 611 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 612 | whereClauseClear(&p->wc); |
| 613 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* |
| 617 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 618 | */ |
| 619 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 620 | whereClauseClear(&p->wc); |
| 621 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 622 | } |
| 623 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 624 | /* |
| 625 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 626 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 627 | */ |
| 628 | static void whereClauseClear(WhereClause *pWC){ |
| 629 | int i; |
| 630 | WhereTerm *a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 631 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 632 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 633 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 634 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 635 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 636 | if( a->wtFlags & TERM_ORINFO ){ |
| 637 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 638 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 639 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 640 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 641 | } |
| 642 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 643 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 647 | /* Forward declaration */ |
| 648 | static WhereCost whereCost(tRowcnt x); |
| 649 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 650 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 651 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 652 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 653 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 654 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 655 | ** allocation error. The memory allocation failure will be recorded in |
| 656 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 657 | ** |
| 658 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 659 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 660 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 661 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 662 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 663 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 664 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 665 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 666 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 667 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 668 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 669 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 670 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 671 | int idx; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 672 | testcase( wtFlags & TERM_VIRTUAL ); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 673 | if( pWC->nTerm>=pWC->nSlot ){ |
| 674 | WhereTerm *pOld = pWC->a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 675 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 676 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 677 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 678 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 679 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 680 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 681 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 682 | return 0; |
| 683 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 684 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 685 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 686 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 687 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 688 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 689 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 690 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 691 | if( p && ExprHasAnyProperty(p, EP_Hint) ){ |
| 692 | pTerm->truthProb = whereCost(p->iTable) - 99; |
| 693 | }else{ |
| 694 | pTerm->truthProb = -1; |
| 695 | } |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 696 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 697 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 698 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 699 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 700 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 701 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 702 | |
| 703 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 704 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 705 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 706 | ** operator specified in the op parameter. The WhereClause structure |
| 707 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 708 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 709 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 710 | ** \________/ \_______________/ \________________/ |
| 711 | ** slot[0] slot[1] slot[2] |
| 712 | ** |
| 713 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 714 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 715 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 716 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 717 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 718 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 719 | */ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 720 | static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 721 | pWC->op = op; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 722 | if( pExpr==0 ) return; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 723 | if( pExpr->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 724 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 725 | }else{ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 726 | whereSplit(pWC, pExpr->pLeft, op); |
| 727 | whereSplit(pWC, pExpr->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 728 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 732 | ** Initialize a WhereMaskSet object |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 733 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 734 | #define initMaskSet(P) (P)->n=0 |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 735 | |
| 736 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 737 | ** Return the bitmask for the given cursor number. Return 0 if |
| 738 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 739 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 740 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 741 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 742 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 743 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 744 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 745 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 746 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 747 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 752 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 753 | ** |
| 754 | ** There is one cursor per table in the FROM clause. The number of |
| 755 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 756 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 757 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 758 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 759 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 760 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 761 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /* |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 765 | ** These routines walk (recursively) an expression tree and generate |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 766 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 767 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 768 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 769 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 770 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 771 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 772 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 773 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 774 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 775 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 776 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 777 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 778 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 779 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 780 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 781 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 782 | }else{ |
| 783 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 784 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 785 | return mask; |
| 786 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 787 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 788 | int i; |
| 789 | Bitmask mask = 0; |
| 790 | if( pList ){ |
| 791 | for(i=0; i<pList->nExpr; i++){ |
| 792 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 793 | } |
| 794 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 795 | return mask; |
| 796 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 797 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 798 | Bitmask mask = 0; |
| 799 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 800 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 801 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 802 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 803 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 804 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 805 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 806 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 807 | int i; |
| 808 | for(i=0; i<pSrc->nSrc; i++){ |
| 809 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 810 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 811 | } |
| 812 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 813 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 814 | } |
| 815 | return mask; |
| 816 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 817 | |
| 818 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 819 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 820 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 821 | ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 822 | */ |
| 823 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 824 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 825 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 826 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 827 | assert( TK_GE==TK_EQ+4 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 828 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /* |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 832 | ** Swap two objects of type TYPE. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 833 | */ |
| 834 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 835 | |
| 836 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 837 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 838 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 839 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 840 | ** If left/right precedence rules come into play when determining the |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 841 | ** collating sequence, then COLLATE operators are adjusted to ensure |
| 842 | ** that the collating sequence does not change. For example: |
| 843 | ** "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] | 844 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 845 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 846 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 847 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 848 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 849 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 850 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 851 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 852 | if( expRight==expLeft ){ |
| 853 | /* Either X and Y both have COLLATE operator or neither do */ |
| 854 | if( expRight ){ |
| 855 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 856 | ** used by clearing the EP_Collate flag from Y. */ |
| 857 | pExpr->pRight->flags &= ~EP_Collate; |
| 858 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 859 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 860 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 861 | ** it to be searched first. */ |
| 862 | pExpr->pLeft->flags |= EP_Collate; |
| 863 | } |
| 864 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 865 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 866 | if( pExpr->op>=TK_GT ){ |
| 867 | assert( TK_LT==TK_GT+2 ); |
| 868 | assert( TK_GE==TK_LE+2 ); |
| 869 | assert( TK_GT>TK_EQ ); |
| 870 | assert( TK_GT<TK_LE ); |
| 871 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 872 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 873 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 877 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 878 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 879 | static u16 operatorMask(int op){ |
| 880 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 881 | assert( allowedOp(op) ); |
| 882 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 883 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 884 | }else if( op==TK_ISNULL ){ |
| 885 | c = WO_ISNULL; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 886 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 887 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 888 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 889 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 890 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 891 | assert( op!=TK_IN || c==WO_IN ); |
| 892 | assert( op!=TK_EQ || c==WO_EQ ); |
| 893 | assert( op!=TK_LT || c==WO_LT ); |
| 894 | assert( op!=TK_LE || c==WO_LE ); |
| 895 | assert( op!=TK_GT || c==WO_GT ); |
| 896 | assert( op!=TK_GE || c==WO_GE ); |
| 897 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 901 | ** Advance to the next WhereTerm that matches according to the criteria |
| 902 | ** established when the pScan object was initialized by whereScanInit(). |
| 903 | ** Return NULL if there are no more matching WhereTerms. |
| 904 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 905 | static WhereTerm *whereScanNext(WhereScan *pScan){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 906 | int iCur; /* The cursor on the LHS of the term */ |
| 907 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 908 | Expr *pX; /* An expression being tested */ |
| 909 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 910 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 911 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 912 | |
| 913 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 914 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 915 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 916 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 917 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 918 | if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){ |
| 919 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 920 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 921 | ){ |
| 922 | int j; |
| 923 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 924 | assert( pX->op==TK_COLUMN ); |
| 925 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 926 | if( pScan->aEquiv[j]==pX->iTable |
| 927 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 928 | break; |
| 929 | } |
| 930 | } |
| 931 | if( j==pScan->nEquiv ){ |
| 932 | pScan->aEquiv[j] = pX->iTable; |
| 933 | pScan->aEquiv[j+1] = pX->iColumn; |
| 934 | pScan->nEquiv += 2; |
| 935 | } |
| 936 | } |
| 937 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 938 | /* Verify the affinity and collating sequence match */ |
| 939 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 940 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 941 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 942 | pX = pTerm->pExpr; |
| 943 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 944 | continue; |
| 945 | } |
| 946 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 947 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 948 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 949 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 950 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 951 | continue; |
| 952 | } |
| 953 | } |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 954 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 955 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 956 | && pX->iTable==pScan->aEquiv[0] |
| 957 | && pX->iColumn==pScan->aEquiv[1] |
| 958 | ){ |
| 959 | continue; |
| 960 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 961 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 962 | return pTerm; |
| 963 | } |
| 964 | } |
| 965 | } |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 966 | pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 967 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 968 | } |
| 969 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 970 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 971 | pScan->iEquiv += 2; |
| 972 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 978 | ** first match. Return NULL if there are no matches. |
| 979 | ** |
| 980 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 981 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 982 | ** iCur. The <op> must be one of the operators described by opMask. |
| 983 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 984 | ** If the search is for X and the WHERE clause contains terms of the |
| 985 | ** form X=Y then this routine might also return terms of the form |
| 986 | ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 987 | ** but is enough to handle most commonly occurring SQL statements. |
| 988 | ** |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 989 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 990 | ** index pIdx. |
| 991 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 992 | static WhereTerm *whereScanInit( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 993 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 994 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 995 | int iCur, /* Cursor to scan for */ |
| 996 | int iColumn, /* Column to scan for */ |
| 997 | u32 opMask, /* Operator(s) to scan for */ |
| 998 | Index *pIdx /* Must be compatible with this index */ |
| 999 | ){ |
| 1000 | int j; |
| 1001 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 1002 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1003 | pScan->pOrigWC = pWC; |
| 1004 | pScan->pWC = pWC; |
| 1005 | if( pIdx && iColumn>=0 ){ |
| 1006 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 1007 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 1008 | if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 1009 | } |
| 1010 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 1011 | }else{ |
| 1012 | pScan->idxaff = 0; |
| 1013 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1014 | } |
| 1015 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 1016 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1017 | pScan->aEquiv[0] = iCur; |
| 1018 | pScan->aEquiv[1] = iColumn; |
| 1019 | pScan->nEquiv = 2; |
| 1020 | pScan->iEquiv = 2; |
| 1021 | return whereScanNext(pScan); |
| 1022 | } |
| 1023 | |
| 1024 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1025 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 1026 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 1027 | ** the WO_xx operator codes specified by the op parameter. |
| 1028 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 1029 | ** |
| 1030 | ** The term returned might by Y=<expr> if there is another constraint in |
| 1031 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 1032 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 1033 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 1034 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 1035 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 1036 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 1037 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 1038 | ** and ... and A9=A10 and A10=<expr>. |
| 1039 | ** |
| 1040 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 1041 | ** then try for the one with no dependencies on <expr> - in other words where |
| 1042 | ** <expr> is a constant expression of some kind. Only return entries of |
| 1043 | ** 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] | 1044 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 1045 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1046 | */ |
| 1047 | static WhereTerm *findTerm( |
| 1048 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 1049 | int iCur, /* Cursor number of LHS */ |
| 1050 | int iColumn, /* Column number of LHS */ |
| 1051 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 1052 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1053 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 1054 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1055 | WhereTerm *pResult = 0; |
| 1056 | WhereTerm *p; |
| 1057 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1058 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1059 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 1060 | while( p ){ |
| 1061 | if( (p->prereqRight & notReady)==0 ){ |
| 1062 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 1063 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1064 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1065 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1066 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1067 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1068 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1069 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1072 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1073 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1074 | |
| 1075 | /* |
| 1076 | ** Call exprAnalyze on all terms in a WHERE clause. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1077 | */ |
| 1078 | static void exprAnalyzeAll( |
| 1079 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1080 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 1081 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1082 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1083 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1084 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1088 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1089 | /* |
| 1090 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 1091 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 1092 | ** so and false if not. |
| 1093 | ** |
| 1094 | ** In order for the operator to be optimizible, the RHS must be a string |
| 1095 | ** literal that does not begin with a wildcard. |
| 1096 | */ |
| 1097 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1098 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1099 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1100 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1101 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 1102 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1103 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1104 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 1105 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 1106 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 1107 | int c; /* One character in z[] */ |
| 1108 | int cnt; /* Number of non-wildcard prefix characters */ |
| 1109 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 1110 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1111 | sqlite3_value *pVal = 0; |
| 1112 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1113 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1114 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1117 | #ifdef SQLITE_EBCDIC |
| 1118 | if( *pnoCase ) return 0; |
| 1119 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1120 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1121 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 1122 | if( pLeft->op!=TK_COLUMN |
| 1123 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 1124 | || IsVirtual(pLeft->pTab) |
| 1125 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1126 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 1127 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1128 | return 0; |
| 1129 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1130 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1131 | |
| 1132 | pRight = pList->a[0].pExpr; |
| 1133 | op = pRight->op; |
| 1134 | if( op==TK_REGISTER ){ |
| 1135 | op = pRight->op2; |
| 1136 | } |
| 1137 | if( op==TK_VARIABLE ){ |
| 1138 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 1139 | int iCol = pRight->iColumn; |
drh | cf0fd4a | 2013-08-01 12:21:58 +0000 | [diff] [blame] | 1140 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1141 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 1142 | z = (char *)sqlite3_value_text(pVal); |
| 1143 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1144 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1145 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 1146 | }else if( op==TK_STRING ){ |
| 1147 | z = pRight->u.zToken; |
| 1148 | } |
| 1149 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1150 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1151 | 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] | 1152 | cnt++; |
| 1153 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1154 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1155 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1156 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1157 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 1158 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 1159 | *ppPrefix = pPrefix; |
| 1160 | if( op==TK_VARIABLE ){ |
| 1161 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1162 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1163 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 1164 | /* If the rhs of the LIKE expression is a variable, and the current |
| 1165 | ** value of the variable means there is no need to invoke the LIKE |
| 1166 | ** function, then no OP_Variable will be added to the program. |
| 1167 | ** This causes problems for the sqlite3_bind_parameter_name() |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1168 | ** API. To workaround them, add a dummy OP_Variable here. |
| 1169 | */ |
| 1170 | int r1 = sqlite3GetTempReg(pParse); |
| 1171 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1172 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1173 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | }else{ |
| 1177 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1178 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1179 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1180 | |
| 1181 | sqlite3ValueFree(pVal); |
| 1182 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1183 | } |
| 1184 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 1185 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1186 | |
| 1187 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1188 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1189 | ** Check to see if the given expression is of the form |
| 1190 | ** |
| 1191 | ** column MATCH expr |
| 1192 | ** |
| 1193 | ** If it is then return TRUE. If not, return FALSE. |
| 1194 | */ |
| 1195 | static int isMatchOfColumn( |
| 1196 | Expr *pExpr /* Test this expression */ |
| 1197 | ){ |
| 1198 | ExprList *pList; |
| 1199 | |
| 1200 | if( pExpr->op!=TK_FUNCTION ){ |
| 1201 | return 0; |
| 1202 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1203 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1206 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1207 | if( pList->nExpr!=2 ){ |
| 1208 | return 0; |
| 1209 | } |
| 1210 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 1211 | return 0; |
| 1212 | } |
| 1213 | return 1; |
| 1214 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1215 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1216 | |
| 1217 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1218 | ** If the pBase expression originated in the ON or USING clause of |
| 1219 | ** a join, then transfer the appropriate markings over to derived. |
| 1220 | */ |
| 1221 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1222 | if( pDerived ){ |
| 1223 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 1224 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 1225 | } |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1228 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 1229 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1230 | ** Analyze a term that consists of two or more OR-connected |
| 1231 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1232 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1233 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 1234 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1235 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1236 | ** This routine analyzes terms such as the middle term in the above example. |
| 1237 | ** A WhereOrTerm object is computed and attached to the term under |
| 1238 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1239 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1240 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 1241 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1242 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1243 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 1244 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1245 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1246 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1247 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 1248 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 1249 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 1250 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 1251 | ** (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] | 1252 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1253 | ** CASE 1: |
| 1254 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1255 | ** 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] | 1256 | ** a single table T (as shown in example B above) then create a new virtual |
| 1257 | ** term that is an equivalent IN expression. In other words, if the term |
| 1258 | ** being analyzed is: |
| 1259 | ** |
| 1260 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 1261 | ** |
| 1262 | ** then create a new virtual term like this: |
| 1263 | ** |
| 1264 | ** x IN (expr1,expr2,expr3) |
| 1265 | ** |
| 1266 | ** CASE 2: |
| 1267 | ** |
| 1268 | ** If all subterms are indexable by a single table T, then set |
| 1269 | ** |
| 1270 | ** WhereTerm.eOperator = WO_OR |
| 1271 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 1272 | ** |
| 1273 | ** A subterm is "indexable" if it is of the form |
| 1274 | ** "T.C <op> <expr>" where C is any column of table T and |
| 1275 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 1276 | ** A subterm is also indexable if it is an AND of two or more |
| 1277 | ** subsubterms at least one of which is indexable. Indexable AND |
| 1278 | ** subterms have their eOperator set to WO_AND and they have |
| 1279 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 1280 | ** |
| 1281 | ** From another point of view, "indexable" means that the subterm could |
| 1282 | ** potentially be used with an index if an appropriate index exists. |
| 1283 | ** This analysis does not consider whether or not the index exists; that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 1284 | ** is decided elsewhere. This analysis only looks at whether subterms |
| 1285 | ** appropriate for indexing exist. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1286 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 1287 | ** All examples A through E above satisfy case 2. But if a term |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1288 | ** also statisfies case 1 (such as B) we know that the optimizer will |
| 1289 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 1290 | ** satisfied. |
| 1291 | ** |
| 1292 | ** It might be the case that multiple tables are indexable. For example, |
| 1293 | ** (E) above is indexable on tables P, Q, and R. |
| 1294 | ** |
| 1295 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 1296 | ** separate indices to find rowids for each subterm and composing |
| 1297 | ** the union of all rowids using a RowSet object. This is similar |
| 1298 | ** to "bitmap indices" in other database engines. |
| 1299 | ** |
| 1300 | ** OTHERWISE: |
| 1301 | ** |
| 1302 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 1303 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1304 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1305 | static void exprAnalyzeOrTerm( |
| 1306 | SrcList *pSrc, /* the FROM clause */ |
| 1307 | WhereClause *pWC, /* the complete WHERE clause */ |
| 1308 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 1309 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1310 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 1311 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1312 | sqlite3 *db = pParse->db; /* Database connection */ |
| 1313 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 1314 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1315 | int i; /* Loop counters */ |
| 1316 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 1317 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 1318 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 1319 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 1320 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1321 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1322 | /* |
| 1323 | ** Break the OR clause into its separate subterms. The subterms are |
| 1324 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 1325 | ** object that is attached to the original OR clause term. |
| 1326 | */ |
| 1327 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 1328 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 1329 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1330 | if( pOrInfo==0 ) return; |
| 1331 | pTerm->wtFlags |= TERM_ORINFO; |
| 1332 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1333 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1334 | whereSplit(pOrWc, pExpr, TK_OR); |
| 1335 | exprAnalyzeAll(pSrc, pOrWc); |
| 1336 | if( db->mallocFailed ) return; |
| 1337 | assert( pOrWc->nTerm>=2 ); |
| 1338 | |
| 1339 | /* |
| 1340 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 1341 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 1342 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1343 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1344 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 1345 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1346 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1347 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1348 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1349 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 1350 | if( pAndInfo ){ |
| 1351 | WhereClause *pAndWC; |
| 1352 | WhereTerm *pAndTerm; |
| 1353 | int j; |
| 1354 | Bitmask b = 0; |
| 1355 | pOrTerm->u.pAndInfo = pAndInfo; |
| 1356 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 1357 | pOrTerm->eOperator = WO_AND; |
| 1358 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1359 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1360 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 1361 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 1362 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 1363 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1364 | if( !db->mallocFailed ){ |
| 1365 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 1366 | assert( pAndTerm->pExpr ); |
| 1367 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1368 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1369 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | indexable &= b; |
| 1373 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1374 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 1375 | /* Skip this term for now. We revisit it when we process the |
| 1376 | ** corresponding TERM_VIRTUAL term */ |
| 1377 | }else{ |
| 1378 | Bitmask b; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1379 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1380 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 1381 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1382 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1383 | } |
| 1384 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1385 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1386 | chngToIN = 0; |
| 1387 | }else{ |
| 1388 | chngToIN &= b; |
| 1389 | } |
| 1390 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1391 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1392 | |
| 1393 | /* |
| 1394 | ** Record the set of tables that satisfy case 2. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1395 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1396 | */ |
| 1397 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1398 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1399 | |
| 1400 | /* |
| 1401 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 1402 | ** we have to do some additional checking to see if case 1 really |
| 1403 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1404 | ** |
| 1405 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 1406 | ** that there is no possibility of transforming the OR clause into an |
| 1407 | ** IN operator because one or more terms in the OR clause contain |
| 1408 | ** something other than == on a column in the single table. The 1-bit |
| 1409 | ** case means that every term of the OR clause is of the form |
| 1410 | ** "table.column=expr" for some single table. The one bit that is set |
| 1411 | ** will correspond to the common table. We still need to check to make |
| 1412 | ** sure the same column is used on all terms. The 2-bit case is when |
| 1413 | ** the all terms are of the form "table1.column=table2.column". It |
| 1414 | ** might be possible to form an IN operator with either table1.column |
| 1415 | ** or table2.column as the LHS if either is common to every term of |
| 1416 | ** the OR clause. |
| 1417 | ** |
| 1418 | ** Note that terms of the form "table.column1=table.column2" (the |
| 1419 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1420 | */ |
| 1421 | if( chngToIN ){ |
| 1422 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 1423 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 1424 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1425 | int j = 0; /* Loop counter */ |
| 1426 | |
| 1427 | /* Search for a table and column that appears on one side or the |
| 1428 | ** other of the == operator in every subterm. That table and column |
| 1429 | ** will be recorded in iCursor and iColumn. There might not be any |
| 1430 | ** such table and column. Set okToChngToIN if an appropriate table |
| 1431 | ** and column is found but leave okToChngToIN false if not found. |
| 1432 | */ |
| 1433 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 1434 | pOrTerm = pOrWc->a; |
| 1435 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1436 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1437 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1438 | if( pOrTerm->leftCursor==iCursor ){ |
| 1439 | /* This is the 2-bit case and we are on the second iteration and |
| 1440 | ** current term is from the first iteration. So skip this term. */ |
| 1441 | assert( j==1 ); |
| 1442 | continue; |
| 1443 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1444 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1445 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 1446 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 1447 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 1448 | ** and use its inversion. */ |
| 1449 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 1450 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 1451 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 1452 | continue; |
| 1453 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1454 | iColumn = pOrTerm->u.leftColumn; |
| 1455 | iCursor = pOrTerm->leftCursor; |
| 1456 | break; |
| 1457 | } |
| 1458 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1459 | /* No candidate table+column was found. This can only occur |
| 1460 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1461 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1462 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1463 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1464 | break; |
| 1465 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1466 | testcase( j==1 ); |
| 1467 | |
| 1468 | /* We have found a candidate table and column. Check to see if that |
| 1469 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1470 | okToChngToIN = 1; |
| 1471 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1472 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1473 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1474 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1475 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1476 | okToChngToIN = 0; |
| 1477 | }else{ |
| 1478 | int affLeft, affRight; |
| 1479 | /* If the right-hand side is also a column, then the affinities |
| 1480 | ** of both right and left sides must be such that no type |
| 1481 | ** conversions are required on the right. (Ticket #2249) |
| 1482 | */ |
| 1483 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1484 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1485 | if( affRight!=0 && affRight!=affLeft ){ |
| 1486 | okToChngToIN = 0; |
| 1487 | }else{ |
| 1488 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1495 | ** case 1. In that case, construct a new virtual term that is |
| 1496 | ** pTerm converted into an IN operator. |
| 1497 | */ |
| 1498 | if( okToChngToIN ){ |
| 1499 | Expr *pDup; /* A transient duplicate expression */ |
| 1500 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1501 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1502 | Expr *pNew; /* The complete IN operator */ |
| 1503 | |
| 1504 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1505 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1506 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1507 | assert( pOrTerm->leftCursor==iCursor ); |
| 1508 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1509 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1510 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1511 | pLeft = pOrTerm->pExpr->pLeft; |
| 1512 | } |
| 1513 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1514 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1515 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1516 | if( pNew ){ |
| 1517 | int idxNew; |
| 1518 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1519 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1520 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1521 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1522 | testcase( idxNew==0 ); |
| 1523 | exprAnalyze(pSrc, pWC, idxNew); |
| 1524 | pTerm = &pWC->a[idxTerm]; |
| 1525 | pWC->a[idxNew].iParent = idxTerm; |
| 1526 | pTerm->nChild = 1; |
| 1527 | }else{ |
| 1528 | sqlite3ExprListDelete(db, pList); |
| 1529 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1530 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1531 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1532 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1533 | } |
| 1534 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1535 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1536 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1537 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1538 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1539 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1540 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1541 | ** |
| 1542 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1543 | ** to the standard form of "X <op> <expr>". |
| 1544 | ** |
| 1545 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1546 | ** columns, then the original expression is unchanged and a new virtual |
| 1547 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1548 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1549 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1550 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1551 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1552 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1553 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1554 | static void exprAnalyze( |
| 1555 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1556 | WhereClause *pWC, /* the WHERE clause */ |
| 1557 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1558 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1559 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1560 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1561 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1562 | Expr *pExpr; /* The expression to be analyzed */ |
| 1563 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1564 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1565 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1566 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1567 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1568 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1569 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1570 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1571 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1572 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1573 | if( db->mallocFailed ){ |
| 1574 | return; |
| 1575 | } |
| 1576 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1577 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1578 | pExpr = pTerm->pExpr; |
| 1579 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1580 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1581 | op = pExpr->op; |
| 1582 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1583 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1584 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1585 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1586 | }else{ |
| 1587 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1588 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1589 | }else if( op==TK_ISNULL ){ |
| 1590 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1591 | }else{ |
| 1592 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1593 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1594 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1595 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1596 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1597 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1598 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1599 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1600 | } |
| 1601 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1602 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1603 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1604 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1605 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1606 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1607 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1608 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1609 | if( pLeft->op==TK_COLUMN ){ |
| 1610 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1611 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1612 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1613 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1614 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1615 | WhereTerm *pNew; |
| 1616 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1617 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1618 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1619 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1620 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1621 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1622 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1623 | return; |
| 1624 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1625 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1626 | if( idxNew==0 ) return; |
| 1627 | pNew = &pWC->a[idxNew]; |
| 1628 | pNew->iParent = idxTerm; |
| 1629 | pTerm = &pWC->a[idxTerm]; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1630 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1631 | pTerm->wtFlags |= TERM_COPIED; |
drh | eb5bc92 | 2013-01-17 16:43:33 +0000 | [diff] [blame] | 1632 | if( pExpr->op==TK_EQ |
| 1633 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1634 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 1635 | ){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1636 | pTerm->eOperator |= WO_EQUIV; |
| 1637 | eExtraOp = WO_EQUIV; |
| 1638 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1639 | }else{ |
| 1640 | pDup = pExpr; |
| 1641 | pNew = pTerm; |
| 1642 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1643 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1644 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1645 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1646 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1647 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1648 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1649 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1650 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1651 | } |
| 1652 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1653 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1654 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1655 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1656 | ** that define the range that the BETWEEN implements. For example: |
| 1657 | ** |
| 1658 | ** a BETWEEN b AND c |
| 1659 | ** |
| 1660 | ** is converted into: |
| 1661 | ** |
| 1662 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1663 | ** |
| 1664 | ** The two new terms are added onto the end of the WhereClause object. |
| 1665 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1666 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1667 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1668 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1669 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1670 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1671 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1672 | int i; |
| 1673 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1674 | assert( pList!=0 ); |
| 1675 | assert( pList->nExpr==2 ); |
| 1676 | for(i=0; i<2; i++){ |
| 1677 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1678 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1679 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1680 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1681 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1682 | transferJoinMarkings(pNewExpr, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1683 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1684 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1685 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1686 | pTerm = &pWC->a[idxTerm]; |
| 1687 | pWC->a[idxNew].iParent = idxTerm; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1688 | } |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1689 | pTerm->nChild = 2; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1690 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1691 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1692 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1693 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1694 | /* Analyze a term that is composed of two or more subterms connected by |
| 1695 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1696 | */ |
| 1697 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1698 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1699 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1700 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1701 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1702 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1703 | |
| 1704 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1705 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1706 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1707 | ** |
| 1708 | ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 1709 | ** |
| 1710 | ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 1711 | ** |
| 1712 | ** The last character of the prefix "abc" is incremented to form the |
shane | 7bc71e5 | 2008-05-28 18:01:44 +0000 | [diff] [blame] | 1713 | ** termination condition "abd". |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1714 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1715 | if( pWC->op==TK_AND |
| 1716 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1717 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1718 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1719 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1720 | Expr *pNewExpr1; |
| 1721 | Expr *pNewExpr2; |
| 1722 | int idxNew1; |
| 1723 | int idxNew2; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1724 | Token sCollSeqName; /* Name of collating sequence */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1725 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1726 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1727 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1728 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1729 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1730 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1731 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1732 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1733 | /* The point is to increment the last character before the first |
| 1734 | ** wildcard. But if we increment '@', that will push it into the |
| 1735 | ** alphabetic range where case conversions will mess up the |
| 1736 | ** inequality. To avoid this, make sure to also run the full |
| 1737 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1738 | */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 1739 | if( c=='A'-1 ) isComplete = 0; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1740 | c = sqlite3UpperToLower[c]; |
| 1741 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1742 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1743 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1744 | sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 1745 | sCollSeqName.n = 6; |
| 1746 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1747 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1748 | sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1749 | pStr1, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1750 | transferJoinMarkings(pNewExpr1, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1751 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1752 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1753 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1754 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1755 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1756 | sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1757 | pStr2, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1758 | transferJoinMarkings(pNewExpr2, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1759 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1760 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1761 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1762 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1763 | if( isComplete ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1764 | pWC->a[idxNew1].iParent = idxTerm; |
| 1765 | pWC->a[idxNew2].iParent = idxTerm; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1766 | pTerm->nChild = 2; |
| 1767 | } |
| 1768 | } |
| 1769 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1770 | |
| 1771 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1772 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1773 | ** current expression is of the form: column MATCH expr. |
| 1774 | ** This information is used by the xBestIndex methods of |
| 1775 | ** virtual tables. The native query optimizer does not attempt |
| 1776 | ** to do anything with MATCH functions. |
| 1777 | */ |
| 1778 | if( isMatchOfColumn(pExpr) ){ |
| 1779 | int idxNew; |
| 1780 | Expr *pRight, *pLeft; |
| 1781 | WhereTerm *pNewTerm; |
| 1782 | Bitmask prereqColumn, prereqExpr; |
| 1783 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1784 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1785 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1786 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1787 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1788 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1789 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1790 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1791 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1792 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1793 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1794 | pNewTerm = &pWC->a[idxNew]; |
| 1795 | pNewTerm->prereqRight = prereqExpr; |
| 1796 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1797 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1798 | pNewTerm->eOperator = WO_MATCH; |
| 1799 | pNewTerm->iParent = idxTerm; |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1800 | pTerm = &pWC->a[idxTerm]; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1801 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1802 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1803 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1804 | } |
| 1805 | } |
| 1806 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1807 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1808 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1809 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1810 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1811 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1812 | ** virtual term of that form. |
| 1813 | ** |
| 1814 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 1815 | ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 1816 | ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 1817 | ** the start of the loop will prevent any results from being returned. |
| 1818 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1819 | if( pExpr->op==TK_NOTNULL |
| 1820 | && pExpr->pLeft->op==TK_COLUMN |
| 1821 | && pExpr->pLeft->iColumn>=0 |
drh | 40aa936 | 2013-06-28 17:29:25 +0000 | [diff] [blame] | 1822 | && OptimizationEnabled(db, SQLITE_Stat3) |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1823 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1824 | Expr *pNewExpr; |
| 1825 | Expr *pLeft = pExpr->pLeft; |
| 1826 | int idxNew; |
| 1827 | WhereTerm *pNewTerm; |
| 1828 | |
| 1829 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1830 | sqlite3ExprDup(db, pLeft, 0), |
| 1831 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1832 | |
| 1833 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1834 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1835 | if( idxNew ){ |
| 1836 | pNewTerm = &pWC->a[idxNew]; |
| 1837 | pNewTerm->prereqRight = 0; |
| 1838 | pNewTerm->leftCursor = pLeft->iTable; |
| 1839 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1840 | pNewTerm->eOperator = WO_GT; |
| 1841 | pNewTerm->iParent = idxTerm; |
| 1842 | pTerm = &pWC->a[idxTerm]; |
| 1843 | pTerm->nChild = 1; |
| 1844 | pTerm->wtFlags |= TERM_COPIED; |
| 1845 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1846 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1847 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1848 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1849 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1850 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1851 | ** an index for tables to the left of the join. |
| 1852 | */ |
| 1853 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1856 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1857 | ** This function searches pList for a entry that matches the iCol-th column |
| 1858 | ** of index pIdx. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1859 | ** |
| 1860 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1861 | ** no expression is found, -1 is returned. |
| 1862 | */ |
| 1863 | static int findIndexCol( |
| 1864 | Parse *pParse, /* Parse context */ |
| 1865 | ExprList *pList, /* Expression list to search */ |
| 1866 | int iBase, /* Cursor for table associated with pIdx */ |
| 1867 | Index *pIdx, /* Index to match column of */ |
| 1868 | int iCol /* Column of index to match */ |
| 1869 | ){ |
| 1870 | int i; |
| 1871 | const char *zColl = pIdx->azColl[iCol]; |
| 1872 | |
| 1873 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1874 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1875 | if( p->op==TK_COLUMN |
| 1876 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1877 | && p->iTable==iBase |
| 1878 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1879 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1880 | if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1881 | return i; |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | return -1; |
| 1887 | } |
| 1888 | |
| 1889 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1890 | ** Return true if the DISTINCT expression-list passed as the third argument |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1891 | ** is redundant. |
| 1892 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1893 | ** A DISTINCT list is redundant if the database contains some subset of |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1894 | ** columns that are unique and non-null. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1895 | */ |
| 1896 | static int isDistinctRedundant( |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1897 | Parse *pParse, /* Parsing context */ |
| 1898 | SrcList *pTabList, /* The FROM clause */ |
| 1899 | WhereClause *pWC, /* The WHERE clause */ |
| 1900 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1901 | ){ |
| 1902 | Table *pTab; |
| 1903 | Index *pIdx; |
| 1904 | int i; |
| 1905 | int iBase; |
| 1906 | |
| 1907 | /* If there is more than one table or sub-select in the FROM clause of |
| 1908 | ** this query, then it will not be possible to show that the DISTINCT |
| 1909 | ** clause is redundant. */ |
| 1910 | if( pTabList->nSrc!=1 ) return 0; |
| 1911 | iBase = pTabList->a[0].iCursor; |
| 1912 | pTab = pTabList->a[0].pTab; |
| 1913 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1914 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1915 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1916 | ** current SELECT is a correlated sub-query. |
| 1917 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1918 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1919 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1920 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | /* Loop through all indices on the table, checking each to see if it makes |
| 1924 | ** the DISTINCT qualifier redundant. It does so if: |
| 1925 | ** |
| 1926 | ** 1. The index is itself UNIQUE, and |
| 1927 | ** |
| 1928 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1929 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1930 | ** where X is a constant value. The collation sequences of the |
| 1931 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1932 | ** |
| 1933 | ** 3. All of those index columns for which the WHERE clause does not |
| 1934 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1935 | */ |
| 1936 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1937 | if( pIdx->onError==OE_None ) continue; |
| 1938 | for(i=0; i<pIdx->nColumn; i++){ |
| 1939 | int iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1940 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1941 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
| 1942 | if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){ |
| 1943 | break; |
| 1944 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1945 | } |
| 1946 | } |
| 1947 | if( i==pIdx->nColumn ){ |
| 1948 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1949 | return 1; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | return 0; |
| 1954 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1955 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1956 | /* |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 1957 | ** Find (an approximate) sum of two WhereCosts. This computation is |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1958 | ** not a simple "+" operator because WhereCost is stored as a logarithmic |
| 1959 | ** value. |
| 1960 | ** |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1961 | */ |
| 1962 | static WhereCost whereCostAdd(WhereCost a, WhereCost b){ |
| 1963 | static const unsigned char x[] = { |
| 1964 | 10, 10, /* 0,1 */ |
| 1965 | 9, 9, /* 2,3 */ |
| 1966 | 8, 8, /* 4,5 */ |
| 1967 | 7, 7, 7, /* 6,7,8 */ |
| 1968 | 6, 6, 6, /* 9,10,11 */ |
| 1969 | 5, 5, 5, /* 12-14 */ |
| 1970 | 4, 4, 4, 4, /* 15-18 */ |
| 1971 | 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 1972 | 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 1973 | }; |
| 1974 | if( a>=b ){ |
| 1975 | if( a>b+49 ) return a; |
| 1976 | if( a>b+31 ) return a+1; |
| 1977 | return a+x[a-b]; |
| 1978 | }else{ |
| 1979 | if( b>a+49 ) return b; |
| 1980 | if( b>a+31 ) return b+1; |
| 1981 | return b+x[b-a]; |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1986 | ** Convert an integer into a WhereCost. In other words, compute a |
| 1987 | ** good approximatation for 10*log2(x). |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1988 | */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 1989 | static WhereCost whereCost(tRowcnt x){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1990 | static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 1991 | WhereCost y = 40; |
| 1992 | if( x<8 ){ |
| 1993 | if( x<2 ) return 0; |
| 1994 | while( x<8 ){ y -= 10; x <<= 1; } |
| 1995 | }else{ |
| 1996 | while( x>255 ){ y += 40; x >>= 4; } |
| 1997 | while( x>15 ){ y += 10; x >>= 1; } |
| 1998 | } |
| 1999 | return a[x&7] + y - 10; |
| 2000 | } |
| 2001 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 2002 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2003 | /* |
| 2004 | ** Convert a double (as received from xBestIndex of a virtual table) |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2005 | ** into a WhereCost. In other words, compute an approximation for |
| 2006 | ** 10*log2(x). |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 2007 | */ |
| 2008 | static WhereCost whereCostFromDouble(double x){ |
| 2009 | u64 a; |
| 2010 | WhereCost e; |
| 2011 | assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 2012 | if( x<=1 ) return 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2013 | if( x<=2000000000 ) return whereCost((tRowcnt)x); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 2014 | memcpy(&a, &x, 8); |
| 2015 | e = (a>>52) - 1022; |
| 2016 | return e*10; |
| 2017 | } |
| 2018 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 2019 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2020 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2021 | ** Estimate the logarithm of the input value to base 2. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2022 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2023 | static WhereCost estLog(WhereCost N){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2024 | WhereCost x = whereCost(N); |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 2025 | return x>33 ? x - 33 : 0; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 2028 | /* |
| 2029 | ** Two routines for printing the content of an sqlite3_index_info |
| 2030 | ** structure. Used for testing and debugging only. If neither |
| 2031 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 2032 | ** are no-ops. |
| 2033 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 2034 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 2035 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 2036 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 2037 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 2038 | for(i=0; i<p->nConstraint; i++){ |
| 2039 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 2040 | i, |
| 2041 | p->aConstraint[i].iColumn, |
| 2042 | p->aConstraint[i].iTermOffset, |
| 2043 | p->aConstraint[i].op, |
| 2044 | p->aConstraint[i].usable); |
| 2045 | } |
| 2046 | for(i=0; i<p->nOrderBy; i++){ |
| 2047 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 2048 | i, |
| 2049 | p->aOrderBy[i].iColumn, |
| 2050 | p->aOrderBy[i].desc); |
| 2051 | } |
| 2052 | } |
| 2053 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 2054 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 2055 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 2056 | for(i=0; i<p->nConstraint; i++){ |
| 2057 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 2058 | i, |
| 2059 | p->aConstraintUsage[i].argvIndex, |
| 2060 | p->aConstraintUsage[i].omit); |
| 2061 | } |
| 2062 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 2063 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 2064 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 2065 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
| 2066 | } |
| 2067 | #else |
| 2068 | #define TRACE_IDX_INPUTS(A) |
| 2069 | #define TRACE_IDX_OUTPUTS(A) |
| 2070 | #endif |
| 2071 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2072 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2073 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2074 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 2075 | ** could be used with an index to access pSrc, assuming an appropriate |
| 2076 | ** index existed. |
| 2077 | */ |
| 2078 | static int termCanDriveIndex( |
| 2079 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 2080 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 2081 | Bitmask notReady /* Tables in outer loops of the join */ |
| 2082 | ){ |
| 2083 | char aff; |
| 2084 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2085 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2086 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 2087 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2088 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 2089 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 2090 | return 1; |
| 2091 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2092 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2093 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2094 | |
| 2095 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2096 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2097 | ** Generate code to construct the Index object for an automatic index |
| 2098 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 2099 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2100 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2101 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2102 | Parse *pParse, /* The parsing context */ |
| 2103 | WhereClause *pWC, /* The WHERE clause */ |
| 2104 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 2105 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 2106 | WhereLevel *pLevel /* Write new index here */ |
| 2107 | ){ |
| 2108 | int nColumn; /* Number of columns in the constructed index */ |
| 2109 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 2110 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 2111 | int nByte; /* Byte of memory needed for pIdx */ |
| 2112 | Index *pIdx; /* Object describing the transient index */ |
| 2113 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2114 | int addrInit; /* Address of the initialization bypass jump */ |
| 2115 | Table *pTable; /* The table being indexed */ |
| 2116 | KeyInfo *pKeyinfo; /* Key information for the index */ |
| 2117 | int addrTop; /* Top of the index fill loop */ |
| 2118 | int regRecord; /* Register holding an index record */ |
| 2119 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2120 | int i; /* Loop counter */ |
| 2121 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2122 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2123 | WhereLoop *pLoop; /* The Loop object */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2124 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 2125 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 2126 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2127 | |
| 2128 | /* Generate code to skip over the creation and initialization of the |
| 2129 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 2130 | v = pParse->pVdbe; |
| 2131 | assert( v!=0 ); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2132 | addrInit = sqlite3CodeOnce(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2133 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2134 | /* Count the number of columns that will be added to the index |
| 2135 | ** and used to match WHERE clause constraints */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2136 | nColumn = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2137 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2138 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2139 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2140 | idxCols = 0; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 2141 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2142 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 2143 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2144 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2145 | testcase( iCol==BMS ); |
| 2146 | testcase( iCol==BMS-1 ); |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 2147 | if( !sentWarning ){ |
| 2148 | sqlite3_log(SQLITE_WARNING_AUTOINDEX, |
| 2149 | "automatic index on %s(%s)", pTable->zName, |
| 2150 | pTable->aCol[iCol].zName); |
| 2151 | sentWarning = 1; |
| 2152 | } |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2153 | if( (idxCols & cMask)==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2154 | if( whereLoopResize(pParse->db, pLoop, nColumn+1) ) return; |
| 2155 | pLoop->aLTerm[nColumn++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2156 | idxCols |= cMask; |
| 2157 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | assert( nColumn>0 ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2161 | pLoop->u.btree.nEq = pLoop->nLTerm = nColumn; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 2162 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 2163 | | WHERE_AUTO_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2164 | |
| 2165 | /* Count the number of additional columns needed to create a |
| 2166 | ** covering index. A "covering index" is an index that contains all |
| 2167 | ** columns that are needed by the query. With a covering index, the |
| 2168 | ** original table never needs to be accessed. Automatic indices must |
| 2169 | ** be a covering index because the index will not be updated if the |
| 2170 | ** original table changes and the index and table cannot both be used |
| 2171 | ** if they go out of sync. |
| 2172 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2173 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2174 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2175 | testcase( pTable->nCol==BMS-1 ); |
| 2176 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2177 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2178 | if( extraCols & MASKBIT(i) ) nColumn++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2179 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2180 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2181 | nColumn += pTable->nCol - BMS + 1; |
| 2182 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2183 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2184 | |
| 2185 | /* Construct the Index object to describe this index */ |
| 2186 | nByte = sizeof(Index); |
| 2187 | nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 2188 | nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 2189 | nByte += nColumn; /* Index.aSortOrder */ |
| 2190 | pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 2191 | if( pIdx==0 ) return; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2192 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2193 | pIdx->azColl = (char**)&pIdx[1]; |
| 2194 | pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 2195 | pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 2196 | pIdx->zName = "auto-index"; |
| 2197 | pIdx->nColumn = nColumn; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2198 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2199 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2200 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2201 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2202 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2203 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2204 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 2205 | testcase( iCol==BMS-1 ); |
| 2206 | testcase( iCol==BMS ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2207 | if( (idxCols & cMask)==0 ){ |
| 2208 | Expr *pX = pTerm->pExpr; |
| 2209 | idxCols |= cMask; |
| 2210 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 2211 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 6f2e6c0 | 2011-02-17 13:33:15 +0000 | [diff] [blame] | 2212 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2213 | n++; |
| 2214 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2215 | } |
| 2216 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2217 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2218 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2219 | /* Add additional columns needed to make the automatic index into |
| 2220 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2221 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2222 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2223 | pIdx->aiColumn[n] = i; |
| 2224 | pIdx->azColl[n] = "BINARY"; |
| 2225 | n++; |
| 2226 | } |
| 2227 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2228 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2229 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 2230 | pIdx->aiColumn[n] = i; |
| 2231 | pIdx->azColl[n] = "BINARY"; |
| 2232 | n++; |
| 2233 | } |
| 2234 | } |
| 2235 | assert( n==nColumn ); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2236 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2237 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2238 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 2239 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 2240 | pLevel->iIdxCur = pParse->nTab++; |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2241 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2242 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2243 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2244 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2245 | /* Fill the automatic index with content */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2246 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); |
| 2247 | regRecord = sqlite3GetTempReg(pParse); |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 2248 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1, 0); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2249 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 2250 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2251 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2252 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2253 | sqlite3VdbeJumpHere(v, addrTop); |
| 2254 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 2255 | |
| 2256 | /* Jump here when skipping the initialization */ |
| 2257 | sqlite3VdbeJumpHere(v, addrInit); |
| 2258 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2259 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2260 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 2261 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2262 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2263 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 2264 | ** responsibility of the caller to eventually release the structure |
| 2265 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 2266 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2267 | static sqlite3_index_info *allocateIndexInfo( |
| 2268 | Parse *pParse, |
| 2269 | WhereClause *pWC, |
| 2270 | struct SrcList_item *pSrc, |
| 2271 | ExprList *pOrderBy |
| 2272 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2273 | int i, j; |
| 2274 | int nTerm; |
| 2275 | struct sqlite3_index_constraint *pIdxCons; |
| 2276 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 2277 | struct sqlite3_index_constraint_usage *pUsage; |
| 2278 | WhereTerm *pTerm; |
| 2279 | int nOrderBy; |
| 2280 | sqlite3_index_info *pIdxInfo; |
| 2281 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2282 | /* Count the number of possible WHERE clause constraints referring |
| 2283 | ** to this virtual table */ |
| 2284 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 2285 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2286 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2287 | testcase( pTerm->eOperator & WO_IN ); |
| 2288 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2289 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2290 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2291 | nTerm++; |
| 2292 | } |
| 2293 | |
| 2294 | /* If the ORDER BY clause contains only columns in the current |
| 2295 | ** virtual table then allocate space for the aOrderBy part of |
| 2296 | ** the sqlite3_index_info structure. |
| 2297 | */ |
| 2298 | nOrderBy = 0; |
| 2299 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2300 | int n = pOrderBy->nExpr; |
| 2301 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2302 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2303 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 2304 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2305 | if( i==n){ |
| 2306 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | /* Allocate the sqlite3_index_info structure |
| 2311 | */ |
| 2312 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 2313 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 2314 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 2315 | if( pIdxInfo==0 ){ |
| 2316 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2317 | return 0; |
| 2318 | } |
| 2319 | |
| 2320 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 2321 | ** many fields that are declared "const" to prevent xBestIndex from |
| 2322 | ** changing them. We have to do some funky casting in order to |
| 2323 | ** initialize those fields. |
| 2324 | */ |
| 2325 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 2326 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 2327 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 2328 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 2329 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 2330 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 2331 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 2332 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 2333 | pUsage; |
| 2334 | |
| 2335 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2336 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2337 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2338 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2339 | testcase( pTerm->eOperator & WO_IN ); |
| 2340 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2341 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2342 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2343 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 2344 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2345 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2346 | if( op==WO_IN ) op = WO_EQ; |
| 2347 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2348 | /* The direct assignment in the previous line is possible only because |
| 2349 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 2350 | ** following asserts verify this fact. */ |
| 2351 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 2352 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 2353 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 2354 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 2355 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 2356 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2357 | 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] | 2358 | j++; |
| 2359 | } |
| 2360 | for(i=0; i<nOrderBy; i++){ |
| 2361 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2362 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 2363 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 2364 | } |
| 2365 | |
| 2366 | return pIdxInfo; |
| 2367 | } |
| 2368 | |
| 2369 | /* |
| 2370 | ** The table object reference passed as the second argument to this function |
| 2371 | ** must represent a virtual table. This function invokes the xBestIndex() |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2372 | ** method of the virtual table with the sqlite3_index_info object that |
| 2373 | ** comes in as the 3rd argument to this function. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2374 | ** |
| 2375 | ** If an error occurs, pParse is populated with an error message and a |
| 2376 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 2377 | ** part of the sqlite3_index_info structure is left populated. |
| 2378 | ** |
| 2379 | ** Whether or not an error is returned, it is the responsibility of the |
| 2380 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 2381 | ** that this is required. |
| 2382 | */ |
| 2383 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 2384 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2385 | int i; |
| 2386 | int rc; |
| 2387 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2388 | TRACE_IDX_INPUTS(p); |
| 2389 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 2390 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2391 | |
| 2392 | if( rc!=SQLITE_OK ){ |
| 2393 | if( rc==SQLITE_NOMEM ){ |
| 2394 | pParse->db->mallocFailed = 1; |
| 2395 | }else if( !pVtab->zErrMsg ){ |
| 2396 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 2397 | }else{ |
| 2398 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 2399 | } |
| 2400 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2401 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2402 | pVtab->zErrMsg = 0; |
| 2403 | |
| 2404 | for(i=0; i<p->nConstraint; i++){ |
| 2405 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 2406 | sqlite3ErrorMsg(pParse, |
| 2407 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | return pParse->nErr; |
| 2412 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2413 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2414 | |
| 2415 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2416 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2417 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2418 | ** Estimate the location of a particular key among all keys in an |
| 2419 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2420 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2421 | ** aStat[0] Est. number of rows less than pVal |
| 2422 | ** aStat[1] Est. number of rows equal to pVal |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2423 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2424 | ** Return SQLITE_OK on success. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2425 | */ |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2426 | static void whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2427 | Parse *pParse, /* Database connection */ |
| 2428 | Index *pIdx, /* Index to consider domain of */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2429 | UnpackedRecord *pRec, /* Vector of values to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2430 | int roundUp, /* Round up if true. Round down if false */ |
| 2431 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2432 | ){ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 2433 | IndexSample *aSample = pIdx->aSample; |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 2434 | int iCol; /* Index of required stats in anEq[] etc. */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2435 | int iMin = 0; /* Smallest sample not yet tested */ |
| 2436 | int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */ |
| 2437 | int iTest; /* Next sample to test */ |
| 2438 | int res; /* Result of comparison operation */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2439 | |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 2440 | assert( pRec!=0 || pParse->db->mallocFailed ); |
| 2441 | if( pRec==0 ) return; |
| 2442 | iCol = pRec->nField - 1; |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2443 | assert( pIdx->nSample>0 ); |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 2444 | assert( pRec->nField>0 && iCol<pIdx->nSampleCol ); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2445 | do{ |
| 2446 | iTest = (iMin+i)/2; |
| 2447 | res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec); |
| 2448 | if( res<0 ){ |
| 2449 | iMin = iTest+1; |
| 2450 | }else{ |
| 2451 | i = iTest; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2452 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2453 | }while( res && iMin<i ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2454 | |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2455 | #ifdef SQLITE_DEBUG |
| 2456 | /* The following assert statements check that the binary search code |
| 2457 | ** above found the right answer. This block serves no purpose other |
| 2458 | ** than to invoke the asserts. */ |
| 2459 | if( res==0 ){ |
| 2460 | /* If (res==0) is true, then sample $i must be equal to pRec */ |
| 2461 | assert( i<pIdx->nSample ); |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 2462 | assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 2463 | || pParse->db->mallocFailed ); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2464 | }else{ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2465 | /* Otherwise, pRec must be smaller than sample $i and larger than |
| 2466 | ** sample ($i-1). */ |
| 2467 | assert( i==pIdx->nSample |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 2468 | || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 2469 | || pParse->db->mallocFailed ); |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2470 | assert( i==0 |
drh | 0e1f002 | 2013-08-16 14:49:00 +0000 | [diff] [blame] | 2471 | || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 2472 | || pParse->db->mallocFailed ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2473 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2474 | #endif /* ifdef SQLITE_DEBUG */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2475 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2476 | /* At this point, aSample[i] is the first sample that is greater than |
| 2477 | ** 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] | 2478 | ** than pVal. If aSample[i]==pVal, then res==0. |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2479 | */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2480 | if( res==0 ){ |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2481 | aStat[0] = aSample[i].anLt[iCol]; |
| 2482 | aStat[1] = aSample[i].anEq[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2483 | }else{ |
| 2484 | tRowcnt iLower, iUpper, iGap; |
| 2485 | if( i==0 ){ |
| 2486 | iLower = 0; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2487 | iUpper = aSample[0].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2488 | }else{ |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2489 | iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol]; |
| 2490 | iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2491 | } |
dan | 568cd51 | 2013-08-12 09:29:04 +0000 | [diff] [blame] | 2492 | aStat[1] = (pIdx->nColumn>iCol ? pIdx->aAvgEq[iCol] : 1); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2493 | if( iLower>=iUpper ){ |
| 2494 | iGap = 0; |
| 2495 | }else{ |
| 2496 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2497 | } |
| 2498 | if( roundUp ){ |
| 2499 | iGap = (iGap*2)/3; |
| 2500 | }else{ |
| 2501 | iGap = iGap/3; |
| 2502 | } |
| 2503 | aStat[0] = iLower + iGap; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2504 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2505 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2506 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2507 | |
| 2508 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2509 | ** This function is used to estimate the number of rows that will be visited |
| 2510 | ** by scanning an index for a range of values. The range may have an upper |
| 2511 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2512 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2513 | ** example, assuming that index p is on t1(a): |
| 2514 | ** |
| 2515 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2516 | ** |_____| |_____| |
| 2517 | ** | | |
| 2518 | ** pLower pUpper |
| 2519 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2520 | ** 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] | 2521 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2522 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2523 | ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index |
| 2524 | ** column subject to the range constraint. Or, equivalently, the number of |
| 2525 | ** equality constraints optimized by the proposed index scan. For example, |
| 2526 | ** assuming index p is on t1(a, b), and the SQL query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2527 | ** |
| 2528 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2529 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2530 | ** then nEq is set to 1 (as the range restricted column, b, is the second |
| 2531 | ** left-most column of the index). Or, if the query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2532 | ** |
| 2533 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2534 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2535 | ** then nEq is set to 0. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2536 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2537 | ** When this function is called, *pnOut is set to the whereCost() of the |
| 2538 | ** number of rows that the index scan is expected to visit without |
| 2539 | ** considering the range constraints. If nEq is 0, this is the number of |
| 2540 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
| 2541 | ** to account for the range contraints pLower and pUpper. |
| 2542 | ** |
| 2543 | ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be |
| 2544 | ** used, each range inequality reduces the search space by a factor of 4. |
| 2545 | ** Hence a pair of constraints (x>? AND x<?) reduces the expected number of |
| 2546 | ** rows visited by a factor of 16. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2547 | */ |
| 2548 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2549 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2550 | WhereLoopBuilder *pBuilder, |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2551 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2552 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2553 | WhereCost *pnOut /* IN/OUT: Number of rows visited */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2554 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2555 | int rc = SQLITE_OK; |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2556 | int nOut = (int)*pnOut; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2557 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2558 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2559 | Index *p = pBuilder->pNew->u.btree.pIndex; |
| 2560 | int nEq = pBuilder->pNew->u.btree.nEq; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2561 | |
drh | 74dade2 | 2013-09-04 18:14:53 +0000 | [diff] [blame] | 2562 | if( p->nSample>0 |
| 2563 | && nEq==pBuilder->nRecValid |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 2564 | && nEq<p->nSampleCol |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2565 | && OptimizationEnabled(pParse->db, SQLITE_Stat3) |
| 2566 | ){ |
| 2567 | UnpackedRecord *pRec = pBuilder->pRec; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2568 | tRowcnt a[2]; |
dan | 575ab2f | 2013-09-02 07:16:40 +0000 | [diff] [blame] | 2569 | u8 aff; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2570 | |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2571 | /* Variable iLower will be set to the estimate of the number of rows in |
| 2572 | ** the index that are less than the lower bound of the range query. The |
| 2573 | ** lower bound being the concatenation of $P and $L, where $P is the |
| 2574 | ** key-prefix formed by the nEq values matched against the nEq left-most |
| 2575 | ** columns of the index, and $L is the value in pLower. |
| 2576 | ** |
| 2577 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 2578 | ** is not a simple variable or literal value), the lower bound of the |
| 2579 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 2580 | ** if $L is available, whereKeyStats() is called for both ($P) and |
| 2581 | ** ($P:$L) and the larger of the two returned values used. |
| 2582 | ** |
| 2583 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 2584 | ** less than the upper bound of the range query. Where the upper bound |
| 2585 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 2586 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 2587 | */ |
| 2588 | tRowcnt iLower; |
| 2589 | tRowcnt iUpper; |
| 2590 | |
mistachkin | c2cfb51 | 2013-09-04 04:04:08 +0000 | [diff] [blame] | 2591 | if( nEq==p->nColumn ){ |
| 2592 | aff = SQLITE_AFF_INTEGER; |
| 2593 | }else{ |
| 2594 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
| 2595 | } |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2596 | /* Determine iLower and iUpper using ($P) only. */ |
| 2597 | if( nEq==0 ){ |
| 2598 | iLower = 0; |
| 2599 | iUpper = p->aiRowEst[0]; |
| 2600 | }else{ |
| 2601 | /* Note: this call could be optimized away - since the same values must |
| 2602 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 2603 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2604 | iLower = a[0]; |
| 2605 | iUpper = a[0] + a[1]; |
| 2606 | } |
| 2607 | |
| 2608 | /* If possible, improve on the iLower estimate using ($P:$L). */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2609 | if( pLower ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2610 | int bOk; /* True if value is extracted from pExpr */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2611 | Expr *pExpr = pLower->pExpr->pRight; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2612 | assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2613 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2614 | if( rc==SQLITE_OK && bOk ){ |
| 2615 | tRowcnt iNew; |
| 2616 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2617 | iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0); |
| 2618 | if( iNew>iLower ) iLower = iNew; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2619 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2620 | } |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2621 | |
| 2622 | /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 2623 | if( pUpper ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2624 | int bOk; /* True if value is extracted from pExpr */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2625 | Expr *pExpr = pUpper->pExpr->pRight; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2626 | assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2627 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2628 | if( rc==SQLITE_OK && bOk ){ |
| 2629 | tRowcnt iNew; |
| 2630 | whereKeyStats(pParse, p, pRec, 1, a); |
| 2631 | iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0); |
| 2632 | if( iNew<iUpper ) iUpper = iNew; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2633 | } |
| 2634 | } |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2635 | |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2636 | pBuilder->pRec = pRec; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2637 | if( rc==SQLITE_OK ){ |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2638 | WhereCost nNew; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2639 | if( iUpper>iLower ){ |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2640 | nNew = whereCost(iUpper - iLower); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2641 | }else{ |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2642 | nNew = 10; assert( 10==whereCost(2) ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2643 | } |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2644 | if( nNew<nOut ){ |
| 2645 | nOut = nNew; |
| 2646 | } |
| 2647 | *pnOut = (WhereCost)nOut; |
| 2648 | WHERETRACE(0x100, ("range scan regions: %u..%u est=%d\n", |
| 2649 | (u32)iLower, (u32)iUpper, nOut)); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2650 | return SQLITE_OK; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2651 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2652 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2653 | #else |
| 2654 | UNUSED_PARAMETER(pParse); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2655 | UNUSED_PARAMETER(pBuilder); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2656 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2657 | assert( pLower || pUpper ); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2658 | /* TUNING: Each inequality constraint reduces the search space 4-fold. |
| 2659 | ** A BETWEEN operator, therefore, reduces the search space 16-fold */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2660 | if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){ |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2661 | nOut -= 20; assert( 20==whereCost(4) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2662 | } |
| 2663 | if( pUpper ){ |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2664 | nOut -= 20; assert( 20==whereCost(4) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2665 | } |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2666 | if( nOut<10 ) nOut = 10; |
| 2667 | *pnOut = (WhereCost)nOut; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2668 | return rc; |
| 2669 | } |
| 2670 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2671 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2672 | /* |
| 2673 | ** Estimate the number of rows that will be returned based on |
| 2674 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2675 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2676 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2677 | ** for that index. When pExpr==NULL that means the constraint is |
| 2678 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2679 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2680 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2681 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2682 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2683 | ** |
| 2684 | ** This routine can fail if it is unable to load a collating sequence |
| 2685 | ** required for string comparison, or if unable to allocate memory |
| 2686 | ** for a UTF conversion required for comparison. The error is stored |
| 2687 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2688 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2689 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2690 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2691 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2692 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2693 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2694 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2695 | Index *p = pBuilder->pNew->u.btree.pIndex; |
| 2696 | int nEq = pBuilder->pNew->u.btree.nEq; |
| 2697 | UnpackedRecord *pRec = pBuilder->pRec; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2698 | u8 aff; /* Column affinity */ |
| 2699 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2700 | tRowcnt a[2]; /* Statistics */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2701 | int bOk; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2702 | |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2703 | assert( nEq>=1 ); |
| 2704 | assert( nEq<=(p->nColumn+1) ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2705 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2706 | assert( p->nSample>0 ); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2707 | assert( pBuilder->nRecValid<nEq ); |
| 2708 | |
| 2709 | /* If values are not available for all fields of the index to the left |
| 2710 | ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ |
| 2711 | if( pBuilder->nRecValid<(nEq-1) ){ |
| 2712 | return SQLITE_NOTFOUND; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2713 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2714 | |
dan | dd6e1f1 | 2013-08-10 19:08:30 +0000 | [diff] [blame] | 2715 | /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() |
| 2716 | ** below would return the same value. */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2717 | if( nEq>p->nColumn ){ |
| 2718 | *pnRow = 1; |
| 2719 | return SQLITE_OK; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2720 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2721 | |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2722 | aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity; |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2723 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk); |
| 2724 | pBuilder->pRec = pRec; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2725 | if( rc!=SQLITE_OK ) return rc; |
| 2726 | if( bOk==0 ) return SQLITE_NOTFOUND; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2727 | pBuilder->nRecValid = nEq; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2728 | |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2729 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2730 | WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1])); |
| 2731 | *pnRow = a[1]; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2732 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2733 | return rc; |
| 2734 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2735 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2736 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2737 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2738 | /* |
| 2739 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2740 | ** an IN constraint where the right-hand side of the IN operator |
| 2741 | ** is a list of values. Example: |
| 2742 | ** |
| 2743 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2744 | ** |
| 2745 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2746 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2747 | ** non-zero. |
| 2748 | ** |
| 2749 | ** This routine can fail if it is unable to load a collating sequence |
| 2750 | ** required for string comparison, or if unable to allocate memory |
| 2751 | ** for a UTF conversion required for comparison. The error is stored |
| 2752 | ** in the pParse structure. |
| 2753 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2754 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2755 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2756 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2757 | 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] | 2758 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2759 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2760 | Index *p = pBuilder->pNew->u.btree.pIndex; |
| 2761 | int nRecValid = pBuilder->nRecValid; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2762 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2763 | tRowcnt nEst; /* Number of rows for a single term */ |
| 2764 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 2765 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2766 | |
| 2767 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2768 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 2769 | nEst = p->aiRowEst[0]; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2770 | rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2771 | nRowEst += nEst; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2772 | pBuilder->nRecValid = nRecValid; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2773 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2774 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2775 | if( rc==SQLITE_OK ){ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2776 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 2777 | *pnRow = nRowEst; |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2778 | WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst)); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2779 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2780 | assert( pBuilder->nRecValid==nRecValid ); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2781 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2782 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2783 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2784 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2785 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2786 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2787 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2788 | ** or USING clause of that join. |
| 2789 | ** |
| 2790 | ** Consider the term t2.z='ok' in the following queries: |
| 2791 | ** |
| 2792 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2793 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2794 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2795 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2796 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2797 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2798 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2799 | ** |
| 2800 | ** 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] | 2801 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2802 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2803 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2804 | ** but joins might run a little slower. The trick is to disable as much |
| 2805 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2806 | ** the wrong answer. See ticket #813. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2807 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2808 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 2809 | if( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2810 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2811 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 2812 | ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 2813 | pTerm->wtFlags |= TERM_CODED; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 2814 | if( pTerm->iParent>=0 ){ |
| 2815 | WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 2816 | if( (--pOther->nChild)==0 ){ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 2817 | disableTerm(pLevel, pOther); |
| 2818 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2819 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2824 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2825 | ** to the n registers starting at base. |
| 2826 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2827 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2828 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2829 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2830 | ** |
| 2831 | ** This routine makes its own copy of zAff so that the caller is free |
| 2832 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2833 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2834 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2835 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2836 | if( zAff==0 ){ |
| 2837 | assert( pParse->db->mallocFailed ); |
| 2838 | return; |
| 2839 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2840 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2841 | |
| 2842 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2843 | ** and end of the affinity string. |
| 2844 | */ |
| 2845 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2846 | n--; |
| 2847 | base++; |
| 2848 | zAff++; |
| 2849 | } |
| 2850 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2851 | n--; |
| 2852 | } |
| 2853 | |
| 2854 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2855 | if( n>0 ){ |
| 2856 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2857 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2858 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2859 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2862 | |
| 2863 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2864 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2865 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2866 | ** coded. |
| 2867 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2868 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2869 | ** |
| 2870 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2871 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2872 | ** 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] | 2873 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2874 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2875 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2876 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2877 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2878 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2879 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2880 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2881 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2882 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2883 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2884 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2885 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2886 | assert( iTarget>0 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2887 | if( pX->op==TK_EQ ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2888 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2889 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2890 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2891 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2892 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2893 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2894 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2895 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2896 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2897 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2898 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2899 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2900 | && pLoop->u.btree.pIndex!=0 |
| 2901 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2902 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2903 | testcase( iEq==0 ); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2904 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2905 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2906 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2907 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2908 | iReg = iTarget; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2909 | eType = sqlite3FindInIndex(pParse, pX, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2910 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2911 | testcase( bRev ); |
| 2912 | bRev = !bRev; |
| 2913 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2914 | iTab = pX->iTable; |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2915 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2916 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2917 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2918 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2919 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2920 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2921 | pLevel->u.in.nIn++; |
| 2922 | pLevel->u.in.aInLoop = |
| 2923 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2924 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2925 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2926 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2927 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2928 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2929 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2930 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2931 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2932 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2933 | } |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2934 | pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2935 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2936 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2937 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2938 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2939 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2940 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2941 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2942 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2945 | /* |
| 2946 | ** Generate code that will evaluate all == and IN constraints for an |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2947 | ** index. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2948 | ** |
| 2949 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2950 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2951 | ** The index has as many as three equality constraints, but in this |
| 2952 | ** example, the third "c" value is an inequality. So only two |
| 2953 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2954 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2955 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2956 | ** |
| 2957 | ** In the example above nEq==2. But this subroutine works for any value |
| 2958 | ** 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] | 2959 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2960 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2961 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2962 | ** This routine always allocates at least one memory cell and returns |
| 2963 | ** the index of that memory cell. The code that |
| 2964 | ** calls this routine will use that memory cell to store the termination |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2965 | ** key value of the loop. If one or more IN operators appear, then |
| 2966 | ** this routine allocates an additional nEq memory cells for internal |
| 2967 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2968 | ** |
| 2969 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2970 | ** copy of the column affinity string of the index allocated using |
| 2971 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2972 | ** with equality constraints that use NONE affinity are set to |
| 2973 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2974 | ** |
| 2975 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2976 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2977 | ** |
| 2978 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2979 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2980 | ** no conversion should be attempted before using a t2.b value as part of |
| 2981 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2982 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2983 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2984 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2985 | Parse *pParse, /* Parsing context */ |
| 2986 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2987 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2988 | int nExtraReg, /* Number of extra registers to allocate */ |
| 2989 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2990 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2991 | int nEq; /* The number of == or IN constraints to code */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2992 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 2993 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2994 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2995 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2996 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2997 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2998 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2999 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3000 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3001 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3002 | pLoop = pLevel->pWLoop; |
| 3003 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 3004 | nEq = pLoop->u.btree.nEq; |
| 3005 | pIdx = pLoop->u.btree.pIndex; |
| 3006 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3007 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3008 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3009 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 3010 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3011 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3012 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3013 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3014 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 3015 | if( !zAff ){ |
| 3016 | pParse->db->mallocFailed = 1; |
| 3017 | } |
| 3018 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3019 | /* Evaluate the equality constraints |
| 3020 | */ |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 3021 | assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
drh | c49de5d | 2007-01-19 01:06:01 +0000 | [diff] [blame] | 3022 | for(j=0; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3023 | int r1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3024 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3025 | assert( pTerm!=0 ); |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 3026 | /* The following true for indices with redundant columns. |
| 3027 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 3028 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3029 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3030 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3031 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3032 | if( nReg==1 ){ |
| 3033 | sqlite3ReleaseTempReg(pParse, regBase); |
| 3034 | regBase = r1; |
| 3035 | }else{ |
| 3036 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 3037 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3038 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 3039 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 3040 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 3041 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3042 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 2f2855b | 2009-11-18 01:25:26 +0000 | [diff] [blame] | 3043 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3044 | if( zAff ){ |
| 3045 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 3046 | zAff[j] = SQLITE_AFF_NONE; |
| 3047 | } |
| 3048 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 3049 | zAff[j] = SQLITE_AFF_NONE; |
| 3050 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3051 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3052 | } |
| 3053 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3054 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3055 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3058 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3059 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3060 | ** This routine is a helper for explainIndexRange() below |
| 3061 | ** |
| 3062 | ** pStr holds the text of an expression that we are building up one term |
| 3063 | ** at a time. This routine adds a new term to the end of the expression. |
| 3064 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 3065 | ** terms only. |
| 3066 | */ |
| 3067 | static void explainAppendTerm( |
| 3068 | StrAccum *pStr, /* The text expression being built */ |
| 3069 | int iTerm, /* Index of this term. First is zero */ |
| 3070 | const char *zColumn, /* Name of the column */ |
| 3071 | const char *zOp /* Name of the operator */ |
| 3072 | ){ |
| 3073 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 3074 | sqlite3StrAccumAppend(pStr, zColumn, -1); |
| 3075 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 3076 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 3077 | } |
| 3078 | |
| 3079 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3080 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 3081 | ** function returns a pointer to a string buffer containing a description |
| 3082 | ** of the subset of table rows scanned by the strategy in the form of an |
| 3083 | ** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 3084 | ** |
| 3085 | ** For example, if the query: |
| 3086 | ** |
| 3087 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 3088 | ** |
| 3089 | ** is run and there is an index on (a, b), then this function returns a |
| 3090 | ** string similar to: |
| 3091 | ** |
| 3092 | ** "a=? AND b>?" |
| 3093 | ** |
| 3094 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 3095 | ** It is the responsibility of the caller to free the buffer when it is |
| 3096 | ** no longer required. |
| 3097 | */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3098 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 3099 | Index *pIndex = pLoop->u.btree.pIndex; |
| 3100 | int nEq = pLoop->u.btree.nEq; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3101 | int i, j; |
| 3102 | Column *aCol = pTab->aCol; |
| 3103 | int *aiColumn = pIndex->aiColumn; |
| 3104 | StrAccum txt; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3105 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3106 | if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3107 | return 0; |
| 3108 | } |
| 3109 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
drh | 03b6df1 | 2010-11-15 16:29:30 +0000 | [diff] [blame] | 3110 | txt.db = db; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3111 | sqlite3StrAccumAppend(&txt, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3112 | for(i=0; i<nEq; i++){ |
dan | e934e63 | 2013-08-20 17:14:57 +0000 | [diff] [blame] | 3113 | char *z = (i==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[i]].zName; |
| 3114 | explainAppendTerm(&txt, i, z, "="); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3117 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3118 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3119 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 3120 | explainAppendTerm(&txt, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3121 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3122 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3123 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 3124 | explainAppendTerm(&txt, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3125 | } |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3126 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 3127 | return sqlite3StrAccumFinish(&txt); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3130 | /* |
| 3131 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 3132 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 3133 | ** record is added to the output to describe the table scan strategy in |
| 3134 | ** pLevel. |
| 3135 | */ |
| 3136 | static void explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3137 | Parse *pParse, /* Parse context */ |
| 3138 | SrcList *pTabList, /* Table list this loop refers to */ |
| 3139 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 3140 | int iLevel, /* Value for "level" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3141 | int iFrom, /* Value for "from" column of output */ |
| 3142 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3143 | ){ |
| 3144 | if( pParse->explain==2 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3145 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3146 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 3147 | sqlite3 *db = pParse->db; /* Database handle */ |
| 3148 | char *zMsg; /* Text to add to EQP output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3149 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3150 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3151 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 3152 | u32 flags; /* Flags that describe this loop */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3153 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3154 | pLoop = pLevel->pWLoop; |
| 3155 | flags = pLoop->wsFlags; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3156 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3157 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3158 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 3159 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 3160 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3161 | |
| 3162 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3163 | if( pItem->pSelect ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3164 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3165 | }else{ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3166 | zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3169 | if( pItem->zAlias ){ |
| 3170 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 3171 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3172 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3173 | && ALWAYS(pLoop->u.btree.pIndex!=0) |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3174 | ){ |
| 3175 | char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3176 | zMsg = sqlite3MAppendf(db, zMsg, |
| 3177 | ((flags & WHERE_AUTO_INDEX) ? |
| 3178 | "%s USING AUTOMATIC %sINDEX%.0s%s" : |
| 3179 | "%s USING %sINDEX %s%s"), |
| 3180 | zMsg, ((flags & WHERE_IDX_ONLY) ? "COVERING " : ""), |
| 3181 | pLoop->u.btree.pIndex->zName, zWhere); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3182 | sqlite3DbFree(db, zWhere); |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 3183 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3184 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3185 | |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 3186 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3187 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 3188 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3189 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 3190 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 3191 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3192 | }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3193 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 3194 | } |
| 3195 | } |
| 3196 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3197 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3198 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3199 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3200 | } |
| 3201 | #endif |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3202 | zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3203 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3204 | } |
| 3205 | } |
| 3206 | #else |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3207 | # define explainOneScan(u,v,w,x,y,z) |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3208 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 3209 | |
| 3210 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3211 | /* |
| 3212 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 3213 | ** implementation described by pWInfo. |
| 3214 | */ |
| 3215 | static Bitmask codeOneLoopStart( |
| 3216 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 3217 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3218 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3219 | ){ |
| 3220 | int j, k; /* Loop counters */ |
| 3221 | int iCur; /* The VDBE cursor for the table */ |
| 3222 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 3223 | int omitTable; /* True if we use the index only */ |
| 3224 | int bRev; /* True if we need to scan in reverse order */ |
| 3225 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3226 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3227 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 3228 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 3229 | Parse *pParse; /* Parsing context */ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3230 | sqlite3 *db; /* Database connection */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3231 | Vdbe *v; /* The prepared stmt under constructions */ |
| 3232 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3233 | int addrBrk; /* Jump here to break out of the loop */ |
| 3234 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3235 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 3236 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3237 | Bitmask newNotReady; /* Return value */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3238 | |
| 3239 | pParse = pWInfo->pParse; |
| 3240 | v = pParse->pVdbe; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3241 | pWC = &pWInfo->sWC; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3242 | db = pParse->db; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3243 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3244 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3245 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 3246 | iCur = pTabItem->iCursor; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3247 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 3248 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3249 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3250 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3251 | |
| 3252 | /* Create labels for the "break" and "continue" instructions |
| 3253 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 3254 | ** Jump to cont to go immediately to the next iteration of the |
| 3255 | ** loop. |
| 3256 | ** |
| 3257 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 3258 | ** means to continue with the next IN value combination. When |
| 3259 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 3260 | ** is the same as "addrBrk". |
| 3261 | */ |
| 3262 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 3263 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 3264 | |
| 3265 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 3266 | ** initialize a memory cell that records if this table matches any |
| 3267 | ** row of the left table of the join. |
| 3268 | */ |
| 3269 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 3270 | pLevel->iLeftJoin = ++pParse->nMem; |
| 3271 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 3272 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 3273 | } |
| 3274 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 3275 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 3276 | if( pTabItem->viaCoroutine ){ |
| 3277 | int regYield = pTabItem->regReturn; |
| 3278 | sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield); |
| 3279 | pLevel->p2 = sqlite3VdbeAddOp1(v, OP_Yield, regYield); |
| 3280 | VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName)); |
| 3281 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 3282 | pLevel->op = OP_Goto; |
| 3283 | }else |
| 3284 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3285 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3286 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 3287 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3288 | ** to access the data. |
| 3289 | */ |
| 3290 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3291 | int addrNotFound; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3292 | int nConstraint = pLoop->nLTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3293 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3294 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3295 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3296 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3297 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 3298 | int iTarget = iReg+j+2; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3299 | pTerm = pLoop->aLTerm[j]; |
drh | 95ed68d | 2013-06-12 17:55:50 +0000 | [diff] [blame] | 3300 | if( pTerm==0 ) continue; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3301 | if( pTerm->eOperator & WO_IN ){ |
| 3302 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 3303 | addrNotFound = pLevel->addrNxt; |
| 3304 | }else{ |
| 3305 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 3306 | } |
| 3307 | } |
| 3308 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 3309 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3310 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 3311 | pLoop->u.vtab.idxStr, |
| 3312 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 3313 | pLoop->u.vtab.needFree = 0; |
| 3314 | for(j=0; j<nConstraint && j<16; j++){ |
| 3315 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3316 | disableTerm(pLevel, pLoop->aLTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3317 | } |
| 3318 | } |
| 3319 | pLevel->op = OP_VNext; |
| 3320 | pLevel->p1 = iCur; |
| 3321 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3322 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3323 | sqlite3ExprCachePop(pParse, 1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3324 | }else |
| 3325 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3326 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3327 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3328 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 3329 | ){ |
| 3330 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3331 | ** equality comparison against the ROWID field. Or |
| 3332 | ** we reference multiple rows using a "rowid IN (...)" |
| 3333 | ** construct. |
| 3334 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3335 | assert( pLoop->u.btree.nEq==1 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3336 | iReleaseReg = sqlite3GetTempReg(pParse); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3337 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3338 | assert( pTerm!=0 ); |
| 3339 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3340 | assert( omitTable==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3341 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3342 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3343 | addrNxt = pLevel->addrNxt; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3344 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 3345 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 3346 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3347 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3348 | VdbeComment((v, "pk")); |
| 3349 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3350 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3351 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 3352 | ){ |
| 3353 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3354 | */ |
| 3355 | int testOp = OP_Noop; |
| 3356 | int start; |
| 3357 | int memEndValue = 0; |
| 3358 | WhereTerm *pStart, *pEnd; |
| 3359 | |
| 3360 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3361 | j = 0; |
| 3362 | pStart = pEnd = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3363 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 3364 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3365 | assert( pStart!=0 || pEnd!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3366 | if( bRev ){ |
| 3367 | pTerm = pStart; |
| 3368 | pStart = pEnd; |
| 3369 | pEnd = pTerm; |
| 3370 | } |
| 3371 | if( pStart ){ |
| 3372 | Expr *pX; /* The expression that defines the start bound */ |
| 3373 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 3374 | |
| 3375 | /* The following constant maps TK_xx codes into corresponding |
| 3376 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 3377 | */ |
| 3378 | const u8 aMoveOp[] = { |
| 3379 | /* TK_GT */ OP_SeekGt, |
| 3380 | /* TK_LE */ OP_SeekLe, |
| 3381 | /* TK_LT */ OP_SeekLt, |
| 3382 | /* TK_GE */ OP_SeekGe |
| 3383 | }; |
| 3384 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 3385 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 3386 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 3387 | |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3388 | assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3389 | testcase( pStart->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3390 | pX = pStart->pExpr; |
| 3391 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3392 | testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3393 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 3394 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| 3395 | VdbeComment((v, "pk")); |
| 3396 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 3397 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 3398 | disableTerm(pLevel, pStart); |
| 3399 | }else{ |
| 3400 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 3401 | } |
| 3402 | if( pEnd ){ |
| 3403 | Expr *pX; |
| 3404 | pX = pEnd->pExpr; |
| 3405 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3406 | assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 3407 | testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3408 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3409 | memEndValue = ++pParse->nMem; |
| 3410 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 3411 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 3412 | testOp = bRev ? OP_Le : OP_Ge; |
| 3413 | }else{ |
| 3414 | testOp = bRev ? OP_Lt : OP_Gt; |
| 3415 | } |
| 3416 | disableTerm(pLevel, pEnd); |
| 3417 | } |
| 3418 | start = sqlite3VdbeCurrentAddr(v); |
| 3419 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 3420 | pLevel->p1 = iCur; |
| 3421 | pLevel->p2 = start; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3422 | assert( pLevel->p5==0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3423 | if( testOp!=OP_Noop ){ |
| 3424 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3425 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3426 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3427 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 3428 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3429 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3430 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3431 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3432 | ** |
| 3433 | ** The WHERE clause may contain zero or more equality |
| 3434 | ** terms ("==" or "IN" operators) that refer to the N |
| 3435 | ** left-most columns of the index. It may also contain |
| 3436 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3437 | ** column that immediately follows the N equalities. Only |
| 3438 | ** the right-most column can be an inequality - the rest must |
| 3439 | ** use the "==" and "IN" operators. For example, if the |
| 3440 | ** index is on (x,y,z), then the following clauses are all |
| 3441 | ** optimized: |
| 3442 | ** |
| 3443 | ** x=5 |
| 3444 | ** x=5 AND y=10 |
| 3445 | ** x=5 AND y<10 |
| 3446 | ** x=5 AND y>5 AND y<10 |
| 3447 | ** x=5 AND y=5 AND z<=10 |
| 3448 | ** |
| 3449 | ** The z<10 term of the following cannot be used, only |
| 3450 | ** the x=5 term: |
| 3451 | ** |
| 3452 | ** x=5 AND z<10 |
| 3453 | ** |
| 3454 | ** N may be zero if there are inequality constraints. |
| 3455 | ** If there are no inequality constraints, then N is at |
| 3456 | ** least one. |
| 3457 | ** |
| 3458 | ** This case is also used when there are no WHERE clause |
| 3459 | ** constraints but an index is selected anyway, in order |
| 3460 | ** to force the output order to conform to an ORDER BY. |
| 3461 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3462 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3463 | 0, |
| 3464 | 0, |
| 3465 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3466 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 3467 | OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3468 | OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ |
| 3469 | OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ |
| 3470 | OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ |
| 3471 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3472 | static const u8 aEndOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3473 | OP_Noop, /* 0: (!end_constraints) */ |
| 3474 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 3475 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 3476 | }; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3477 | int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 3478 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3479 | int regBase; /* Base register holding constraint values */ |
| 3480 | int r1; /* Temp register */ |
| 3481 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3482 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3483 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3484 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3485 | int start_constraints; /* Start of range is constrained */ |
| 3486 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3487 | Index *pIdx; /* The index we will be using */ |
| 3488 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3489 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3490 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3491 | char *zStartAff; /* Affinity for start of range constraint */ |
| 3492 | char *zEndAff; /* Affinity for end of range constraint */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3493 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3494 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3495 | iIdxCur = pLevel->iIdxCur; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3496 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3497 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3498 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3499 | ** query, then the caller will only allow the loop to run for |
| 3500 | ** a single iteration. This means that the first row returned |
| 3501 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3502 | ** the first one after the nEq equality constraints in the index, |
| 3503 | ** this requires some special handling. |
| 3504 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3505 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 3506 | && (pWInfo->bOBSat!=0) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3507 | && (pIdx->nColumn>nEq) |
| 3508 | ){ |
| 3509 | /* assert( pOrderBy->nExpr==1 ); */ |
| 3510 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 3511 | isMinQuery = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3512 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | /* Find any inequality constraint terms for the start and end |
| 3516 | ** of the range. |
| 3517 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3518 | j = nEq; |
| 3519 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3520 | pRangeStart = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3521 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3522 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3523 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3524 | pRangeEnd = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3525 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3528 | /* Generate code to evaluate all constraint terms using == or IN |
| 3529 | ** and store the values of those terms in an array of registers |
| 3530 | ** starting at regBase. |
| 3531 | */ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 3532 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3533 | zEndAff = sqlite3DbStrDup(db, zStartAff); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3534 | addrNxt = pLevel->addrNxt; |
| 3535 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3536 | /* If we are doing a reverse order scan on an ascending index, or |
| 3537 | ** a forward order scan on a descending index, interchange the |
| 3538 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3539 | */ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3540 | if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3541 | || (bRev && pIdx->nColumn==nEq) |
| 3542 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3543 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 3544 | } |
| 3545 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3546 | testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 3547 | testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 3548 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 3549 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3550 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3551 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3552 | start_constraints = pRangeStart || nEq>0; |
| 3553 | |
| 3554 | /* Seek the index cursor to the start of the range. */ |
| 3555 | nConstraint = nEq; |
| 3556 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3557 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3558 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3559 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){ |
| 3560 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3561 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3562 | if( zStartAff ){ |
| 3563 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3564 | /* Since the comparison is to be performed with no conversions |
| 3565 | ** applied to the operands, set the affinity to apply to pRight to |
| 3566 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3567 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3568 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3569 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3570 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3571 | } |
| 3572 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3573 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3574 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3575 | }else if( isMinQuery ){ |
| 3576 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3577 | nConstraint++; |
| 3578 | startEq = 0; |
| 3579 | start_constraints = 1; |
| 3580 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3581 | codeApplyAffinity(pParse, regBase, nConstraint, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3582 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3583 | assert( op!=0 ); |
| 3584 | testcase( op==OP_Rewind ); |
| 3585 | testcase( op==OP_Last ); |
| 3586 | testcase( op==OP_SeekGt ); |
| 3587 | testcase( op==OP_SeekGe ); |
| 3588 | testcase( op==OP_SeekLe ); |
| 3589 | testcase( op==OP_SeekLt ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3590 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3591 | |
| 3592 | /* Load the value for the inequality constraint at the end of the |
| 3593 | ** range (if any). |
| 3594 | */ |
| 3595 | nConstraint = nEq; |
| 3596 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3597 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3598 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3599 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3600 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ |
| 3601 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3602 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3603 | if( zEndAff ){ |
| 3604 | if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3605 | /* Since the comparison is to be performed with no conversions |
| 3606 | ** applied to the operands, set the affinity to apply to pRight to |
| 3607 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3608 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3609 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3610 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ |
| 3611 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3612 | } |
| 3613 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3614 | codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3615 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3616 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3617 | } |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3618 | sqlite3DbFree(db, zStartAff); |
| 3619 | sqlite3DbFree(db, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3620 | |
| 3621 | /* Top of the loop body */ |
| 3622 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3623 | |
| 3624 | /* Check if the index cursor is past the end of the range. */ |
| 3625 | op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)]; |
| 3626 | testcase( op==OP_Noop ); |
| 3627 | testcase( op==OP_IdxGE ); |
| 3628 | testcase( op==OP_IdxLT ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3629 | if( op!=OP_Noop ){ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3630 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3631 | sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0); |
| 3632 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3633 | |
| 3634 | /* If there are inequality constraints, check that the value |
| 3635 | ** of the table column that the inequality contrains is not NULL. |
| 3636 | ** If it is, jump to the next iteration of the loop. |
| 3637 | */ |
| 3638 | r1 = sqlite3GetTempReg(pParse); |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 3639 | testcase( pLoop->wsFlags & WHERE_BTM_LIMIT ); |
| 3640 | testcase( pLoop->wsFlags & WHERE_TOP_LIMIT ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3641 | if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3642 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 3643 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 3644 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3645 | sqlite3ReleaseTempReg(pParse, r1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3646 | |
| 3647 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3648 | disableTerm(pLevel, pRangeStart); |
| 3649 | disableTerm(pLevel, pRangeEnd); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3650 | if( !omitTable ){ |
| 3651 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3652 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3653 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3654 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3655 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3656 | |
| 3657 | /* Record the instruction used to terminate the loop. Disable |
| 3658 | ** WHERE clause terms made redundant by the index range scan. |
| 3659 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3660 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3661 | pLevel->op = OP_Noop; |
| 3662 | }else if( bRev ){ |
| 3663 | pLevel->op = OP_Prev; |
| 3664 | }else{ |
| 3665 | pLevel->op = OP_Next; |
| 3666 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3667 | pLevel->p1 = iIdxCur; |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3668 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3669 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3670 | }else{ |
| 3671 | assert( pLevel->p5==0 ); |
| 3672 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3673 | }else |
| 3674 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3675 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3676 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3677 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3678 | ** |
| 3679 | ** Example: |
| 3680 | ** |
| 3681 | ** CREATE TABLE t1(a,b,c,d); |
| 3682 | ** CREATE INDEX i1 ON t1(a); |
| 3683 | ** CREATE INDEX i2 ON t1(b); |
| 3684 | ** CREATE INDEX i3 ON t1(c); |
| 3685 | ** |
| 3686 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3687 | ** |
| 3688 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3689 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3690 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3691 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3692 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3693 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3694 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3695 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3696 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3697 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3698 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3699 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3700 | ** Gosub 2 A |
| 3701 | ** sqlite3WhereEnd() |
| 3702 | ** |
| 3703 | ** Following the above, code to terminate the loop. Label A, the target |
| 3704 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3705 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3706 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3707 | ** Goto B # The loop is finished. |
| 3708 | ** |
| 3709 | ** A: <loop body> # Return data, whatever. |
| 3710 | ** |
| 3711 | ** Return 2 # Jump back to the Gosub |
| 3712 | ** |
| 3713 | ** B: <after the loop> |
| 3714 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3715 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3716 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3717 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3718 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3719 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3720 | |
| 3721 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3722 | int regRowset = 0; /* Register for RowSet object */ |
| 3723 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3724 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3725 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3726 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3727 | int ii; /* Loop counter */ |
| 3728 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3729 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3730 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3731 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3732 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3733 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3734 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3735 | pLevel->op = OP_Return; |
| 3736 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3737 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3738 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3739 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3740 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3741 | */ |
| 3742 | if( pWInfo->nLevel>1 ){ |
| 3743 | int nNotReady; /* The number of notReady tables */ |
| 3744 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3745 | nNotReady = pWInfo->nLevel - iLevel - 1; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3746 | pOrTab = sqlite3StackAllocRaw(db, |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3747 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3748 | if( pOrTab==0 ) return notReady; |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 3749 | pOrTab->nAlloc = (u8)(nNotReady + 1); |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3750 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3751 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3752 | origSrc = pWInfo->pTabList->a; |
| 3753 | for(k=1; k<=nNotReady; k++){ |
| 3754 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3755 | } |
| 3756 | }else{ |
| 3757 | pOrTab = pWInfo->pTabList; |
| 3758 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3759 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3760 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 3761 | ** equivalent to an empty rowset. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3762 | ** |
| 3763 | ** Also initialize regReturn to contain the address of the instruction |
| 3764 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3765 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3766 | ** over the top of the loop into the body of it. In this case the |
| 3767 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3768 | ** fall through to the next instruction, just as an OP_Next does if |
| 3769 | ** called on an uninitialized cursor. |
| 3770 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3771 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3772 | regRowset = ++pParse->nMem; |
| 3773 | regRowid = ++pParse->nMem; |
| 3774 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3775 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3776 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3777 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3778 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3779 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3780 | ** That way, terms in y that are factored into the disjunction will |
| 3781 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3782 | ** |
| 3783 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3784 | ** the "interesting" terms of z - terms that did not originate in the |
| 3785 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3786 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3787 | ** |
| 3788 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3789 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3790 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3791 | */ |
| 3792 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3793 | int iTerm; |
| 3794 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3795 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 3796 | if( &pWC->a[iTerm] == pTerm ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3797 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 3798 | if( pWC->a[iTerm].wtFlags & (TERM_ORINFO) ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3799 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3800 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 3801 | pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3802 | } |
| 3803 | if( pAndExpr ){ |
| 3804 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3805 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3808 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3809 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3810 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3811 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3812 | Expr *pOrExpr = pOrTerm->pExpr; |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3813 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3814 | pAndExpr->pLeft = pOrExpr; |
| 3815 | pOrExpr = pAndExpr; |
| 3816 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3817 | /* Loop through table entries that match term pOrTerm. */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3818 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 3819 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3820 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3821 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3822 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3823 | WhereLoop *pSubLoop; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3824 | explainOneScan( |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3825 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3826 | ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3827 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3828 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3829 | int r; |
| 3830 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3831 | regRowid, 0); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3832 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| 3833 | sqlite3VdbeCurrentAddr(v)+2, r, iSet); |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3834 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3835 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 3836 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3837 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 3838 | ** contained one or more AND term from a notReady table. The |
| 3839 | ** terms from the notReady table could not be tested and will |
| 3840 | ** need to be tested later. |
| 3841 | */ |
| 3842 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 3843 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3844 | /* If all of the OR-connected terms are optimized using the same |
| 3845 | ** index, and the index is opened using the same cursor number |
| 3846 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 3847 | ** be possible to use that index as a covering index. |
| 3848 | ** |
| 3849 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 3850 | ** uses an index, and this is either the first OR-connected term |
| 3851 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3852 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 3853 | ** pCov to NULL to indicate that no candidate covering index will |
| 3854 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3855 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3856 | pSubLoop = pSubWInfo->a[0].pWLoop; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3857 | assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3858 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3859 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3860 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3861 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
drh | 907717f | 2013-06-04 18:03:22 +0000 | [diff] [blame] | 3862 | pCov = pSubLoop->u.btree.pIndex; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3863 | }else{ |
| 3864 | pCov = 0; |
| 3865 | } |
| 3866 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3867 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 3868 | sqlite3WhereEnd(pSubWInfo); |
| 3869 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3870 | } |
| 3871 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 3872 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 3873 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3874 | if( pAndExpr ){ |
| 3875 | pAndExpr->pLeft = 0; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3876 | sqlite3ExprDelete(db, pAndExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3877 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3878 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3879 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 3880 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 3881 | |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3882 | if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3883 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3884 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3885 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3886 | |
| 3887 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3888 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3889 | ** scan of the entire table. |
| 3890 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3891 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 3892 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 3893 | assert( bRev==0 || bRev==1 ); |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3894 | pLevel->op = aStep[bRev]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3895 | pLevel->p1 = iCur; |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3896 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3897 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3898 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3899 | newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3900 | |
| 3901 | /* Insert code to test every subexpression that can be completely |
| 3902 | ** computed using the current set of tables. |
| 3903 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3904 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3905 | Expr *pE; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3906 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3907 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3908 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3909 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3910 | testcase( pWInfo->untestedTerms==0 |
| 3911 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 3912 | pWInfo->untestedTerms = 1; |
| 3913 | continue; |
| 3914 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3915 | pE = pTerm->pExpr; |
| 3916 | assert( pE!=0 ); |
| 3917 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 3918 | continue; |
| 3919 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3920 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3921 | pTerm->wtFlags |= TERM_CODED; |
| 3922 | } |
| 3923 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3924 | /* Insert code to test for implied constraints based on transitivity |
| 3925 | ** of the "==" operator. |
| 3926 | ** |
| 3927 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 3928 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 3929 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 3930 | ** the implied "t1.a=123" constraint. |
| 3931 | */ |
| 3932 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3933 | Expr *pE, *pEAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3934 | WhereTerm *pAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3935 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 3936 | if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue; |
| 3937 | if( pTerm->leftCursor!=iCur ) continue; |
drh | cdc2e43 | 2013-07-01 17:27:19 +0000 | [diff] [blame] | 3938 | if( pLevel->iLeftJoin ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3939 | pE = pTerm->pExpr; |
| 3940 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 3941 | assert( (pTerm->prereqRight & newNotReady)!=0 ); |
| 3942 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 3943 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 3944 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3945 | testcase( pAlt->eOperator & WO_EQ ); |
| 3946 | testcase( pAlt->eOperator & WO_IN ); |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3947 | VdbeNoopComment((v, "begin transitive constraint")); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3948 | pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 3949 | if( pEAlt ){ |
| 3950 | *pEAlt = *pAlt->pExpr; |
| 3951 | pEAlt->pLeft = pE->pLeft; |
| 3952 | sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| 3953 | sqlite3StackFree(db, pEAlt); |
| 3954 | } |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3957 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 3958 | ** at least one row of the right table has matched the left table. |
| 3959 | */ |
| 3960 | if( pLevel->iLeftJoin ){ |
| 3961 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 3962 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 3963 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3964 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3965 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3966 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3967 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3968 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3969 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 3970 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3971 | continue; |
| 3972 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3973 | assert( pTerm->pExpr ); |
| 3974 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 3975 | pTerm->wtFlags |= TERM_CODED; |
| 3976 | } |
| 3977 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3978 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3979 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3980 | return newNotReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3983 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3984 | /* |
| 3985 | ** Print a WhereLoop object for debugging purposes |
| 3986 | */ |
| 3987 | static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3988 | int nb = 1+(pTabList->nSrc+7)/8; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3989 | struct SrcList_item *pItem = pTabList->a + p->iTab; |
| 3990 | Table *pTab = pItem->pTab; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3991 | sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 3992 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3993 | sqlite3DebugPrintf(" %12s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3994 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3995 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 3996 | if( p->u.btree.pIndex ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3997 | const char *zName = p->u.btree.pIndex->zName; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3998 | if( zName==0 ) zName = "ipk"; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3999 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 4000 | int i = sqlite3Strlen30(zName) - 1; |
| 4001 | while( zName[i]!='_' ) i--; |
| 4002 | zName += i; |
| 4003 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4004 | sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4005 | }else{ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4006 | sqlite3DebugPrintf("%20s",""); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4007 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4008 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4009 | char *z; |
| 4010 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4011 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 4012 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4013 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4014 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4015 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4016 | sqlite3DebugPrintf(" %-19s", z); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4017 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4018 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4019 | sqlite3DebugPrintf(" f %04x N %d", p->wsFlags, p->nLTerm); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4020 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4021 | } |
| 4022 | #endif |
| 4023 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4024 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4025 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 4026 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4027 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4028 | static void whereLoopInit(WhereLoop *p){ |
| 4029 | p->aLTerm = p->aLTermSpace; |
| 4030 | p->nLTerm = 0; |
| 4031 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 4032 | p->wsFlags = 0; |
| 4033 | } |
| 4034 | |
| 4035 | /* |
| 4036 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 4037 | */ |
| 4038 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4039 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4040 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 4041 | sqlite3_free(p->u.vtab.idxStr); |
| 4042 | p->u.vtab.needFree = 0; |
| 4043 | p->u.vtab.idxStr = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4044 | }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4045 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 4046 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 4047 | p->u.btree.pIndex = 0; |
| 4048 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4049 | } |
| 4050 | } |
| 4051 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4052 | /* |
| 4053 | ** Deallocate internal memory used by a WhereLoop object |
| 4054 | */ |
| 4055 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 4056 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 4057 | whereLoopClearUnion(db, p); |
| 4058 | whereLoopInit(p); |
| 4059 | } |
| 4060 | |
| 4061 | /* |
| 4062 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 4063 | */ |
| 4064 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 4065 | WhereTerm **paNew; |
| 4066 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 4067 | n = (n+7)&~7; |
| 4068 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 4069 | if( paNew==0 ) return SQLITE_NOMEM; |
| 4070 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 4071 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 4072 | p->aLTerm = paNew; |
| 4073 | p->nLSlot = n; |
| 4074 | return SQLITE_OK; |
| 4075 | } |
| 4076 | |
| 4077 | /* |
| 4078 | ** Transfer content from the second pLoop into the first. |
| 4079 | */ |
| 4080 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4081 | whereLoopClearUnion(db, pTo); |
drh | 0d31dc3 | 2013-09-06 00:40:59 +0000 | [diff] [blame] | 4082 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ |
| 4083 | memset(&pTo->u, 0, sizeof(pTo->u)); |
| 4084 | return SQLITE_NOMEM; |
| 4085 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4086 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 4087 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4088 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 4089 | pFrom->u.vtab.needFree = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4090 | }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4091 | pFrom->u.btree.pIndex = 0; |
| 4092 | } |
| 4093 | return SQLITE_OK; |
| 4094 | } |
| 4095 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4096 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4097 | ** Delete a WhereLoop object |
| 4098 | */ |
| 4099 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4100 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4101 | sqlite3DbFree(db, p); |
| 4102 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 4103 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4104 | /* |
| 4105 | ** Free a WhereInfo structure |
| 4106 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 4107 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 4108 | if( ALWAYS(pWInfo) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4109 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4110 | while( pWInfo->pLoops ){ |
| 4111 | WhereLoop *p = pWInfo->pLoops; |
| 4112 | pWInfo->pLoops = p->pNextLoop; |
| 4113 | whereLoopDelete(db, p); |
| 4114 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4115 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4116 | } |
| 4117 | } |
| 4118 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4119 | /* |
| 4120 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 4121 | ** |
| 4122 | ** An existing WhereLoop entry might be overwritten if the new template |
| 4123 | ** is better and has fewer dependencies. Or the template will be ignored |
| 4124 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 4125 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4126 | ** added based on the template. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4127 | ** |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4128 | ** If pBuilder->pOrSet is not NULL then we only care about only the |
| 4129 | ** prerequisites and rRun and nOut costs of the N best loops. That |
| 4130 | ** information is gathered in the pBuilder->pOrSet object. This special |
| 4131 | ** processing mode is used only for OR clause processing. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4132 | ** |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4133 | ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4134 | ** still might overwrite similar loops with the new template if the |
| 4135 | ** template is better. Loops may be overwritten if the following |
| 4136 | ** conditions are met: |
| 4137 | ** |
| 4138 | ** (1) They have the same iTab. |
| 4139 | ** (2) They have the same iSortIdx. |
| 4140 | ** (3) The template has same or fewer dependencies than the current loop |
| 4141 | ** (4) The template has the same or lower cost than the current loop |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4142 | ** (5) The template uses more terms of the same index but has no additional |
| 4143 | ** dependencies |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4144 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4145 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4146 | WhereLoop **ppPrev, *p, *pNext = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4147 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4148 | sqlite3 *db = pWInfo->pParse->db; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4149 | |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4150 | /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 4151 | ** and prereqs. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4152 | */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4153 | if( pBuilder->pOrSet!=0 ){ |
| 4154 | #if WHERETRACE_ENABLED |
| 4155 | u16 n = pBuilder->pOrSet->n; |
| 4156 | int x = |
| 4157 | #endif |
| 4158 | whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, |
| 4159 | pTemplate->nOut); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4160 | #if WHERETRACE_ENABLED |
| 4161 | if( sqlite3WhereTrace & 0x8 ){ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4162 | sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4163 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4164 | } |
| 4165 | #endif |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4166 | return SQLITE_OK; |
| 4167 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4168 | |
| 4169 | /* Search for an existing WhereLoop to overwrite, or which takes |
| 4170 | ** priority over pTemplate. |
| 4171 | */ |
| 4172 | for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4173 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 4174 | /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 4175 | ** then those WhereLoops need to be considered separately. Neither is |
| 4176 | ** a candidate to replace the other. */ |
| 4177 | continue; |
| 4178 | } |
| 4179 | /* In the current implementation, the rSetup value is either zero |
| 4180 | ** or the cost of building an automatic index (NlogN) and the NlogN |
| 4181 | ** is the same for compatible WhereLoops. */ |
| 4182 | assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 4183 | || p->rSetup==pTemplate->rSetup ); |
| 4184 | |
| 4185 | /* whereLoopAddBtree() always generates and inserts the automatic index |
| 4186 | ** case first. Hence compatible candidate WhereLoops never have a larger |
| 4187 | ** rSetup. Call this SETUP-INVARIANT */ |
| 4188 | assert( p->rSetup>=pTemplate->rSetup ); |
| 4189 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4190 | if( (p->prereq & pTemplate->prereq)==p->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4191 | && p->rSetup<=pTemplate->rSetup |
| 4192 | && p->rRun<=pTemplate->rRun |
drh | f46af73 | 2013-08-30 17:35:44 +0000 | [diff] [blame] | 4193 | && p->nOut<=pTemplate->nOut |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4194 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4195 | /* This branch taken when p is equal or better than pTemplate in |
drh | e56dd3a | 2013-08-30 17:50:35 +0000 | [diff] [blame] | 4196 | ** all of (1) dependencies (2) setup-cost, (3) run-cost, and |
drh | f46af73 | 2013-08-30 17:35:44 +0000 | [diff] [blame] | 4197 | ** (4) number of output rows. */ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4198 | assert( p->rSetup==pTemplate->rSetup ); |
drh | 05db3c7 | 2013-09-02 20:22:18 +0000 | [diff] [blame] | 4199 | if( p->prereq==pTemplate->prereq |
| 4200 | && p->nLTerm<pTemplate->nLTerm |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4201 | && (p->wsFlags & WHERE_INDEXED)!=0 |
| 4202 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 4203 | && p->u.btree.pIndex==pTemplate->u.btree.pIndex |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4204 | ){ |
| 4205 | /* Overwrite an existing WhereLoop with an similar one that uses |
| 4206 | ** more terms of the index */ |
| 4207 | pNext = p->pNextLoop; |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4208 | break; |
| 4209 | }else{ |
| 4210 | /* pTemplate is not helpful. |
| 4211 | ** Return without changing or adding anything */ |
| 4212 | goto whereLoopInsert_noop; |
| 4213 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4214 | } |
| 4215 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4216 | && p->rRun>=pTemplate->rRun |
drh | f46af73 | 2013-08-30 17:35:44 +0000 | [diff] [blame] | 4217 | && p->nOut>=pTemplate->nOut |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4218 | && ALWAYS(p->rSetup>=pTemplate->rSetup) /* See SETUP-INVARIANT above */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4219 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4220 | /* Overwrite an existing WhereLoop with a better one: one that is |
drh | e56dd3a | 2013-08-30 17:50:35 +0000 | [diff] [blame] | 4221 | ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost |
drh | f46af73 | 2013-08-30 17:35:44 +0000 | [diff] [blame] | 4222 | ** or (4) number of output rows, and is no worse in any of those |
| 4223 | ** categories. */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4224 | pNext = p->pNextLoop; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4225 | break; |
| 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | /* If we reach this point it means that either p[] should be overwritten |
| 4230 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 4231 | ** WhereLoop and insert it. |
| 4232 | */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4233 | #if WHERETRACE_ENABLED |
| 4234 | if( sqlite3WhereTrace & 0x8 ){ |
| 4235 | if( p!=0 ){ |
| 4236 | sqlite3DebugPrintf("ins-del: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4237 | whereLoopPrint(p, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4238 | } |
| 4239 | sqlite3DebugPrintf("ins-new: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4240 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4241 | } |
| 4242 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4243 | if( p==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4244 | p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4245 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4246 | whereLoopInit(p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4247 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4248 | whereLoopXfer(db, p, pTemplate); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4249 | p->pNextLoop = pNext; |
| 4250 | *ppPrev = p; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4251 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4252 | Index *pIndex = p->u.btree.pIndex; |
| 4253 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4254 | p->u.btree.pIndex = 0; |
| 4255 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4256 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4257 | return SQLITE_OK; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4258 | |
| 4259 | /* Jump here if the insert is a no-op */ |
| 4260 | whereLoopInsert_noop: |
| 4261 | #if WHERETRACE_ENABLED |
| 4262 | if( sqlite3WhereTrace & 0x8 ){ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4263 | sqlite3DebugPrintf("ins-noop: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4264 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4265 | } |
| 4266 | #endif |
| 4267 | return SQLITE_OK; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | /* |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 4271 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 4272 | ** WHERE clause that reference the loop but which are not used by an |
| 4273 | ** index. |
| 4274 | ** |
| 4275 | ** In the current implementation, the first extra WHERE clause term reduces |
| 4276 | ** the number of output rows by a factor of 10 and each additional term |
| 4277 | ** reduces the number of output rows by sqrt(2). |
| 4278 | */ |
| 4279 | static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop, int iCur){ |
| 4280 | WhereTerm *pTerm; |
| 4281 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
| 4282 | int x = 0; |
| 4283 | int i; |
| 4284 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
| 4285 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue; |
| 4286 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 4287 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
| 4288 | x += pTerm->truthProb; |
| 4289 | } |
| 4290 | for(i=pLoop->nLTerm-1; i>=0; i--){ |
| 4291 | x -= pLoop->aLTerm[i]->truthProb; |
| 4292 | } |
| 4293 | if( x<0 ) pLoop->nOut += x; |
| 4294 | } |
| 4295 | |
| 4296 | /* |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4297 | ** 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] | 4298 | ** Try to match one more. |
| 4299 | ** |
| 4300 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 4301 | ** INTEGER PRIMARY KEY. |
| 4302 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4303 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4304 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 4305 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 4306 | Index *pProbe, /* An index on pSrc */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4307 | WhereCost nInMul /* log(Number of iterations due to IN) */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4308 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4309 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 4310 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 4311 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4312 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 4313 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4314 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4315 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4316 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 4317 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 4318 | int saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 4319 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 4320 | WhereCost saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4321 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4322 | int rc = SQLITE_OK; /* Return code */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4323 | WhereCost nRowEst; /* Estimated index selectivity */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4324 | WhereCost rLogSize; /* Logarithm of table size */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4325 | WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4326 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4327 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4328 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4329 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4330 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4331 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 4332 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 4333 | opMask = WO_LT|WO_LE; |
| 4334 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 4335 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4336 | }else{ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4337 | 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] | 4338 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4339 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4340 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4341 | assert( pNew->u.btree.nEq<=pProbe->nColumn ); |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4342 | if( pNew->u.btree.nEq < pProbe->nColumn ){ |
| 4343 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4344 | nRowEst = whereCost(pProbe->aiRowEst[pNew->u.btree.nEq+1]); |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4345 | if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4346 | }else{ |
| 4347 | iCol = -1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4348 | nRowEst = 0; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4349 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4350 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4351 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4352 | saved_nEq = pNew->u.btree.nEq; |
| 4353 | saved_nLTerm = pNew->nLTerm; |
| 4354 | saved_wsFlags = pNew->wsFlags; |
| 4355 | saved_prereq = pNew->prereq; |
| 4356 | saved_nOut = pNew->nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4357 | pNew->rSetup = 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4358 | rLogSize = estLog(whereCost(pProbe->aiRowEst[0])); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4359 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4360 | int nIn = 0; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4361 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4362 | int nRecValid = pBuilder->nRecValid; |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 4363 | #endif |
dan | 8bff07a | 2013-08-29 14:56:14 +0000 | [diff] [blame] | 4364 | if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) |
| 4365 | && (iCol<0 || pSrc->pTab->aCol[iCol].notNull) |
| 4366 | ){ |
| 4367 | continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 4368 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4369 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 4370 | |
dan | ad45ed7 | 2013-08-08 12:21:32 +0000 | [diff] [blame] | 4371 | assert( pNew->nOut==saved_nOut ); |
| 4372 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4373 | pNew->wsFlags = saved_wsFlags; |
| 4374 | pNew->u.btree.nEq = saved_nEq; |
| 4375 | pNew->nLTerm = saved_nLTerm; |
| 4376 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4377 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 4378 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4379 | pNew->rRun = rLogSize; /* Baseline cost is log2(N). Adjustments below */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4380 | if( pTerm->eOperator & WO_IN ){ |
| 4381 | Expr *pExpr = pTerm->pExpr; |
| 4382 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4383 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4384 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
| 4385 | nIn = 46; assert( 46==whereCost(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4386 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4387 | /* "x IN (value, value, ...)" */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4388 | nIn = whereCost(pExpr->x.pList->nExpr); |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4389 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4390 | pNew->rRun += nIn; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4391 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4392 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4393 | }else if( pTerm->eOperator & (WO_EQ) ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4394 | assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4395 | || nInMul==0 ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4396 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4397 | if( iCol<0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4398 | || (pProbe->onError!=OE_None && nInMul==0 |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4399 | && pNew->u.btree.nEq==pProbe->nColumn-1) |
| 4400 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4401 | assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4402 | pNew->wsFlags |= WHERE_ONEROW; |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4403 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4404 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4405 | pNew->nOut = nRowEst + nInMul; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4406 | }else if( pTerm->eOperator & (WO_ISNULL) ){ |
| 4407 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 4408 | pNew->u.btree.nEq++; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4409 | /* TUNING: IS NULL selects 2 rows */ |
| 4410 | nIn = 10; assert( 10==whereCost(2) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4411 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4412 | }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4413 | testcase( pTerm->eOperator & WO_GT ); |
| 4414 | testcase( pTerm->eOperator & WO_GE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4415 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4416 | pBtm = pTerm; |
| 4417 | pTop = 0; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4418 | }else{ |
| 4419 | assert( pTerm->eOperator & (WO_LT|WO_LE) ); |
| 4420 | testcase( pTerm->eOperator & WO_LT ); |
| 4421 | testcase( pTerm->eOperator & WO_LE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4422 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4423 | pTop = pTerm; |
| 4424 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4425 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4426 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4427 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4428 | /* Adjust nOut and rRun for STAT3 range values */ |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 4429 | assert( pNew->nOut==saved_nOut ); |
| 4430 | whereRangeScanEst(pParse, pBuilder, pBtm, pTop, &pNew->nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4431 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4432 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 4433 | if( nInMul==0 |
| 4434 | && pProbe->nSample |
| 4435 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 4436 | && OptimizationEnabled(db, SQLITE_Stat3) |
| 4437 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4438 | Expr *pExpr = pTerm->pExpr; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4439 | tRowcnt nOut = 0; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4440 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 4441 | testcase( pTerm->eOperator & WO_EQ ); |
| 4442 | testcase( pTerm->eOperator & WO_ISNULL ); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4443 | rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4444 | }else if( (pTerm->eOperator & WO_IN) |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4445 | && !ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 4446 | rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4447 | } |
drh | 8284633 | 2013-08-01 17:21:26 +0000 | [diff] [blame] | 4448 | assert( nOut==0 || rc==SQLITE_OK ); |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 4449 | if( nOut ){ |
| 4450 | nOut = whereCost(nOut); |
| 4451 | pNew->nOut = MIN(nOut, saved_nOut); |
| 4452 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4453 | } |
| 4454 | #endif |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4455 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4456 | /* Each row involves a step of the index, then a binary search of |
| 4457 | ** the main table */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4458 | pNew->rRun = whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4459 | } |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4460 | /* Step cost for each output row */ |
| 4461 | pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 4462 | whereLoopOutputAdjust(pBuilder->pWC, pNew, pSrc->iCursor); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4463 | rc = whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4464 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
drh | bbe8b24 | 2013-06-13 15:50:59 +0000 | [diff] [blame] | 4465 | && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0)) |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4466 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4467 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4468 | } |
dan | ad45ed7 | 2013-08-08 12:21:32 +0000 | [diff] [blame] | 4469 | pNew->nOut = saved_nOut; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4470 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4471 | pBuilder->nRecValid = nRecValid; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4472 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4473 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4474 | pNew->prereq = saved_prereq; |
| 4475 | pNew->u.btree.nEq = saved_nEq; |
| 4476 | pNew->wsFlags = saved_wsFlags; |
| 4477 | pNew->nOut = saved_nOut; |
| 4478 | pNew->nLTerm = saved_nLTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4479 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
| 4482 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4483 | ** Return True if it is possible that pIndex might be useful in |
| 4484 | ** implementing the ORDER BY clause in pBuilder. |
| 4485 | ** |
| 4486 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 4487 | ** if there is no way for pIndex to be useful in implementing that |
| 4488 | ** ORDER BY clause. |
| 4489 | */ |
| 4490 | static int indexMightHelpWithOrderBy( |
| 4491 | WhereLoopBuilder *pBuilder, |
| 4492 | Index *pIndex, |
| 4493 | int iCursor |
| 4494 | ){ |
| 4495 | ExprList *pOB; |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4496 | int ii, jj; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4497 | |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4498 | if( pIndex->bUnordered ) return 0; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4499 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4500 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 4501 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4502 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 4503 | if( pExpr->iTable==iCursor ){ |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4504 | for(jj=0; jj<pIndex->nColumn; jj++){ |
| 4505 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 4506 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4507 | } |
| 4508 | } |
| 4509 | return 0; |
| 4510 | } |
| 4511 | |
| 4512 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4513 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 4514 | ** the table is used by an index. Only the first 63 columns are considered. |
| 4515 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4516 | static Bitmask columnsInIndex(Index *pIdx){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4517 | Bitmask m = 0; |
| 4518 | int j; |
| 4519 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 4520 | int x = pIdx->aiColumn[j]; |
dan | 2c18788 | 2013-08-19 19:29:50 +0000 | [diff] [blame] | 4521 | assert( x>=0 ); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4522 | testcase( x==BMS-1 ); |
| 4523 | testcase( x==BMS-2 ); |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4524 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 4525 | } |
| 4526 | return m; |
| 4527 | } |
| 4528 | |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4529 | /* Check to see if a partial index with pPartIndexWhere can be used |
| 4530 | ** in the current query. Return true if it can be and false if not. |
| 4531 | */ |
| 4532 | static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ |
| 4533 | int i; |
| 4534 | WhereTerm *pTerm; |
| 4535 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 4536 | if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1; |
| 4537 | } |
| 4538 | return 0; |
| 4539 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4540 | |
| 4541 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 4542 | ** 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] | 4543 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 4544 | ** a b-tree table, not a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4545 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4546 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4547 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4548 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4549 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4550 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4551 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4552 | Index sPk; /* A fake index object for the primary key */ |
| 4553 | tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 4554 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4555 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4556 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4557 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4558 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4559 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4560 | int b; /* A boolean value */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4561 | WhereCost rSize; /* number of rows in the table */ |
| 4562 | WhereCost rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4563 | WhereClause *pWC; /* The parsed WHERE clause */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4564 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4565 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4566 | pWInfo = pBuilder->pWInfo; |
| 4567 | pTabList = pWInfo->pTabList; |
| 4568 | pSrc = pTabList->a + pNew->iTab; |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4569 | pWC = pBuilder->pWC; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4570 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4571 | |
| 4572 | if( pSrc->pIndex ){ |
| 4573 | /* An INDEXED BY clause specifies a particular index to use */ |
| 4574 | pProbe = pSrc->pIndex; |
| 4575 | }else{ |
| 4576 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 4577 | ** variable sPk to represent the rowid primary key index. Make this |
| 4578 | ** fake index the first in a chain of Index objects with all of the real |
| 4579 | ** indices to follow */ |
| 4580 | Index *pFirst; /* First of real indices on the table */ |
| 4581 | memset(&sPk, 0, sizeof(Index)); |
| 4582 | sPk.nColumn = 1; |
| 4583 | sPk.aiColumn = &aiColumnPk; |
| 4584 | sPk.aiRowEst = aiRowEstPk; |
| 4585 | sPk.onError = OE_Replace; |
| 4586 | sPk.pTable = pSrc->pTab; |
| 4587 | aiRowEstPk[0] = pSrc->pTab->nRowEst; |
| 4588 | aiRowEstPk[1] = 1; |
| 4589 | pFirst = pSrc->pTab->pIndex; |
| 4590 | if( pSrc->notIndexed==0 ){ |
| 4591 | /* The real indices of the table are only considered if the |
| 4592 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 4593 | sPk.pNext = pFirst; |
| 4594 | } |
| 4595 | pProbe = &sPk; |
| 4596 | } |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4597 | rSize = whereCost(pSrc->pTab->nRowEst); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4598 | rLogSize = estLog(rSize); |
| 4599 | |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 4600 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4601 | /* Automatic indexes */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4602 | if( !pBuilder->pOrSet |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 4603 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 4604 | && pSrc->pIndex==0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4605 | && !pSrc->viaCoroutine |
| 4606 | && !pSrc->notIndexed |
| 4607 | && !pSrc->isCorrelated |
| 4608 | ){ |
| 4609 | /* Generate auto-index WhereLoops */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4610 | WhereTerm *pTerm; |
| 4611 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 4612 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4613 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4614 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 4615 | pNew->u.btree.nEq = 1; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4616 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4617 | pNew->nLTerm = 1; |
| 4618 | pNew->aLTerm[0] = pTerm; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4619 | /* TUNING: One-time cost for computing the automatic index is |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4620 | ** approximately 7*N*log2(N) where N is the number of rows in |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4621 | ** the table being indexed. */ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4622 | pNew->rSetup = rLogSize + rSize + 28; assert( 28==whereCost(7) ); |
| 4623 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 4624 | ** is more than the usual guess of 10 rows, since we have no way |
| 4625 | ** of knowning how selective the index will ultimately be. It would |
| 4626 | ** not be unreasonable to make this value much larger. */ |
| 4627 | pNew->nOut = 43; assert( 43==whereCost(20) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4628 | pNew->rRun = whereCostAdd(rLogSize,pNew->nOut); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4629 | pNew->wsFlags = WHERE_AUTO_INDEX; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4630 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4631 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4632 | } |
| 4633 | } |
| 4634 | } |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 4635 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4636 | |
| 4637 | /* Loop over all indices |
| 4638 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4639 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4640 | if( pProbe->pPartIdxWhere!=0 |
| 4641 | && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){ |
| 4642 | continue; /* Partial index inappropriate for this query */ |
| 4643 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4644 | pNew->u.btree.nEq = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4645 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4646 | pNew->iSortIdx = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4647 | pNew->rSetup = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4648 | pNew->prereq = mExtra; |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4649 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4650 | pNew->u.btree.pIndex = pProbe; |
| 4651 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4652 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 4653 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4654 | if( pProbe->tnum<=0 ){ |
| 4655 | /* Integer primary key index */ |
| 4656 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4657 | |
| 4658 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4659 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4660 | /* TUNING: Cost of full table scan is 3*(N + log2(N)). |
| 4661 | ** + The extra 3 factor is to encourage the use of indexed lookups |
| 4662 | ** over full scans. A smaller constant 2 is used for covering |
| 4663 | ** index scans so that a covering index scan will be favored over |
| 4664 | ** a table scan. */ |
| 4665 | pNew->rRun = whereCostAdd(rSize,rLogSize) + 16; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 4666 | whereLoopOutputAdjust(pWC, pNew, pSrc->iCursor); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4667 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 4668 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4669 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4670 | }else{ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4671 | Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4672 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4673 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4674 | /* Full scan via index */ |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4675 | if( b |
| 4676 | || ( m==0 |
| 4677 | && pProbe->bUnordered==0 |
| 4678 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 4679 | && sqlite3GlobalConfig.bUseCis |
| 4680 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 4681 | ) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4682 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4683 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4684 | if( m==0 ){ |
| 4685 | /* TUNING: Cost of a covering index scan is 2*(N + log2(N)). |
| 4686 | ** + The extra 2 factor is to encourage the use of indexed lookups |
| 4687 | ** over index scans. A table scan uses a factor of 3 so that |
| 4688 | ** index scans are favored over table scans. |
| 4689 | ** + If this covering index might also help satisfy the ORDER BY |
| 4690 | ** clause, then the cost is fudged down slightly so that this |
| 4691 | ** index is favored above other indices that have no hope of |
| 4692 | ** helping with the ORDER BY. */ |
| 4693 | pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b; |
| 4694 | }else{ |
| 4695 | assert( b!=0 ); |
| 4696 | /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N) |
| 4697 | ** which we will simplify to just N*log2(N) */ |
| 4698 | pNew->rRun = rSize + rLogSize; |
| 4699 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 4700 | whereLoopOutputAdjust(pWC, pNew, pSrc->iCursor); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4701 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame^] | 4702 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4703 | if( rc ) break; |
| 4704 | } |
| 4705 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4706 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4707 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4708 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 4709 | sqlite3Stat4ProbeFree(pBuilder->pRec); |
| 4710 | pBuilder->nRecValid = 0; |
| 4711 | pBuilder->pRec = 0; |
dan | ddc2d6e | 2013-08-06 20:15:06 +0000 | [diff] [blame] | 4712 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4713 | |
| 4714 | /* If there was an INDEXED BY clause, then only that one index is |
| 4715 | ** considered. */ |
| 4716 | if( pSrc->pIndex ) break; |
| 4717 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4718 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4721 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4722 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4723 | ** Add all WhereLoop objects for a table of the join identified by |
| 4724 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4725 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4726 | static int whereLoopAddVirtual( |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4727 | WhereLoopBuilder *pBuilder /* WHERE clause information */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4728 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4729 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4730 | Parse *pParse; /* The parsing context */ |
| 4731 | WhereClause *pWC; /* The WHERE clause */ |
| 4732 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 4733 | Table *pTab; |
| 4734 | sqlite3 *db; |
| 4735 | sqlite3_index_info *pIdxInfo; |
| 4736 | struct sqlite3_index_constraint *pIdxCons; |
| 4737 | struct sqlite3_index_constraint_usage *pUsage; |
| 4738 | WhereTerm *pTerm; |
| 4739 | int i, j; |
| 4740 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4741 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4742 | int seenIn = 0; /* True if an IN operator is seen */ |
| 4743 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 4744 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 4745 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4746 | int rc = SQLITE_OK; |
| 4747 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4748 | pWInfo = pBuilder->pWInfo; |
| 4749 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4750 | db = pParse->db; |
| 4751 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4752 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4753 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4754 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4755 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4756 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4757 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4758 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4759 | pNew->rSetup = 0; |
| 4760 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4761 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4762 | pNew->u.vtab.needFree = 0; |
| 4763 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4764 | nConstraint = pIdxInfo->nConstraint; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4765 | if( whereLoopResize(db, pNew, nConstraint) ){ |
| 4766 | sqlite3DbFree(db, pIdxInfo); |
| 4767 | return SQLITE_NOMEM; |
| 4768 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4769 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4770 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4771 | if( !seenIn && (iPhase&1)!=0 ){ |
| 4772 | iPhase++; |
| 4773 | if( iPhase>3 ) break; |
| 4774 | } |
| 4775 | if( !seenVar && iPhase>1 ) break; |
| 4776 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4777 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4778 | j = pIdxCons->iTermOffset; |
| 4779 | pTerm = &pWC->a[j]; |
| 4780 | switch( iPhase ){ |
| 4781 | case 0: /* Constants without IN operator */ |
| 4782 | pIdxCons->usable = 0; |
| 4783 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4784 | seenIn = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4785 | } |
| 4786 | if( pTerm->prereqRight!=0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4787 | seenVar = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4788 | }else if( (pTerm->eOperator & WO_IN)==0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4789 | pIdxCons->usable = 1; |
| 4790 | } |
| 4791 | break; |
| 4792 | case 1: /* Constants with IN operators */ |
| 4793 | assert( seenIn ); |
| 4794 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 4795 | break; |
| 4796 | case 2: /* Variables without IN */ |
| 4797 | assert( seenVar ); |
| 4798 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 4799 | break; |
| 4800 | default: /* Variables with IN */ |
| 4801 | assert( seenVar && seenIn ); |
| 4802 | pIdxCons->usable = 1; |
| 4803 | break; |
| 4804 | } |
| 4805 | } |
| 4806 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 4807 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4808 | pIdxInfo->idxStr = 0; |
| 4809 | pIdxInfo->idxNum = 0; |
| 4810 | pIdxInfo->needToFreeIdxStr = 0; |
| 4811 | pIdxInfo->orderByConsumed = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4812 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4813 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 4814 | if( rc ) goto whereLoopAddVtab_exit; |
| 4815 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4816 | pNew->prereq = 0; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4817 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4818 | assert( pNew->nLSlot>=nConstraint ); |
| 4819 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4820 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4821 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4822 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 4823 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4824 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4825 | || j<0 |
| 4826 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4827 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4828 | ){ |
| 4829 | rc = SQLITE_ERROR; |
| 4830 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4831 | goto whereLoopAddVtab_exit; |
| 4832 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4833 | testcase( iTerm==nConstraint-1 ); |
| 4834 | testcase( j==0 ); |
| 4835 | testcase( j==pWC->nTerm-1 ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4836 | pTerm = &pWC->a[j]; |
| 4837 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4838 | assert( iTerm<pNew->nLSlot ); |
| 4839 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4840 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4841 | testcase( iTerm==15 ); |
| 4842 | testcase( iTerm==16 ); |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4843 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4844 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4845 | if( pUsage[i].omit==0 ){ |
| 4846 | /* Do not attempt to use an IN constraint if the virtual table |
| 4847 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4848 | ** If we do attempt to use such a constraint, some rows might be |
| 4849 | ** repeated in the output. */ |
| 4850 | break; |
| 4851 | } |
| 4852 | /* A virtual table that is constrained by an IN clause may not |
| 4853 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4854 | ** is not necessarily related to the order of output terms and |
| 4855 | ** (2) Multiple outputs from a single IN value will not merge |
| 4856 | ** together. */ |
| 4857 | pIdxInfo->orderByConsumed = 0; |
| 4858 | } |
| 4859 | } |
| 4860 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4861 | if( i>=nConstraint ){ |
| 4862 | pNew->nLTerm = mxTerm+1; |
| 4863 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4864 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4865 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 4866 | pIdxInfo->needToFreeIdxStr = 0; |
| 4867 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 3b1d808 | 2013-06-03 16:56:37 +0000 | [diff] [blame] | 4868 | pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) |
| 4869 | && pIdxInfo->orderByConsumed); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4870 | pNew->rSetup = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4871 | pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4872 | /* TUNING: Every virtual table query returns 25 rows */ |
| 4873 | pNew->nOut = 46; assert( 46==whereCost(25) ); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4874 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4875 | if( pNew->u.vtab.needFree ){ |
| 4876 | sqlite3_free(pNew->u.vtab.idxStr); |
| 4877 | pNew->u.vtab.needFree = 0; |
| 4878 | } |
| 4879 | } |
| 4880 | } |
| 4881 | |
| 4882 | whereLoopAddVtab_exit: |
| 4883 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4884 | sqlite3DbFree(db, pIdxInfo); |
| 4885 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4886 | } |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4887 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4888 | |
| 4889 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4890 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 4891 | ** btrees or virtual tables. |
| 4892 | */ |
| 4893 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4894 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4895 | WhereClause *pWC; |
| 4896 | WhereLoop *pNew; |
| 4897 | WhereTerm *pTerm, *pWCEnd; |
| 4898 | int rc = SQLITE_OK; |
| 4899 | int iCur; |
| 4900 | WhereClause tempWC; |
| 4901 | WhereLoopBuilder sSubBuild; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4902 | WhereOrSet sSum, sCur, sPrev; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4903 | struct SrcList_item *pItem; |
| 4904 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4905 | pWC = pBuilder->pWC; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4906 | if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4907 | pWCEnd = pWC->a + pWC->nTerm; |
| 4908 | pNew = pBuilder->pNew; |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 4909 | memset(&sSum, 0, sizeof(sSum)); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4910 | |
| 4911 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 4912 | if( (pTerm->eOperator & WO_OR)!=0 |
| 4913 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 4914 | ){ |
| 4915 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 4916 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 4917 | WhereTerm *pOrTerm; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4918 | int once = 1; |
| 4919 | int i, j; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4920 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4921 | pItem = pWInfo->pTabList->a + pNew->iTab; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4922 | iCur = pItem->iCursor; |
| 4923 | sSubBuild = *pBuilder; |
| 4924 | sSubBuild.pOrderBy = 0; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4925 | sSubBuild.pOrSet = &sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4926 | |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4927 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4928 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4929 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 4930 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4931 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4932 | tempWC.pOuter = pWC; |
| 4933 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4934 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4935 | tempWC.a = pOrTerm; |
| 4936 | sSubBuild.pWC = &tempWC; |
| 4937 | }else{ |
| 4938 | continue; |
| 4939 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4940 | sCur.n = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4941 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4942 | if( IsVirtual(pItem->pTab) ){ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4943 | rc = whereLoopAddVirtual(&sSubBuild); |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4944 | for(i=0; i<sCur.n; i++) sCur.a[i].prereq |= mExtra; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4945 | }else |
| 4946 | #endif |
| 4947 | { |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4948 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 4949 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4950 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 4951 | if( sCur.n==0 ){ |
| 4952 | sSum.n = 0; |
| 4953 | break; |
| 4954 | }else if( once ){ |
| 4955 | whereOrMove(&sSum, &sCur); |
| 4956 | once = 0; |
| 4957 | }else{ |
| 4958 | whereOrMove(&sPrev, &sSum); |
| 4959 | sSum.n = 0; |
| 4960 | for(i=0; i<sPrev.n; i++){ |
| 4961 | for(j=0; j<sCur.n; j++){ |
| 4962 | whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, |
| 4963 | whereCostAdd(sPrev.a[i].rRun, sCur.a[j].rRun), |
| 4964 | whereCostAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); |
| 4965 | } |
| 4966 | } |
| 4967 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4968 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4969 | pNew->nLTerm = 1; |
| 4970 | pNew->aLTerm[0] = pTerm; |
| 4971 | pNew->wsFlags = WHERE_MULTI_OR; |
| 4972 | pNew->rSetup = 0; |
| 4973 | pNew->iSortIdx = 0; |
| 4974 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 4975 | for(i=0; rc==SQLITE_OK && i<sSum.n; i++){ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4976 | /* TUNING: Multiple by 3.5 for the secondary table lookup */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4977 | pNew->rRun = sSum.a[i].rRun + 18; |
| 4978 | pNew->nOut = sSum.a[i].nOut; |
| 4979 | pNew->prereq = sSum.a[i].prereq; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4980 | rc = whereLoopInsert(pBuilder, pNew); |
| 4981 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4982 | } |
| 4983 | } |
| 4984 | return rc; |
| 4985 | } |
| 4986 | |
| 4987 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4988 | ** Add all WhereLoop objects for all tables |
| 4989 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4990 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4991 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4992 | Bitmask mExtra = 0; |
| 4993 | Bitmask mPrior = 0; |
| 4994 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4995 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4996 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4997 | sqlite3 *db = pWInfo->pParse->db; |
| 4998 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4999 | int rc = SQLITE_OK; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 5000 | u8 priorJoinType = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5001 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5002 | |
| 5003 | /* Loop over the tables in the join, from left to right */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5004 | pNew = pBuilder->pNew; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 5005 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5006 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5007 | pNew->iTab = iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5008 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 5009 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5010 | mExtra = mPrior; |
| 5011 | } |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 5012 | priorJoinType = pItem->jointype; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5013 | if( IsVirtual(pItem->pTab) ){ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 5014 | rc = whereLoopAddVirtual(pBuilder); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5015 | }else{ |
| 5016 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 5017 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5018 | if( rc==SQLITE_OK ){ |
| 5019 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 5020 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5021 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5022 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5023 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 5024 | whereLoopClear(db, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5025 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5028 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5029 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5030 | ** parameters) to see if it outputs rows in the requested ORDER BY |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5031 | ** (or GROUP BY) without requiring a separate sort operation. Return: |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5032 | ** |
| 5033 | ** 0: ORDER BY is not satisfied. Sorting required |
| 5034 | ** 1: ORDER BY is satisfied. Omit sorting |
| 5035 | ** -1: Unknown at this time |
| 5036 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5037 | ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as |
| 5038 | ** strict. With GROUP BY and DISTINCT the only requirement is that |
| 5039 | ** equivalent rows appear immediately adjacent to one another. GROUP BY |
| 5040 | ** and DISTINT do not require rows to appear in any particular order as long |
| 5041 | ** as equivelent rows are grouped together. Thus for GROUP BY and DISTINCT |
| 5042 | ** the pOrderBy terms can be matched in any order. With ORDER BY, the |
| 5043 | ** pOrderBy terms must be matched in strict left-to-right order. |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5044 | */ |
| 5045 | static int wherePathSatisfiesOrderBy( |
| 5046 | WhereInfo *pWInfo, /* The WHERE clause */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5047 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5048 | WherePath *pPath, /* The WherePath to check */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5049 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 5050 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5051 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5052 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5053 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5054 | u8 revSet; /* True if rev is known */ |
| 5055 | u8 rev; /* Composite sort order */ |
| 5056 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5057 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 5058 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 5059 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5060 | u16 nColumn; /* Number of columns in pIndex */ |
| 5061 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 5062 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 5063 | int i, j; /* Loop counters */ |
| 5064 | int iCur; /* Cursor number for current WhereLoop */ |
| 5065 | int iColumn; /* A column number within table iCur */ |
drh | e8ae583 | 2013-06-19 13:32:46 +0000 | [diff] [blame] | 5066 | WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5067 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 5068 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 5069 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 5070 | Index *pIndex; /* The index associated with pLoop */ |
| 5071 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 5072 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 5073 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5074 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5075 | Bitmask ready; /* Mask of inner loops */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5076 | |
| 5077 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5078 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 5079 | ** 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] | 5080 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 5081 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5082 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 5083 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5084 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5085 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 5086 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 5087 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 5088 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 5089 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 5090 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 5091 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 5092 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 5093 | ** |
| 5094 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 5095 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 5096 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5097 | */ |
| 5098 | |
| 5099 | assert( pOrderBy!=0 ); |
| 5100 | |
| 5101 | /* Sortability of virtual tables is determined by the xBestIndex method |
| 5102 | ** of the virtual table itself */ |
| 5103 | if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5104 | testcase( nLoop>0 ); /* True when outer loops are one-row and match |
| 5105 | ** no ORDER BY terms */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5106 | return pLast->u.vtab.isOrdered; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5107 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5108 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5109 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5110 | nOrderBy = pOrderBy->nExpr; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5111 | testcase( nOrderBy==BMS-1 ); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5112 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 5113 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5114 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5115 | orderDistinctMask = 0; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5116 | ready = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5117 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5118 | if( iLoop>0 ) ready |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5119 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5120 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5121 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5122 | |
| 5123 | /* Mark off any ORDER BY term X that is a column in the table of |
| 5124 | ** the current loop for which there is term in the WHERE |
| 5125 | ** clause of the form X IS NULL or X=? that reference only outer |
| 5126 | ** loops. |
| 5127 | */ |
| 5128 | for(i=0; i<nOrderBy; i++){ |
| 5129 | if( MASKBIT(i) & obSat ) continue; |
| 5130 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 5131 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 5132 | if( pOBExpr->iTable!=iCur ) continue; |
| 5133 | pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 5134 | ~ready, WO_EQ|WO_ISNULL, 0); |
| 5135 | if( pTerm==0 ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5136 | if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5137 | const char *z1, *z2; |
| 5138 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5139 | if( !pColl ) pColl = db->pDfltColl; |
| 5140 | z1 = pColl->zName; |
| 5141 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 5142 | if( !pColl ) pColl = db->pDfltColl; |
| 5143 | z2 = pColl->zName; |
| 5144 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 5145 | } |
| 5146 | obSat |= MASKBIT(i); |
| 5147 | } |
| 5148 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5149 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 5150 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 5151 | pIndex = 0; |
| 5152 | nColumn = 0; |
| 5153 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5154 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5155 | }else{ |
| 5156 | nColumn = pIndex->nColumn; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5157 | isOrderDistinct = pIndex->onError!=OE_None; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5158 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5159 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5160 | /* Loop through all columns of the index and deal with the ones |
| 5161 | ** that are not constrained by == or IN. |
| 5162 | */ |
| 5163 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5164 | distinctColumns = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5165 | for(j=0; j<=nColumn; j++){ |
| 5166 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 5167 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5168 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5169 | if( j<pLoop->u.btree.nEq |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5170 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5171 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5172 | if( i & WO_ISNULL ){ |
| 5173 | testcase( isOrderDistinct ); |
| 5174 | isOrderDistinct = 0; |
| 5175 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5176 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5177 | } |
| 5178 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5179 | /* Get the column number in the table (iColumn) and sort order |
| 5180 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5181 | */ |
| 5182 | if( j<nColumn ){ |
| 5183 | /* Normal index columns */ |
| 5184 | iColumn = pIndex->aiColumn[j]; |
| 5185 | revIdx = pIndex->aSortOrder[j]; |
| 5186 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5187 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5188 | /* The ROWID column at the end */ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5189 | assert( j==nColumn ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5190 | iColumn = -1; |
| 5191 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5192 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5193 | |
| 5194 | /* An unconstrained column that might be NULL means that this |
| 5195 | ** WhereLoop is not well-ordered |
| 5196 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5197 | if( isOrderDistinct |
| 5198 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5199 | && j>=pLoop->u.btree.nEq |
| 5200 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 5201 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5202 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | /* Find the ORDER BY term that corresponds to the j-th column |
| 5206 | ** of the index and and mark that ORDER BY term off |
| 5207 | */ |
| 5208 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5209 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5210 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 5211 | if( MASKBIT(i) & obSat ) continue; |
| 5212 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5213 | testcase( wctrlFlags & WHERE_GROUPBY ); |
| 5214 | testcase( wctrlFlags & WHERE_DISTINCTBY ); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5215 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5216 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5217 | if( pOBExpr->iTable!=iCur ) continue; |
| 5218 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 5219 | if( iColumn>=0 ){ |
| 5220 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5221 | if( !pColl ) pColl = db->pDfltColl; |
| 5222 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 5223 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5224 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5225 | break; |
| 5226 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5227 | if( isMatch ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5228 | if( iColumn<0 ){ |
| 5229 | testcase( distinctColumns==0 ); |
| 5230 | distinctColumns = 1; |
| 5231 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5232 | obSat |= MASKBIT(i); |
| 5233 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5234 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 5235 | ** Sort order is irrelevant for a GROUP BY clause. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5236 | if( revSet ){ |
| 5237 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; |
| 5238 | }else{ |
| 5239 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 5240 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 5241 | revSet = 1; |
| 5242 | } |
| 5243 | } |
| 5244 | }else{ |
| 5245 | /* No match found */ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5246 | if( j==0 || j<nColumn ){ |
| 5247 | testcase( isOrderDistinct!=0 ); |
| 5248 | isOrderDistinct = 0; |
| 5249 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5250 | break; |
| 5251 | } |
| 5252 | } /* end Loop over all index columns */ |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5253 | if( distinctColumns ){ |
| 5254 | testcase( isOrderDistinct==0 ); |
| 5255 | isOrderDistinct = 1; |
| 5256 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5257 | } /* end-if not one-row */ |
| 5258 | |
| 5259 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5260 | if( isOrderDistinct ){ |
| 5261 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5262 | for(i=0; i<nOrderBy; i++){ |
| 5263 | Expr *p; |
| 5264 | if( MASKBIT(i) & obSat ) continue; |
| 5265 | p = pOrderBy->a[i].pExpr; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5266 | if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5267 | obSat |= MASKBIT(i); |
| 5268 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 5269 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5270 | } |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5271 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5272 | if( obSat==obDone ) return 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5273 | if( !isOrderDistinct ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5274 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5277 | #ifdef WHERETRACE_ENABLED |
| 5278 | /* For debugging use only: */ |
| 5279 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 5280 | static char zName[65]; |
| 5281 | int i; |
| 5282 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 5283 | if( pLast ) zName[i++] = pLast->cId; |
| 5284 | zName[i] = 0; |
| 5285 | return zName; |
| 5286 | } |
| 5287 | #endif |
| 5288 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5289 | |
| 5290 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 5291 | ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5292 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 5293 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 5294 | ** |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5295 | ** Assume that the total number of output rows that will need to be sorted |
| 5296 | ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 5297 | ** costs if nRowEst==0. |
| 5298 | ** |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5299 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 5300 | ** error occurs. |
| 5301 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5302 | static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5303 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5304 | int nLoop; /* Number of terms in the join */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5305 | Parse *pParse; /* Parsing context */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5306 | sqlite3 *db; /* The database connection */ |
| 5307 | int iLoop; /* Loop counter over the terms of the join */ |
| 5308 | int ii, jj; /* Loop counters */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5309 | WhereCost rCost; /* Cost of a path */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5310 | WhereCost mxCost = 0; /* Maximum cost of a set of paths */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5311 | WhereCost rSortCost; /* Cost to do a sort */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5312 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 5313 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 5314 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 5315 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 5316 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 5317 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 5318 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 5319 | char *pSpace; /* Temporary memory used by this routine */ |
| 5320 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5321 | pParse = pWInfo->pParse; |
| 5322 | db = pParse->db; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5323 | nLoop = pWInfo->nLevel; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5324 | /* TUNING: For simple queries, only the best path is tracked. |
| 5325 | ** For 2-way joins, the 5 best paths are followed. |
| 5326 | ** For joins of 3 or more tables, track the 10 best paths */ |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5327 | mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5328 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5329 | WHERETRACE(0x002, ("---- begin solver\n")); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5330 | |
| 5331 | /* Allocate and initialize space for aTo and aFrom */ |
| 5332 | ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 5333 | pSpace = sqlite3DbMallocRaw(db, ii); |
| 5334 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 5335 | aTo = (WherePath*)pSpace; |
| 5336 | aFrom = aTo+mxChoice; |
| 5337 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 5338 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5339 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5340 | pFrom->aLoop = pX; |
| 5341 | } |
| 5342 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5343 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 5344 | ** |
| 5345 | ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 5346 | ** of computing an automatic index is not paid back within the first 25 |
| 5347 | ** rows, then do not use the automatic index. */ |
| 5348 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==whereCost(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5349 | nFrom = 1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5350 | |
| 5351 | /* Precompute the cost of sorting the final result set, if the caller |
| 5352 | ** to sqlite3WhereBegin() was concerned about sorting */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5353 | rSortCost = 0; |
| 5354 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5355 | aFrom[0].isOrderedValid = 1; |
| 5356 | }else{ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5357 | /* TUNING: Estimated cost of sorting is N*log2(N) where N is the |
| 5358 | ** number of output rows. */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5359 | rSortCost = nRowEst + estLog(nRowEst); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5360 | WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost)); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
| 5363 | /* Compute successively longer WherePaths using the previous generation |
| 5364 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 5365 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5366 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 5367 | nTo = 0; |
| 5368 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 5369 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 5370 | Bitmask maskNew; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5371 | Bitmask revMask = 0; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5372 | u8 isOrderedValid = pFrom->isOrderedValid; |
| 5373 | u8 isOrdered = pFrom->isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5374 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 5375 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5376 | /* At this point, pWLoop is a candidate to be the next loop. |
| 5377 | ** Compute its cost */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5378 | rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 5379 | rCost = whereCostAdd(rCost, pFrom->rCost); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5380 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5381 | if( !isOrderedValid ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5382 | switch( wherePathSatisfiesOrderBy(pWInfo, |
| 5383 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5384 | iLoop, pWLoop, &revMask) ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5385 | case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ |
| 5386 | isOrdered = 1; |
| 5387 | isOrderedValid = 1; |
| 5388 | break; |
| 5389 | case 0: /* No. pFrom+pWLoop will require a separate sort */ |
| 5390 | isOrdered = 0; |
| 5391 | isOrderedValid = 1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5392 | rCost = whereCostAdd(rCost, rSortCost); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5393 | break; |
| 5394 | default: /* Cannot tell yet. Try again on the next iteration */ |
| 5395 | break; |
| 5396 | } |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 5397 | }else{ |
| 5398 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5399 | } |
| 5400 | /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 5401 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
| 5402 | if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5403 | testcase( jj==nTo-1 ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5404 | break; |
| 5405 | } |
| 5406 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5407 | if( jj>=nTo ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5408 | if( nTo>=mxChoice && rCost>=mxCost ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5409 | #ifdef WHERETRACE_ENABLED |
| 5410 | if( sqlite3WhereTrace&0x4 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5411 | sqlite3DebugPrintf("Skip %s cost=%3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5412 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5413 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5414 | } |
| 5415 | #endif |
| 5416 | continue; |
| 5417 | } |
| 5418 | /* Add a new Path to the aTo[] set */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5419 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5420 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5421 | jj = nTo++; |
| 5422 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5423 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 5424 | for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5425 | } |
| 5426 | pTo = &aTo[jj]; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5427 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5428 | if( sqlite3WhereTrace&0x4 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5429 | sqlite3DebugPrintf("New %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5430 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5431 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5432 | } |
| 5433 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5434 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5435 | if( pTo->rCost<=rCost ){ |
| 5436 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5437 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5438 | sqlite3DebugPrintf( |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5439 | "Skip %s cost=%-3d order=%c", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5440 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5441 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5442 | sqlite3DebugPrintf(" vs %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5443 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5444 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5445 | } |
| 5446 | #endif |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5447 | testcase( pTo->rCost==rCost ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5448 | continue; |
| 5449 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5450 | testcase( pTo->rCost==rCost+1 ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5451 | /* A new and better score for a previously created equivalent path */ |
| 5452 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5453 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5454 | sqlite3DebugPrintf( |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5455 | "Update %s cost=%-3d order=%c", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5456 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5457 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5458 | sqlite3DebugPrintf(" was %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5459 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5460 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5461 | } |
| 5462 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5463 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5464 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5465 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5466 | pTo->revLoop = revMask; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5467 | pTo->nRow = pFrom->nRow + pWLoop->nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5468 | pTo->rCost = rCost; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5469 | pTo->isOrderedValid = isOrderedValid; |
| 5470 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5471 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 5472 | pTo->aLoop[iLoop] = pWLoop; |
| 5473 | if( nTo>=mxChoice ){ |
| 5474 | mxCost = aTo[0].rCost; |
| 5475 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
| 5476 | if( pTo->rCost>mxCost ) mxCost = pTo->rCost; |
| 5477 | } |
| 5478 | } |
| 5479 | } |
| 5480 | } |
| 5481 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5482 | #ifdef WHERETRACE_ENABLED |
| 5483 | if( sqlite3WhereTrace>=2 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5484 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5485 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5486 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5487 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5488 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5489 | if( pTo->isOrderedValid && pTo->isOrdered ){ |
| 5490 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 5491 | }else{ |
| 5492 | sqlite3DebugPrintf("\n"); |
| 5493 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5494 | } |
| 5495 | } |
| 5496 | #endif |
| 5497 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5498 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5499 | pFrom = aTo; |
| 5500 | aTo = aFrom; |
| 5501 | aFrom = pFrom; |
| 5502 | nFrom = nTo; |
| 5503 | } |
| 5504 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5505 | if( nFrom==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5506 | sqlite3ErrorMsg(pParse, "no query solution"); |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5507 | sqlite3DbFree(db, pSpace); |
| 5508 | return SQLITE_ERROR; |
| 5509 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5510 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5511 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5512 | pFrom = aFrom; |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5513 | assert( nFrom==1 ); |
| 5514 | #if 0 /* The following is needed if nFrom is ever more than 1 */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5515 | for(ii=1; ii<nFrom; ii++){ |
| 5516 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 5517 | } |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5518 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5519 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5520 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5521 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5522 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 5523 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5524 | pLevel->iFrom = pWLoop->iTab; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5525 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5526 | } |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5527 | if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 5528 | && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 5529 | && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5530 | && nRowEst |
| 5531 | ){ |
| 5532 | Bitmask notUsed; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5533 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5534 | WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5535 | if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5536 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5537 | if( pFrom->isOrdered ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5538 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 5539 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5540 | }else{ |
| 5541 | pWInfo->bOBSat = 1; |
| 5542 | pWInfo->revMask = pFrom->revLoop; |
| 5543 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5544 | } |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5545 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5546 | |
| 5547 | /* Free temporary memory and return success */ |
| 5548 | sqlite3DbFree(db, pSpace); |
| 5549 | return SQLITE_OK; |
| 5550 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 5551 | |
| 5552 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5553 | ** Most queries use only a single table (they are not joins) and have |
| 5554 | ** simple == constraints against indexed fields. This routine attempts |
| 5555 | ** to plan those simple cases using much less ceremony than the |
| 5556 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 5557 | ** times for the common case. |
| 5558 | ** |
| 5559 | ** Return non-zero on success, if this query can be handled by this |
| 5560 | ** no-frills query planner. Return zero if this query needs the |
| 5561 | ** general-purpose query planner. |
| 5562 | */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5563 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5564 | WhereInfo *pWInfo; |
| 5565 | struct SrcList_item *pItem; |
| 5566 | WhereClause *pWC; |
| 5567 | WhereTerm *pTerm; |
| 5568 | WhereLoop *pLoop; |
| 5569 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5570 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5571 | Table *pTab; |
| 5572 | Index *pIdx; |
| 5573 | |
| 5574 | pWInfo = pBuilder->pWInfo; |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 5575 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5576 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 5577 | pItem = pWInfo->pTabList->a; |
| 5578 | pTab = pItem->pTab; |
| 5579 | if( IsVirtual(pTab) ) return 0; |
| 5580 | if( pItem->zIndex ) return 0; |
| 5581 | iCur = pItem->iCursor; |
| 5582 | pWC = &pWInfo->sWC; |
| 5583 | pLoop = pBuilder->pNew; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5584 | pLoop->wsFlags = 0; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5585 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5586 | if( pTerm ){ |
| 5587 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 5588 | pLoop->aLTerm[0] = pTerm; |
| 5589 | pLoop->nLTerm = 1; |
| 5590 | pLoop->u.btree.nEq = 1; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5591 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 5592 | pLoop->rRun = 33; /* 33==whereCost(10) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5593 | }else{ |
| 5594 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 5595 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 5596 | assert( ArraySize(pLoop->aLTermSpace)==4 ); |
| 5597 | if( pIdx->onError==OE_None |
| 5598 | || pIdx->pPartIdxWhere!=0 |
| 5599 | || pIdx->nColumn>ArraySize(pLoop->aLTermSpace) |
| 5600 | ) continue; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5601 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5602 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5603 | if( pTerm==0 ) break; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5604 | pLoop->aLTerm[j] = pTerm; |
| 5605 | } |
| 5606 | if( j!=pIdx->nColumn ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5607 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 5608 | if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5609 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 5610 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5611 | pLoop->nLTerm = j; |
| 5612 | pLoop->u.btree.nEq = j; |
| 5613 | pLoop->u.btree.pIndex = pIdx; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5614 | /* TUNING: Cost of a unique index lookup is 15 */ |
| 5615 | pLoop->rRun = 39; /* 39==whereCost(15) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5616 | break; |
| 5617 | } |
| 5618 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5619 | if( pLoop->wsFlags ){ |
| 5620 | pLoop->nOut = (WhereCost)1; |
| 5621 | pWInfo->a[0].pWLoop = pLoop; |
| 5622 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 5623 | pWInfo->a[0].iTabCur = iCur; |
| 5624 | pWInfo->nRowOut = 1; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5625 | if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5626 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5627 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5628 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5629 | #ifdef SQLITE_DEBUG |
| 5630 | pLoop->cId = '0'; |
| 5631 | #endif |
| 5632 | return 1; |
| 5633 | } |
| 5634 | return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5635 | } |
| 5636 | |
| 5637 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5638 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 5639 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5640 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5641 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5642 | ** in order to complete the WHERE clause processing. |
| 5643 | ** |
| 5644 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5645 | ** |
| 5646 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5647 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5648 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5649 | ** example, if the SQL is this: |
| 5650 | ** |
| 5651 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5652 | ** |
| 5653 | ** Then the code generated is conceptually like the following: |
| 5654 | ** |
| 5655 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5656 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5657 | ** foreach row3 in t3 do / |
| 5658 | ** ... |
| 5659 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5660 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5661 | ** end / |
| 5662 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5663 | ** Note that the loops might not be nested in the order in which they |
| 5664 | ** 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] | 5665 | ** use of indices. Note also that when the IN operator appears in |
| 5666 | ** the WHERE clause, it might result in additional nested loops for |
| 5667 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5668 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5669 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5670 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5671 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5672 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5673 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5674 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5675 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5676 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5677 | ** data from the various tables of the loop. |
| 5678 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5679 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5680 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5681 | ** the tables have indices and there are terms in the WHERE clause that |
| 5682 | ** refer to those indices, a complete table scan can be avoided and the |
| 5683 | ** code will run much faster. Most of the work of this routine is checking |
| 5684 | ** to see if there are indices that can be used to speed up the loop. |
| 5685 | ** |
| 5686 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 5687 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 5688 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 5689 | ** loops are evaluated and if false a jump is made around all subsequent |
| 5690 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 5691 | ** most loop) |
| 5692 | ** |
| 5693 | ** OUTER JOINS |
| 5694 | ** |
| 5695 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 5696 | ** |
| 5697 | ** foreach row1 in t1 do |
| 5698 | ** flag = 0 |
| 5699 | ** foreach row2 in t2 do |
| 5700 | ** start: |
| 5701 | ** ... |
| 5702 | ** flag = 1 |
| 5703 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5704 | ** if flag==0 then |
| 5705 | ** move the row2 cursor to a null row |
| 5706 | ** goto start |
| 5707 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5708 | ** end |
| 5709 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5710 | ** ORDER BY CLAUSE PROCESSING |
| 5711 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5712 | ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause |
| 5713 | ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5714 | ** 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] | 5715 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5716 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5717 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5718 | Parse *pParse, /* The parser context */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5719 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5720 | Expr *pWhere, /* The WHERE clause */ |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5721 | ExprList *pOrderBy, /* An ORDER BY clause, or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5722 | ExprList *pResultSet, /* Result set of the query */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 5723 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 5724 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5725 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5726 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5727 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5728 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 5729 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5730 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5731 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5732 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5733 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5734 | WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5735 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5736 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5737 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5738 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5739 | |
| 5740 | /* Variable initialization */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5741 | db = pParse->db; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5742 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5743 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5744 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5745 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 5746 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 5747 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 5748 | wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 5749 | } |
| 5750 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5751 | /* 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] | 5752 | ** bits in a Bitmask |
| 5753 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5754 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5755 | if( pTabList->nSrc>BMS ){ |
| 5756 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5757 | return 0; |
| 5758 | } |
| 5759 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5760 | /* This function normally generates a nested loop for all tables in |
| 5761 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 5762 | ** only generate code for the first table in pTabList and assume that |
| 5763 | ** any cursors associated with subsequent tables are uninitialized. |
| 5764 | */ |
| 5765 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 5766 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5767 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5768 | ** return value. A single allocation is used to store the WhereInfo |
| 5769 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 5770 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 5771 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 5772 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5773 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5774 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5775 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5776 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5777 | sqlite3DbFree(db, pWInfo); |
| 5778 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5779 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5780 | } |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5781 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5782 | pWInfo->pParse = pParse; |
| 5783 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5784 | pWInfo->pOrderBy = pOrderBy; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5785 | pWInfo->pResultSet = pResultSet; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5786 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5787 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5788 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5789 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5790 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5791 | sWLB.pWC = &pWInfo->sWC; |
drh | 1ac87e1 | 2013-07-18 14:50:56 +0000 | [diff] [blame] | 5792 | sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); |
| 5793 | assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5794 | whereLoopInit(sWLB.pNew); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5795 | #ifdef SQLITE_DEBUG |
| 5796 | sWLB.pNew->cId = '*'; |
| 5797 | #endif |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5798 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5799 | /* Split the WHERE clause into separate subexpressions where each |
| 5800 | ** subexpression is separated by an AND operator. |
| 5801 | */ |
| 5802 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5803 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5804 | sqlite3ExprCodeConstants(pParse, pWhere); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 5805 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
drh | 5e128b2 | 2013-07-09 03:04:32 +0000 | [diff] [blame] | 5806 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5807 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5808 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 5809 | ** expression and either jump over all of the code or fall thru. |
| 5810 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5811 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 5812 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 5813 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5814 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5815 | |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5816 | /* Special case: No FROM clause |
| 5817 | */ |
| 5818 | if( nTabList==0 ){ |
| 5819 | if( pOrderBy ) pWInfo->bOBSat = 1; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5820 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5821 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5822 | } |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5823 | } |
| 5824 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5825 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 5826 | ** |
| 5827 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 5828 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 5829 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 5830 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 5831 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 5832 | ** of the join. Subtracting one from the right table bitmask gives a |
| 5833 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 5834 | ** 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] | 5835 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5836 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 5837 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 5838 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 5839 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5840 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5841 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5842 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5843 | } |
| 5844 | #ifndef NDEBUG |
| 5845 | { |
| 5846 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5847 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5848 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5849 | assert( (m-1)==toTheLeft ); |
| 5850 | toTheLeft |= m; |
| 5851 | } |
| 5852 | } |
| 5853 | #endif |
| 5854 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5855 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 5856 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 5857 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 5858 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5859 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5860 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5861 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5862 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 5863 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5864 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5865 | /* If the ORDER BY (or GROUP BY) clause contains references to general |
| 5866 | ** expressions, then we won't be able to satisfy it using indices, so |
| 5867 | ** go ahead and disable it now. |
| 5868 | */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5869 | if( pOrderBy && (wctrlFlags & WHERE_WANT_DISTINCT)!=0 ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5870 | for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 5871 | Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr); |
| 5872 | if( pExpr->op!=TK_COLUMN ){ |
| 5873 | pWInfo->pOrderBy = pOrderBy = 0; |
| 5874 | break; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5875 | }else if( pExpr->iColumn<0 ){ |
| 5876 | break; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5881 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5882 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ |
| 5883 | /* The DISTINCT marking is pointless. Ignore it. */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5884 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5885 | }else if( pOrderBy==0 ){ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5886 | /* Try to ORDER BY the result set to make distinct processing easier */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5887 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5888 | pWInfo->pOrderBy = pResultSet; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5889 | } |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5890 | } |
| 5891 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5892 | /* Construct the WhereLoop objects */ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5893 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5894 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5895 | rc = whereLoopAddAll(&sWLB); |
| 5896 | if( rc ) goto whereBeginError; |
| 5897 | |
| 5898 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5899 | #ifdef WHERETRACE_ENABLED |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5900 | if( sqlite3WhereTrace ){ |
| 5901 | WhereLoop *p; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5902 | int i; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5903 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 5904 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5905 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 5906 | p->cId = zLabel[i%sizeof(zLabel)]; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5907 | whereLoopPrint(p, pTabList); |
| 5908 | } |
| 5909 | } |
| 5910 | #endif |
| 5911 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5912 | wherePathSolver(pWInfo, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5913 | if( db->mallocFailed ) goto whereBeginError; |
| 5914 | if( pWInfo->pOrderBy ){ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5915 | wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5916 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5917 | } |
| 5918 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5919 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 5920 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5921 | } |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5922 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5923 | goto whereBeginError; |
| 5924 | } |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5925 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5926 | if( sqlite3WhereTrace ){ |
| 5927 | int ii; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5928 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 5929 | if( pWInfo->bOBSat ){ |
| 5930 | sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5931 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5932 | switch( pWInfo->eDistinct ){ |
| 5933 | case WHERE_DISTINCT_UNIQUE: { |
| 5934 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 5935 | break; |
| 5936 | } |
| 5937 | case WHERE_DISTINCT_ORDERED: { |
| 5938 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 5939 | break; |
| 5940 | } |
| 5941 | case WHERE_DISTINCT_UNORDERED: { |
| 5942 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 5943 | break; |
| 5944 | } |
| 5945 | } |
| 5946 | sqlite3DebugPrintf("\n"); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5947 | for(ii=0; ii<pWInfo->nLevel; ii++){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5948 | whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5949 | } |
| 5950 | } |
| 5951 | #endif |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5952 | /* Attempt to omit tables from the join that do not effect the result */ |
drh | 1031bd9 | 2013-06-22 15:44:26 +0000 | [diff] [blame] | 5953 | if( pWInfo->nLevel>=2 |
| 5954 | && pResultSet!=0 |
| 5955 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 5956 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5957 | Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet); |
drh | 67a5ec7 | 2013-09-03 14:03:47 +0000 | [diff] [blame] | 5958 | if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5959 | while( pWInfo->nLevel>=2 ){ |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 5960 | WhereTerm *pTerm, *pEnd; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5961 | pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5962 | if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break; |
| 5963 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 5964 | && (pLoop->wsFlags & WHERE_ONEROW)==0 |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5965 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5966 | break; |
| 5967 | } |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5968 | if( (tabUsed & pLoop->maskSelf)!=0 ) break; |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 5969 | pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; |
| 5970 | for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){ |
| 5971 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 |
| 5972 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 5973 | ){ |
| 5974 | break; |
| 5975 | } |
| 5976 | } |
| 5977 | if( pTerm<pEnd ) break; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5978 | WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 5979 | pWInfo->nLevel--; |
| 5980 | nTabList--; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5981 | } |
| 5982 | } |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5983 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 5984 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5985 | |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5986 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 5987 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 5988 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 5989 | ** the statement to update a single row. |
| 5990 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 5991 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5992 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 5993 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5994 | pWInfo->okOnePass = 1; |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5995 | pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5996 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5997 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5998 | /* Open all tables in the pTabList and any indices selected for |
| 5999 | ** searching those tables. |
| 6000 | */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6001 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6002 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6003 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6004 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6005 | struct SrcList_item *pTabItem; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6006 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6007 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6008 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6009 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6010 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 6011 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 6012 | /* Do nothing */ |
| 6013 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6014 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6015 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6016 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 6017 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6018 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 6019 | }else if( IsVirtual(pTab) ){ |
| 6020 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6021 | }else |
| 6022 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6023 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6024 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6025 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 6026 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 6027 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); |
| 6028 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 6029 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6030 | Bitmask b = pTabItem->colUsed; |
| 6031 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6032 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 6033 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 6034 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6035 | assert( n<=pTab->nCol ); |
| 6036 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6037 | }else{ |
| 6038 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6039 | } |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 6040 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6041 | Index *pIx = pLoop->u.btree.pIndex; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 6042 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 6043 | /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */ |
| 6044 | int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6045 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 6046 | assert( iIndexCur>=0 ); |
| 6047 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6048 | (char*)pKey, P4_KEYINFO_HANDOFF); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 6049 | VdbeComment((v, "%s", pIx->zName)); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6050 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6051 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6052 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6053 | } |
| 6054 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 6055 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6056 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6057 | /* Generate the code to do the search. Each iteration of the for |
| 6058 | ** loop below generates code for a single nested loop of the VM |
| 6059 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6060 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 6061 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6062 | for(ii=0; ii<nTabList; ii++){ |
| 6063 | pLevel = &pWInfo->a[ii]; |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6064 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 6065 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 6066 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 6067 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 6068 | if( db->mallocFailed ) goto whereBeginError; |
| 6069 | } |
| 6070 | #endif |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6071 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6072 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6073 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 6074 | pWInfo->iContinue = pLevel->addrCont; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6075 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 6076 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 6077 | /* Done. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6078 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6079 | |
| 6080 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6081 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6082 | if( pWInfo ){ |
| 6083 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6084 | whereInfoFree(db, pWInfo); |
| 6085 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6086 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
| 6089 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6090 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6091 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6092 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6093 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6094 | Parse *pParse = pWInfo->pParse; |
| 6095 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6096 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6097 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6098 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 6099 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6100 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6101 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6102 | /* Generate loop termination code. |
| 6103 | */ |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 6104 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6105 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6106 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6107 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6108 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6109 | if( pLevel->op!=OP_Noop ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6110 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 6111 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6112 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6113 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 6114 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6115 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6116 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6117 | 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] | 6118 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 6119 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6120 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6121 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6122 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 6123 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6124 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6125 | if( pLevel->iLeftJoin ){ |
| 6126 | int addr; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 6127 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6128 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 6129 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 6130 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 6131 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 6132 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 6133 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 6134 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 6135 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 6136 | if( pLevel->op==OP_Return ){ |
| 6137 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 6138 | }else{ |
| 6139 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 6140 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 6141 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6142 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6143 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6144 | |
| 6145 | /* The "break" point is here, just past the end of the outer loop. |
| 6146 | ** Set it. |
| 6147 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6148 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6149 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6150 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6151 | */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6152 | assert( pWInfo->nLevel<=pTabList->nSrc ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6153 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6154 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6155 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6156 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 6157 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6158 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6159 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 6160 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6161 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6162 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6163 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6164 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6165 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 6166 | } |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 6167 | if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6168 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 6169 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6170 | } |
| 6171 | |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 6172 | /* If this scan uses an index, make VDBE code substitutions to read data |
| 6173 | ** from the index instead of from the table where possible. In some cases |
| 6174 | ** this optimization prevents the table from ever being read, which can |
| 6175 | ** yield a significant performance boost. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6176 | ** |
| 6177 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 6178 | ** sqlite3WhereEnd will have created code that references the table |
| 6179 | ** directly. This loop scans all that code looking for opcodes |
| 6180 | ** that reference the table and converts them into opcodes that |
| 6181 | ** reference the index. |
| 6182 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6183 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 6184 | pIdx = pLoop->u.btree.pIndex; |
| 6185 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 6186 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6187 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6188 | if( pIdx && !db->mallocFailed ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 6189 | int k, j, last; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6190 | VdbeOp *pOp; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6191 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6192 | last = sqlite3VdbeCurrentAddr(v); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6193 | k = pLevel->addrBody; |
| 6194 | pOp = sqlite3VdbeGetOp(v, k); |
| 6195 | for(; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6196 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 6197 | if( pOp->opcode==OP_Column ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6198 | for(j=0; j<pIdx->nColumn; j++){ |
| 6199 | if( pOp->p2==pIdx->aiColumn[j] ){ |
| 6200 | pOp->p2 = j; |
danielk1977 | 21de2e7 | 2007-11-29 17:43:27 +0000 | [diff] [blame] | 6201 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6202 | break; |
| 6203 | } |
| 6204 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6205 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6206 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6207 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6208 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6209 | } |
| 6210 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6211 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6212 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6213 | |
| 6214 | /* Final cleanup |
| 6215 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 6216 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6217 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6218 | return; |
| 6219 | } |