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.) |
| 55 | ** So all costs can be stored in a 16-bit unsigned integer without risk |
| 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. |
| 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. |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 61 | ** |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 62 | ** The tool/wherecosttest.c source file implements a command-line program |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame^] | 63 | ** that will convert WhereCosts to integers, convert integers to WhereCosts |
| 64 | ** and do addition and multiplication on WhereCost values. The wherecosttest |
| 65 | ** command-line program is a useful utility to have around when working with |
| 66 | ** this module. |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 67 | */ |
| 68 | typedef unsigned short int WhereCost; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 71 | ** This object contains information needed to implement a single nested |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 72 | ** loop in WHERE clause. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 73 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 74 | ** Contrast this object with WhereLoop. This object describes the |
| 75 | ** implementation of the loop. WhereLoop describes the algorithm. |
| 76 | ** This object contains a pointer to the WhereLoop algorithm as one of |
| 77 | ** its elements. |
| 78 | ** |
| 79 | ** The WhereInfo object contains a single instance of this object for |
| 80 | ** each term in the FROM clause (which is to say, for each of the |
| 81 | ** nested loops as implemented). The order of WhereLevel objects determines |
| 82 | ** the loop nested order, with WhereInfo.a[0] being the outer loop and |
| 83 | ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 84 | */ |
| 85 | struct WhereLevel { |
| 86 | int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 87 | int iTabCur; /* The VDBE cursor used to access the table */ |
| 88 | int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 89 | int addrBrk; /* Jump here to break out of the loop */ |
| 90 | int addrNxt; /* Jump here to start the next IN combination */ |
| 91 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 92 | int addrFirst; /* First instruction of interior of the loop */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 93 | u8 iFrom; /* Which entry in the FROM clause */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 94 | u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ |
| 95 | int p1, p2; /* Operands of the opcode used to ends the loop */ |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 96 | union { /* Information that depends on pWLoop->wsFlags */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 97 | struct { |
| 98 | int nIn; /* Number of entries in aInLoop[] */ |
| 99 | struct InLoop { |
| 100 | int iCur; /* The VDBE cursor used by this IN operator */ |
| 101 | int addrInTop; /* Top of the IN loop */ |
| 102 | u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 103 | } *aInLoop; /* Information about each nested IN operator */ |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 104 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 105 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 106 | } u; |
| 107 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 108 | }; |
| 109 | |
| 110 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 111 | ** Each instance of this object represents an algorithm for evaluating one |
| 112 | ** term of a join. Every term of the FROM clause will have at least |
| 113 | ** one corresponding WhereLoop object (unless INDEXED BY constraints |
| 114 | ** prevent a query solution - which is an error) and many terms of the |
| 115 | ** FROM clause will have multiple WhereLoop objects, each describing a |
| 116 | ** potential way of implementing that FROM-clause term, together with |
| 117 | ** dependencies and cost estimates for using the chosen algorithm. |
| 118 | ** |
| 119 | ** Query planning consists of building up a collection of these WhereLoop |
| 120 | ** objects, then computing a particular sequence of WhereLoop objects, with |
| 121 | ** one WhereLoop object per FROM clause term, that satisfy all dependencies |
| 122 | ** and that minimize the overall cost. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 123 | */ |
| 124 | struct WhereLoop { |
| 125 | Bitmask prereq; /* Bitmask of other loops that must run first */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 126 | Bitmask maskSelf; /* Bitmask identifying table iTab */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 127 | #ifdef SQLITE_DEBUG |
| 128 | char cId; /* Symbolic ID of this loop for debugging use */ |
| 129 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 130 | u8 iTab; /* Position in FROM clause of table for this loop */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 131 | u8 iSortIdx; /* Sorting index number. 0==None */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 132 | WhereCost rSetup; /* One-time setup cost (ex: create transient index) */ |
| 133 | WhereCost rRun; /* Cost of running each loop */ |
| 134 | WhereCost nOut; /* Estimated number of output rows */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 135 | union { |
| 136 | struct { /* Information for internal btree tables */ |
| 137 | int nEq; /* Number of equality constraints */ |
| 138 | Index *pIndex; /* Index used, or NULL */ |
| 139 | } btree; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 140 | struct { /* Information for virtual tables */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 141 | int idxNum; /* Index number */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 142 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 143 | u8 isOrdered; /* True if satisfies ORDER BY */ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 144 | u16 omitMask; /* Terms that may be omitted */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 145 | char *idxStr; /* Index identifier string */ |
| 146 | } vtab; |
| 147 | } u; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 148 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 149 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 150 | /**** whereLoopXfer() copies fields above ***********************/ |
| 151 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 152 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 153 | WhereTerm **aLTerm; /* WhereTerms used */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 154 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 155 | WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 158 | /* This object holds the prerequisites and the cost of running a |
| 159 | ** subquery on one operand of an OR operator in the WHERE clause. |
| 160 | ** See WhereOrSet for additional information |
| 161 | */ |
| 162 | struct WhereOrCost { |
| 163 | Bitmask prereq; /* Prerequisites */ |
| 164 | WhereCost rRun; /* Cost of running this subquery */ |
| 165 | WhereCost nOut; /* Number of outputs for this subquery */ |
| 166 | }; |
| 167 | |
| 168 | /* The WhereOrSet object holds a set of possible WhereOrCosts that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame^] | 169 | ** correspond to the subquery(s) of OR-clause processing. Only the |
| 170 | ** best N_OR_COST elements are retained. |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 171 | */ |
| 172 | #define N_OR_COST 3 |
| 173 | struct WhereOrSet { |
| 174 | u16 n; /* Number of valid a[] entries */ |
| 175 | WhereOrCost a[N_OR_COST]; /* Set of best costs */ |
| 176 | }; |
| 177 | |
| 178 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 179 | /* Forward declaration of methods */ |
| 180 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 181 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 182 | /* |
| 183 | ** Each instance of this object holds a sequence of WhereLoop objects |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 184 | ** that implement some or all of a query plan. |
| 185 | ** |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 186 | ** Think of each WhereLoop object as a node in a graph with arcs |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 187 | ** showing dependences and costs for travelling between nodes. (That is |
| 188 | ** not a completely accurate description because WhereLoop costs are a |
| 189 | ** vector, not a scalar, and because dependences are many-to-one, not |
| 190 | ** one-to-one as are graph nodes. But it is a useful visualization aid.) |
| 191 | ** Then a WherePath object is a path through the graph that visits some |
| 192 | ** or all of the WhereLoop objects once. |
| 193 | ** |
| 194 | ** The "solver" works by creating the N best WherePath objects of length |
| 195 | ** 1. Then using those as a basis to compute the N best WherePath objects |
| 196 | ** of length 2. And so forth until the length of WherePaths equals the |
| 197 | ** number of nodes in the FROM clause. The best (lowest cost) WherePath |
| 198 | ** at the end is the choosen query plan. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 199 | */ |
| 200 | struct WherePath { |
| 201 | Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 202 | Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 203 | WhereCost nRow; /* Estimated number of rows generated by this path */ |
| 204 | WhereCost rCost; /* Total cost of this path */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 205 | u8 isOrdered; /* True if this path satisfies ORDER BY */ |
| 206 | u8 isOrderedValid; /* True if the isOrdered field is valid */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 207 | WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 208 | }; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | ** The query generator uses an array of instances of this structure to |
| 212 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 213 | ** clause subexpression is separated from the others by AND operators, |
| 214 | ** usually, or sometimes subexpressions separated by OR. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 215 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 216 | ** All WhereTerms are collected into a single WhereClause structure. |
| 217 | ** The following identity holds: |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 218 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 219 | ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 220 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 221 | ** When a term is of the form: |
| 222 | ** |
| 223 | ** X <op> <expr> |
| 224 | ** |
| 225 | ** where X is a column name and <op> is one of certain operators, |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 226 | ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the |
| 227 | ** cursor number and column number for X. WhereTerm.eOperator records |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 228 | ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 229 | ** use of a bitmask encoding for the operator allows us to search |
| 230 | ** quickly for terms that match any of several different operators. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 231 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 232 | ** A WhereTerm might also be two or more subterms connected by OR: |
| 233 | ** |
| 234 | ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... |
| 235 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame^] | 236 | ** 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] | 237 | ** and the WhereTerm.u.pOrInfo field points to auxiliary information that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame^] | 238 | ** is collected about the OR clause. |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 239 | ** |
| 240 | ** If a term in the WHERE clause does not match either of the two previous |
| 241 | ** categories, then eOperator==0. The WhereTerm.pExpr field is still set |
| 242 | ** to the original subexpression content and wtFlags is set up appropriately |
| 243 | ** but no other fields in the WhereTerm object are meaningful. |
| 244 | ** |
| 245 | ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 246 | ** but they do so indirectly. A single WhereMaskSet structure translates |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 247 | ** cursor number into bits and the translated bit is stored in the prereq |
| 248 | ** fields. The translation is used in order to maximize the number of |
| 249 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 250 | ** spread out over the non-negative integers. For example, the cursor |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 251 | ** 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] | 252 | ** translates these sparse cursor numbers into consecutive integers |
| 253 | ** beginning with 0 in order to make the best possible use of the available |
| 254 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 255 | ** would be mapped into integers 0 through 7. |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 256 | ** |
| 257 | ** The number of terms in a join is limited by the number of bits |
| 258 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 259 | ** is only able to process joins with 64 or fewer tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 260 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 261 | struct WhereTerm { |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 262 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 263 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 264 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 265 | union { |
| 266 | int leftColumn; /* Column number of X in "X <op> <expr>" */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 267 | WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 268 | WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 269 | } u; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 270 | u16 eOperator; /* A WO_xx value describing <op> */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 271 | u8 wtFlags; /* TERM_xxx bit flags. See below */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 272 | u8 nChild; /* Number of children that must disable us */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 273 | WhereClause *pWC; /* The clause this term is part of */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 274 | Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 275 | Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | /* |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 279 | ** Allowed values of WhereTerm.wtFlags |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 280 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 281 | #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 282 | #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
| 283 | #define TERM_CODED 0x04 /* This term is already coded */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 284 | #define TERM_COPIED 0x08 /* Has a child */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 285 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 286 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 287 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 288 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 289 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 290 | #else |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 291 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 292 | #endif |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 293 | |
| 294 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 295 | ** An instance of the WhereScan object is used as an iterator for locating |
| 296 | ** terms in the WHERE clause that are useful to the query planner. |
| 297 | */ |
| 298 | struct WhereScan { |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 299 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 300 | WhereClause *pWC; /* WhereClause currently being scanned */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 301 | char *zCollName; /* Required collating sequence, if not NULL */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 302 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 303 | unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 304 | unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 305 | u32 opMask; /* Acceptable operators */ |
| 306 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 307 | int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ |
| 308 | }; |
| 309 | |
| 310 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 311 | ** An instance of the following structure holds all information about a |
| 312 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 313 | ** |
| 314 | ** Explanation of pOuter: For a WHERE clause of the form |
| 315 | ** |
| 316 | ** a AND ((b AND c) OR (d AND e)) AND f |
| 317 | ** |
| 318 | ** There are separate WhereClause objects for the whole clause and for |
| 319 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 320 | ** subclauses points to the WhereClause object for the whole clause. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 321 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 322 | struct WhereClause { |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 323 | WhereInfo *pWInfo; /* WHERE clause processing context */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 324 | WhereClause *pOuter; /* Outer conjunction */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 325 | u8 op; /* Split operator. TK_AND or TK_OR */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 326 | int nTerm; /* Number of terms */ |
| 327 | int nSlot; /* Number of entries in a[] */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 328 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 329 | #if defined(SQLITE_SMALL_STACK) |
| 330 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| 331 | #else |
| 332 | WhereTerm aStatic[8]; /* Initial static space for a[] */ |
| 333 | #endif |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 337 | ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to |
| 338 | ** a dynamically allocated instance of the following structure. |
| 339 | */ |
| 340 | struct WhereOrInfo { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 341 | WhereClause wc; /* Decomposition into subterms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 342 | Bitmask indexable; /* Bitmask of all indexable tables in the clause */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | /* |
| 346 | ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to |
| 347 | ** a dynamically allocated instance of the following structure. |
| 348 | */ |
| 349 | struct WhereAndInfo { |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 350 | WhereClause wc; /* The subexpression broken out */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 354 | ** An instance of the following structure keeps track of a mapping |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 355 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 356 | ** |
| 357 | ** The VDBE cursor numbers are small integers contained in |
| 358 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 359 | ** clause, the cursor numbers might not begin with 0 and they might |
| 360 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 361 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 362 | ** from the sparse cursor numbers into consecutive integers beginning |
| 363 | ** with 0. |
| 364 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 365 | ** 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] | 366 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 367 | ** |
| 368 | ** For example, if the WHERE clause expression used these VDBE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 369 | ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 370 | ** would map those cursor numbers into bits 0 through 5. |
| 371 | ** |
| 372 | ** Note that the mapping is not necessarily ordered. In the example |
| 373 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 374 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 375 | ** does not really matter. What is important is that sparse cursor |
| 376 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 377 | ** no gaps. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 378 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 379 | struct WhereMaskSet { |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 380 | int n; /* Number of assigned cursor values */ |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 381 | int ix[BMS]; /* Cursor assigned to each bit */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 384 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 385 | ** This object is a convenience wrapper holding all information needed |
| 386 | ** to construct WhereLoop objects for a particular query. |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 387 | */ |
| 388 | struct WhereLoopBuilder { |
| 389 | WhereInfo *pWInfo; /* Information about this WHERE */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 390 | WhereClause *pWC; /* WHERE clause terms */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 391 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 392 | WhereLoop *pNew; /* Template WhereLoop */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 393 | WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | /* |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 397 | ** The WHERE clause processing routine has two halves. The |
| 398 | ** first part does the start of the WHERE loop and the second |
| 399 | ** half does the tail of the WHERE loop. An instance of |
| 400 | ** this structure is returned by the first half and passed |
| 401 | ** into the second half to give some continuity. |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 402 | ** |
| 403 | ** An instance of this object holds the complete state of the query |
| 404 | ** planner. |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 405 | */ |
| 406 | struct WhereInfo { |
| 407 | Parse *pParse; /* Parsing and code generating context */ |
| 408 | SrcList *pTabList; /* List of tables in the join */ |
| 409 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 410 | ExprList *pResultSet; /* Result set. DISTINCT operates on these */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 411 | WhereLoop *pLoops; /* List of all WhereLoop objects */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 412 | Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 413 | WhereCost nRowOut; /* Estimated number of output rows */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 414 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 415 | u8 bOBSat; /* ORDER BY satisfied by indices */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 416 | u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ |
| 417 | u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
| 418 | u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 419 | u8 nLevel; /* Number of nested loop */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 420 | int iTop; /* The very beginning of the WHERE loop */ |
| 421 | int iContinue; /* Jump here to continue with next record */ |
| 422 | int iBreak; /* Jump here to break out of the loop */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 423 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 424 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 425 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 426 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 427 | }; |
| 428 | |
| 429 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 430 | ** Bitmasks for the operators on WhereTerm objects. These are all |
| 431 | ** operators that are of interest to the query planner. An |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 432 | ** OR-ed combination of these values can be used when searching for |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 433 | ** particular WhereTerms within a WhereClause. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 434 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 435 | #define WO_IN 0x001 |
| 436 | #define WO_EQ 0x002 |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 437 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 438 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 439 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 440 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 441 | #define WO_MATCH 0x040 |
| 442 | #define WO_ISNULL 0x080 |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 443 | #define WO_OR 0x100 /* Two or more OR-connected terms */ |
| 444 | #define WO_AND 0x200 /* Two or more AND-connected terms */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 445 | #define WO_EQUIV 0x400 /* Of the form A==B, both columns */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 446 | #define WO_NOOP 0x800 /* This term does not restrict search space */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 447 | |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 448 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 449 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 450 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 451 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 452 | ** These are definitions of bits in the WhereLoop.wsFlags field. |
| 453 | ** The particular combination of bits in each WhereLoop help to |
| 454 | ** determine the algorithm that WhereLoop represents. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 455 | */ |
drh | be4fe3a | 2013-07-01 10:38:35 +0000 | [diff] [blame] | 456 | #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 457 | #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 458 | #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 459 | #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 460 | #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 461 | #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 462 | #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 463 | #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 464 | #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 465 | #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 466 | #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 467 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 468 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 469 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 470 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 471 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 472 | |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 473 | |
| 474 | /* Convert a WhereCost value (10 times log2(X)) into its integer value X. |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 475 | ** A rough approximation is used. The value returned is not exact. |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 476 | */ |
| 477 | static u64 whereCostToInt(WhereCost x){ |
| 478 | u64 n; |
drh | 12ffbc7 | 2013-06-13 14:51:53 +0000 | [diff] [blame] | 479 | if( x<10 ) return 1; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 480 | n = x%10; |
| 481 | x /= 10; |
| 482 | if( n>=5 ) n -= 2; |
| 483 | else if( n>=1 ) n -= 1; |
| 484 | if( x>=3 ) return (n+8)<<(x-3); |
| 485 | return (n+8)>>(3-x); |
| 486 | } |
| 487 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 488 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 489 | ** Return the estimated number of output rows from a WHERE clause |
| 490 | */ |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 491 | u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 492 | return whereCostToInt(pWInfo->nRowOut); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* |
| 496 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 497 | ** WHERE clause returns outputs for DISTINCT processing. |
| 498 | */ |
| 499 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 500 | return pWInfo->eDistinct; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 505 | ** Return FALSE if the output needs to be sorted. |
| 506 | */ |
| 507 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 508 | return pWInfo->bOBSat!=0; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* |
| 512 | ** Return the VDBE address or label to jump to in order to continue |
| 513 | ** immediately with the next row of a WHERE clause. |
| 514 | */ |
| 515 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 516 | return pWInfo->iContinue; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | ** Return the VDBE address or label to jump to in order to break |
| 521 | ** out of a WHERE loop. |
| 522 | */ |
| 523 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 524 | return pWInfo->iBreak; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 529 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 530 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
| 531 | */ |
| 532 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo){ |
| 533 | return pWInfo->okOnePass; |
| 534 | } |
| 535 | |
| 536 | /* |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 537 | ** Move the content of pSrc into pDest |
| 538 | */ |
| 539 | static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 540 | pDest->n = pSrc->n; |
| 541 | memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 546 | ** |
| 547 | ** The new entry might overwrite an existing entry, or it might be |
| 548 | ** appended, or it might be discarded. Do whatever is the right thing |
| 549 | ** so that pSet keeps the N_OR_COST best entries seen so far. |
| 550 | */ |
| 551 | static int whereOrInsert( |
| 552 | WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 553 | Bitmask prereq, /* Prerequisites of the new entry */ |
| 554 | WhereCost rRun, /* Run-cost of the new entry */ |
| 555 | WhereCost nOut /* Number of outputs for the new entry */ |
| 556 | ){ |
| 557 | u16 i; |
| 558 | WhereOrCost *p; |
| 559 | for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 560 | if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 561 | goto whereOrInsert_done; |
| 562 | } |
| 563 | if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 564 | return 0; |
| 565 | } |
| 566 | } |
| 567 | if( pSet->n<N_OR_COST ){ |
| 568 | p = &pSet->a[pSet->n++]; |
| 569 | p->nOut = nOut; |
| 570 | }else{ |
| 571 | p = pSet->a; |
| 572 | for(i=1; i<pSet->n; i++){ |
| 573 | if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 574 | } |
| 575 | if( p->rRun<=rRun ) return 0; |
| 576 | } |
| 577 | whereOrInsert_done: |
| 578 | p->prereq = prereq; |
| 579 | p->rRun = rRun; |
| 580 | if( p->nOut>nOut ) p->nOut = nOut; |
| 581 | return 1; |
| 582 | } |
| 583 | |
| 584 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 585 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 586 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 587 | static void whereClauseInit( |
| 588 | WhereClause *pWC, /* The WhereClause to be initialized */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 589 | WhereInfo *pWInfo /* The WHERE processing context */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 590 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 591 | pWC->pWInfo = pWInfo; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 592 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 593 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 594 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 595 | pWC->a = pWC->aStatic; |
| 596 | } |
| 597 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 598 | /* Forward reference */ |
| 599 | static void whereClauseClear(WhereClause*); |
| 600 | |
| 601 | /* |
| 602 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 603 | */ |
| 604 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 605 | whereClauseClear(&p->wc); |
| 606 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* |
| 610 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 611 | */ |
| 612 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 613 | whereClauseClear(&p->wc); |
| 614 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 615 | } |
| 616 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 617 | /* |
| 618 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 619 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 620 | */ |
| 621 | static void whereClauseClear(WhereClause *pWC){ |
| 622 | int i; |
| 623 | WhereTerm *a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 624 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 625 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 626 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 627 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 628 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 629 | if( a->wtFlags & TERM_ORINFO ){ |
| 630 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 631 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 632 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 633 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 634 | } |
| 635 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 636 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 641 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 642 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 643 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 644 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 645 | ** allocation error. The memory allocation failure will be recorded in |
| 646 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 647 | ** |
| 648 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 649 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 650 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 651 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 652 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 653 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 654 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 655 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 656 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 657 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 658 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 659 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 660 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 661 | int idx; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 662 | testcase( wtFlags & TERM_VIRTUAL ); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 663 | if( pWC->nTerm>=pWC->nSlot ){ |
| 664 | WhereTerm *pOld = pWC->a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 665 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 666 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 667 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 668 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 669 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 670 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 671 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 672 | return 0; |
| 673 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 674 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 675 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 676 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 677 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 678 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 679 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 680 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 681 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 682 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 683 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 684 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 685 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 686 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 687 | |
| 688 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 689 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 690 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 691 | ** operator specified in the op parameter. The WhereClause structure |
| 692 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 693 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 694 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 695 | ** \________/ \_______________/ \________________/ |
| 696 | ** slot[0] slot[1] slot[2] |
| 697 | ** |
| 698 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 699 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 700 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 701 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 702 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 703 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 704 | */ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 705 | static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 706 | pWC->op = op; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 707 | if( pExpr==0 ) return; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 708 | if( pExpr->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 709 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 710 | }else{ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 711 | whereSplit(pWC, pExpr->pLeft, op); |
| 712 | whereSplit(pWC, pExpr->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 713 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 717 | ** Initialize a WhereMaskSet object |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 718 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 719 | #define initMaskSet(P) (P)->n=0 |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 720 | |
| 721 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 722 | ** Return the bitmask for the given cursor number. Return 0 if |
| 723 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 724 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 725 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 726 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 727 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 728 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 729 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 730 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 731 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 732 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 737 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 738 | ** |
| 739 | ** There is one cursor per table in the FROM clause. The number of |
| 740 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 741 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 742 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 743 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 744 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 745 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 746 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame^] | 750 | ** These routines walk (recursively) an expression tree and generate |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 751 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 752 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 753 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 754 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 755 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 756 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 757 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 758 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 759 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 760 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 761 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 762 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 763 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 764 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 765 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 766 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 767 | }else{ |
| 768 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 769 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 770 | return mask; |
| 771 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 772 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 773 | int i; |
| 774 | Bitmask mask = 0; |
| 775 | if( pList ){ |
| 776 | for(i=0; i<pList->nExpr; i++){ |
| 777 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 780 | return mask; |
| 781 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 782 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 783 | Bitmask mask = 0; |
| 784 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 785 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 786 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 787 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 788 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 789 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 790 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 791 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 792 | int i; |
| 793 | for(i=0; i<pSrc->nSrc; i++){ |
| 794 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 795 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 796 | } |
| 797 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 798 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 799 | } |
| 800 | return mask; |
| 801 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 802 | |
| 803 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 804 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 805 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 806 | ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 807 | */ |
| 808 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 809 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 810 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 811 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 812 | assert( TK_GE==TK_EQ+4 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 813 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /* |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 817 | ** Swap two objects of type TYPE. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 818 | */ |
| 819 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 820 | |
| 821 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 822 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 823 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 824 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 825 | ** If left/right precedence rules come into play when determining the |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 826 | ** collating sequence, then COLLATE operators are adjusted to ensure |
| 827 | ** that the collating sequence does not change. For example: |
| 828 | ** "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] | 829 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 830 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 831 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 832 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 833 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 834 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 835 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 836 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 837 | if( expRight==expLeft ){ |
| 838 | /* Either X and Y both have COLLATE operator or neither do */ |
| 839 | if( expRight ){ |
| 840 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 841 | ** used by clearing the EP_Collate flag from Y. */ |
| 842 | pExpr->pRight->flags &= ~EP_Collate; |
| 843 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 844 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 845 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 846 | ** it to be searched first. */ |
| 847 | pExpr->pLeft->flags |= EP_Collate; |
| 848 | } |
| 849 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 850 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 851 | if( pExpr->op>=TK_GT ){ |
| 852 | assert( TK_LT==TK_GT+2 ); |
| 853 | assert( TK_GE==TK_LE+2 ); |
| 854 | assert( TK_GT>TK_EQ ); |
| 855 | assert( TK_GT<TK_LE ); |
| 856 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 857 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 858 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 862 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 863 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 864 | static u16 operatorMask(int op){ |
| 865 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 866 | assert( allowedOp(op) ); |
| 867 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 868 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 869 | }else if( op==TK_ISNULL ){ |
| 870 | c = WO_ISNULL; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 871 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 872 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 873 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 874 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 875 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 876 | assert( op!=TK_IN || c==WO_IN ); |
| 877 | assert( op!=TK_EQ || c==WO_EQ ); |
| 878 | assert( op!=TK_LT || c==WO_LT ); |
| 879 | assert( op!=TK_LE || c==WO_LE ); |
| 880 | assert( op!=TK_GT || c==WO_GT ); |
| 881 | assert( op!=TK_GE || c==WO_GE ); |
| 882 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 886 | ** Advance to the next WhereTerm that matches according to the criteria |
| 887 | ** established when the pScan object was initialized by whereScanInit(). |
| 888 | ** Return NULL if there are no more matching WhereTerms. |
| 889 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 890 | static WhereTerm *whereScanNext(WhereScan *pScan){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 891 | int iCur; /* The cursor on the LHS of the term */ |
| 892 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 893 | Expr *pX; /* An expression being tested */ |
| 894 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 895 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 896 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 897 | |
| 898 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 899 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 900 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 901 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 902 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 903 | if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){ |
| 904 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 905 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 906 | ){ |
| 907 | int j; |
| 908 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 909 | assert( pX->op==TK_COLUMN ); |
| 910 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 911 | if( pScan->aEquiv[j]==pX->iTable |
| 912 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 913 | break; |
| 914 | } |
| 915 | } |
| 916 | if( j==pScan->nEquiv ){ |
| 917 | pScan->aEquiv[j] = pX->iTable; |
| 918 | pScan->aEquiv[j+1] = pX->iColumn; |
| 919 | pScan->nEquiv += 2; |
| 920 | } |
| 921 | } |
| 922 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 923 | /* Verify the affinity and collating sequence match */ |
| 924 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 925 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 926 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 927 | pX = pTerm->pExpr; |
| 928 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 929 | continue; |
| 930 | } |
| 931 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 932 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 933 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 934 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 935 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 936 | continue; |
| 937 | } |
| 938 | } |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 939 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 940 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 941 | && pX->iTable==pScan->aEquiv[0] |
| 942 | && pX->iColumn==pScan->aEquiv[1] |
| 943 | ){ |
| 944 | continue; |
| 945 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 946 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 947 | return pTerm; |
| 948 | } |
| 949 | } |
| 950 | } |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 951 | pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 952 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 953 | } |
| 954 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 955 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 956 | pScan->iEquiv += 2; |
| 957 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 963 | ** first match. Return NULL if there are no matches. |
| 964 | ** |
| 965 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 966 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 967 | ** iCur. The <op> must be one of the operators described by opMask. |
| 968 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 969 | ** If the search is for X and the WHERE clause contains terms of the |
| 970 | ** form X=Y then this routine might also return terms of the form |
| 971 | ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 972 | ** but is enough to handle most commonly occurring SQL statements. |
| 973 | ** |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 974 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 975 | ** index pIdx. |
| 976 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 977 | static WhereTerm *whereScanInit( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 978 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 979 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 980 | int iCur, /* Cursor to scan for */ |
| 981 | int iColumn, /* Column to scan for */ |
| 982 | u32 opMask, /* Operator(s) to scan for */ |
| 983 | Index *pIdx /* Must be compatible with this index */ |
| 984 | ){ |
| 985 | int j; |
| 986 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 987 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 988 | pScan->pOrigWC = pWC; |
| 989 | pScan->pWC = pWC; |
| 990 | if( pIdx && iColumn>=0 ){ |
| 991 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 992 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 993 | if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 994 | } |
| 995 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 996 | }else{ |
| 997 | pScan->idxaff = 0; |
| 998 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 999 | } |
| 1000 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 1001 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1002 | pScan->aEquiv[0] = iCur; |
| 1003 | pScan->aEquiv[1] = iColumn; |
| 1004 | pScan->nEquiv = 2; |
| 1005 | pScan->iEquiv = 2; |
| 1006 | return whereScanNext(pScan); |
| 1007 | } |
| 1008 | |
| 1009 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1010 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 1011 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 1012 | ** the WO_xx operator codes specified by the op parameter. |
| 1013 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 1014 | ** |
| 1015 | ** The term returned might by Y=<expr> if there is another constraint in |
| 1016 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 1017 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 1018 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 1019 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 1020 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 1021 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 1022 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 1023 | ** and ... and A9=A10 and A10=<expr>. |
| 1024 | ** |
| 1025 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 1026 | ** then try for the one with no dependencies on <expr> - in other words where |
| 1027 | ** <expr> is a constant expression of some kind. Only return entries of |
| 1028 | ** 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] | 1029 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 1030 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1031 | */ |
| 1032 | static WhereTerm *findTerm( |
| 1033 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 1034 | int iCur, /* Cursor number of LHS */ |
| 1035 | int iColumn, /* Column number of LHS */ |
| 1036 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 1037 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1038 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 1039 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1040 | WhereTerm *pResult = 0; |
| 1041 | WhereTerm *p; |
| 1042 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1043 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1044 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 1045 | while( p ){ |
| 1046 | if( (p->prereqRight & notReady)==0 ){ |
| 1047 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 1048 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1049 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1050 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1051 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 1052 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1053 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1054 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1057 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1058 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1059 | |
| 1060 | /* |
| 1061 | ** Call exprAnalyze on all terms in a WHERE clause. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1062 | */ |
| 1063 | static void exprAnalyzeAll( |
| 1064 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1065 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 1066 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1067 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1068 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1069 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1073 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1074 | /* |
| 1075 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 1076 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 1077 | ** so and false if not. |
| 1078 | ** |
| 1079 | ** In order for the operator to be optimizible, the RHS must be a string |
| 1080 | ** literal that does not begin with a wildcard. |
| 1081 | */ |
| 1082 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1083 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1084 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1085 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1086 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 1087 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1088 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1089 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 1090 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 1091 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 1092 | int c; /* One character in z[] */ |
| 1093 | int cnt; /* Number of non-wildcard prefix characters */ |
| 1094 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 1095 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1096 | sqlite3_value *pVal = 0; |
| 1097 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1098 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1099 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1100 | return 0; |
| 1101 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1102 | #ifdef SQLITE_EBCDIC |
| 1103 | if( *pnoCase ) return 0; |
| 1104 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1105 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1106 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 1107 | if( pLeft->op!=TK_COLUMN |
| 1108 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 1109 | || IsVirtual(pLeft->pTab) |
| 1110 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1111 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 1112 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1113 | return 0; |
| 1114 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1115 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1116 | |
| 1117 | pRight = pList->a[0].pExpr; |
| 1118 | op = pRight->op; |
| 1119 | if( op==TK_REGISTER ){ |
| 1120 | op = pRight->op2; |
| 1121 | } |
| 1122 | if( op==TK_VARIABLE ){ |
| 1123 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 1124 | int iCol = pRight->iColumn; |
drh | cf0fd4a | 2013-08-01 12:21:58 +0000 | [diff] [blame] | 1125 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1126 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 1127 | z = (char *)sqlite3_value_text(pVal); |
| 1128 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1129 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1130 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 1131 | }else if( op==TK_STRING ){ |
| 1132 | z = pRight->u.zToken; |
| 1133 | } |
| 1134 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1135 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1136 | 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] | 1137 | cnt++; |
| 1138 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1139 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1140 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1141 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1142 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 1143 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 1144 | *ppPrefix = pPrefix; |
| 1145 | if( op==TK_VARIABLE ){ |
| 1146 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1147 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1148 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 1149 | /* If the rhs of the LIKE expression is a variable, and the current |
| 1150 | ** value of the variable means there is no need to invoke the LIKE |
| 1151 | ** function, then no OP_Variable will be added to the program. |
| 1152 | ** This causes problems for the sqlite3_bind_parameter_name() |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1153 | ** API. To workaround them, add a dummy OP_Variable here. |
| 1154 | */ |
| 1155 | int r1 = sqlite3GetTempReg(pParse); |
| 1156 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1157 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1158 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | }else{ |
| 1162 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1163 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1164 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1165 | |
| 1166 | sqlite3ValueFree(pVal); |
| 1167 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1168 | } |
| 1169 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 1170 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1171 | |
| 1172 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1173 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1174 | ** Check to see if the given expression is of the form |
| 1175 | ** |
| 1176 | ** column MATCH expr |
| 1177 | ** |
| 1178 | ** If it is then return TRUE. If not, return FALSE. |
| 1179 | */ |
| 1180 | static int isMatchOfColumn( |
| 1181 | Expr *pExpr /* Test this expression */ |
| 1182 | ){ |
| 1183 | ExprList *pList; |
| 1184 | |
| 1185 | if( pExpr->op!=TK_FUNCTION ){ |
| 1186 | return 0; |
| 1187 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1188 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1189 | return 0; |
| 1190 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1191 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1192 | if( pList->nExpr!=2 ){ |
| 1193 | return 0; |
| 1194 | } |
| 1195 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 1196 | return 0; |
| 1197 | } |
| 1198 | return 1; |
| 1199 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1200 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1201 | |
| 1202 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1203 | ** If the pBase expression originated in the ON or USING clause of |
| 1204 | ** a join, then transfer the appropriate markings over to derived. |
| 1205 | */ |
| 1206 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
| 1207 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 1208 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 1209 | } |
| 1210 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1211 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 1212 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1213 | ** Analyze a term that consists of two or more OR-connected |
| 1214 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1215 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1216 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 1217 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1218 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1219 | ** This routine analyzes terms such as the middle term in the above example. |
| 1220 | ** A WhereOrTerm object is computed and attached to the term under |
| 1221 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1222 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1223 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 1224 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1225 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1226 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 1227 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1228 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1229 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1230 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 1231 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 1232 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 1233 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 1234 | ** (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] | 1235 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1236 | ** CASE 1: |
| 1237 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1238 | ** 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] | 1239 | ** a single table T (as shown in example B above) then create a new virtual |
| 1240 | ** term that is an equivalent IN expression. In other words, if the term |
| 1241 | ** being analyzed is: |
| 1242 | ** |
| 1243 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 1244 | ** |
| 1245 | ** then create a new virtual term like this: |
| 1246 | ** |
| 1247 | ** x IN (expr1,expr2,expr3) |
| 1248 | ** |
| 1249 | ** CASE 2: |
| 1250 | ** |
| 1251 | ** If all subterms are indexable by a single table T, then set |
| 1252 | ** |
| 1253 | ** WhereTerm.eOperator = WO_OR |
| 1254 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 1255 | ** |
| 1256 | ** A subterm is "indexable" if it is of the form |
| 1257 | ** "T.C <op> <expr>" where C is any column of table T and |
| 1258 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 1259 | ** A subterm is also indexable if it is an AND of two or more |
| 1260 | ** subsubterms at least one of which is indexable. Indexable AND |
| 1261 | ** subterms have their eOperator set to WO_AND and they have |
| 1262 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 1263 | ** |
| 1264 | ** From another point of view, "indexable" means that the subterm could |
| 1265 | ** potentially be used with an index if an appropriate index exists. |
| 1266 | ** This analysis does not consider whether or not the index exists; that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame^] | 1267 | ** is decided elsewhere. This analysis only looks at whether subterms |
| 1268 | ** appropriate for indexing exist. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1269 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame^] | 1270 | ** All examples A through E above satisfy case 2. But if a term |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1271 | ** also statisfies case 1 (such as B) we know that the optimizer will |
| 1272 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 1273 | ** satisfied. |
| 1274 | ** |
| 1275 | ** It might be the case that multiple tables are indexable. For example, |
| 1276 | ** (E) above is indexable on tables P, Q, and R. |
| 1277 | ** |
| 1278 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 1279 | ** separate indices to find rowids for each subterm and composing |
| 1280 | ** the union of all rowids using a RowSet object. This is similar |
| 1281 | ** to "bitmap indices" in other database engines. |
| 1282 | ** |
| 1283 | ** OTHERWISE: |
| 1284 | ** |
| 1285 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 1286 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1287 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1288 | static void exprAnalyzeOrTerm( |
| 1289 | SrcList *pSrc, /* the FROM clause */ |
| 1290 | WhereClause *pWC, /* the complete WHERE clause */ |
| 1291 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 1292 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1293 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 1294 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1295 | sqlite3 *db = pParse->db; /* Database connection */ |
| 1296 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 1297 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1298 | int i; /* Loop counters */ |
| 1299 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 1300 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 1301 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 1302 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 1303 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1304 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1305 | /* |
| 1306 | ** Break the OR clause into its separate subterms. The subterms are |
| 1307 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 1308 | ** object that is attached to the original OR clause term. |
| 1309 | */ |
| 1310 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 1311 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 1312 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1313 | if( pOrInfo==0 ) return; |
| 1314 | pTerm->wtFlags |= TERM_ORINFO; |
| 1315 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1316 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1317 | whereSplit(pOrWc, pExpr, TK_OR); |
| 1318 | exprAnalyzeAll(pSrc, pOrWc); |
| 1319 | if( db->mallocFailed ) return; |
| 1320 | assert( pOrWc->nTerm>=2 ); |
| 1321 | |
| 1322 | /* |
| 1323 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 1324 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 1325 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1326 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1327 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 1328 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1329 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1330 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1331 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1332 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 1333 | if( pAndInfo ){ |
| 1334 | WhereClause *pAndWC; |
| 1335 | WhereTerm *pAndTerm; |
| 1336 | int j; |
| 1337 | Bitmask b = 0; |
| 1338 | pOrTerm->u.pAndInfo = pAndInfo; |
| 1339 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 1340 | pOrTerm->eOperator = WO_AND; |
| 1341 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1342 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1343 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 1344 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 1345 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 1346 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1347 | if( !db->mallocFailed ){ |
| 1348 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 1349 | assert( pAndTerm->pExpr ); |
| 1350 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1351 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1352 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | indexable &= b; |
| 1356 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1357 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 1358 | /* Skip this term for now. We revisit it when we process the |
| 1359 | ** corresponding TERM_VIRTUAL term */ |
| 1360 | }else{ |
| 1361 | Bitmask b; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1362 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1363 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 1364 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1365 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1366 | } |
| 1367 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1368 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1369 | chngToIN = 0; |
| 1370 | }else{ |
| 1371 | chngToIN &= b; |
| 1372 | } |
| 1373 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1374 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | ** Record the set of tables that satisfy case 2. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1378 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1379 | */ |
| 1380 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1381 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1382 | |
| 1383 | /* |
| 1384 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 1385 | ** we have to do some additional checking to see if case 1 really |
| 1386 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1387 | ** |
| 1388 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 1389 | ** that there is no possibility of transforming the OR clause into an |
| 1390 | ** IN operator because one or more terms in the OR clause contain |
| 1391 | ** something other than == on a column in the single table. The 1-bit |
| 1392 | ** case means that every term of the OR clause is of the form |
| 1393 | ** "table.column=expr" for some single table. The one bit that is set |
| 1394 | ** will correspond to the common table. We still need to check to make |
| 1395 | ** sure the same column is used on all terms. The 2-bit case is when |
| 1396 | ** the all terms are of the form "table1.column=table2.column". It |
| 1397 | ** might be possible to form an IN operator with either table1.column |
| 1398 | ** or table2.column as the LHS if either is common to every term of |
| 1399 | ** the OR clause. |
| 1400 | ** |
| 1401 | ** Note that terms of the form "table.column1=table.column2" (the |
| 1402 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1403 | */ |
| 1404 | if( chngToIN ){ |
| 1405 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 1406 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 1407 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1408 | int j = 0; /* Loop counter */ |
| 1409 | |
| 1410 | /* Search for a table and column that appears on one side or the |
| 1411 | ** other of the == operator in every subterm. That table and column |
| 1412 | ** will be recorded in iCursor and iColumn. There might not be any |
| 1413 | ** such table and column. Set okToChngToIN if an appropriate table |
| 1414 | ** and column is found but leave okToChngToIN false if not found. |
| 1415 | */ |
| 1416 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 1417 | pOrTerm = pOrWc->a; |
| 1418 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1419 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1420 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1421 | if( pOrTerm->leftCursor==iCursor ){ |
| 1422 | /* This is the 2-bit case and we are on the second iteration and |
| 1423 | ** current term is from the first iteration. So skip this term. */ |
| 1424 | assert( j==1 ); |
| 1425 | continue; |
| 1426 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1427 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1428 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 1429 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 1430 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 1431 | ** and use its inversion. */ |
| 1432 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 1433 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 1434 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 1435 | continue; |
| 1436 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1437 | iColumn = pOrTerm->u.leftColumn; |
| 1438 | iCursor = pOrTerm->leftCursor; |
| 1439 | break; |
| 1440 | } |
| 1441 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1442 | /* No candidate table+column was found. This can only occur |
| 1443 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1444 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1445 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1446 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1447 | break; |
| 1448 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1449 | testcase( j==1 ); |
| 1450 | |
| 1451 | /* We have found a candidate table and column. Check to see if that |
| 1452 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1453 | okToChngToIN = 1; |
| 1454 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1455 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1456 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1457 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1458 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1459 | okToChngToIN = 0; |
| 1460 | }else{ |
| 1461 | int affLeft, affRight; |
| 1462 | /* If the right-hand side is also a column, then the affinities |
| 1463 | ** of both right and left sides must be such that no type |
| 1464 | ** conversions are required on the right. (Ticket #2249) |
| 1465 | */ |
| 1466 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1467 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1468 | if( affRight!=0 && affRight!=affLeft ){ |
| 1469 | okToChngToIN = 0; |
| 1470 | }else{ |
| 1471 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1478 | ** case 1. In that case, construct a new virtual term that is |
| 1479 | ** pTerm converted into an IN operator. |
| 1480 | */ |
| 1481 | if( okToChngToIN ){ |
| 1482 | Expr *pDup; /* A transient duplicate expression */ |
| 1483 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1484 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1485 | Expr *pNew; /* The complete IN operator */ |
| 1486 | |
| 1487 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1488 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1489 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1490 | assert( pOrTerm->leftCursor==iCursor ); |
| 1491 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1492 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1493 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1494 | pLeft = pOrTerm->pExpr->pLeft; |
| 1495 | } |
| 1496 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1497 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1498 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1499 | if( pNew ){ |
| 1500 | int idxNew; |
| 1501 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1502 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1503 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1504 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1505 | testcase( idxNew==0 ); |
| 1506 | exprAnalyze(pSrc, pWC, idxNew); |
| 1507 | pTerm = &pWC->a[idxTerm]; |
| 1508 | pWC->a[idxNew].iParent = idxTerm; |
| 1509 | pTerm->nChild = 1; |
| 1510 | }else{ |
| 1511 | sqlite3ExprListDelete(db, pList); |
| 1512 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1513 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1514 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1515 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1516 | } |
| 1517 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1518 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1519 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1520 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1521 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1522 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1523 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1524 | ** |
| 1525 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1526 | ** to the standard form of "X <op> <expr>". |
| 1527 | ** |
| 1528 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1529 | ** columns, then the original expression is unchanged and a new virtual |
| 1530 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1531 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1532 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1533 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1534 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1535 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1536 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1537 | static void exprAnalyze( |
| 1538 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1539 | WhereClause *pWC, /* the WHERE clause */ |
| 1540 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1541 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1542 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1543 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1544 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1545 | Expr *pExpr; /* The expression to be analyzed */ |
| 1546 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1547 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1548 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1549 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1550 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1551 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1552 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1553 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1554 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1555 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1556 | if( db->mallocFailed ){ |
| 1557 | return; |
| 1558 | } |
| 1559 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1560 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1561 | pExpr = pTerm->pExpr; |
| 1562 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1563 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1564 | op = pExpr->op; |
| 1565 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1566 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1567 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1568 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1569 | }else{ |
| 1570 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1571 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1572 | }else if( op==TK_ISNULL ){ |
| 1573 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1574 | }else{ |
| 1575 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1576 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1577 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1578 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1579 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1580 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1581 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1582 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1583 | } |
| 1584 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1585 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1586 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1587 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1588 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1589 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1590 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1591 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1592 | if( pLeft->op==TK_COLUMN ){ |
| 1593 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1594 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1595 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1596 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1597 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1598 | WhereTerm *pNew; |
| 1599 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1600 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1601 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1602 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1603 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1604 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1605 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1606 | return; |
| 1607 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1608 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1609 | if( idxNew==0 ) return; |
| 1610 | pNew = &pWC->a[idxNew]; |
| 1611 | pNew->iParent = idxTerm; |
| 1612 | pTerm = &pWC->a[idxTerm]; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1613 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1614 | pTerm->wtFlags |= TERM_COPIED; |
drh | eb5bc92 | 2013-01-17 16:43:33 +0000 | [diff] [blame] | 1615 | if( pExpr->op==TK_EQ |
| 1616 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1617 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 1618 | ){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1619 | pTerm->eOperator |= WO_EQUIV; |
| 1620 | eExtraOp = WO_EQUIV; |
| 1621 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1622 | }else{ |
| 1623 | pDup = pExpr; |
| 1624 | pNew = pTerm; |
| 1625 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1626 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1627 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1628 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1629 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1630 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1631 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1632 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1633 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1634 | } |
| 1635 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1636 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1637 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1638 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1639 | ** that define the range that the BETWEEN implements. For example: |
| 1640 | ** |
| 1641 | ** a BETWEEN b AND c |
| 1642 | ** |
| 1643 | ** is converted into: |
| 1644 | ** |
| 1645 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1646 | ** |
| 1647 | ** The two new terms are added onto the end of the WhereClause object. |
| 1648 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1649 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1650 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1651 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1652 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1653 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1654 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1655 | int i; |
| 1656 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1657 | assert( pList!=0 ); |
| 1658 | assert( pList->nExpr==2 ); |
| 1659 | for(i=0; i<2; i++){ |
| 1660 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1661 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1662 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1663 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1664 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1665 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1666 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1667 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1668 | pTerm = &pWC->a[idxTerm]; |
| 1669 | pWC->a[idxNew].iParent = idxTerm; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1670 | } |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1671 | pTerm->nChild = 2; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1672 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1673 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1674 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1675 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1676 | /* Analyze a term that is composed of two or more subterms connected by |
| 1677 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1678 | */ |
| 1679 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1680 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1681 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1682 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1683 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1684 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1685 | |
| 1686 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1687 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1688 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1689 | ** |
| 1690 | ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 1691 | ** |
| 1692 | ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 1693 | ** |
| 1694 | ** The last character of the prefix "abc" is incremented to form the |
shane | 7bc71e5 | 2008-05-28 18:01:44 +0000 | [diff] [blame] | 1695 | ** termination condition "abd". |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1696 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1697 | if( pWC->op==TK_AND |
| 1698 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1699 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1700 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1701 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1702 | Expr *pNewExpr1; |
| 1703 | Expr *pNewExpr2; |
| 1704 | int idxNew1; |
| 1705 | int idxNew2; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1706 | Token sCollSeqName; /* Name of collating sequence */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1707 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1708 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1709 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1710 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1711 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1712 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1713 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1714 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1715 | /* The point is to increment the last character before the first |
| 1716 | ** wildcard. But if we increment '@', that will push it into the |
| 1717 | ** alphabetic range where case conversions will mess up the |
| 1718 | ** inequality. To avoid this, make sure to also run the full |
| 1719 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1720 | */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 1721 | if( c=='A'-1 ) isComplete = 0; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1722 | c = sqlite3UpperToLower[c]; |
| 1723 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1724 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1725 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1726 | sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 1727 | sCollSeqName.n = 6; |
| 1728 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1729 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1730 | sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1731 | pStr1, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1732 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1733 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1734 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1735 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1736 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1737 | sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1738 | pStr2, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1739 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1740 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1741 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1742 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1743 | if( isComplete ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1744 | pWC->a[idxNew1].iParent = idxTerm; |
| 1745 | pWC->a[idxNew2].iParent = idxTerm; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1746 | pTerm->nChild = 2; |
| 1747 | } |
| 1748 | } |
| 1749 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1750 | |
| 1751 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1752 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1753 | ** current expression is of the form: column MATCH expr. |
| 1754 | ** This information is used by the xBestIndex methods of |
| 1755 | ** virtual tables. The native query optimizer does not attempt |
| 1756 | ** to do anything with MATCH functions. |
| 1757 | */ |
| 1758 | if( isMatchOfColumn(pExpr) ){ |
| 1759 | int idxNew; |
| 1760 | Expr *pRight, *pLeft; |
| 1761 | WhereTerm *pNewTerm; |
| 1762 | Bitmask prereqColumn, prereqExpr; |
| 1763 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1764 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1765 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1766 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1767 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1768 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1769 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1770 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1771 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1772 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1773 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1774 | pNewTerm = &pWC->a[idxNew]; |
| 1775 | pNewTerm->prereqRight = prereqExpr; |
| 1776 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1777 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1778 | pNewTerm->eOperator = WO_MATCH; |
| 1779 | pNewTerm->iParent = idxTerm; |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1780 | pTerm = &pWC->a[idxTerm]; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1781 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1782 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1783 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1784 | } |
| 1785 | } |
| 1786 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1787 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1788 | #ifdef SQLITE_ENABLE_STAT3 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1789 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1790 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1791 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1792 | ** virtual term of that form. |
| 1793 | ** |
| 1794 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 1795 | ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 1796 | ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 1797 | ** the start of the loop will prevent any results from being returned. |
| 1798 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1799 | if( pExpr->op==TK_NOTNULL |
| 1800 | && pExpr->pLeft->op==TK_COLUMN |
| 1801 | && pExpr->pLeft->iColumn>=0 |
drh | 40aa936 | 2013-06-28 17:29:25 +0000 | [diff] [blame] | 1802 | && OptimizationEnabled(db, SQLITE_Stat3) |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1803 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1804 | Expr *pNewExpr; |
| 1805 | Expr *pLeft = pExpr->pLeft; |
| 1806 | int idxNew; |
| 1807 | WhereTerm *pNewTerm; |
| 1808 | |
| 1809 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1810 | sqlite3ExprDup(db, pLeft, 0), |
| 1811 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1812 | |
| 1813 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1814 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1815 | if( idxNew ){ |
| 1816 | pNewTerm = &pWC->a[idxNew]; |
| 1817 | pNewTerm->prereqRight = 0; |
| 1818 | pNewTerm->leftCursor = pLeft->iTable; |
| 1819 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1820 | pNewTerm->eOperator = WO_GT; |
| 1821 | pNewTerm->iParent = idxTerm; |
| 1822 | pTerm = &pWC->a[idxTerm]; |
| 1823 | pTerm->nChild = 1; |
| 1824 | pTerm->wtFlags |= TERM_COPIED; |
| 1825 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1826 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1827 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1828 | #endif /* SQLITE_ENABLE_STAT */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1829 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1830 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1831 | ** an index for tables to the left of the join. |
| 1832 | */ |
| 1833 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1836 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1837 | ** This function searches pList for a entry that matches the iCol-th column |
| 1838 | ** of index pIdx. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1839 | ** |
| 1840 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1841 | ** no expression is found, -1 is returned. |
| 1842 | */ |
| 1843 | static int findIndexCol( |
| 1844 | Parse *pParse, /* Parse context */ |
| 1845 | ExprList *pList, /* Expression list to search */ |
| 1846 | int iBase, /* Cursor for table associated with pIdx */ |
| 1847 | Index *pIdx, /* Index to match column of */ |
| 1848 | int iCol /* Column of index to match */ |
| 1849 | ){ |
| 1850 | int i; |
| 1851 | const char *zColl = pIdx->azColl[iCol]; |
| 1852 | |
| 1853 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1854 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1855 | if( p->op==TK_COLUMN |
| 1856 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1857 | && p->iTable==iBase |
| 1858 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1859 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1860 | if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1861 | return i; |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | return -1; |
| 1867 | } |
| 1868 | |
| 1869 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1870 | ** Return true if the DISTINCT expression-list passed as the third argument |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1871 | ** is redundant. |
| 1872 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1873 | ** A DISTINCT list is redundant if the database contains some subset of |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1874 | ** columns that are unique and non-null. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1875 | */ |
| 1876 | static int isDistinctRedundant( |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1877 | Parse *pParse, /* Parsing context */ |
| 1878 | SrcList *pTabList, /* The FROM clause */ |
| 1879 | WhereClause *pWC, /* The WHERE clause */ |
| 1880 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1881 | ){ |
| 1882 | Table *pTab; |
| 1883 | Index *pIdx; |
| 1884 | int i; |
| 1885 | int iBase; |
| 1886 | |
| 1887 | /* If there is more than one table or sub-select in the FROM clause of |
| 1888 | ** this query, then it will not be possible to show that the DISTINCT |
| 1889 | ** clause is redundant. */ |
| 1890 | if( pTabList->nSrc!=1 ) return 0; |
| 1891 | iBase = pTabList->a[0].iCursor; |
| 1892 | pTab = pTabList->a[0].pTab; |
| 1893 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1894 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1895 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1896 | ** current SELECT is a correlated sub-query. |
| 1897 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1898 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1899 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1900 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | /* Loop through all indices on the table, checking each to see if it makes |
| 1904 | ** the DISTINCT qualifier redundant. It does so if: |
| 1905 | ** |
| 1906 | ** 1. The index is itself UNIQUE, and |
| 1907 | ** |
| 1908 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1909 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1910 | ** where X is a constant value. The collation sequences of the |
| 1911 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1912 | ** |
| 1913 | ** 3. All of those index columns for which the WHERE clause does not |
| 1914 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1915 | */ |
| 1916 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1917 | if( pIdx->onError==OE_None ) continue; |
| 1918 | for(i=0; i<pIdx->nColumn; i++){ |
| 1919 | int iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1920 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1921 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
| 1922 | if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){ |
| 1923 | break; |
| 1924 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | if( i==pIdx->nColumn ){ |
| 1928 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1929 | return 1; |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | return 0; |
| 1934 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1935 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1936 | /* |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame^] | 1937 | ** Find (an approximate) sum of two WhereCosts. This computation is |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1938 | ** not a simple "+" operator because WhereCost is stored as a logarithmic |
| 1939 | ** value. |
| 1940 | ** |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1941 | */ |
| 1942 | static WhereCost whereCostAdd(WhereCost a, WhereCost b){ |
| 1943 | static const unsigned char x[] = { |
| 1944 | 10, 10, /* 0,1 */ |
| 1945 | 9, 9, /* 2,3 */ |
| 1946 | 8, 8, /* 4,5 */ |
| 1947 | 7, 7, 7, /* 6,7,8 */ |
| 1948 | 6, 6, 6, /* 9,10,11 */ |
| 1949 | 5, 5, 5, /* 12-14 */ |
| 1950 | 4, 4, 4, 4, /* 15-18 */ |
| 1951 | 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 1952 | 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 1953 | }; |
| 1954 | if( a>=b ){ |
| 1955 | if( a>b+49 ) return a; |
| 1956 | if( a>b+31 ) return a+1; |
| 1957 | return a+x[a-b]; |
| 1958 | }else{ |
| 1959 | if( b>a+49 ) return b; |
| 1960 | if( b>a+31 ) return b+1; |
| 1961 | return b+x[b-a]; |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1966 | ** Convert an integer into a WhereCost. In other words, compute a |
| 1967 | ** good approximatation for 10*log2(x). |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1968 | */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 1969 | static WhereCost whereCost(tRowcnt x){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1970 | static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 1971 | WhereCost y = 40; |
| 1972 | if( x<8 ){ |
| 1973 | if( x<2 ) return 0; |
| 1974 | while( x<8 ){ y -= 10; x <<= 1; } |
| 1975 | }else{ |
| 1976 | while( x>255 ){ y += 40; x >>= 4; } |
| 1977 | while( x>15 ){ y += 10; x >>= 1; } |
| 1978 | } |
| 1979 | return a[x&7] + y - 10; |
| 1980 | } |
| 1981 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1982 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1983 | /* |
| 1984 | ** Convert a double (as received from xBestIndex of a virtual table) |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1985 | ** into a WhereCost. In other words, compute an approximation for |
| 1986 | ** 10*log2(x). |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1987 | */ |
| 1988 | static WhereCost whereCostFromDouble(double x){ |
| 1989 | u64 a; |
| 1990 | WhereCost e; |
| 1991 | assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 1992 | if( x<=1 ) return 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 1993 | if( x<=2000000000 ) return whereCost((tRowcnt)x); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1994 | memcpy(&a, &x, 8); |
| 1995 | e = (a>>52) - 1022; |
| 1996 | return e*10; |
| 1997 | } |
| 1998 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 1999 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2000 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2001 | ** Estimate the logarithm of the input value to base 2. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2002 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2003 | static WhereCost estLog(WhereCost N){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2004 | WhereCost x = whereCost(N); |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 2005 | return x>33 ? x - 33 : 0; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 2008 | /* |
| 2009 | ** Two routines for printing the content of an sqlite3_index_info |
| 2010 | ** structure. Used for testing and debugging only. If neither |
| 2011 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 2012 | ** are no-ops. |
| 2013 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 2014 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 2015 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 2016 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 2017 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 2018 | for(i=0; i<p->nConstraint; i++){ |
| 2019 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 2020 | i, |
| 2021 | p->aConstraint[i].iColumn, |
| 2022 | p->aConstraint[i].iTermOffset, |
| 2023 | p->aConstraint[i].op, |
| 2024 | p->aConstraint[i].usable); |
| 2025 | } |
| 2026 | for(i=0; i<p->nOrderBy; i++){ |
| 2027 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 2028 | i, |
| 2029 | p->aOrderBy[i].iColumn, |
| 2030 | p->aOrderBy[i].desc); |
| 2031 | } |
| 2032 | } |
| 2033 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 2034 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 2035 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 2036 | for(i=0; i<p->nConstraint; i++){ |
| 2037 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 2038 | i, |
| 2039 | p->aConstraintUsage[i].argvIndex, |
| 2040 | p->aConstraintUsage[i].omit); |
| 2041 | } |
| 2042 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 2043 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 2044 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 2045 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
| 2046 | } |
| 2047 | #else |
| 2048 | #define TRACE_IDX_INPUTS(A) |
| 2049 | #define TRACE_IDX_OUTPUTS(A) |
| 2050 | #endif |
| 2051 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2052 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2053 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2054 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 2055 | ** could be used with an index to access pSrc, assuming an appropriate |
| 2056 | ** index existed. |
| 2057 | */ |
| 2058 | static int termCanDriveIndex( |
| 2059 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 2060 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 2061 | Bitmask notReady /* Tables in outer loops of the join */ |
| 2062 | ){ |
| 2063 | char aff; |
| 2064 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2065 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2066 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 2067 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2068 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 2069 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 2070 | return 1; |
| 2071 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2072 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2073 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2074 | |
| 2075 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2076 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2077 | ** Generate code to construct the Index object for an automatic index |
| 2078 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 2079 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2080 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2081 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2082 | Parse *pParse, /* The parsing context */ |
| 2083 | WhereClause *pWC, /* The WHERE clause */ |
| 2084 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 2085 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 2086 | WhereLevel *pLevel /* Write new index here */ |
| 2087 | ){ |
| 2088 | int nColumn; /* Number of columns in the constructed index */ |
| 2089 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 2090 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 2091 | int nByte; /* Byte of memory needed for pIdx */ |
| 2092 | Index *pIdx; /* Object describing the transient index */ |
| 2093 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2094 | int addrInit; /* Address of the initialization bypass jump */ |
| 2095 | Table *pTable; /* The table being indexed */ |
| 2096 | KeyInfo *pKeyinfo; /* Key information for the index */ |
| 2097 | int addrTop; /* Top of the index fill loop */ |
| 2098 | int regRecord; /* Register holding an index record */ |
| 2099 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2100 | int i; /* Loop counter */ |
| 2101 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2102 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2103 | WhereLoop *pLoop; /* The Loop object */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2104 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 2105 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 2106 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2107 | |
| 2108 | /* Generate code to skip over the creation and initialization of the |
| 2109 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 2110 | v = pParse->pVdbe; |
| 2111 | assert( v!=0 ); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2112 | addrInit = sqlite3CodeOnce(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2113 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2114 | /* Count the number of columns that will be added to the index |
| 2115 | ** and used to match WHERE clause constraints */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2116 | nColumn = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2117 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2118 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2119 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2120 | idxCols = 0; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 2121 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2122 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 2123 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2124 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2125 | testcase( iCol==BMS ); |
| 2126 | testcase( iCol==BMS-1 ); |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 2127 | if( !sentWarning ){ |
| 2128 | sqlite3_log(SQLITE_WARNING_AUTOINDEX, |
| 2129 | "automatic index on %s(%s)", pTable->zName, |
| 2130 | pTable->aCol[iCol].zName); |
| 2131 | sentWarning = 1; |
| 2132 | } |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2133 | if( (idxCols & cMask)==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2134 | if( whereLoopResize(pParse->db, pLoop, nColumn+1) ) return; |
| 2135 | pLoop->aLTerm[nColumn++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2136 | idxCols |= cMask; |
| 2137 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2138 | } |
| 2139 | } |
| 2140 | assert( nColumn>0 ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2141 | pLoop->u.btree.nEq = pLoop->nLTerm = nColumn; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 2142 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 2143 | | WHERE_AUTO_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2144 | |
| 2145 | /* Count the number of additional columns needed to create a |
| 2146 | ** covering index. A "covering index" is an index that contains all |
| 2147 | ** columns that are needed by the query. With a covering index, the |
| 2148 | ** original table never needs to be accessed. Automatic indices must |
| 2149 | ** be a covering index because the index will not be updated if the |
| 2150 | ** original table changes and the index and table cannot both be used |
| 2151 | ** if they go out of sync. |
| 2152 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2153 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2154 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2155 | testcase( pTable->nCol==BMS-1 ); |
| 2156 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2157 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2158 | if( extraCols & MASKBIT(i) ) nColumn++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2159 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2160 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2161 | nColumn += pTable->nCol - BMS + 1; |
| 2162 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2163 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2164 | |
| 2165 | /* Construct the Index object to describe this index */ |
| 2166 | nByte = sizeof(Index); |
| 2167 | nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 2168 | nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 2169 | nByte += nColumn; /* Index.aSortOrder */ |
| 2170 | pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 2171 | if( pIdx==0 ) return; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2172 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2173 | pIdx->azColl = (char**)&pIdx[1]; |
| 2174 | pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 2175 | pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 2176 | pIdx->zName = "auto-index"; |
| 2177 | pIdx->nColumn = nColumn; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2178 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2179 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2180 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2181 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2182 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2183 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2184 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 2185 | testcase( iCol==BMS-1 ); |
| 2186 | testcase( iCol==BMS ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2187 | if( (idxCols & cMask)==0 ){ |
| 2188 | Expr *pX = pTerm->pExpr; |
| 2189 | idxCols |= cMask; |
| 2190 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 2191 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 6f2e6c0 | 2011-02-17 13:33:15 +0000 | [diff] [blame] | 2192 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2193 | n++; |
| 2194 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2195 | } |
| 2196 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2197 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2198 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2199 | /* Add additional columns needed to make the automatic index into |
| 2200 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2201 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2202 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2203 | pIdx->aiColumn[n] = i; |
| 2204 | pIdx->azColl[n] = "BINARY"; |
| 2205 | n++; |
| 2206 | } |
| 2207 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2208 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2209 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 2210 | pIdx->aiColumn[n] = i; |
| 2211 | pIdx->azColl[n] = "BINARY"; |
| 2212 | n++; |
| 2213 | } |
| 2214 | } |
| 2215 | assert( n==nColumn ); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2216 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2217 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2218 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 2219 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 2220 | pLevel->iIdxCur = pParse->nTab++; |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2221 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2222 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2223 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2224 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2225 | /* Fill the automatic index with content */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2226 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); |
| 2227 | regRecord = sqlite3GetTempReg(pParse); |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 2228 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1, 0); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2229 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 2230 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2231 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2232 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2233 | sqlite3VdbeJumpHere(v, addrTop); |
| 2234 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 2235 | |
| 2236 | /* Jump here when skipping the initialization */ |
| 2237 | sqlite3VdbeJumpHere(v, addrInit); |
| 2238 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2239 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2240 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 2241 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2242 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2243 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 2244 | ** responsibility of the caller to eventually release the structure |
| 2245 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 2246 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2247 | static sqlite3_index_info *allocateIndexInfo( |
| 2248 | Parse *pParse, |
| 2249 | WhereClause *pWC, |
| 2250 | struct SrcList_item *pSrc, |
| 2251 | ExprList *pOrderBy |
| 2252 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2253 | int i, j; |
| 2254 | int nTerm; |
| 2255 | struct sqlite3_index_constraint *pIdxCons; |
| 2256 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 2257 | struct sqlite3_index_constraint_usage *pUsage; |
| 2258 | WhereTerm *pTerm; |
| 2259 | int nOrderBy; |
| 2260 | sqlite3_index_info *pIdxInfo; |
| 2261 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2262 | /* Count the number of possible WHERE clause constraints referring |
| 2263 | ** to this virtual table */ |
| 2264 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 2265 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2266 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2267 | testcase( pTerm->eOperator & WO_IN ); |
| 2268 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2269 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2270 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2271 | nTerm++; |
| 2272 | } |
| 2273 | |
| 2274 | /* If the ORDER BY clause contains only columns in the current |
| 2275 | ** virtual table then allocate space for the aOrderBy part of |
| 2276 | ** the sqlite3_index_info structure. |
| 2277 | */ |
| 2278 | nOrderBy = 0; |
| 2279 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2280 | int n = pOrderBy->nExpr; |
| 2281 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2282 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2283 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 2284 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2285 | if( i==n){ |
| 2286 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | /* Allocate the sqlite3_index_info structure |
| 2291 | */ |
| 2292 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 2293 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 2294 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 2295 | if( pIdxInfo==0 ){ |
| 2296 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2297 | return 0; |
| 2298 | } |
| 2299 | |
| 2300 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 2301 | ** many fields that are declared "const" to prevent xBestIndex from |
| 2302 | ** changing them. We have to do some funky casting in order to |
| 2303 | ** initialize those fields. |
| 2304 | */ |
| 2305 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 2306 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 2307 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 2308 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 2309 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 2310 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 2311 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 2312 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 2313 | pUsage; |
| 2314 | |
| 2315 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2316 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2317 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2318 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2319 | testcase( pTerm->eOperator & WO_IN ); |
| 2320 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2321 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2322 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2323 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 2324 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2325 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2326 | if( op==WO_IN ) op = WO_EQ; |
| 2327 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2328 | /* The direct assignment in the previous line is possible only because |
| 2329 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 2330 | ** following asserts verify this fact. */ |
| 2331 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 2332 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 2333 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 2334 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 2335 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 2336 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2337 | 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] | 2338 | j++; |
| 2339 | } |
| 2340 | for(i=0; i<nOrderBy; i++){ |
| 2341 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2342 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 2343 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 2344 | } |
| 2345 | |
| 2346 | return pIdxInfo; |
| 2347 | } |
| 2348 | |
| 2349 | /* |
| 2350 | ** The table object reference passed as the second argument to this function |
| 2351 | ** must represent a virtual table. This function invokes the xBestIndex() |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2352 | ** method of the virtual table with the sqlite3_index_info object that |
| 2353 | ** comes in as the 3rd argument to this function. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2354 | ** |
| 2355 | ** If an error occurs, pParse is populated with an error message and a |
| 2356 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 2357 | ** part of the sqlite3_index_info structure is left populated. |
| 2358 | ** |
| 2359 | ** Whether or not an error is returned, it is the responsibility of the |
| 2360 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 2361 | ** that this is required. |
| 2362 | */ |
| 2363 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 2364 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2365 | int i; |
| 2366 | int rc; |
| 2367 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2368 | TRACE_IDX_INPUTS(p); |
| 2369 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 2370 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2371 | |
| 2372 | if( rc!=SQLITE_OK ){ |
| 2373 | if( rc==SQLITE_NOMEM ){ |
| 2374 | pParse->db->mallocFailed = 1; |
| 2375 | }else if( !pVtab->zErrMsg ){ |
| 2376 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 2377 | }else{ |
| 2378 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 2379 | } |
| 2380 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2381 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2382 | pVtab->zErrMsg = 0; |
| 2383 | |
| 2384 | for(i=0; i<p->nConstraint; i++){ |
| 2385 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 2386 | sqlite3ErrorMsg(pParse, |
| 2387 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | return pParse->nErr; |
| 2392 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2393 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2394 | |
| 2395 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2396 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2397 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2398 | ** Estimate the location of a particular key among all keys in an |
| 2399 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2400 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2401 | ** aStat[0] Est. number of rows less than pVal |
| 2402 | ** aStat[1] Est. number of rows equal to pVal |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2403 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2404 | ** Return SQLITE_OK on success. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2405 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2406 | static int whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2407 | Parse *pParse, /* Database connection */ |
| 2408 | Index *pIdx, /* Index to consider domain of */ |
| 2409 | sqlite3_value *pVal, /* Value to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2410 | int roundUp, /* Round up if true. Round down if false */ |
| 2411 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2412 | ){ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2413 | tRowcnt n; |
| 2414 | IndexSample *aSample; |
| 2415 | int i, eType; |
| 2416 | int isEq = 0; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2417 | i64 v; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2418 | double r, rS; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2419 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2420 | assert( roundUp==0 || roundUp==1 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2421 | assert( pIdx->nSample>0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2422 | if( pVal==0 ) return SQLITE_ERROR; |
| 2423 | n = pIdx->aiRowEst[0]; |
| 2424 | aSample = pIdx->aSample; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2425 | eType = sqlite3_value_type(pVal); |
| 2426 | |
| 2427 | if( eType==SQLITE_INTEGER ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2428 | v = sqlite3_value_int64(pVal); |
| 2429 | r = (i64)v; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2430 | for(i=0; i<pIdx->nSample; i++){ |
| 2431 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2432 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2433 | if( aSample[i].eType==SQLITE_INTEGER ){ |
| 2434 | if( aSample[i].u.i>=v ){ |
| 2435 | isEq = aSample[i].u.i==v; |
| 2436 | break; |
| 2437 | } |
| 2438 | }else{ |
| 2439 | assert( aSample[i].eType==SQLITE_FLOAT ); |
| 2440 | if( aSample[i].u.r>=r ){ |
| 2441 | isEq = aSample[i].u.r==r; |
| 2442 | break; |
| 2443 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2444 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2445 | } |
| 2446 | }else if( eType==SQLITE_FLOAT ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2447 | r = sqlite3_value_double(pVal); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2448 | for(i=0; i<pIdx->nSample; i++){ |
| 2449 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2450 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2451 | if( aSample[i].eType==SQLITE_FLOAT ){ |
| 2452 | rS = aSample[i].u.r; |
| 2453 | }else{ |
| 2454 | rS = aSample[i].u.i; |
| 2455 | } |
| 2456 | if( rS>=r ){ |
| 2457 | isEq = rS==r; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2458 | break; |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2459 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2460 | } |
| 2461 | }else if( eType==SQLITE_NULL ){ |
| 2462 | i = 0; |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2463 | if( aSample[0].eType==SQLITE_NULL ) isEq = 1; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2464 | }else{ |
| 2465 | assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 2466 | for(i=0; i<pIdx->nSample; i++){ |
| 2467 | if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){ |
| 2468 | break; |
| 2469 | } |
| 2470 | } |
| 2471 | if( i<pIdx->nSample ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2472 | sqlite3 *db = pParse->db; |
| 2473 | CollSeq *pColl; |
| 2474 | const u8 *z; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2475 | if( eType==SQLITE_BLOB ){ |
| 2476 | z = (const u8 *)sqlite3_value_blob(pVal); |
| 2477 | pColl = db->pDfltColl; |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2478 | assert( pColl->enc==SQLITE_UTF8 ); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2479 | }else{ |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 2480 | pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl); |
drh | 472eae8 | 2013-06-20 17:32:28 +0000 | [diff] [blame] | 2481 | /* If the collating sequence was unavailable, we should have failed |
| 2482 | ** long ago and never reached this point. But we'll check just to |
| 2483 | ** be doubly sure. */ |
| 2484 | if( NEVER(pColl==0) ) return SQLITE_ERROR; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2485 | z = (const u8 *)sqlite3ValueText(pVal, pColl->enc); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2486 | if( !z ){ |
| 2487 | return SQLITE_NOMEM; |
| 2488 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2489 | assert( z && pColl && pColl->xCmp ); |
| 2490 | } |
| 2491 | n = sqlite3ValueBytes(pVal, pColl->enc); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2492 | |
| 2493 | for(; i<pIdx->nSample; i++){ |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2494 | int c; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2495 | int eSampletype = aSample[i].eType; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2496 | if( eSampletype<eType ) continue; |
| 2497 | if( eSampletype!=eType ) break; |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2498 | #ifndef SQLITE_OMIT_UTF16 |
| 2499 | if( pColl->enc!=SQLITE_UTF8 ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2500 | int nSample; |
| 2501 | char *zSample = sqlite3Utf8to16( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2502 | db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample |
| 2503 | ); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2504 | if( !zSample ){ |
| 2505 | assert( db->mallocFailed ); |
| 2506 | return SQLITE_NOMEM; |
| 2507 | } |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2508 | c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2509 | sqlite3DbFree(db, zSample); |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2510 | }else |
| 2511 | #endif |
| 2512 | { |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2513 | c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2514 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2515 | if( c>=0 ){ |
| 2516 | if( c==0 ) isEq = 1; |
| 2517 | break; |
| 2518 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2519 | } |
| 2520 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2521 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2522 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2523 | /* At this point, aSample[i] is the first sample that is greater than |
| 2524 | ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 2525 | ** than pVal. If aSample[i]==pVal, then isEq==1. |
| 2526 | */ |
| 2527 | if( isEq ){ |
| 2528 | assert( i<pIdx->nSample ); |
| 2529 | aStat[0] = aSample[i].nLt; |
| 2530 | aStat[1] = aSample[i].nEq; |
| 2531 | }else{ |
| 2532 | tRowcnt iLower, iUpper, iGap; |
| 2533 | if( i==0 ){ |
| 2534 | iLower = 0; |
| 2535 | iUpper = aSample[0].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2536 | }else{ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2537 | iUpper = i>=pIdx->nSample ? n : aSample[i].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2538 | iLower = aSample[i-1].nEq + aSample[i-1].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2539 | } |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2540 | aStat[1] = pIdx->avgEq; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2541 | if( iLower>=iUpper ){ |
| 2542 | iGap = 0; |
| 2543 | }else{ |
| 2544 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2545 | } |
| 2546 | if( roundUp ){ |
| 2547 | iGap = (iGap*2)/3; |
| 2548 | }else{ |
| 2549 | iGap = iGap/3; |
| 2550 | } |
| 2551 | aStat[0] = iLower + iGap; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2552 | } |
| 2553 | return SQLITE_OK; |
| 2554 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2555 | #endif /* SQLITE_ENABLE_STAT3 */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2556 | |
| 2557 | /* |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2558 | ** If expression pExpr represents a literal value, set *pp to point to |
| 2559 | ** an sqlite3_value structure containing the same value, with affinity |
| 2560 | ** aff applied to it, before returning. It is the responsibility of the |
| 2561 | ** caller to eventually release this structure by passing it to |
| 2562 | ** sqlite3ValueFree(). |
| 2563 | ** |
| 2564 | ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr |
| 2565 | ** is an SQL variable that currently has a non-NULL value bound to it, |
| 2566 | ** create an sqlite3_value structure containing this value, again with |
| 2567 | ** affinity aff applied to it, instead. |
| 2568 | ** |
| 2569 | ** If neither of the above apply, set *pp to NULL. |
| 2570 | ** |
| 2571 | ** If an error occurs, return an error code. Otherwise, SQLITE_OK. |
| 2572 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2573 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2574 | static int valueFromExpr( |
| 2575 | Parse *pParse, |
| 2576 | Expr *pExpr, |
| 2577 | u8 aff, |
| 2578 | sqlite3_value **pp |
| 2579 | ){ |
drh | 4278d53 | 2010-12-16 19:52:52 +0000 | [diff] [blame] | 2580 | if( pExpr->op==TK_VARIABLE |
| 2581 | || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) |
| 2582 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2583 | int iVar = pExpr->iColumn; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 2584 | sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); |
drh | cf0fd4a | 2013-08-01 12:21:58 +0000 | [diff] [blame] | 2585 | *pp = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, aff); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2586 | return SQLITE_OK; |
| 2587 | } |
| 2588 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 2589 | } |
dan | f7b0b0a | 2009-10-19 15:52:32 +0000 | [diff] [blame] | 2590 | #endif |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2591 | |
| 2592 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2593 | ** This function is used to estimate the number of rows that will be visited |
| 2594 | ** by scanning an index for a range of values. The range may have an upper |
| 2595 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2596 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2597 | ** example, assuming that index p is on t1(a): |
| 2598 | ** |
| 2599 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2600 | ** |_____| |_____| |
| 2601 | ** | | |
| 2602 | ** pLower pUpper |
| 2603 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2604 | ** 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] | 2605 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2606 | ** |
| 2607 | ** The nEq parameter is passed the index of the index column subject to the |
| 2608 | ** range constraint. Or, equivalently, the number of equality constraints |
| 2609 | ** optimized by the proposed index scan. For example, assuming index p is |
| 2610 | ** on t1(a, b), and the SQL query is: |
| 2611 | ** |
| 2612 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2613 | ** |
| 2614 | ** then nEq should be passed the value 1 (as the range restricted column, |
| 2615 | ** b, is the second left-most column of the index). Or, if the query is: |
| 2616 | ** |
| 2617 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2618 | ** |
| 2619 | ** then nEq should be passed 0. |
| 2620 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2621 | ** The returned value is an integer divisor to reduce the estimated |
| 2622 | ** search space. A return value of 1 means that range constraints are |
| 2623 | ** no help at all. A return value of 2 means range constraints are |
| 2624 | ** expected to reduce the search space by half. And so forth... |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2625 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2626 | ** In the absence of sqlite_stat3 ANALYZE data, each range inequality |
| 2627 | ** reduces the search space by a factor of 4. Hence a single constraint (x>?) |
| 2628 | ** results in a return of 4 and a range constraint (x>? AND x<?) results |
| 2629 | ** in a return of 16. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2630 | */ |
| 2631 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2632 | Parse *pParse, /* Parsing & code generating context */ |
| 2633 | Index *p, /* The index containing the range-compared column; "x" */ |
| 2634 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 2635 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2636 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2637 | WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2638 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2639 | int rc = SQLITE_OK; |
| 2640 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2641 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2642 | |
drh | 40aa936 | 2013-06-28 17:29:25 +0000 | [diff] [blame] | 2643 | if( nEq==0 && p->nSample && OptimizationEnabled(pParse->db, SQLITE_Stat3) ){ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2644 | sqlite3_value *pRangeVal; |
| 2645 | tRowcnt iLower = 0; |
| 2646 | tRowcnt iUpper = p->aiRowEst[0]; |
| 2647 | tRowcnt a[2]; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2648 | u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2649 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2650 | if( pLower ){ |
| 2651 | Expr *pExpr = pLower->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2652 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2653 | assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2654 | if( rc==SQLITE_OK |
| 2655 | && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK |
| 2656 | ){ |
| 2657 | iLower = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2658 | if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2659 | } |
| 2660 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2661 | } |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2662 | if( rc==SQLITE_OK && pUpper ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2663 | Expr *pExpr = pUpper->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2664 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2665 | assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2666 | if( rc==SQLITE_OK |
| 2667 | && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK |
| 2668 | ){ |
| 2669 | iUpper = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2670 | if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1]; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2671 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2672 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2673 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2674 | if( rc==SQLITE_OK ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2675 | WhereCost iBase = whereCost(p->aiRowEst[0]); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2676 | if( iUpper>iLower ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2677 | iBase -= whereCost(iUpper - iLower); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2678 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2679 | *pRangeDiv = iBase; |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2680 | WHERETRACE(0x100, ("range scan regions: %u..%u div=%d\n", |
| 2681 | (u32)iLower, (u32)iUpper, *pRangeDiv)); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2682 | return SQLITE_OK; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2683 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2684 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2685 | #else |
| 2686 | UNUSED_PARAMETER(pParse); |
| 2687 | UNUSED_PARAMETER(p); |
| 2688 | UNUSED_PARAMETER(nEq); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2689 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2690 | assert( pLower || pUpper ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2691 | *pRangeDiv = 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2692 | /* TUNING: Each inequality constraint reduces the search space 4-fold. |
| 2693 | ** A BETWEEN operator, therefore, reduces the search space 16-fold */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2694 | if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2695 | *pRangeDiv += 20; assert( 20==whereCost(4) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2696 | } |
| 2697 | if( pUpper ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2698 | *pRangeDiv += 20; assert( 20==whereCost(4) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2699 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2700 | return rc; |
| 2701 | } |
| 2702 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2703 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2704 | /* |
| 2705 | ** Estimate the number of rows that will be returned based on |
| 2706 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2707 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2708 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2709 | ** for that index. When pExpr==NULL that means the constraint is |
| 2710 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2711 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2712 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2713 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2714 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2715 | ** |
| 2716 | ** This routine can fail if it is unable to load a collating sequence |
| 2717 | ** required for string comparison, or if unable to allocate memory |
| 2718 | ** for a UTF conversion required for comparison. The error is stored |
| 2719 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2720 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2721 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2722 | Parse *pParse, /* Parsing & code generating context */ |
| 2723 | Index *p, /* The index whose left-most column is pTerm */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2724 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2725 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2726 | ){ |
| 2727 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2728 | u8 aff; /* Column affinity */ |
| 2729 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2730 | tRowcnt a[2]; /* Statistics */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2731 | |
| 2732 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2733 | assert( p->nSample>0 ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2734 | aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2735 | if( pExpr ){ |
| 2736 | rc = valueFromExpr(pParse, pExpr, aff, &pRhs); |
| 2737 | if( rc ) goto whereEqualScanEst_cancel; |
| 2738 | }else{ |
| 2739 | pRhs = sqlite3ValueNew(pParse->db); |
| 2740 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2741 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2742 | rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 2743 | if( rc==SQLITE_OK ){ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2744 | WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1])); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2745 | *pnRow = a[1]; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2746 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2747 | whereEqualScanEst_cancel: |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2748 | sqlite3ValueFree(pRhs); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2749 | return rc; |
| 2750 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2751 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2752 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2753 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2754 | /* |
| 2755 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2756 | ** an IN constraint where the right-hand side of the IN operator |
| 2757 | ** is a list of values. Example: |
| 2758 | ** |
| 2759 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2760 | ** |
| 2761 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2762 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2763 | ** non-zero. |
| 2764 | ** |
| 2765 | ** This routine can fail if it is unable to load a collating sequence |
| 2766 | ** required for string comparison, or if unable to allocate memory |
| 2767 | ** for a UTF conversion required for comparison. The error is stored |
| 2768 | ** in the pParse structure. |
| 2769 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2770 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2771 | Parse *pParse, /* Parsing & code generating context */ |
| 2772 | Index *p, /* The index whose left-most column is pTerm */ |
| 2773 | 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] | 2774 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2775 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2776 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2777 | tRowcnt nEst; /* Number of rows for a single term */ |
| 2778 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 2779 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2780 | |
| 2781 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2782 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 2783 | nEst = p->aiRowEst[0]; |
| 2784 | rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| 2785 | nRowEst += nEst; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2786 | } |
| 2787 | if( rc==SQLITE_OK ){ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2788 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 2789 | *pnRow = nRowEst; |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2790 | WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst)); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2791 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2792 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2793 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2794 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2795 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2796 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2797 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2798 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2799 | ** or USING clause of that join. |
| 2800 | ** |
| 2801 | ** Consider the term t2.z='ok' in the following queries: |
| 2802 | ** |
| 2803 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2804 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2805 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2806 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2807 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2808 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2809 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2810 | ** |
| 2811 | ** 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] | 2812 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2813 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2814 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2815 | ** but joins might run a little slower. The trick is to disable as much |
| 2816 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2817 | ** the wrong answer. See ticket #813. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2818 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2819 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 2820 | if( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2821 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2822 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 2823 | ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 2824 | pTerm->wtFlags |= TERM_CODED; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 2825 | if( pTerm->iParent>=0 ){ |
| 2826 | WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 2827 | if( (--pOther->nChild)==0 ){ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 2828 | disableTerm(pLevel, pOther); |
| 2829 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2830 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2835 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2836 | ** to the n registers starting at base. |
| 2837 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2838 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2839 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2840 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2841 | ** |
| 2842 | ** This routine makes its own copy of zAff so that the caller is free |
| 2843 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2844 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2845 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2846 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2847 | if( zAff==0 ){ |
| 2848 | assert( pParse->db->mallocFailed ); |
| 2849 | return; |
| 2850 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2851 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2852 | |
| 2853 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2854 | ** and end of the affinity string. |
| 2855 | */ |
| 2856 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2857 | n--; |
| 2858 | base++; |
| 2859 | zAff++; |
| 2860 | } |
| 2861 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2862 | n--; |
| 2863 | } |
| 2864 | |
| 2865 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2866 | if( n>0 ){ |
| 2867 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2868 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2869 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2870 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2873 | |
| 2874 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2875 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2876 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2877 | ** coded. |
| 2878 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2879 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2880 | ** |
| 2881 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2882 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2883 | ** 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] | 2884 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2885 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2886 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2887 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2888 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2889 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2890 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2891 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2892 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2893 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2894 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2895 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2896 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2897 | assert( iTarget>0 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2898 | if( pX->op==TK_EQ ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2899 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2900 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2901 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2902 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2903 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2904 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2905 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2906 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2907 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2908 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2909 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2910 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2911 | && pLoop->u.btree.pIndex!=0 |
| 2912 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2913 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2914 | testcase( iEq==0 ); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2915 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2916 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2917 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2918 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2919 | iReg = iTarget; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2920 | eType = sqlite3FindInIndex(pParse, pX, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2921 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2922 | testcase( bRev ); |
| 2923 | bRev = !bRev; |
| 2924 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2925 | iTab = pX->iTable; |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2926 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2927 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2928 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2929 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2930 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2931 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2932 | pLevel->u.in.nIn++; |
| 2933 | pLevel->u.in.aInLoop = |
| 2934 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2935 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2936 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2937 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2938 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2939 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2940 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2941 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2942 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2943 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2944 | } |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2945 | pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2946 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2947 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2948 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2949 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2950 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2951 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2952 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2953 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2956 | /* |
| 2957 | ** Generate code that will evaluate all == and IN constraints for an |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2958 | ** index. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2959 | ** |
| 2960 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2961 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2962 | ** The index has as many as three equality constraints, but in this |
| 2963 | ** example, the third "c" value is an inequality. So only two |
| 2964 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2965 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2966 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2967 | ** |
| 2968 | ** In the example above nEq==2. But this subroutine works for any value |
| 2969 | ** 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] | 2970 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2971 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2972 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2973 | ** This routine always allocates at least one memory cell and returns |
| 2974 | ** the index of that memory cell. The code that |
| 2975 | ** calls this routine will use that memory cell to store the termination |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2976 | ** key value of the loop. If one or more IN operators appear, then |
| 2977 | ** this routine allocates an additional nEq memory cells for internal |
| 2978 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2979 | ** |
| 2980 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2981 | ** copy of the column affinity string of the index allocated using |
| 2982 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2983 | ** with equality constraints that use NONE affinity are set to |
| 2984 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2985 | ** |
| 2986 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2987 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2988 | ** |
| 2989 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2990 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2991 | ** no conversion should be attempted before using a t2.b value as part of |
| 2992 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2993 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2994 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2995 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2996 | Parse *pParse, /* Parsing context */ |
| 2997 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2998 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2999 | int nExtraReg, /* Number of extra registers to allocate */ |
| 3000 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3001 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3002 | int nEq; /* The number of == or IN constraints to code */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3003 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 3004 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3005 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3006 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3007 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3008 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3009 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3010 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3011 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3012 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3013 | pLoop = pLevel->pWLoop; |
| 3014 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 3015 | nEq = pLoop->u.btree.nEq; |
| 3016 | pIdx = pLoop->u.btree.pIndex; |
| 3017 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3018 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3019 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3020 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 3021 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3022 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3023 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3024 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3025 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 3026 | if( !zAff ){ |
| 3027 | pParse->db->mallocFailed = 1; |
| 3028 | } |
| 3029 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3030 | /* Evaluate the equality constraints |
| 3031 | */ |
drh | c49de5d | 2007-01-19 01:06:01 +0000 | [diff] [blame] | 3032 | assert( pIdx->nColumn>=nEq ); |
| 3033 | for(j=0; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3034 | int r1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3035 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3036 | assert( pTerm!=0 ); |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 3037 | /* The following true for indices with redundant columns. |
| 3038 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 3039 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3040 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3041 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3042 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3043 | if( nReg==1 ){ |
| 3044 | sqlite3ReleaseTempReg(pParse, regBase); |
| 3045 | regBase = r1; |
| 3046 | }else{ |
| 3047 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 3048 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3049 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 3050 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 3051 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 3052 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3053 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 2f2855b | 2009-11-18 01:25:26 +0000 | [diff] [blame] | 3054 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3055 | if( zAff ){ |
| 3056 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 3057 | zAff[j] = SQLITE_AFF_NONE; |
| 3058 | } |
| 3059 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 3060 | zAff[j] = SQLITE_AFF_NONE; |
| 3061 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3062 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3063 | } |
| 3064 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3065 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3066 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3069 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3070 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3071 | ** This routine is a helper for explainIndexRange() below |
| 3072 | ** |
| 3073 | ** pStr holds the text of an expression that we are building up one term |
| 3074 | ** at a time. This routine adds a new term to the end of the expression. |
| 3075 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 3076 | ** terms only. |
| 3077 | */ |
| 3078 | static void explainAppendTerm( |
| 3079 | StrAccum *pStr, /* The text expression being built */ |
| 3080 | int iTerm, /* Index of this term. First is zero */ |
| 3081 | const char *zColumn, /* Name of the column */ |
| 3082 | const char *zOp /* Name of the operator */ |
| 3083 | ){ |
| 3084 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 3085 | sqlite3StrAccumAppend(pStr, zColumn, -1); |
| 3086 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 3087 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 3088 | } |
| 3089 | |
| 3090 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3091 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 3092 | ** function returns a pointer to a string buffer containing a description |
| 3093 | ** of the subset of table rows scanned by the strategy in the form of an |
| 3094 | ** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 3095 | ** |
| 3096 | ** For example, if the query: |
| 3097 | ** |
| 3098 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 3099 | ** |
| 3100 | ** is run and there is an index on (a, b), then this function returns a |
| 3101 | ** string similar to: |
| 3102 | ** |
| 3103 | ** "a=? AND b>?" |
| 3104 | ** |
| 3105 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 3106 | ** It is the responsibility of the caller to free the buffer when it is |
| 3107 | ** no longer required. |
| 3108 | */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3109 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 3110 | Index *pIndex = pLoop->u.btree.pIndex; |
| 3111 | int nEq = pLoop->u.btree.nEq; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3112 | int i, j; |
| 3113 | Column *aCol = pTab->aCol; |
| 3114 | int *aiColumn = pIndex->aiColumn; |
| 3115 | StrAccum txt; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3116 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3117 | if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3118 | return 0; |
| 3119 | } |
| 3120 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
drh | 03b6df1 | 2010-11-15 16:29:30 +0000 | [diff] [blame] | 3121 | txt.db = db; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3122 | sqlite3StrAccumAppend(&txt, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3123 | for(i=0; i<nEq; i++){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3124 | explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "="); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3127 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3128 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3129 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 3130 | explainAppendTerm(&txt, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3131 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3132 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3133 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 3134 | explainAppendTerm(&txt, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3135 | } |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3136 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 3137 | return sqlite3StrAccumFinish(&txt); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3140 | /* |
| 3141 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 3142 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 3143 | ** record is added to the output to describe the table scan strategy in |
| 3144 | ** pLevel. |
| 3145 | */ |
| 3146 | static void explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3147 | Parse *pParse, /* Parse context */ |
| 3148 | SrcList *pTabList, /* Table list this loop refers to */ |
| 3149 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 3150 | int iLevel, /* Value for "level" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3151 | int iFrom, /* Value for "from" column of output */ |
| 3152 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3153 | ){ |
| 3154 | if( pParse->explain==2 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3155 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3156 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 3157 | sqlite3 *db = pParse->db; /* Database handle */ |
| 3158 | char *zMsg; /* Text to add to EQP output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3159 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3160 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3161 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 3162 | u32 flags; /* Flags that describe this loop */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3163 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3164 | pLoop = pLevel->pWLoop; |
| 3165 | flags = pLoop->wsFlags; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3166 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3167 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3168 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 3169 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 3170 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3171 | |
| 3172 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3173 | if( pItem->pSelect ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3174 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3175 | }else{ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3176 | zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3179 | if( pItem->zAlias ){ |
| 3180 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 3181 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3182 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3183 | && ALWAYS(pLoop->u.btree.pIndex!=0) |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3184 | ){ |
| 3185 | char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3186 | zMsg = sqlite3MAppendf(db, zMsg, |
| 3187 | ((flags & WHERE_AUTO_INDEX) ? |
| 3188 | "%s USING AUTOMATIC %sINDEX%.0s%s" : |
| 3189 | "%s USING %sINDEX %s%s"), |
| 3190 | zMsg, ((flags & WHERE_IDX_ONLY) ? "COVERING " : ""), |
| 3191 | pLoop->u.btree.pIndex->zName, zWhere); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3192 | sqlite3DbFree(db, zWhere); |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 3193 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3194 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3195 | |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 3196 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3197 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 3198 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3199 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 3200 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 3201 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3202 | }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3203 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 3204 | } |
| 3205 | } |
| 3206 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3207 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3208 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3209 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3210 | } |
| 3211 | #endif |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3212 | zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3213 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3214 | } |
| 3215 | } |
| 3216 | #else |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3217 | # define explainOneScan(u,v,w,x,y,z) |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3218 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 3219 | |
| 3220 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3221 | /* |
| 3222 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 3223 | ** implementation described by pWInfo. |
| 3224 | */ |
| 3225 | static Bitmask codeOneLoopStart( |
| 3226 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 3227 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3228 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3229 | ){ |
| 3230 | int j, k; /* Loop counters */ |
| 3231 | int iCur; /* The VDBE cursor for the table */ |
| 3232 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 3233 | int omitTable; /* True if we use the index only */ |
| 3234 | int bRev; /* True if we need to scan in reverse order */ |
| 3235 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3236 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3237 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 3238 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 3239 | Parse *pParse; /* Parsing context */ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3240 | sqlite3 *db; /* Database connection */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3241 | Vdbe *v; /* The prepared stmt under constructions */ |
| 3242 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3243 | int addrBrk; /* Jump here to break out of the loop */ |
| 3244 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3245 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 3246 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3247 | Bitmask newNotReady; /* Return value */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3248 | |
| 3249 | pParse = pWInfo->pParse; |
| 3250 | v = pParse->pVdbe; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3251 | pWC = &pWInfo->sWC; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3252 | db = pParse->db; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3253 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3254 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3255 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 3256 | iCur = pTabItem->iCursor; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3257 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 3258 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3259 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3260 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3261 | |
| 3262 | /* Create labels for the "break" and "continue" instructions |
| 3263 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 3264 | ** Jump to cont to go immediately to the next iteration of the |
| 3265 | ** loop. |
| 3266 | ** |
| 3267 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 3268 | ** means to continue with the next IN value combination. When |
| 3269 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 3270 | ** is the same as "addrBrk". |
| 3271 | */ |
| 3272 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 3273 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 3274 | |
| 3275 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 3276 | ** initialize a memory cell that records if this table matches any |
| 3277 | ** row of the left table of the join. |
| 3278 | */ |
| 3279 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 3280 | pLevel->iLeftJoin = ++pParse->nMem; |
| 3281 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 3282 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 3283 | } |
| 3284 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 3285 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 3286 | if( pTabItem->viaCoroutine ){ |
| 3287 | int regYield = pTabItem->regReturn; |
| 3288 | sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield); |
| 3289 | pLevel->p2 = sqlite3VdbeAddOp1(v, OP_Yield, regYield); |
| 3290 | VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName)); |
| 3291 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 3292 | pLevel->op = OP_Goto; |
| 3293 | }else |
| 3294 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3295 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3296 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 3297 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3298 | ** to access the data. |
| 3299 | */ |
| 3300 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3301 | int addrNotFound; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3302 | int nConstraint = pLoop->nLTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3303 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3304 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3305 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3306 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3307 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 3308 | int iTarget = iReg+j+2; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3309 | pTerm = pLoop->aLTerm[j]; |
drh | 95ed68d | 2013-06-12 17:55:50 +0000 | [diff] [blame] | 3310 | if( pTerm==0 ) continue; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3311 | if( pTerm->eOperator & WO_IN ){ |
| 3312 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 3313 | addrNotFound = pLevel->addrNxt; |
| 3314 | }else{ |
| 3315 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 3316 | } |
| 3317 | } |
| 3318 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 3319 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3320 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 3321 | pLoop->u.vtab.idxStr, |
| 3322 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 3323 | pLoop->u.vtab.needFree = 0; |
| 3324 | for(j=0; j<nConstraint && j<16; j++){ |
| 3325 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3326 | disableTerm(pLevel, pLoop->aLTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | pLevel->op = OP_VNext; |
| 3330 | pLevel->p1 = iCur; |
| 3331 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3332 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3333 | sqlite3ExprCachePop(pParse, 1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3334 | }else |
| 3335 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3336 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3337 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3338 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 3339 | ){ |
| 3340 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3341 | ** equality comparison against the ROWID field. Or |
| 3342 | ** we reference multiple rows using a "rowid IN (...)" |
| 3343 | ** construct. |
| 3344 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3345 | assert( pLoop->u.btree.nEq==1 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3346 | iReleaseReg = sqlite3GetTempReg(pParse); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3347 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3348 | assert( pTerm!=0 ); |
| 3349 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3350 | assert( omitTable==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3351 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3352 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3353 | addrNxt = pLevel->addrNxt; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3354 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 3355 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 3356 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3357 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3358 | VdbeComment((v, "pk")); |
| 3359 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3360 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3361 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 3362 | ){ |
| 3363 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3364 | */ |
| 3365 | int testOp = OP_Noop; |
| 3366 | int start; |
| 3367 | int memEndValue = 0; |
| 3368 | WhereTerm *pStart, *pEnd; |
| 3369 | |
| 3370 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3371 | j = 0; |
| 3372 | pStart = pEnd = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3373 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 3374 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3375 | assert( pStart!=0 || pEnd!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3376 | if( bRev ){ |
| 3377 | pTerm = pStart; |
| 3378 | pStart = pEnd; |
| 3379 | pEnd = pTerm; |
| 3380 | } |
| 3381 | if( pStart ){ |
| 3382 | Expr *pX; /* The expression that defines the start bound */ |
| 3383 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 3384 | |
| 3385 | /* The following constant maps TK_xx codes into corresponding |
| 3386 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 3387 | */ |
| 3388 | const u8 aMoveOp[] = { |
| 3389 | /* TK_GT */ OP_SeekGt, |
| 3390 | /* TK_LE */ OP_SeekLe, |
| 3391 | /* TK_LT */ OP_SeekLt, |
| 3392 | /* TK_GE */ OP_SeekGe |
| 3393 | }; |
| 3394 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 3395 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 3396 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 3397 | |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3398 | assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3399 | testcase( pStart->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3400 | pX = pStart->pExpr; |
| 3401 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3402 | testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3403 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 3404 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| 3405 | VdbeComment((v, "pk")); |
| 3406 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 3407 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 3408 | disableTerm(pLevel, pStart); |
| 3409 | }else{ |
| 3410 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 3411 | } |
| 3412 | if( pEnd ){ |
| 3413 | Expr *pX; |
| 3414 | pX = pEnd->pExpr; |
| 3415 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3416 | assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 3417 | testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3418 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3419 | memEndValue = ++pParse->nMem; |
| 3420 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 3421 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 3422 | testOp = bRev ? OP_Le : OP_Ge; |
| 3423 | }else{ |
| 3424 | testOp = bRev ? OP_Lt : OP_Gt; |
| 3425 | } |
| 3426 | disableTerm(pLevel, pEnd); |
| 3427 | } |
| 3428 | start = sqlite3VdbeCurrentAddr(v); |
| 3429 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 3430 | pLevel->p1 = iCur; |
| 3431 | pLevel->p2 = start; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3432 | assert( pLevel->p5==0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3433 | if( testOp!=OP_Noop ){ |
| 3434 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3435 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3436 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3437 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 3438 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3439 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3440 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3441 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3442 | ** |
| 3443 | ** The WHERE clause may contain zero or more equality |
| 3444 | ** terms ("==" or "IN" operators) that refer to the N |
| 3445 | ** left-most columns of the index. It may also contain |
| 3446 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3447 | ** column that immediately follows the N equalities. Only |
| 3448 | ** the right-most column can be an inequality - the rest must |
| 3449 | ** use the "==" and "IN" operators. For example, if the |
| 3450 | ** index is on (x,y,z), then the following clauses are all |
| 3451 | ** optimized: |
| 3452 | ** |
| 3453 | ** x=5 |
| 3454 | ** x=5 AND y=10 |
| 3455 | ** x=5 AND y<10 |
| 3456 | ** x=5 AND y>5 AND y<10 |
| 3457 | ** x=5 AND y=5 AND z<=10 |
| 3458 | ** |
| 3459 | ** The z<10 term of the following cannot be used, only |
| 3460 | ** the x=5 term: |
| 3461 | ** |
| 3462 | ** x=5 AND z<10 |
| 3463 | ** |
| 3464 | ** N may be zero if there are inequality constraints. |
| 3465 | ** If there are no inequality constraints, then N is at |
| 3466 | ** least one. |
| 3467 | ** |
| 3468 | ** This case is also used when there are no WHERE clause |
| 3469 | ** constraints but an index is selected anyway, in order |
| 3470 | ** to force the output order to conform to an ORDER BY. |
| 3471 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3472 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3473 | 0, |
| 3474 | 0, |
| 3475 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3476 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 3477 | OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3478 | OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ |
| 3479 | OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ |
| 3480 | OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ |
| 3481 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3482 | static const u8 aEndOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3483 | OP_Noop, /* 0: (!end_constraints) */ |
| 3484 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 3485 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 3486 | }; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3487 | int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 3488 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3489 | int regBase; /* Base register holding constraint values */ |
| 3490 | int r1; /* Temp register */ |
| 3491 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3492 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3493 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3494 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3495 | int start_constraints; /* Start of range is constrained */ |
| 3496 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3497 | Index *pIdx; /* The index we will be using */ |
| 3498 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3499 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3500 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3501 | char *zStartAff; /* Affinity for start of range constraint */ |
| 3502 | char *zEndAff; /* Affinity for end of range constraint */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3503 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3504 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3505 | iIdxCur = pLevel->iIdxCur; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3506 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3507 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3508 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3509 | ** query, then the caller will only allow the loop to run for |
| 3510 | ** a single iteration. This means that the first row returned |
| 3511 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3512 | ** the first one after the nEq equality constraints in the index, |
| 3513 | ** this requires some special handling. |
| 3514 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3515 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 3516 | && (pWInfo->bOBSat!=0) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3517 | && (pIdx->nColumn>nEq) |
| 3518 | ){ |
| 3519 | /* assert( pOrderBy->nExpr==1 ); */ |
| 3520 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 3521 | isMinQuery = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3522 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | /* Find any inequality constraint terms for the start and end |
| 3526 | ** of the range. |
| 3527 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3528 | j = nEq; |
| 3529 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3530 | pRangeStart = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3531 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3532 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3533 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3534 | pRangeEnd = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3535 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3538 | /* Generate code to evaluate all constraint terms using == or IN |
| 3539 | ** and store the values of those terms in an array of registers |
| 3540 | ** starting at regBase. |
| 3541 | */ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 3542 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3543 | zEndAff = sqlite3DbStrDup(db, zStartAff); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3544 | addrNxt = pLevel->addrNxt; |
| 3545 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3546 | /* If we are doing a reverse order scan on an ascending index, or |
| 3547 | ** a forward order scan on a descending index, interchange the |
| 3548 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3549 | */ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3550 | if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3551 | || (bRev && pIdx->nColumn==nEq) |
| 3552 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3553 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 3554 | } |
| 3555 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3556 | testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 3557 | testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 3558 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 3559 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3560 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3561 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3562 | start_constraints = pRangeStart || nEq>0; |
| 3563 | |
| 3564 | /* Seek the index cursor to the start of the range. */ |
| 3565 | nConstraint = nEq; |
| 3566 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3567 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3568 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3569 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){ |
| 3570 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3571 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3572 | if( zStartAff ){ |
| 3573 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3574 | /* Since the comparison is to be performed with no conversions |
| 3575 | ** applied to the operands, set the affinity to apply to pRight to |
| 3576 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3577 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3578 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3579 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3580 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3581 | } |
| 3582 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3583 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3584 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3585 | }else if( isMinQuery ){ |
| 3586 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3587 | nConstraint++; |
| 3588 | startEq = 0; |
| 3589 | start_constraints = 1; |
| 3590 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3591 | codeApplyAffinity(pParse, regBase, nConstraint, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3592 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3593 | assert( op!=0 ); |
| 3594 | testcase( op==OP_Rewind ); |
| 3595 | testcase( op==OP_Last ); |
| 3596 | testcase( op==OP_SeekGt ); |
| 3597 | testcase( op==OP_SeekGe ); |
| 3598 | testcase( op==OP_SeekLe ); |
| 3599 | testcase( op==OP_SeekLt ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3600 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3601 | |
| 3602 | /* Load the value for the inequality constraint at the end of the |
| 3603 | ** range (if any). |
| 3604 | */ |
| 3605 | nConstraint = nEq; |
| 3606 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3607 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3608 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3609 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3610 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ |
| 3611 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3612 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3613 | if( zEndAff ){ |
| 3614 | if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3615 | /* Since the comparison is to be performed with no conversions |
| 3616 | ** applied to the operands, set the affinity to apply to pRight to |
| 3617 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3618 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3619 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3620 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ |
| 3621 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3622 | } |
| 3623 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3624 | codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3625 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3626 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3627 | } |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3628 | sqlite3DbFree(db, zStartAff); |
| 3629 | sqlite3DbFree(db, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3630 | |
| 3631 | /* Top of the loop body */ |
| 3632 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3633 | |
| 3634 | /* Check if the index cursor is past the end of the range. */ |
| 3635 | op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)]; |
| 3636 | testcase( op==OP_Noop ); |
| 3637 | testcase( op==OP_IdxGE ); |
| 3638 | testcase( op==OP_IdxLT ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3639 | if( op!=OP_Noop ){ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3640 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3641 | sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0); |
| 3642 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3643 | |
| 3644 | /* If there are inequality constraints, check that the value |
| 3645 | ** of the table column that the inequality contrains is not NULL. |
| 3646 | ** If it is, jump to the next iteration of the loop. |
| 3647 | */ |
| 3648 | r1 = sqlite3GetTempReg(pParse); |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 3649 | testcase( pLoop->wsFlags & WHERE_BTM_LIMIT ); |
| 3650 | testcase( pLoop->wsFlags & WHERE_TOP_LIMIT ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3651 | if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3652 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 3653 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 3654 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3655 | sqlite3ReleaseTempReg(pParse, r1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3656 | |
| 3657 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3658 | disableTerm(pLevel, pRangeStart); |
| 3659 | disableTerm(pLevel, pRangeEnd); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3660 | if( !omitTable ){ |
| 3661 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3662 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3663 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3664 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3665 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3666 | |
| 3667 | /* Record the instruction used to terminate the loop. Disable |
| 3668 | ** WHERE clause terms made redundant by the index range scan. |
| 3669 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3670 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3671 | pLevel->op = OP_Noop; |
| 3672 | }else if( bRev ){ |
| 3673 | pLevel->op = OP_Prev; |
| 3674 | }else{ |
| 3675 | pLevel->op = OP_Next; |
| 3676 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3677 | pLevel->p1 = iIdxCur; |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3678 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3679 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3680 | }else{ |
| 3681 | assert( pLevel->p5==0 ); |
| 3682 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3683 | }else |
| 3684 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3685 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3686 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3687 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3688 | ** |
| 3689 | ** Example: |
| 3690 | ** |
| 3691 | ** CREATE TABLE t1(a,b,c,d); |
| 3692 | ** CREATE INDEX i1 ON t1(a); |
| 3693 | ** CREATE INDEX i2 ON t1(b); |
| 3694 | ** CREATE INDEX i3 ON t1(c); |
| 3695 | ** |
| 3696 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3697 | ** |
| 3698 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3699 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3700 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3701 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3702 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3703 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3704 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3705 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3706 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3707 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3708 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3709 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3710 | ** Gosub 2 A |
| 3711 | ** sqlite3WhereEnd() |
| 3712 | ** |
| 3713 | ** Following the above, code to terminate the loop. Label A, the target |
| 3714 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3715 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3716 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3717 | ** Goto B # The loop is finished. |
| 3718 | ** |
| 3719 | ** A: <loop body> # Return data, whatever. |
| 3720 | ** |
| 3721 | ** Return 2 # Jump back to the Gosub |
| 3722 | ** |
| 3723 | ** B: <after the loop> |
| 3724 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3725 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3726 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3727 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3728 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3729 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3730 | |
| 3731 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3732 | int regRowset = 0; /* Register for RowSet object */ |
| 3733 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3734 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3735 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3736 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3737 | int ii; /* Loop counter */ |
| 3738 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3739 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3740 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3741 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3742 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3743 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3744 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3745 | pLevel->op = OP_Return; |
| 3746 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3747 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3748 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3749 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3750 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3751 | */ |
| 3752 | if( pWInfo->nLevel>1 ){ |
| 3753 | int nNotReady; /* The number of notReady tables */ |
| 3754 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3755 | nNotReady = pWInfo->nLevel - iLevel - 1; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3756 | pOrTab = sqlite3StackAllocRaw(db, |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3757 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3758 | if( pOrTab==0 ) return notReady; |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 3759 | pOrTab->nAlloc = (u8)(nNotReady + 1); |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3760 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3761 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3762 | origSrc = pWInfo->pTabList->a; |
| 3763 | for(k=1; k<=nNotReady; k++){ |
| 3764 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3765 | } |
| 3766 | }else{ |
| 3767 | pOrTab = pWInfo->pTabList; |
| 3768 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3769 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3770 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 3771 | ** equivalent to an empty rowset. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3772 | ** |
| 3773 | ** Also initialize regReturn to contain the address of the instruction |
| 3774 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3775 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3776 | ** over the top of the loop into the body of it. In this case the |
| 3777 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3778 | ** fall through to the next instruction, just as an OP_Next does if |
| 3779 | ** called on an uninitialized cursor. |
| 3780 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3781 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3782 | regRowset = ++pParse->nMem; |
| 3783 | regRowid = ++pParse->nMem; |
| 3784 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3785 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3786 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3787 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3788 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3789 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3790 | ** That way, terms in y that are factored into the disjunction will |
| 3791 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3792 | ** |
| 3793 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3794 | ** the "interesting" terms of z - terms that did not originate in the |
| 3795 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3796 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3797 | ** |
| 3798 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3799 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3800 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3801 | */ |
| 3802 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3803 | int iTerm; |
| 3804 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3805 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 3806 | if( &pWC->a[iTerm] == pTerm ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3807 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 3808 | if( pWC->a[iTerm].wtFlags & (TERM_ORINFO) ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3809 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3810 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 3811 | pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3812 | } |
| 3813 | if( pAndExpr ){ |
| 3814 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3815 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3818 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3819 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3820 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3821 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3822 | Expr *pOrExpr = pOrTerm->pExpr; |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3823 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3824 | pAndExpr->pLeft = pOrExpr; |
| 3825 | pOrExpr = pAndExpr; |
| 3826 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3827 | /* Loop through table entries that match term pOrTerm. */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3828 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 3829 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3830 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3831 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3832 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3833 | WhereLoop *pSubLoop; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3834 | explainOneScan( |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3835 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3836 | ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3837 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3838 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3839 | int r; |
| 3840 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3841 | regRowid, 0); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3842 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| 3843 | sqlite3VdbeCurrentAddr(v)+2, r, iSet); |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3844 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3845 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 3846 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3847 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 3848 | ** contained one or more AND term from a notReady table. The |
| 3849 | ** terms from the notReady table could not be tested and will |
| 3850 | ** need to be tested later. |
| 3851 | */ |
| 3852 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 3853 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3854 | /* If all of the OR-connected terms are optimized using the same |
| 3855 | ** index, and the index is opened using the same cursor number |
| 3856 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 3857 | ** be possible to use that index as a covering index. |
| 3858 | ** |
| 3859 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 3860 | ** uses an index, and this is either the first OR-connected term |
| 3861 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3862 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 3863 | ** pCov to NULL to indicate that no candidate covering index will |
| 3864 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3865 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3866 | pSubLoop = pSubWInfo->a[0].pWLoop; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 3867 | assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3868 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3869 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3870 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3871 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
drh | 907717f | 2013-06-04 18:03:22 +0000 | [diff] [blame] | 3872 | pCov = pSubLoop->u.btree.pIndex; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3873 | }else{ |
| 3874 | pCov = 0; |
| 3875 | } |
| 3876 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3877 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 3878 | sqlite3WhereEnd(pSubWInfo); |
| 3879 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3880 | } |
| 3881 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 3882 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 3883 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3884 | if( pAndExpr ){ |
| 3885 | pAndExpr->pLeft = 0; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3886 | sqlite3ExprDelete(db, pAndExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3887 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3888 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3889 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 3890 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 3891 | |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3892 | if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3893 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3894 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3895 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3896 | |
| 3897 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3898 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3899 | ** scan of the entire table. |
| 3900 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3901 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 3902 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 3903 | assert( bRev==0 || bRev==1 ); |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3904 | pLevel->op = aStep[bRev]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3905 | pLevel->p1 = iCur; |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3906 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3907 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3908 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3909 | newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3910 | |
| 3911 | /* Insert code to test every subexpression that can be completely |
| 3912 | ** computed using the current set of tables. |
| 3913 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3914 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3915 | Expr *pE; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3916 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3917 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3918 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3919 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3920 | testcase( pWInfo->untestedTerms==0 |
| 3921 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 3922 | pWInfo->untestedTerms = 1; |
| 3923 | continue; |
| 3924 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3925 | pE = pTerm->pExpr; |
| 3926 | assert( pE!=0 ); |
| 3927 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 3928 | continue; |
| 3929 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3930 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3931 | pTerm->wtFlags |= TERM_CODED; |
| 3932 | } |
| 3933 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3934 | /* Insert code to test for implied constraints based on transitivity |
| 3935 | ** of the "==" operator. |
| 3936 | ** |
| 3937 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 3938 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 3939 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 3940 | ** the implied "t1.a=123" constraint. |
| 3941 | */ |
| 3942 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3943 | Expr *pE, *pEAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3944 | WhereTerm *pAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3945 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 3946 | if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue; |
| 3947 | if( pTerm->leftCursor!=iCur ) continue; |
drh | cdc2e43 | 2013-07-01 17:27:19 +0000 | [diff] [blame] | 3948 | if( pLevel->iLeftJoin ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3949 | pE = pTerm->pExpr; |
| 3950 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 3951 | assert( (pTerm->prereqRight & newNotReady)!=0 ); |
| 3952 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 3953 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 3954 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3955 | testcase( pAlt->eOperator & WO_EQ ); |
| 3956 | testcase( pAlt->eOperator & WO_IN ); |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3957 | VdbeNoopComment((v, "begin transitive constraint")); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3958 | pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 3959 | if( pEAlt ){ |
| 3960 | *pEAlt = *pAlt->pExpr; |
| 3961 | pEAlt->pLeft = pE->pLeft; |
| 3962 | sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| 3963 | sqlite3StackFree(db, pEAlt); |
| 3964 | } |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3967 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 3968 | ** at least one row of the right table has matched the left table. |
| 3969 | */ |
| 3970 | if( pLevel->iLeftJoin ){ |
| 3971 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 3972 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 3973 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3974 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3975 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3976 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3977 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3978 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3979 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 3980 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3981 | continue; |
| 3982 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3983 | assert( pTerm->pExpr ); |
| 3984 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 3985 | pTerm->wtFlags |= TERM_CODED; |
| 3986 | } |
| 3987 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3988 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3989 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3990 | return newNotReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3993 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3994 | /* |
| 3995 | ** Print a WhereLoop object for debugging purposes |
| 3996 | */ |
| 3997 | static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3998 | int nb = 1+(pTabList->nSrc+7)/8; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3999 | struct SrcList_item *pItem = pTabList->a + p->iTab; |
| 4000 | Table *pTab = pItem->pTab; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4001 | sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 4002 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4003 | sqlite3DebugPrintf(" %12s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4004 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4005 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 4006 | if( p->u.btree.pIndex ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4007 | const char *zName = p->u.btree.pIndex->zName; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4008 | if( zName==0 ) zName = "ipk"; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4009 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 4010 | int i = sqlite3Strlen30(zName) - 1; |
| 4011 | while( zName[i]!='_' ) i--; |
| 4012 | zName += i; |
| 4013 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4014 | sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4015 | }else{ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4016 | sqlite3DebugPrintf("%20s",""); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4017 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4018 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4019 | char *z; |
| 4020 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4021 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 4022 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4023 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4024 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4025 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4026 | sqlite3DebugPrintf(" %-19s", z); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4027 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4028 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4029 | sqlite3DebugPrintf(" f %04x N %d", p->wsFlags, p->nLTerm); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4030 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4031 | } |
| 4032 | #endif |
| 4033 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4034 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4035 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 4036 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4037 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4038 | static void whereLoopInit(WhereLoop *p){ |
| 4039 | p->aLTerm = p->aLTermSpace; |
| 4040 | p->nLTerm = 0; |
| 4041 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 4042 | p->wsFlags = 0; |
| 4043 | } |
| 4044 | |
| 4045 | /* |
| 4046 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 4047 | */ |
| 4048 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4049 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4050 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 4051 | sqlite3_free(p->u.vtab.idxStr); |
| 4052 | p->u.vtab.needFree = 0; |
| 4053 | p->u.vtab.idxStr = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4054 | }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4055 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 4056 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 4057 | p->u.btree.pIndex = 0; |
| 4058 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4059 | } |
| 4060 | } |
| 4061 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4062 | /* |
| 4063 | ** Deallocate internal memory used by a WhereLoop object |
| 4064 | */ |
| 4065 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 4066 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 4067 | whereLoopClearUnion(db, p); |
| 4068 | whereLoopInit(p); |
| 4069 | } |
| 4070 | |
| 4071 | /* |
| 4072 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 4073 | */ |
| 4074 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 4075 | WhereTerm **paNew; |
| 4076 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 4077 | n = (n+7)&~7; |
| 4078 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 4079 | if( paNew==0 ) return SQLITE_NOMEM; |
| 4080 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 4081 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 4082 | p->aLTerm = paNew; |
| 4083 | p->nLSlot = n; |
| 4084 | return SQLITE_OK; |
| 4085 | } |
| 4086 | |
| 4087 | /* |
| 4088 | ** Transfer content from the second pLoop into the first. |
| 4089 | */ |
| 4090 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
| 4091 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ) return SQLITE_NOMEM; |
| 4092 | whereLoopClearUnion(db, pTo); |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4093 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 4094 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4095 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 4096 | pFrom->u.vtab.needFree = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4097 | }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4098 | pFrom->u.btree.pIndex = 0; |
| 4099 | } |
| 4100 | return SQLITE_OK; |
| 4101 | } |
| 4102 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4103 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4104 | ** Delete a WhereLoop object |
| 4105 | */ |
| 4106 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4107 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4108 | sqlite3DbFree(db, p); |
| 4109 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 4110 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4111 | /* |
| 4112 | ** Free a WhereInfo structure |
| 4113 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 4114 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 4115 | if( ALWAYS(pWInfo) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4116 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4117 | while( pWInfo->pLoops ){ |
| 4118 | WhereLoop *p = pWInfo->pLoops; |
| 4119 | pWInfo->pLoops = p->pNextLoop; |
| 4120 | whereLoopDelete(db, p); |
| 4121 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4122 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4123 | } |
| 4124 | } |
| 4125 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4126 | /* |
| 4127 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 4128 | ** |
| 4129 | ** An existing WhereLoop entry might be overwritten if the new template |
| 4130 | ** is better and has fewer dependencies. Or the template will be ignored |
| 4131 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 4132 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4133 | ** added based on the template. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4134 | ** |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4135 | ** If pBuilder->pOrSet is not NULL then we only care about only the |
| 4136 | ** prerequisites and rRun and nOut costs of the N best loops. That |
| 4137 | ** information is gathered in the pBuilder->pOrSet object. This special |
| 4138 | ** processing mode is used only for OR clause processing. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4139 | ** |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4140 | ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4141 | ** still might overwrite similar loops with the new template if the |
| 4142 | ** template is better. Loops may be overwritten if the following |
| 4143 | ** conditions are met: |
| 4144 | ** |
| 4145 | ** (1) They have the same iTab. |
| 4146 | ** (2) They have the same iSortIdx. |
| 4147 | ** (3) The template has same or fewer dependencies than the current loop |
| 4148 | ** (4) The template has the same or lower cost than the current loop |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4149 | ** (5) The template uses more terms of the same index but has no additional |
| 4150 | ** dependencies |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4151 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4152 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4153 | WhereLoop **ppPrev, *p, *pNext = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4154 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4155 | sqlite3 *db = pWInfo->pParse->db; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4156 | |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4157 | /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 4158 | ** and prereqs. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4159 | */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4160 | if( pBuilder->pOrSet!=0 ){ |
| 4161 | #if WHERETRACE_ENABLED |
| 4162 | u16 n = pBuilder->pOrSet->n; |
| 4163 | int x = |
| 4164 | #endif |
| 4165 | whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, |
| 4166 | pTemplate->nOut); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4167 | #if WHERETRACE_ENABLED |
| 4168 | if( sqlite3WhereTrace & 0x8 ){ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4169 | sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4170 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4171 | } |
| 4172 | #endif |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4173 | return SQLITE_OK; |
| 4174 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4175 | |
| 4176 | /* Search for an existing WhereLoop to overwrite, or which takes |
| 4177 | ** priority over pTemplate. |
| 4178 | */ |
| 4179 | for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4180 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 4181 | /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 4182 | ** then those WhereLoops need to be considered separately. Neither is |
| 4183 | ** a candidate to replace the other. */ |
| 4184 | continue; |
| 4185 | } |
| 4186 | /* In the current implementation, the rSetup value is either zero |
| 4187 | ** or the cost of building an automatic index (NlogN) and the NlogN |
| 4188 | ** is the same for compatible WhereLoops. */ |
| 4189 | assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 4190 | || p->rSetup==pTemplate->rSetup ); |
| 4191 | |
| 4192 | /* whereLoopAddBtree() always generates and inserts the automatic index |
| 4193 | ** case first. Hence compatible candidate WhereLoops never have a larger |
| 4194 | ** rSetup. Call this SETUP-INVARIANT */ |
| 4195 | assert( p->rSetup>=pTemplate->rSetup ); |
| 4196 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4197 | if( (p->prereq & pTemplate->prereq)==p->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4198 | && p->rSetup<=pTemplate->rSetup |
| 4199 | && p->rRun<=pTemplate->rRun |
| 4200 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4201 | /* This branch taken when p is equal or better than pTemplate in |
| 4202 | ** all of (1) dependences (2) setup-cost, and (3) run-cost. */ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4203 | assert( p->rSetup==pTemplate->rSetup ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4204 | if( p->nLTerm<pTemplate->nLTerm |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4205 | && (p->wsFlags & WHERE_INDEXED)!=0 |
| 4206 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 4207 | && p->u.btree.pIndex==pTemplate->u.btree.pIndex |
| 4208 | && p->prereq==pTemplate->prereq |
| 4209 | ){ |
| 4210 | /* Overwrite an existing WhereLoop with an similar one that uses |
| 4211 | ** more terms of the index */ |
| 4212 | pNext = p->pNextLoop; |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4213 | break; |
| 4214 | }else{ |
| 4215 | /* pTemplate is not helpful. |
| 4216 | ** Return without changing or adding anything */ |
| 4217 | goto whereLoopInsert_noop; |
| 4218 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4219 | } |
| 4220 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4221 | && p->rRun>=pTemplate->rRun |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4222 | && ALWAYS(p->rSetup>=pTemplate->rSetup) /* See SETUP-INVARIANT above */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4223 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4224 | /* Overwrite an existing WhereLoop with a better one: one that is |
| 4225 | ** better at one of (1) dependences, (2) setup-cost, or (3) run-cost |
| 4226 | ** and is no worse in any of those categories. */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4227 | pNext = p->pNextLoop; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4228 | break; |
| 4229 | } |
| 4230 | } |
| 4231 | |
| 4232 | /* If we reach this point it means that either p[] should be overwritten |
| 4233 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 4234 | ** WhereLoop and insert it. |
| 4235 | */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4236 | #if WHERETRACE_ENABLED |
| 4237 | if( sqlite3WhereTrace & 0x8 ){ |
| 4238 | if( p!=0 ){ |
| 4239 | sqlite3DebugPrintf("ins-del: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4240 | whereLoopPrint(p, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4241 | } |
| 4242 | sqlite3DebugPrintf("ins-new: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4243 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4244 | } |
| 4245 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4246 | if( p==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4247 | p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4248 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4249 | whereLoopInit(p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4250 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4251 | whereLoopXfer(db, p, pTemplate); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4252 | p->pNextLoop = pNext; |
| 4253 | *ppPrev = p; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4254 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4255 | Index *pIndex = p->u.btree.pIndex; |
| 4256 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4257 | p->u.btree.pIndex = 0; |
| 4258 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4259 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4260 | return SQLITE_OK; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4261 | |
| 4262 | /* Jump here if the insert is a no-op */ |
| 4263 | whereLoopInsert_noop: |
| 4264 | #if WHERETRACE_ENABLED |
| 4265 | if( sqlite3WhereTrace & 0x8 ){ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4266 | sqlite3DebugPrintf("ins-noop: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4267 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4268 | } |
| 4269 | #endif |
| 4270 | return SQLITE_OK; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
| 4273 | /* |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4274 | ** 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] | 4275 | ** Try to match one more. |
| 4276 | ** |
| 4277 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 4278 | ** INTEGER PRIMARY KEY. |
| 4279 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4280 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4281 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 4282 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 4283 | Index *pProbe, /* An index on pSrc */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4284 | WhereCost nInMul /* log(Number of iterations due to IN) */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4285 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4286 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 4287 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 4288 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4289 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 4290 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4291 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4292 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4293 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 4294 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 4295 | int saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 4296 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 4297 | WhereCost saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4298 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4299 | int rc = SQLITE_OK; /* Return code */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4300 | WhereCost nRowEst; /* Estimated index selectivity */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4301 | WhereCost rLogSize; /* Logarithm of table size */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4302 | WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4303 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4304 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4305 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4306 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4307 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4308 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 4309 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 4310 | opMask = WO_LT|WO_LE; |
| 4311 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 4312 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4313 | }else{ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4314 | 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] | 4315 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4316 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4317 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4318 | assert( pNew->u.btree.nEq<=pProbe->nColumn ); |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4319 | if( pNew->u.btree.nEq < pProbe->nColumn ){ |
| 4320 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4321 | nRowEst = whereCost(pProbe->aiRowEst[pNew->u.btree.nEq+1]); |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4322 | if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4323 | }else{ |
| 4324 | iCol = -1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4325 | nRowEst = 0; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4326 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4327 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4328 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4329 | saved_nEq = pNew->u.btree.nEq; |
| 4330 | saved_nLTerm = pNew->nLTerm; |
| 4331 | saved_wsFlags = pNew->wsFlags; |
| 4332 | saved_prereq = pNew->prereq; |
| 4333 | saved_nOut = pNew->nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4334 | pNew->rSetup = 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4335 | rLogSize = estLog(whereCost(pProbe->aiRowEst[0])); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4336 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4337 | int nIn = 0; |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4338 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 4339 | #ifdef SQLITE_ENABLE_STAT3 |
| 4340 | if( (pTerm->wtFlags & TERM_VNULL)!=0 && pSrc->pTab->aCol[iCol].notNull ){ |
| 4341 | continue; /* skip IS NOT NULL constraints on a NOT NULL column */ |
| 4342 | } |
| 4343 | #endif |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4344 | pNew->wsFlags = saved_wsFlags; |
| 4345 | pNew->u.btree.nEq = saved_nEq; |
| 4346 | pNew->nLTerm = saved_nLTerm; |
| 4347 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4348 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 4349 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4350 | pNew->rRun = rLogSize; /* Baseline cost is log2(N). Adjustments below */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4351 | if( pTerm->eOperator & WO_IN ){ |
| 4352 | Expr *pExpr = pTerm->pExpr; |
| 4353 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4354 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4355 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
| 4356 | nIn = 46; assert( 46==whereCost(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4357 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4358 | /* "x IN (value, value, ...)" */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4359 | nIn = whereCost(pExpr->x.pList->nExpr); |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4360 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4361 | pNew->rRun += nIn; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4362 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4363 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4364 | }else if( pTerm->eOperator & (WO_EQ) ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4365 | assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4366 | || nInMul==0 ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4367 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4368 | if( iCol<0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4369 | || (pProbe->onError!=OE_None && nInMul==0 |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4370 | && pNew->u.btree.nEq==pProbe->nColumn-1) |
| 4371 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4372 | assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4373 | pNew->wsFlags |= WHERE_ONEROW; |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4374 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4375 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4376 | pNew->nOut = nRowEst + nInMul; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4377 | }else if( pTerm->eOperator & (WO_ISNULL) ){ |
| 4378 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 4379 | pNew->u.btree.nEq++; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4380 | /* TUNING: IS NULL selects 2 rows */ |
| 4381 | nIn = 10; assert( 10==whereCost(2) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4382 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4383 | }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4384 | testcase( pTerm->eOperator & WO_GT ); |
| 4385 | testcase( pTerm->eOperator & WO_GE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4386 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4387 | pBtm = pTerm; |
| 4388 | pTop = 0; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4389 | }else{ |
| 4390 | assert( pTerm->eOperator & (WO_LT|WO_LE) ); |
| 4391 | testcase( pTerm->eOperator & WO_LT ); |
| 4392 | testcase( pTerm->eOperator & WO_LE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4393 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4394 | pTop = pTerm; |
| 4395 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4396 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4397 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4398 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4399 | /* Adjust nOut and rRun for STAT3 range values */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4400 | WhereCost rDiv; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4401 | whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq, |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4402 | pBtm, pTop, &rDiv); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4403 | pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4404 | } |
| 4405 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 8284633 | 2013-08-01 17:21:26 +0000 | [diff] [blame] | 4406 | if( pNew->u.btree.nEq==1 && pProbe->nSample |
drh | 40aa936 | 2013-06-28 17:29:25 +0000 | [diff] [blame] | 4407 | && OptimizationEnabled(db, SQLITE_Stat3) ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4408 | tRowcnt nOut = 0; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4409 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 4410 | testcase( pTerm->eOperator & WO_EQ ); |
| 4411 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4412 | rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4413 | }else if( (pTerm->eOperator & WO_IN) |
| 4414 | && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4415 | rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4416 | } |
drh | 8284633 | 2013-08-01 17:21:26 +0000 | [diff] [blame] | 4417 | assert( nOut==0 || rc==SQLITE_OK ); |
| 4418 | if( nOut ) pNew->nOut = whereCost(nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4419 | } |
| 4420 | #endif |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4421 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4422 | /* Each row involves a step of the index, then a binary search of |
| 4423 | ** the main table */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4424 | pNew->rRun = whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4425 | } |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4426 | /* Step cost for each output row */ |
| 4427 | pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4428 | /* TBD: Adjust nOut for additional constraints */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4429 | rc = whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4430 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
drh | bbe8b24 | 2013-06-13 15:50:59 +0000 | [diff] [blame] | 4431 | && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0)) |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4432 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4433 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4434 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4435 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4436 | pNew->prereq = saved_prereq; |
| 4437 | pNew->u.btree.nEq = saved_nEq; |
| 4438 | pNew->wsFlags = saved_wsFlags; |
| 4439 | pNew->nOut = saved_nOut; |
| 4440 | pNew->nLTerm = saved_nLTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4441 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4445 | ** Return True if it is possible that pIndex might be useful in |
| 4446 | ** implementing the ORDER BY clause in pBuilder. |
| 4447 | ** |
| 4448 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 4449 | ** if there is no way for pIndex to be useful in implementing that |
| 4450 | ** ORDER BY clause. |
| 4451 | */ |
| 4452 | static int indexMightHelpWithOrderBy( |
| 4453 | WhereLoopBuilder *pBuilder, |
| 4454 | Index *pIndex, |
| 4455 | int iCursor |
| 4456 | ){ |
| 4457 | ExprList *pOB; |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4458 | int ii, jj; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4459 | |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4460 | if( pIndex->bUnordered ) return 0; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4461 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4462 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 4463 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4464 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 4465 | if( pExpr->iTable==iCursor ){ |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4466 | for(jj=0; jj<pIndex->nColumn; jj++){ |
| 4467 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 4468 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4469 | } |
| 4470 | } |
| 4471 | return 0; |
| 4472 | } |
| 4473 | |
| 4474 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4475 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 4476 | ** the table is used by an index. Only the first 63 columns are considered. |
| 4477 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4478 | static Bitmask columnsInIndex(Index *pIdx){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4479 | Bitmask m = 0; |
| 4480 | int j; |
| 4481 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 4482 | int x = pIdx->aiColumn[j]; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4483 | testcase( x==BMS-1 ); |
| 4484 | testcase( x==BMS-2 ); |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4485 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 4486 | } |
| 4487 | return m; |
| 4488 | } |
| 4489 | |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4490 | /* Check to see if a partial index with pPartIndexWhere can be used |
| 4491 | ** in the current query. Return true if it can be and false if not. |
| 4492 | */ |
| 4493 | static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ |
| 4494 | int i; |
| 4495 | WhereTerm *pTerm; |
| 4496 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 4497 | if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1; |
| 4498 | } |
| 4499 | return 0; |
| 4500 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4501 | |
| 4502 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 4503 | ** 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] | 4504 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 4505 | ** a b-tree table, not a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4506 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4507 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4508 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4509 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4510 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4511 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4512 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4513 | Index sPk; /* A fake index object for the primary key */ |
| 4514 | tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 4515 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4516 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4517 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4518 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4519 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4520 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4521 | int b; /* A boolean value */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4522 | WhereCost rSize; /* number of rows in the table */ |
| 4523 | WhereCost rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4524 | WhereClause *pWC; /* The parsed WHERE clause */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4525 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4526 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4527 | pWInfo = pBuilder->pWInfo; |
| 4528 | pTabList = pWInfo->pTabList; |
| 4529 | pSrc = pTabList->a + pNew->iTab; |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4530 | pWC = pBuilder->pWC; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4531 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4532 | |
| 4533 | if( pSrc->pIndex ){ |
| 4534 | /* An INDEXED BY clause specifies a particular index to use */ |
| 4535 | pProbe = pSrc->pIndex; |
| 4536 | }else{ |
| 4537 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 4538 | ** variable sPk to represent the rowid primary key index. Make this |
| 4539 | ** fake index the first in a chain of Index objects with all of the real |
| 4540 | ** indices to follow */ |
| 4541 | Index *pFirst; /* First of real indices on the table */ |
| 4542 | memset(&sPk, 0, sizeof(Index)); |
| 4543 | sPk.nColumn = 1; |
| 4544 | sPk.aiColumn = &aiColumnPk; |
| 4545 | sPk.aiRowEst = aiRowEstPk; |
| 4546 | sPk.onError = OE_Replace; |
| 4547 | sPk.pTable = pSrc->pTab; |
| 4548 | aiRowEstPk[0] = pSrc->pTab->nRowEst; |
| 4549 | aiRowEstPk[1] = 1; |
| 4550 | pFirst = pSrc->pTab->pIndex; |
| 4551 | if( pSrc->notIndexed==0 ){ |
| 4552 | /* The real indices of the table are only considered if the |
| 4553 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 4554 | sPk.pNext = pFirst; |
| 4555 | } |
| 4556 | pProbe = &sPk; |
| 4557 | } |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4558 | rSize = whereCost(pSrc->pTab->nRowEst); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4559 | rLogSize = estLog(rSize); |
| 4560 | |
| 4561 | /* Automatic indexes */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4562 | if( !pBuilder->pOrSet |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 4563 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 4564 | && pSrc->pIndex==0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4565 | && !pSrc->viaCoroutine |
| 4566 | && !pSrc->notIndexed |
| 4567 | && !pSrc->isCorrelated |
| 4568 | ){ |
| 4569 | /* Generate auto-index WhereLoops */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4570 | WhereTerm *pTerm; |
| 4571 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 4572 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4573 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4574 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 4575 | pNew->u.btree.nEq = 1; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4576 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4577 | pNew->nLTerm = 1; |
| 4578 | pNew->aLTerm[0] = pTerm; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4579 | /* TUNING: One-time cost for computing the automatic index is |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4580 | ** approximately 7*N*log2(N) where N is the number of rows in |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4581 | ** the table being indexed. */ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4582 | pNew->rSetup = rLogSize + rSize + 28; assert( 28==whereCost(7) ); |
| 4583 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 4584 | ** is more than the usual guess of 10 rows, since we have no way |
| 4585 | ** of knowning how selective the index will ultimately be. It would |
| 4586 | ** not be unreasonable to make this value much larger. */ |
| 4587 | pNew->nOut = 43; assert( 43==whereCost(20) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4588 | pNew->rRun = whereCostAdd(rLogSize,pNew->nOut); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4589 | pNew->wsFlags = WHERE_AUTO_INDEX; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4590 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4591 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4592 | } |
| 4593 | } |
| 4594 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4595 | |
| 4596 | /* Loop over all indices |
| 4597 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4598 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 4599 | if( pProbe->pPartIdxWhere!=0 |
| 4600 | && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){ |
| 4601 | continue; /* Partial index inappropriate for this query */ |
| 4602 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4603 | pNew->u.btree.nEq = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4604 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4605 | pNew->iSortIdx = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4606 | pNew->rSetup = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4607 | pNew->prereq = mExtra; |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4608 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4609 | pNew->u.btree.pIndex = pProbe; |
| 4610 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4611 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 4612 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4613 | if( pProbe->tnum<=0 ){ |
| 4614 | /* Integer primary key index */ |
| 4615 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4616 | |
| 4617 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4618 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4619 | /* TUNING: Cost of full table scan is 3*(N + log2(N)). |
| 4620 | ** + The extra 3 factor is to encourage the use of indexed lookups |
| 4621 | ** over full scans. A smaller constant 2 is used for covering |
| 4622 | ** index scans so that a covering index scan will be favored over |
| 4623 | ** a table scan. */ |
| 4624 | pNew->rRun = whereCostAdd(rSize,rLogSize) + 16; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4625 | rc = whereLoopInsert(pBuilder, pNew); |
| 4626 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4627 | }else{ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4628 | Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4629 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4630 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4631 | /* Full scan via index */ |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4632 | if( b |
| 4633 | || ( m==0 |
| 4634 | && pProbe->bUnordered==0 |
| 4635 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 4636 | && sqlite3GlobalConfig.bUseCis |
| 4637 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 4638 | ) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4639 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4640 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4641 | if( m==0 ){ |
| 4642 | /* TUNING: Cost of a covering index scan is 2*(N + log2(N)). |
| 4643 | ** + The extra 2 factor is to encourage the use of indexed lookups |
| 4644 | ** over index scans. A table scan uses a factor of 3 so that |
| 4645 | ** index scans are favored over table scans. |
| 4646 | ** + If this covering index might also help satisfy the ORDER BY |
| 4647 | ** clause, then the cost is fudged down slightly so that this |
| 4648 | ** index is favored above other indices that have no hope of |
| 4649 | ** helping with the ORDER BY. */ |
| 4650 | pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b; |
| 4651 | }else{ |
| 4652 | assert( b!=0 ); |
| 4653 | /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N) |
| 4654 | ** which we will simplify to just N*log2(N) */ |
| 4655 | pNew->rRun = rSize + rLogSize; |
| 4656 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4657 | rc = whereLoopInsert(pBuilder, pNew); |
| 4658 | if( rc ) break; |
| 4659 | } |
| 4660 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4661 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4662 | |
| 4663 | /* If there was an INDEXED BY clause, then only that one index is |
| 4664 | ** considered. */ |
| 4665 | if( pSrc->pIndex ) break; |
| 4666 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4667 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4670 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4671 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4672 | ** Add all WhereLoop objects for a table of the join identified by |
| 4673 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4674 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4675 | static int whereLoopAddVirtual( |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4676 | WhereLoopBuilder *pBuilder /* WHERE clause information */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4677 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4678 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4679 | Parse *pParse; /* The parsing context */ |
| 4680 | WhereClause *pWC; /* The WHERE clause */ |
| 4681 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 4682 | Table *pTab; |
| 4683 | sqlite3 *db; |
| 4684 | sqlite3_index_info *pIdxInfo; |
| 4685 | struct sqlite3_index_constraint *pIdxCons; |
| 4686 | struct sqlite3_index_constraint_usage *pUsage; |
| 4687 | WhereTerm *pTerm; |
| 4688 | int i, j; |
| 4689 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4690 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4691 | int seenIn = 0; /* True if an IN operator is seen */ |
| 4692 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 4693 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 4694 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4695 | int rc = SQLITE_OK; |
| 4696 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4697 | pWInfo = pBuilder->pWInfo; |
| 4698 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4699 | db = pParse->db; |
| 4700 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4701 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4702 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4703 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4704 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4705 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4706 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4707 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4708 | pNew->rSetup = 0; |
| 4709 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4710 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4711 | pNew->u.vtab.needFree = 0; |
| 4712 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4713 | nConstraint = pIdxInfo->nConstraint; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4714 | if( whereLoopResize(db, pNew, nConstraint) ){ |
| 4715 | sqlite3DbFree(db, pIdxInfo); |
| 4716 | return SQLITE_NOMEM; |
| 4717 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4718 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4719 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4720 | if( !seenIn && (iPhase&1)!=0 ){ |
| 4721 | iPhase++; |
| 4722 | if( iPhase>3 ) break; |
| 4723 | } |
| 4724 | if( !seenVar && iPhase>1 ) break; |
| 4725 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4726 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4727 | j = pIdxCons->iTermOffset; |
| 4728 | pTerm = &pWC->a[j]; |
| 4729 | switch( iPhase ){ |
| 4730 | case 0: /* Constants without IN operator */ |
| 4731 | pIdxCons->usable = 0; |
| 4732 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4733 | seenIn = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4734 | } |
| 4735 | if( pTerm->prereqRight!=0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4736 | seenVar = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4737 | }else if( (pTerm->eOperator & WO_IN)==0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4738 | pIdxCons->usable = 1; |
| 4739 | } |
| 4740 | break; |
| 4741 | case 1: /* Constants with IN operators */ |
| 4742 | assert( seenIn ); |
| 4743 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 4744 | break; |
| 4745 | case 2: /* Variables without IN */ |
| 4746 | assert( seenVar ); |
| 4747 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 4748 | break; |
| 4749 | default: /* Variables with IN */ |
| 4750 | assert( seenVar && seenIn ); |
| 4751 | pIdxCons->usable = 1; |
| 4752 | break; |
| 4753 | } |
| 4754 | } |
| 4755 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 4756 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4757 | pIdxInfo->idxStr = 0; |
| 4758 | pIdxInfo->idxNum = 0; |
| 4759 | pIdxInfo->needToFreeIdxStr = 0; |
| 4760 | pIdxInfo->orderByConsumed = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4761 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4762 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 4763 | if( rc ) goto whereLoopAddVtab_exit; |
| 4764 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4765 | pNew->prereq = 0; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4766 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4767 | assert( pNew->nLSlot>=nConstraint ); |
| 4768 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4769 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4770 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4771 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 4772 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4773 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4774 | || j<0 |
| 4775 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4776 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4777 | ){ |
| 4778 | rc = SQLITE_ERROR; |
| 4779 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4780 | goto whereLoopAddVtab_exit; |
| 4781 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4782 | testcase( iTerm==nConstraint-1 ); |
| 4783 | testcase( j==0 ); |
| 4784 | testcase( j==pWC->nTerm-1 ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4785 | pTerm = &pWC->a[j]; |
| 4786 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4787 | assert( iTerm<pNew->nLSlot ); |
| 4788 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4789 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4790 | testcase( iTerm==15 ); |
| 4791 | testcase( iTerm==16 ); |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4792 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4793 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4794 | if( pUsage[i].omit==0 ){ |
| 4795 | /* Do not attempt to use an IN constraint if the virtual table |
| 4796 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4797 | ** If we do attempt to use such a constraint, some rows might be |
| 4798 | ** repeated in the output. */ |
| 4799 | break; |
| 4800 | } |
| 4801 | /* A virtual table that is constrained by an IN clause may not |
| 4802 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4803 | ** is not necessarily related to the order of output terms and |
| 4804 | ** (2) Multiple outputs from a single IN value will not merge |
| 4805 | ** together. */ |
| 4806 | pIdxInfo->orderByConsumed = 0; |
| 4807 | } |
| 4808 | } |
| 4809 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4810 | if( i>=nConstraint ){ |
| 4811 | pNew->nLTerm = mxTerm+1; |
| 4812 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4813 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4814 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 4815 | pIdxInfo->needToFreeIdxStr = 0; |
| 4816 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 3b1d808 | 2013-06-03 16:56:37 +0000 | [diff] [blame] | 4817 | pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) |
| 4818 | && pIdxInfo->orderByConsumed); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4819 | pNew->rSetup = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4820 | pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4821 | /* TUNING: Every virtual table query returns 25 rows */ |
| 4822 | pNew->nOut = 46; assert( 46==whereCost(25) ); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4823 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4824 | if( pNew->u.vtab.needFree ){ |
| 4825 | sqlite3_free(pNew->u.vtab.idxStr); |
| 4826 | pNew->u.vtab.needFree = 0; |
| 4827 | } |
| 4828 | } |
| 4829 | } |
| 4830 | |
| 4831 | whereLoopAddVtab_exit: |
| 4832 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4833 | sqlite3DbFree(db, pIdxInfo); |
| 4834 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4835 | } |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4836 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4837 | |
| 4838 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4839 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 4840 | ** btrees or virtual tables. |
| 4841 | */ |
| 4842 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4843 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4844 | WhereClause *pWC; |
| 4845 | WhereLoop *pNew; |
| 4846 | WhereTerm *pTerm, *pWCEnd; |
| 4847 | int rc = SQLITE_OK; |
| 4848 | int iCur; |
| 4849 | WhereClause tempWC; |
| 4850 | WhereLoopBuilder sSubBuild; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4851 | WhereOrSet sSum, sCur, sPrev; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4852 | struct SrcList_item *pItem; |
| 4853 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4854 | pWC = pBuilder->pWC; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4855 | if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4856 | pWCEnd = pWC->a + pWC->nTerm; |
| 4857 | pNew = pBuilder->pNew; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4858 | |
| 4859 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 4860 | if( (pTerm->eOperator & WO_OR)!=0 |
| 4861 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 4862 | ){ |
| 4863 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 4864 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 4865 | WhereTerm *pOrTerm; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4866 | int once = 1; |
| 4867 | int i, j; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4868 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4869 | pItem = pWInfo->pTabList->a + pNew->iTab; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4870 | iCur = pItem->iCursor; |
| 4871 | sSubBuild = *pBuilder; |
| 4872 | sSubBuild.pOrderBy = 0; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4873 | sSubBuild.pOrSet = &sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4874 | |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4875 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4876 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4877 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 4878 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4879 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4880 | tempWC.pOuter = pWC; |
| 4881 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4882 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4883 | tempWC.a = pOrTerm; |
| 4884 | sSubBuild.pWC = &tempWC; |
| 4885 | }else{ |
| 4886 | continue; |
| 4887 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4888 | sCur.n = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4889 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4890 | if( IsVirtual(pItem->pTab) ){ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4891 | rc = whereLoopAddVirtual(&sSubBuild); |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4892 | for(i=0; i<sCur.n; i++) sCur.a[i].prereq |= mExtra; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4893 | }else |
| 4894 | #endif |
| 4895 | { |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4896 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 4897 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4898 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 4899 | if( sCur.n==0 ){ |
| 4900 | sSum.n = 0; |
| 4901 | break; |
| 4902 | }else if( once ){ |
| 4903 | whereOrMove(&sSum, &sCur); |
| 4904 | once = 0; |
| 4905 | }else{ |
| 4906 | whereOrMove(&sPrev, &sSum); |
| 4907 | sSum.n = 0; |
| 4908 | for(i=0; i<sPrev.n; i++){ |
| 4909 | for(j=0; j<sCur.n; j++){ |
| 4910 | whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, |
| 4911 | whereCostAdd(sPrev.a[i].rRun, sCur.a[j].rRun), |
| 4912 | whereCostAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); |
| 4913 | } |
| 4914 | } |
| 4915 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4916 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4917 | pNew->nLTerm = 1; |
| 4918 | pNew->aLTerm[0] = pTerm; |
| 4919 | pNew->wsFlags = WHERE_MULTI_OR; |
| 4920 | pNew->rSetup = 0; |
| 4921 | pNew->iSortIdx = 0; |
| 4922 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 4923 | for(i=0; rc==SQLITE_OK && i<sSum.n; i++){ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4924 | /* TUNING: Multiple by 3.5 for the secondary table lookup */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 4925 | pNew->rRun = sSum.a[i].rRun + 18; |
| 4926 | pNew->nOut = sSum.a[i].nOut; |
| 4927 | pNew->prereq = sSum.a[i].prereq; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4928 | rc = whereLoopInsert(pBuilder, pNew); |
| 4929 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4930 | } |
| 4931 | } |
| 4932 | return rc; |
| 4933 | } |
| 4934 | |
| 4935 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4936 | ** Add all WhereLoop objects for all tables |
| 4937 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4938 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4939 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4940 | Bitmask mExtra = 0; |
| 4941 | Bitmask mPrior = 0; |
| 4942 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4943 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4944 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4945 | sqlite3 *db = pWInfo->pParse->db; |
| 4946 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4947 | int rc = SQLITE_OK; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4948 | u8 priorJoinType = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4949 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4950 | |
| 4951 | /* Loop over the tables in the join, from left to right */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4952 | pNew = pBuilder->pNew; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4953 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4954 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4955 | pNew->iTab = iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4956 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4957 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4958 | mExtra = mPrior; |
| 4959 | } |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4960 | priorJoinType = pItem->jointype; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4961 | if( IsVirtual(pItem->pTab) ){ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4962 | rc = whereLoopAddVirtual(pBuilder); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4963 | }else{ |
| 4964 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 4965 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4966 | if( rc==SQLITE_OK ){ |
| 4967 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 4968 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4969 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4970 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4971 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4972 | whereLoopClear(db, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4973 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4976 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4977 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4978 | ** parameters) to see if it outputs rows in the requested ORDER BY |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 4979 | ** (or GROUP BY) without requiring a separate sort operation. Return: |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4980 | ** |
| 4981 | ** 0: ORDER BY is not satisfied. Sorting required |
| 4982 | ** 1: ORDER BY is satisfied. Omit sorting |
| 4983 | ** -1: Unknown at this time |
| 4984 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 4985 | ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as |
| 4986 | ** strict. With GROUP BY and DISTINCT the only requirement is that |
| 4987 | ** equivalent rows appear immediately adjacent to one another. GROUP BY |
| 4988 | ** and DISTINT do not require rows to appear in any particular order as long |
| 4989 | ** as equivelent rows are grouped together. Thus for GROUP BY and DISTINCT |
| 4990 | ** the pOrderBy terms can be matched in any order. With ORDER BY, the |
| 4991 | ** pOrderBy terms must be matched in strict left-to-right order. |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4992 | */ |
| 4993 | static int wherePathSatisfiesOrderBy( |
| 4994 | WhereInfo *pWInfo, /* The WHERE clause */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4995 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4996 | WherePath *pPath, /* The WherePath to check */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4997 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 4998 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4999 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5000 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5001 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5002 | u8 revSet; /* True if rev is known */ |
| 5003 | u8 rev; /* Composite sort order */ |
| 5004 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5005 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 5006 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 5007 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5008 | u16 nColumn; /* Number of columns in pIndex */ |
| 5009 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 5010 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 5011 | int i, j; /* Loop counters */ |
| 5012 | int iCur; /* Cursor number for current WhereLoop */ |
| 5013 | int iColumn; /* A column number within table iCur */ |
drh | e8ae583 | 2013-06-19 13:32:46 +0000 | [diff] [blame] | 5014 | WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5015 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 5016 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 5017 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 5018 | Index *pIndex; /* The index associated with pLoop */ |
| 5019 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 5020 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 5021 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5022 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5023 | Bitmask ready; /* Mask of inner loops */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5024 | |
| 5025 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5026 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 5027 | ** 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] | 5028 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 5029 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5030 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 5031 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5032 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5033 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 5034 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 5035 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 5036 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 5037 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 5038 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 5039 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 5040 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 5041 | ** |
| 5042 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 5043 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 5044 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5045 | */ |
| 5046 | |
| 5047 | assert( pOrderBy!=0 ); |
| 5048 | |
| 5049 | /* Sortability of virtual tables is determined by the xBestIndex method |
| 5050 | ** of the virtual table itself */ |
| 5051 | if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5052 | testcase( nLoop>0 ); /* True when outer loops are one-row and match |
| 5053 | ** no ORDER BY terms */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5054 | return pLast->u.vtab.isOrdered; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5055 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5056 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5057 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5058 | nOrderBy = pOrderBy->nExpr; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5059 | testcase( nOrderBy==BMS-1 ); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5060 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 5061 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5062 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5063 | orderDistinctMask = 0; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5064 | ready = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5065 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5066 | if( iLoop>0 ) ready |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5067 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5068 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5069 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5070 | |
| 5071 | /* Mark off any ORDER BY term X that is a column in the table of |
| 5072 | ** the current loop for which there is term in the WHERE |
| 5073 | ** clause of the form X IS NULL or X=? that reference only outer |
| 5074 | ** loops. |
| 5075 | */ |
| 5076 | for(i=0; i<nOrderBy; i++){ |
| 5077 | if( MASKBIT(i) & obSat ) continue; |
| 5078 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 5079 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 5080 | if( pOBExpr->iTable!=iCur ) continue; |
| 5081 | pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 5082 | ~ready, WO_EQ|WO_ISNULL, 0); |
| 5083 | if( pTerm==0 ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5084 | if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5085 | const char *z1, *z2; |
| 5086 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5087 | if( !pColl ) pColl = db->pDfltColl; |
| 5088 | z1 = pColl->zName; |
| 5089 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 5090 | if( !pColl ) pColl = db->pDfltColl; |
| 5091 | z2 = pColl->zName; |
| 5092 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 5093 | } |
| 5094 | obSat |= MASKBIT(i); |
| 5095 | } |
| 5096 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5097 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 5098 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 5099 | pIndex = 0; |
| 5100 | nColumn = 0; |
| 5101 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5102 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5103 | }else{ |
| 5104 | nColumn = pIndex->nColumn; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5105 | isOrderDistinct = pIndex->onError!=OE_None; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5106 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5107 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5108 | /* Loop through all columns of the index and deal with the ones |
| 5109 | ** that are not constrained by == or IN. |
| 5110 | */ |
| 5111 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5112 | distinctColumns = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5113 | for(j=0; j<=nColumn; j++){ |
| 5114 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 5115 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5116 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5117 | if( j<pLoop->u.btree.nEq |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5118 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5119 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5120 | if( i & WO_ISNULL ){ |
| 5121 | testcase( isOrderDistinct ); |
| 5122 | isOrderDistinct = 0; |
| 5123 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5124 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5125 | } |
| 5126 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5127 | /* Get the column number in the table (iColumn) and sort order |
| 5128 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5129 | */ |
| 5130 | if( j<nColumn ){ |
| 5131 | /* Normal index columns */ |
| 5132 | iColumn = pIndex->aiColumn[j]; |
| 5133 | revIdx = pIndex->aSortOrder[j]; |
| 5134 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5135 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5136 | /* The ROWID column at the end */ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5137 | assert( j==nColumn ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5138 | iColumn = -1; |
| 5139 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5140 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5141 | |
| 5142 | /* An unconstrained column that might be NULL means that this |
| 5143 | ** WhereLoop is not well-ordered |
| 5144 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5145 | if( isOrderDistinct |
| 5146 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5147 | && j>=pLoop->u.btree.nEq |
| 5148 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 5149 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5150 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | /* Find the ORDER BY term that corresponds to the j-th column |
| 5154 | ** of the index and and mark that ORDER BY term off |
| 5155 | */ |
| 5156 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5157 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5158 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 5159 | if( MASKBIT(i) & obSat ) continue; |
| 5160 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5161 | testcase( wctrlFlags & WHERE_GROUPBY ); |
| 5162 | testcase( wctrlFlags & WHERE_DISTINCTBY ); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5163 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5164 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5165 | if( pOBExpr->iTable!=iCur ) continue; |
| 5166 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 5167 | if( iColumn>=0 ){ |
| 5168 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5169 | if( !pColl ) pColl = db->pDfltColl; |
| 5170 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 5171 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5172 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5173 | break; |
| 5174 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5175 | if( isMatch ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5176 | if( iColumn<0 ){ |
| 5177 | testcase( distinctColumns==0 ); |
| 5178 | distinctColumns = 1; |
| 5179 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5180 | obSat |= MASKBIT(i); |
| 5181 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5182 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 5183 | ** Sort order is irrelevant for a GROUP BY clause. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5184 | if( revSet ){ |
| 5185 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; |
| 5186 | }else{ |
| 5187 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 5188 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 5189 | revSet = 1; |
| 5190 | } |
| 5191 | } |
| 5192 | }else{ |
| 5193 | /* No match found */ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5194 | if( j==0 || j<nColumn ){ |
| 5195 | testcase( isOrderDistinct!=0 ); |
| 5196 | isOrderDistinct = 0; |
| 5197 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5198 | break; |
| 5199 | } |
| 5200 | } /* end Loop over all index columns */ |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5201 | if( distinctColumns ){ |
| 5202 | testcase( isOrderDistinct==0 ); |
| 5203 | isOrderDistinct = 1; |
| 5204 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5205 | } /* end-if not one-row */ |
| 5206 | |
| 5207 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5208 | if( isOrderDistinct ){ |
| 5209 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5210 | for(i=0; i<nOrderBy; i++){ |
| 5211 | Expr *p; |
| 5212 | if( MASKBIT(i) & obSat ) continue; |
| 5213 | p = pOrderBy->a[i].pExpr; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5214 | if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5215 | obSat |= MASKBIT(i); |
| 5216 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 5217 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5218 | } |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5219 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5220 | if( obSat==obDone ) return 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5221 | if( !isOrderDistinct ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5222 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5225 | #ifdef WHERETRACE_ENABLED |
| 5226 | /* For debugging use only: */ |
| 5227 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 5228 | static char zName[65]; |
| 5229 | int i; |
| 5230 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 5231 | if( pLast ) zName[i++] = pLast->cId; |
| 5232 | zName[i] = 0; |
| 5233 | return zName; |
| 5234 | } |
| 5235 | #endif |
| 5236 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5237 | |
| 5238 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 5239 | ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5240 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 5241 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 5242 | ** |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5243 | ** Assume that the total number of output rows that will need to be sorted |
| 5244 | ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 5245 | ** costs if nRowEst==0. |
| 5246 | ** |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5247 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 5248 | ** error occurs. |
| 5249 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5250 | static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5251 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5252 | int nLoop; /* Number of terms in the join */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5253 | Parse *pParse; /* Parsing context */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5254 | sqlite3 *db; /* The database connection */ |
| 5255 | int iLoop; /* Loop counter over the terms of the join */ |
| 5256 | int ii, jj; /* Loop counters */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5257 | WhereCost rCost; /* Cost of a path */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5258 | WhereCost mxCost = 0; /* Maximum cost of a set of paths */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5259 | WhereCost rSortCost; /* Cost to do a sort */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5260 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 5261 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 5262 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 5263 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 5264 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 5265 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 5266 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 5267 | char *pSpace; /* Temporary memory used by this routine */ |
| 5268 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5269 | pParse = pWInfo->pParse; |
| 5270 | db = pParse->db; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5271 | nLoop = pWInfo->nLevel; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5272 | /* TUNING: For simple queries, only the best path is tracked. |
| 5273 | ** For 2-way joins, the 5 best paths are followed. |
| 5274 | ** For joins of 3 or more tables, track the 10 best paths */ |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5275 | mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5276 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5277 | WHERETRACE(0x002, ("---- begin solver\n")); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5278 | |
| 5279 | /* Allocate and initialize space for aTo and aFrom */ |
| 5280 | ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 5281 | pSpace = sqlite3DbMallocRaw(db, ii); |
| 5282 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 5283 | aTo = (WherePath*)pSpace; |
| 5284 | aFrom = aTo+mxChoice; |
| 5285 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 5286 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5287 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5288 | pFrom->aLoop = pX; |
| 5289 | } |
| 5290 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5291 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 5292 | ** |
| 5293 | ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 5294 | ** of computing an automatic index is not paid back within the first 25 |
| 5295 | ** rows, then do not use the automatic index. */ |
| 5296 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==whereCost(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5297 | nFrom = 1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5298 | |
| 5299 | /* Precompute the cost of sorting the final result set, if the caller |
| 5300 | ** to sqlite3WhereBegin() was concerned about sorting */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5301 | rSortCost = 0; |
| 5302 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5303 | aFrom[0].isOrderedValid = 1; |
| 5304 | }else{ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5305 | /* TUNING: Estimated cost of sorting is N*log2(N) where N is the |
| 5306 | ** number of output rows. */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5307 | rSortCost = nRowEst + estLog(nRowEst); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5308 | WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost)); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | /* Compute successively longer WherePaths using the previous generation |
| 5312 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 5313 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5314 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 5315 | nTo = 0; |
| 5316 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 5317 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 5318 | Bitmask maskNew; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5319 | Bitmask revMask = 0; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5320 | u8 isOrderedValid = pFrom->isOrderedValid; |
| 5321 | u8 isOrdered = pFrom->isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5322 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 5323 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5324 | /* At this point, pWLoop is a candidate to be the next loop. |
| 5325 | ** Compute its cost */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5326 | rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 5327 | rCost = whereCostAdd(rCost, pFrom->rCost); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5328 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5329 | if( !isOrderedValid ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5330 | switch( wherePathSatisfiesOrderBy(pWInfo, |
| 5331 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5332 | iLoop, pWLoop, &revMask) ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5333 | case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ |
| 5334 | isOrdered = 1; |
| 5335 | isOrderedValid = 1; |
| 5336 | break; |
| 5337 | case 0: /* No. pFrom+pWLoop will require a separate sort */ |
| 5338 | isOrdered = 0; |
| 5339 | isOrderedValid = 1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5340 | rCost = whereCostAdd(rCost, rSortCost); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5341 | break; |
| 5342 | default: /* Cannot tell yet. Try again on the next iteration */ |
| 5343 | break; |
| 5344 | } |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 5345 | }else{ |
| 5346 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5347 | } |
| 5348 | /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 5349 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
| 5350 | if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5351 | testcase( jj==nTo-1 ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5352 | break; |
| 5353 | } |
| 5354 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5355 | if( jj>=nTo ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5356 | if( nTo>=mxChoice && rCost>=mxCost ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5357 | #ifdef WHERETRACE_ENABLED |
| 5358 | if( sqlite3WhereTrace&0x4 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5359 | sqlite3DebugPrintf("Skip %s cost=%3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5360 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5361 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5362 | } |
| 5363 | #endif |
| 5364 | continue; |
| 5365 | } |
| 5366 | /* Add a new Path to the aTo[] set */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5367 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5368 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5369 | jj = nTo++; |
| 5370 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5371 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 5372 | for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5373 | } |
| 5374 | pTo = &aTo[jj]; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5375 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5376 | if( sqlite3WhereTrace&0x4 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5377 | sqlite3DebugPrintf("New %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5378 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5379 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5380 | } |
| 5381 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5382 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5383 | if( pTo->rCost<=rCost ){ |
| 5384 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5385 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5386 | sqlite3DebugPrintf( |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5387 | "Skip %s cost=%-3d order=%c", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5388 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5389 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5390 | sqlite3DebugPrintf(" vs %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5391 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5392 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5393 | } |
| 5394 | #endif |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5395 | testcase( pTo->rCost==rCost ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5396 | continue; |
| 5397 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5398 | testcase( pTo->rCost==rCost+1 ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5399 | /* A new and better score for a previously created equivalent path */ |
| 5400 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5401 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5402 | sqlite3DebugPrintf( |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5403 | "Update %s cost=%-3d order=%c", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5404 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5405 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5406 | sqlite3DebugPrintf(" was %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5407 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5408 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5409 | } |
| 5410 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5411 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5412 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5413 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5414 | pTo->revLoop = revMask; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5415 | pTo->nRow = pFrom->nRow + pWLoop->nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5416 | pTo->rCost = rCost; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5417 | pTo->isOrderedValid = isOrderedValid; |
| 5418 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5419 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 5420 | pTo->aLoop[iLoop] = pWLoop; |
| 5421 | if( nTo>=mxChoice ){ |
| 5422 | mxCost = aTo[0].rCost; |
| 5423 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
| 5424 | if( pTo->rCost>mxCost ) mxCost = pTo->rCost; |
| 5425 | } |
| 5426 | } |
| 5427 | } |
| 5428 | } |
| 5429 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5430 | #ifdef WHERETRACE_ENABLED |
| 5431 | if( sqlite3WhereTrace>=2 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5432 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5433 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5434 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5435 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5436 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5437 | if( pTo->isOrderedValid && pTo->isOrdered ){ |
| 5438 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 5439 | }else{ |
| 5440 | sqlite3DebugPrintf("\n"); |
| 5441 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5442 | } |
| 5443 | } |
| 5444 | #endif |
| 5445 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5446 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5447 | pFrom = aTo; |
| 5448 | aTo = aFrom; |
| 5449 | aFrom = pFrom; |
| 5450 | nFrom = nTo; |
| 5451 | } |
| 5452 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5453 | if( nFrom==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5454 | sqlite3ErrorMsg(pParse, "no query solution"); |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5455 | sqlite3DbFree(db, pSpace); |
| 5456 | return SQLITE_ERROR; |
| 5457 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5458 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5459 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5460 | pFrom = aFrom; |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5461 | assert( nFrom==1 ); |
| 5462 | #if 0 /* The following is needed if nFrom is ever more than 1 */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5463 | for(ii=1; ii<nFrom; ii++){ |
| 5464 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 5465 | } |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5466 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5467 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5468 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5469 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5470 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 5471 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5472 | pLevel->iFrom = pWLoop->iTab; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5473 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5474 | } |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5475 | if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 5476 | && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 5477 | && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5478 | && nRowEst |
| 5479 | ){ |
| 5480 | Bitmask notUsed; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5481 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5482 | WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5483 | if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5484 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5485 | if( pFrom->isOrdered ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5486 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 5487 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5488 | }else{ |
| 5489 | pWInfo->bOBSat = 1; |
| 5490 | pWInfo->revMask = pFrom->revLoop; |
| 5491 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5492 | } |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5493 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5494 | |
| 5495 | /* Free temporary memory and return success */ |
| 5496 | sqlite3DbFree(db, pSpace); |
| 5497 | return SQLITE_OK; |
| 5498 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 5499 | |
| 5500 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5501 | ** Most queries use only a single table (they are not joins) and have |
| 5502 | ** simple == constraints against indexed fields. This routine attempts |
| 5503 | ** to plan those simple cases using much less ceremony than the |
| 5504 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 5505 | ** times for the common case. |
| 5506 | ** |
| 5507 | ** Return non-zero on success, if this query can be handled by this |
| 5508 | ** no-frills query planner. Return zero if this query needs the |
| 5509 | ** general-purpose query planner. |
| 5510 | */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5511 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5512 | WhereInfo *pWInfo; |
| 5513 | struct SrcList_item *pItem; |
| 5514 | WhereClause *pWC; |
| 5515 | WhereTerm *pTerm; |
| 5516 | WhereLoop *pLoop; |
| 5517 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5518 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5519 | Table *pTab; |
| 5520 | Index *pIdx; |
| 5521 | |
| 5522 | pWInfo = pBuilder->pWInfo; |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 5523 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5524 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 5525 | pItem = pWInfo->pTabList->a; |
| 5526 | pTab = pItem->pTab; |
| 5527 | if( IsVirtual(pTab) ) return 0; |
| 5528 | if( pItem->zIndex ) return 0; |
| 5529 | iCur = pItem->iCursor; |
| 5530 | pWC = &pWInfo->sWC; |
| 5531 | pLoop = pBuilder->pNew; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5532 | pLoop->wsFlags = 0; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5533 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5534 | if( pTerm ){ |
| 5535 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 5536 | pLoop->aLTerm[0] = pTerm; |
| 5537 | pLoop->nLTerm = 1; |
| 5538 | pLoop->u.btree.nEq = 1; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5539 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 5540 | pLoop->rRun = 33; /* 33==whereCost(10) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5541 | }else{ |
| 5542 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 66518ca | 2013-08-01 15:09:57 +0000 | [diff] [blame] | 5543 | if( pIdx->onError==OE_None || pIdx->pPartIdxWhere!=0 ) continue; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5544 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5545 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5546 | if( pTerm==0 ) break; |
| 5547 | whereLoopResize(pWInfo->pParse->db, pLoop, j); |
| 5548 | pLoop->aLTerm[j] = pTerm; |
| 5549 | } |
| 5550 | if( j!=pIdx->nColumn ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5551 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 5552 | if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5553 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 5554 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5555 | pLoop->nLTerm = j; |
| 5556 | pLoop->u.btree.nEq = j; |
| 5557 | pLoop->u.btree.pIndex = pIdx; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5558 | /* TUNING: Cost of a unique index lookup is 15 */ |
| 5559 | pLoop->rRun = 39; /* 39==whereCost(15) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5560 | break; |
| 5561 | } |
| 5562 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5563 | if( pLoop->wsFlags ){ |
| 5564 | pLoop->nOut = (WhereCost)1; |
| 5565 | pWInfo->a[0].pWLoop = pLoop; |
| 5566 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 5567 | pWInfo->a[0].iTabCur = iCur; |
| 5568 | pWInfo->nRowOut = 1; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5569 | if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5570 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5571 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5572 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5573 | #ifdef SQLITE_DEBUG |
| 5574 | pLoop->cId = '0'; |
| 5575 | #endif |
| 5576 | return 1; |
| 5577 | } |
| 5578 | return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5582 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 5583 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5584 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5585 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5586 | ** in order to complete the WHERE clause processing. |
| 5587 | ** |
| 5588 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5589 | ** |
| 5590 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5591 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5592 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5593 | ** example, if the SQL is this: |
| 5594 | ** |
| 5595 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5596 | ** |
| 5597 | ** Then the code generated is conceptually like the following: |
| 5598 | ** |
| 5599 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5600 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5601 | ** foreach row3 in t3 do / |
| 5602 | ** ... |
| 5603 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5604 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5605 | ** end / |
| 5606 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5607 | ** Note that the loops might not be nested in the order in which they |
| 5608 | ** 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] | 5609 | ** use of indices. Note also that when the IN operator appears in |
| 5610 | ** the WHERE clause, it might result in additional nested loops for |
| 5611 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5612 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5613 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5614 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5615 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5616 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5617 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5618 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5619 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5620 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5621 | ** data from the various tables of the loop. |
| 5622 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5623 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5624 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5625 | ** the tables have indices and there are terms in the WHERE clause that |
| 5626 | ** refer to those indices, a complete table scan can be avoided and the |
| 5627 | ** code will run much faster. Most of the work of this routine is checking |
| 5628 | ** to see if there are indices that can be used to speed up the loop. |
| 5629 | ** |
| 5630 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 5631 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 5632 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 5633 | ** loops are evaluated and if false a jump is made around all subsequent |
| 5634 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 5635 | ** most loop) |
| 5636 | ** |
| 5637 | ** OUTER JOINS |
| 5638 | ** |
| 5639 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 5640 | ** |
| 5641 | ** foreach row1 in t1 do |
| 5642 | ** flag = 0 |
| 5643 | ** foreach row2 in t2 do |
| 5644 | ** start: |
| 5645 | ** ... |
| 5646 | ** flag = 1 |
| 5647 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5648 | ** if flag==0 then |
| 5649 | ** move the row2 cursor to a null row |
| 5650 | ** goto start |
| 5651 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5652 | ** end |
| 5653 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5654 | ** ORDER BY CLAUSE PROCESSING |
| 5655 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5656 | ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause |
| 5657 | ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5658 | ** 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] | 5659 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5660 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5661 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5662 | Parse *pParse, /* The parser context */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5663 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5664 | Expr *pWhere, /* The WHERE clause */ |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5665 | ExprList *pOrderBy, /* An ORDER BY clause, or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5666 | ExprList *pResultSet, /* Result set of the query */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 5667 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 5668 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5669 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5670 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5671 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5672 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 5673 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5674 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5675 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5676 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5677 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5678 | WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5679 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5680 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5681 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5682 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5683 | |
| 5684 | /* Variable initialization */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5685 | db = pParse->db; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5686 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5687 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5688 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5689 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 5690 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 5691 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 5692 | wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 5693 | } |
| 5694 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5695 | /* 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] | 5696 | ** bits in a Bitmask |
| 5697 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5698 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5699 | if( pTabList->nSrc>BMS ){ |
| 5700 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5701 | return 0; |
| 5702 | } |
| 5703 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5704 | /* This function normally generates a nested loop for all tables in |
| 5705 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 5706 | ** only generate code for the first table in pTabList and assume that |
| 5707 | ** any cursors associated with subsequent tables are uninitialized. |
| 5708 | */ |
| 5709 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 5710 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5711 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5712 | ** return value. A single allocation is used to store the WhereInfo |
| 5713 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 5714 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 5715 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 5716 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5717 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5718 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5719 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5720 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5721 | sqlite3DbFree(db, pWInfo); |
| 5722 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5723 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5724 | } |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5725 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5726 | pWInfo->pParse = pParse; |
| 5727 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5728 | pWInfo->pOrderBy = pOrderBy; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5729 | pWInfo->pResultSet = pResultSet; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5730 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5731 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5732 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5733 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5734 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5735 | sWLB.pWC = &pWInfo->sWC; |
drh | 1ac87e1 | 2013-07-18 14:50:56 +0000 | [diff] [blame] | 5736 | sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); |
| 5737 | assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5738 | whereLoopInit(sWLB.pNew); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5739 | #ifdef SQLITE_DEBUG |
| 5740 | sWLB.pNew->cId = '*'; |
| 5741 | #endif |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5742 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5743 | /* Split the WHERE clause into separate subexpressions where each |
| 5744 | ** subexpression is separated by an AND operator. |
| 5745 | */ |
| 5746 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5747 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5748 | sqlite3ExprCodeConstants(pParse, pWhere); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 5749 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
drh | 5e128b2 | 2013-07-09 03:04:32 +0000 | [diff] [blame] | 5750 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5751 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5752 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 5753 | ** expression and either jump over all of the code or fall thru. |
| 5754 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5755 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 5756 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 5757 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5758 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5759 | |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5760 | /* Special case: No FROM clause |
| 5761 | */ |
| 5762 | if( nTabList==0 ){ |
| 5763 | if( pOrderBy ) pWInfo->bOBSat = 1; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5764 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5765 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5766 | } |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5767 | } |
| 5768 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5769 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 5770 | ** |
| 5771 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 5772 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 5773 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 5774 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 5775 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 5776 | ** of the join. Subtracting one from the right table bitmask gives a |
| 5777 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 5778 | ** 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] | 5779 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5780 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 5781 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 5782 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 5783 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5784 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5785 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5786 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5787 | } |
| 5788 | #ifndef NDEBUG |
| 5789 | { |
| 5790 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5791 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5792 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5793 | assert( (m-1)==toTheLeft ); |
| 5794 | toTheLeft |= m; |
| 5795 | } |
| 5796 | } |
| 5797 | #endif |
| 5798 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5799 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 5800 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 5801 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 5802 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5803 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5804 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5805 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5806 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 5807 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5808 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5809 | /* If the ORDER BY (or GROUP BY) clause contains references to general |
| 5810 | ** expressions, then we won't be able to satisfy it using indices, so |
| 5811 | ** go ahead and disable it now. |
| 5812 | */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5813 | if( pOrderBy && (wctrlFlags & WHERE_WANT_DISTINCT)!=0 ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5814 | for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 5815 | Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr); |
| 5816 | if( pExpr->op!=TK_COLUMN ){ |
| 5817 | pWInfo->pOrderBy = pOrderBy = 0; |
| 5818 | break; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5819 | }else if( pExpr->iColumn<0 ){ |
| 5820 | break; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5821 | } |
| 5822 | } |
| 5823 | } |
| 5824 | |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5825 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5826 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ |
| 5827 | /* The DISTINCT marking is pointless. Ignore it. */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5828 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5829 | }else if( pOrderBy==0 ){ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5830 | /* Try to ORDER BY the result set to make distinct processing easier */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5831 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5832 | pWInfo->pOrderBy = pResultSet; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5833 | } |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5836 | /* Construct the WhereLoop objects */ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5837 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5838 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5839 | rc = whereLoopAddAll(&sWLB); |
| 5840 | if( rc ) goto whereBeginError; |
| 5841 | |
| 5842 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5843 | #ifdef WHERETRACE_ENABLED |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5844 | if( sqlite3WhereTrace ){ |
| 5845 | WhereLoop *p; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5846 | int i; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5847 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 5848 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5849 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 5850 | p->cId = zLabel[i%sizeof(zLabel)]; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5851 | whereLoopPrint(p, pTabList); |
| 5852 | } |
| 5853 | } |
| 5854 | #endif |
| 5855 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5856 | wherePathSolver(pWInfo, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5857 | if( db->mallocFailed ) goto whereBeginError; |
| 5858 | if( pWInfo->pOrderBy ){ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5859 | wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5860 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5861 | } |
| 5862 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5863 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 5864 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5865 | } |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5866 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5867 | goto whereBeginError; |
| 5868 | } |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5869 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5870 | if( sqlite3WhereTrace ){ |
| 5871 | int ii; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5872 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 5873 | if( pWInfo->bOBSat ){ |
| 5874 | sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5875 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5876 | switch( pWInfo->eDistinct ){ |
| 5877 | case WHERE_DISTINCT_UNIQUE: { |
| 5878 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 5879 | break; |
| 5880 | } |
| 5881 | case WHERE_DISTINCT_ORDERED: { |
| 5882 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 5883 | break; |
| 5884 | } |
| 5885 | case WHERE_DISTINCT_UNORDERED: { |
| 5886 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 5887 | break; |
| 5888 | } |
| 5889 | } |
| 5890 | sqlite3DebugPrintf("\n"); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5891 | for(ii=0; ii<pWInfo->nLevel; ii++){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5892 | whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5893 | } |
| 5894 | } |
| 5895 | #endif |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5896 | /* Attempt to omit tables from the join that do not effect the result */ |
drh | 1031bd9 | 2013-06-22 15:44:26 +0000 | [diff] [blame] | 5897 | if( pWInfo->nLevel>=2 |
| 5898 | && pResultSet!=0 |
| 5899 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 5900 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5901 | Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet); |
| 5902 | if( pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, pOrderBy); |
| 5903 | while( pWInfo->nLevel>=2 ){ |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 5904 | WhereTerm *pTerm, *pEnd; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5905 | pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5906 | if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break; |
| 5907 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 5908 | && (pLoop->wsFlags & WHERE_ONEROW)==0 |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5909 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5910 | break; |
| 5911 | } |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5912 | if( (tabUsed & pLoop->maskSelf)!=0 ) break; |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 5913 | pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; |
| 5914 | for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){ |
| 5915 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 |
| 5916 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 5917 | ){ |
| 5918 | break; |
| 5919 | } |
| 5920 | } |
| 5921 | if( pTerm<pEnd ) break; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5922 | WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 5923 | pWInfo->nLevel--; |
| 5924 | nTabList--; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5925 | } |
| 5926 | } |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5927 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 5928 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5929 | |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5930 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 5931 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 5932 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 5933 | ** the statement to update a single row. |
| 5934 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 5935 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5936 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 5937 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5938 | pWInfo->okOnePass = 1; |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5939 | pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5940 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5941 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5942 | /* Open all tables in the pTabList and any indices selected for |
| 5943 | ** searching those tables. |
| 5944 | */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5945 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5946 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5947 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5948 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5949 | struct SrcList_item *pTabItem; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5950 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5951 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5952 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5953 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5954 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 5955 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 5956 | /* Do nothing */ |
| 5957 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5958 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5959 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5960 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 5961 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5962 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 5963 | }else if( IsVirtual(pTab) ){ |
| 5964 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5965 | }else |
| 5966 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5967 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5968 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5969 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 5970 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5971 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); |
| 5972 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 5973 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5974 | Bitmask b = pTabItem->colUsed; |
| 5975 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5976 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5977 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 5978 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5979 | assert( n<=pTab->nCol ); |
| 5980 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5981 | }else{ |
| 5982 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5983 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5984 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 5985 | if( (pLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5986 | constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel); |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5987 | }else |
| 5988 | #endif |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 5989 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5990 | Index *pIx = pLoop->u.btree.pIndex; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5991 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5992 | /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */ |
| 5993 | int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5994 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 5995 | assert( iIndexCur>=0 ); |
| 5996 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5997 | (char*)pKey, P4_KEYINFO_HANDOFF); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 5998 | VdbeComment((v, "%s", pIx->zName)); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5999 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6000 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6001 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6002 | } |
| 6003 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 6004 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6005 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6006 | /* Generate the code to do the search. Each iteration of the for |
| 6007 | ** loop below generates code for a single nested loop of the VM |
| 6008 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6009 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 6010 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6011 | for(ii=0; ii<nTabList; ii++){ |
| 6012 | pLevel = &pWInfo->a[ii]; |
| 6013 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6014 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 6015 | pWInfo->iContinue = pLevel->addrCont; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6016 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 6017 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 6018 | /* Done. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6019 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6020 | |
| 6021 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6022 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6023 | if( pWInfo ){ |
| 6024 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6025 | whereInfoFree(db, pWInfo); |
| 6026 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6027 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6028 | } |
| 6029 | |
| 6030 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6031 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6032 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6033 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6034 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6035 | Parse *pParse = pWInfo->pParse; |
| 6036 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6037 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6038 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6039 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 6040 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6041 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6042 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6043 | /* Generate loop termination code. |
| 6044 | */ |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 6045 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6046 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6047 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6048 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6049 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6050 | if( pLevel->op!=OP_Noop ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 6051 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 6052 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6053 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6054 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 6055 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6056 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6057 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6058 | 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] | 6059 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 6060 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6061 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6062 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6063 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 6064 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6065 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6066 | if( pLevel->iLeftJoin ){ |
| 6067 | int addr; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 6068 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6069 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 6070 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 6071 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 6072 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 6073 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 6074 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 6075 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 6076 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 6077 | if( pLevel->op==OP_Return ){ |
| 6078 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 6079 | }else{ |
| 6080 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 6081 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 6082 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6083 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6084 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6085 | |
| 6086 | /* The "break" point is here, just past the end of the outer loop. |
| 6087 | ** Set it. |
| 6088 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6089 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6090 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6091 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6092 | */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6093 | assert( pWInfo->nLevel<=pTabList->nSrc ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6094 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6095 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6096 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6097 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 6098 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6099 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6100 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 6101 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6102 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 6103 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6104 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6105 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6106 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 6107 | } |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 6108 | if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6109 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 6110 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6111 | } |
| 6112 | |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 6113 | /* If this scan uses an index, make VDBE code substitutions to read data |
| 6114 | ** from the index instead of from the table where possible. In some cases |
| 6115 | ** this optimization prevents the table from ever being read, which can |
| 6116 | ** yield a significant performance boost. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6117 | ** |
| 6118 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 6119 | ** sqlite3WhereEnd will have created code that references the table |
| 6120 | ** directly. This loop scans all that code looking for opcodes |
| 6121 | ** that reference the table and converts them into opcodes that |
| 6122 | ** reference the index. |
| 6123 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6124 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 6125 | pIdx = pLoop->u.btree.pIndex; |
| 6126 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 6127 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6128 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6129 | if( pIdx && !db->mallocFailed ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 6130 | int k, j, last; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6131 | VdbeOp *pOp; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6132 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6133 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 6134 | last = sqlite3VdbeCurrentAddr(v); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 6135 | for(k=pWInfo->iTop; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6136 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 6137 | if( pOp->opcode==OP_Column ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6138 | for(j=0; j<pIdx->nColumn; j++){ |
| 6139 | if( pOp->p2==pIdx->aiColumn[j] ){ |
| 6140 | pOp->p2 = j; |
danielk1977 | 21de2e7 | 2007-11-29 17:43:27 +0000 | [diff] [blame] | 6141 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6142 | break; |
| 6143 | } |
| 6144 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6145 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6146 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6147 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6148 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6149 | } |
| 6150 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6151 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6152 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6153 | |
| 6154 | /* Final cleanup |
| 6155 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 6156 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6157 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6158 | return; |
| 6159 | } |