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 | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 36 | /* Forward reference |
| 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 | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | ** 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] | 51 | ** maximum cost for ordinary tables is 64*(2**63) which becomes 6900. |
| 52 | ** (Virtual tables can return a larger cost, but let's assume they do not.) |
| 53 | ** So all costs can be stored in a 16-bit unsigned integer without risk |
| 54 | ** of overflow. |
| 55 | ** |
| 56 | ** Costs are estimates, so don't go to the computational trouble to compute |
| 57 | ** 10*log2(X) exactly. Instead, a close estimate is used. Any value of |
| 58 | ** X<=1 is stored as 0. X=2 is 10. X=3 is 16. X=1000 is 99. etc. |
| 59 | ** |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 60 | ** The tool/wherecosttest.c source file implements a command-line program |
| 61 | ** that will convert between WhereCost to integers and do addition and |
| 62 | ** multiplication on WhereCost values. That command-line program is a |
| 63 | ** useful utility to have around when working with this module. |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 64 | */ |
| 65 | typedef unsigned short int WhereCost; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 68 | ** This object contains information needed to implement a single nested |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 69 | ** loop in WHERE clause. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 70 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 71 | ** Contrast this object with WhereLoop. This object describes the |
| 72 | ** implementation of the loop. WhereLoop describes the algorithm. |
| 73 | ** This object contains a pointer to the WhereLoop algorithm as one of |
| 74 | ** its elements. |
| 75 | ** |
| 76 | ** The WhereInfo object contains a single instance of this object for |
| 77 | ** each term in the FROM clause (which is to say, for each of the |
| 78 | ** nested loops as implemented). The order of WhereLevel objects determines |
| 79 | ** the loop nested order, with WhereInfo.a[0] being the outer loop and |
| 80 | ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 81 | */ |
| 82 | struct WhereLevel { |
| 83 | int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 84 | int iTabCur; /* The VDBE cursor used to access the table */ |
| 85 | int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 86 | int addrBrk; /* Jump here to break out of the loop */ |
| 87 | int addrNxt; /* Jump here to start the next IN combination */ |
| 88 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 89 | int addrFirst; /* First instruction of interior of the loop */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 90 | u8 iFrom; /* Which entry in the FROM clause */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 91 | u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ |
| 92 | int p1, p2; /* Operands of the opcode used to ends the loop */ |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 93 | union { /* Information that depends on pWLoop->wsFlags */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 94 | struct { |
| 95 | int nIn; /* Number of entries in aInLoop[] */ |
| 96 | struct InLoop { |
| 97 | int iCur; /* The VDBE cursor used by this IN operator */ |
| 98 | int addrInTop; /* Top of the IN loop */ |
| 99 | u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 100 | } *aInLoop; /* Information about each nested IN operator */ |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 101 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 102 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 103 | } u; |
| 104 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 105 | }; |
| 106 | |
| 107 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 108 | ** Each instance of this object represents an algorithm for evaluating one |
| 109 | ** term of a join. Every term of the FROM clause will have at least |
| 110 | ** one corresponding WhereLoop object (unless INDEXED BY constraints |
| 111 | ** prevent a query solution - which is an error) and many terms of the |
| 112 | ** FROM clause will have multiple WhereLoop objects, each describing a |
| 113 | ** potential way of implementing that FROM-clause term, together with |
| 114 | ** dependencies and cost estimates for using the chosen algorithm. |
| 115 | ** |
| 116 | ** Query planning consists of building up a collection of these WhereLoop |
| 117 | ** objects, then computing a particular sequence of WhereLoop objects, with |
| 118 | ** one WhereLoop object per FROM clause term, that satisfy all dependencies |
| 119 | ** and that minimize the overall cost. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 120 | */ |
| 121 | struct WhereLoop { |
| 122 | Bitmask prereq; /* Bitmask of other loops that must run first */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 123 | Bitmask maskSelf; /* Bitmask identifying table iTab */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 124 | #ifdef SQLITE_DEBUG |
| 125 | char cId; /* Symbolic ID of this loop for debugging use */ |
| 126 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 127 | u8 iTab; /* Position in FROM clause of table for this loop */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 128 | u8 iSortIdx; /* Sorting index number. 0==None */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 129 | WhereCost rSetup; /* One-time setup cost (ex: create transient index) */ |
| 130 | WhereCost rRun; /* Cost of running each loop */ |
| 131 | WhereCost nOut; /* Estimated number of output rows */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 132 | union { |
| 133 | struct { /* Information for internal btree tables */ |
| 134 | int nEq; /* Number of equality constraints */ |
| 135 | Index *pIndex; /* Index used, or NULL */ |
| 136 | } btree; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 137 | struct { /* Information for virtual tables */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 138 | int idxNum; /* Index number */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 139 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 140 | u8 isOrdered; /* True if satisfies ORDER BY */ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 141 | u16 omitMask; /* Terms that may be omitted */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 142 | char *idxStr; /* Index identifier string */ |
| 143 | } vtab; |
| 144 | } u; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 145 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 146 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 147 | /**** whereLoopXfer() copies fields above ***********************/ |
| 148 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 149 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 150 | WhereTerm **aLTerm; /* WhereTerms used */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 151 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 152 | WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 155 | /* Forward declaration of methods */ |
| 156 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 157 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 158 | /* |
| 159 | ** Each instance of this object holds a sequence of WhereLoop objects |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 160 | ** that implement some or all of a query plan. |
| 161 | ** |
| 162 | ** Think of each WhereLoop objects as a node in a graph, which arcs |
| 163 | ** showing dependences and costs for travelling between nodes. (That is |
| 164 | ** not a completely accurate description because WhereLoop costs are a |
| 165 | ** vector, not a scalar, and because dependences are many-to-one, not |
| 166 | ** one-to-one as are graph nodes. But it is a useful visualization aid.) |
| 167 | ** Then a WherePath object is a path through the graph that visits some |
| 168 | ** or all of the WhereLoop objects once. |
| 169 | ** |
| 170 | ** The "solver" works by creating the N best WherePath objects of length |
| 171 | ** 1. Then using those as a basis to compute the N best WherePath objects |
| 172 | ** of length 2. And so forth until the length of WherePaths equals the |
| 173 | ** number of nodes in the FROM clause. The best (lowest cost) WherePath |
| 174 | ** at the end is the choosen query plan. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 175 | */ |
| 176 | struct WherePath { |
| 177 | Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 178 | Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 179 | WhereCost nRow; /* Estimated number of rows generated by this path */ |
| 180 | WhereCost rCost; /* Total cost of this path */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 181 | u8 isOrdered; /* True if this path satisfies ORDER BY */ |
| 182 | u8 isOrderedValid; /* True if the isOrdered field is valid */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 183 | WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 184 | }; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 185 | |
| 186 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 187 | ** The query generator uses an array of instances of this structure to |
| 188 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 189 | ** clause subexpression is separated from the others by AND operators, |
| 190 | ** usually, or sometimes subexpressions separated by OR. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 191 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 192 | ** All WhereTerms are collected into a single WhereClause structure. |
| 193 | ** The following identity holds: |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 194 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 195 | ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 196 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 197 | ** When a term is of the form: |
| 198 | ** |
| 199 | ** X <op> <expr> |
| 200 | ** |
| 201 | ** where X is a column name and <op> is one of certain operators, |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 202 | ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the |
| 203 | ** cursor number and column number for X. WhereTerm.eOperator records |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 204 | ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 205 | ** use of a bitmask encoding for the operator allows us to search |
| 206 | ** quickly for terms that match any of several different operators. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 207 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 208 | ** A WhereTerm might also be two or more subterms connected by OR: |
| 209 | ** |
| 210 | ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... |
| 211 | ** |
| 212 | ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR |
| 213 | ** and the WhereTerm.u.pOrInfo field points to auxiliary information that |
| 214 | ** is collected about the |
| 215 | ** |
| 216 | ** If a term in the WHERE clause does not match either of the two previous |
| 217 | ** categories, then eOperator==0. The WhereTerm.pExpr field is still set |
| 218 | ** to the original subexpression content and wtFlags is set up appropriately |
| 219 | ** but no other fields in the WhereTerm object are meaningful. |
| 220 | ** |
| 221 | ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 222 | ** but they do so indirectly. A single WhereMaskSet structure translates |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 223 | ** cursor number into bits and the translated bit is stored in the prereq |
| 224 | ** fields. The translation is used in order to maximize the number of |
| 225 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 226 | ** spread out over the non-negative integers. For example, the cursor |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 227 | ** 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] | 228 | ** translates these sparse cursor numbers into consecutive integers |
| 229 | ** beginning with 0 in order to make the best possible use of the available |
| 230 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 231 | ** would be mapped into integers 0 through 7. |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 232 | ** |
| 233 | ** The number of terms in a join is limited by the number of bits |
| 234 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 235 | ** is only able to process joins with 64 or fewer tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 236 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 237 | struct WhereTerm { |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 238 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 239 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 240 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 241 | union { |
| 242 | int leftColumn; /* Column number of X in "X <op> <expr>" */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 243 | WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 244 | WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 245 | } u; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 246 | u16 eOperator; /* A WO_xx value describing <op> */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 247 | u8 wtFlags; /* TERM_xxx bit flags. See below */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 248 | u8 nChild; /* Number of children that must disable us */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 249 | WhereClause *pWC; /* The clause this term is part of */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 250 | Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 251 | Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | /* |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 255 | ** Allowed values of WhereTerm.wtFlags |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 256 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 257 | #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 258 | #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
| 259 | #define TERM_CODED 0x04 /* This term is already coded */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 260 | #define TERM_COPIED 0x08 /* Has a child */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 261 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 262 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 263 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 264 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 265 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 266 | #else |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 267 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 268 | #endif |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 269 | |
| 270 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 271 | ** An instance of the WhereScan object is used as an iterator for locating |
| 272 | ** terms in the WHERE clause that are useful to the query planner. |
| 273 | */ |
| 274 | struct WhereScan { |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 275 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 276 | WhereClause *pWC; /* WhereClause currently being scanned */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 277 | char *zCollName; /* Required collating sequence, if not NULL */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 278 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 279 | unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 280 | unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 281 | u32 opMask; /* Acceptable operators */ |
| 282 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 283 | int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ |
| 284 | }; |
| 285 | |
| 286 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 287 | ** An instance of the following structure holds all information about a |
| 288 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 289 | ** |
| 290 | ** Explanation of pOuter: For a WHERE clause of the form |
| 291 | ** |
| 292 | ** a AND ((b AND c) OR (d AND e)) AND f |
| 293 | ** |
| 294 | ** There are separate WhereClause objects for the whole clause and for |
| 295 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 296 | ** subclauses points to the WhereClause object for the whole clause. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 297 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 298 | struct WhereClause { |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 299 | WhereInfo *pWInfo; /* WHERE clause processing context */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 300 | WhereClause *pOuter; /* Outer conjunction */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 301 | u8 op; /* Split operator. TK_AND or TK_OR */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 302 | int nTerm; /* Number of terms */ |
| 303 | int nSlot; /* Number of entries in a[] */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 304 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 305 | #if defined(SQLITE_SMALL_STACK) |
| 306 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| 307 | #else |
| 308 | WhereTerm aStatic[8]; /* Initial static space for a[] */ |
| 309 | #endif |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 313 | ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to |
| 314 | ** a dynamically allocated instance of the following structure. |
| 315 | */ |
| 316 | struct WhereOrInfo { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 317 | WhereClause wc; /* Decomposition into subterms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 318 | Bitmask indexable; /* Bitmask of all indexable tables in the clause */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | /* |
| 322 | ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to |
| 323 | ** a dynamically allocated instance of the following structure. |
| 324 | */ |
| 325 | struct WhereAndInfo { |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 326 | WhereClause wc; /* The subexpression broken out */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 330 | ** An instance of the following structure keeps track of a mapping |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 331 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 332 | ** |
| 333 | ** The VDBE cursor numbers are small integers contained in |
| 334 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 335 | ** clause, the cursor numbers might not begin with 0 and they might |
| 336 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 337 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 338 | ** from the sparse cursor numbers into consecutive integers beginning |
| 339 | ** with 0. |
| 340 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 341 | ** 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] | 342 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 343 | ** |
| 344 | ** For example, if the WHERE clause expression used these VDBE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 345 | ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 346 | ** would map those cursor numbers into bits 0 through 5. |
| 347 | ** |
| 348 | ** Note that the mapping is not necessarily ordered. In the example |
| 349 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 350 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 351 | ** does not really matter. What is important is that sparse cursor |
| 352 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 353 | ** no gaps. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 354 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 355 | struct WhereMaskSet { |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 356 | int n; /* Number of assigned cursor values */ |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 357 | int ix[BMS]; /* Cursor assigned to each bit */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 360 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 361 | ** This object is a convenience wrapper holding all information needed |
| 362 | ** to construct WhereLoop objects for a particular query. |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 363 | */ |
| 364 | struct WhereLoopBuilder { |
| 365 | WhereInfo *pWInfo; /* Information about this WHERE */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 366 | WhereClause *pWC; /* WHERE clause terms */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 367 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 368 | WhereLoop *pNew; /* Template WhereLoop */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 369 | WhereLoop *pBest; /* If non-NULL, store single best loop here */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | /* |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 373 | ** The WHERE clause processing routine has two halves. The |
| 374 | ** first part does the start of the WHERE loop and the second |
| 375 | ** half does the tail of the WHERE loop. An instance of |
| 376 | ** this structure is returned by the first half and passed |
| 377 | ** into the second half to give some continuity. |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 378 | ** |
| 379 | ** An instance of this object holds the complete state of the query |
| 380 | ** planner. |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 381 | */ |
| 382 | struct WhereInfo { |
| 383 | Parse *pParse; /* Parsing and code generating context */ |
| 384 | SrcList *pTabList; /* List of tables in the join */ |
| 385 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 386 | ExprList *pResultSet; /* Result set. DISTINCT operates on these */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 387 | WhereLoop *pLoops; /* List of all WhereLoop objects */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 388 | Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 389 | WhereCost nRowOut; /* Estimated number of output rows */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 390 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 391 | u8 bOBSat; /* ORDER BY satisfied by indices */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 392 | u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ |
| 393 | u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
| 394 | u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 395 | u8 nLevel; /* Number of nested loop */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 396 | int iTop; /* The very beginning of the WHERE loop */ |
| 397 | int iContinue; /* Jump here to continue with next record */ |
| 398 | int iBreak; /* Jump here to break out of the loop */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 399 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 400 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 401 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 402 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 403 | }; |
| 404 | |
| 405 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 406 | ** Bitmasks for the operators on WhereTerm objects. These are all |
| 407 | ** operators that are of interest to the query planner. An |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 408 | ** OR-ed combination of these values can be used when searching for |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 409 | ** particular WhereTerms within a WhereClause. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 410 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 411 | #define WO_IN 0x001 |
| 412 | #define WO_EQ 0x002 |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 413 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 414 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 415 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 416 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 417 | #define WO_MATCH 0x040 |
| 418 | #define WO_ISNULL 0x080 |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 419 | #define WO_OR 0x100 /* Two or more OR-connected terms */ |
| 420 | #define WO_AND 0x200 /* Two or more AND-connected terms */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 421 | #define WO_EQUIV 0x400 /* Of the form A==B, both columns */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 422 | #define WO_NOOP 0x800 /* This term does not restrict search space */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 423 | |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 424 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 425 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 426 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 427 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 428 | ** These are definitions of bits in the WhereLoop.wsFlags field. |
| 429 | ** The particular combination of bits in each WhereLoop help to |
| 430 | ** determine the algorithm that WhereLoop represents. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 431 | */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 432 | #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR or x IN (...) or x IS NULL */ |
| 433 | #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 434 | #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 435 | #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 436 | #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 437 | #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 438 | #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 439 | #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 440 | #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 441 | #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 442 | #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 443 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 444 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 445 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 446 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 447 | #define WHERE_TEMP_INDEX 0x00004000 /* Uses an ephemeral index */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 448 | |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 449 | |
| 450 | /* Convert a WhereCost value (10 times log2(X)) into its integer value X. |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 451 | ** A rough approximation is used. The value returned is not exact. |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 452 | */ |
| 453 | static u64 whereCostToInt(WhereCost x){ |
| 454 | u64 n; |
drh | 12ffbc7 | 2013-06-13 14:51:53 +0000 | [diff] [blame] | 455 | if( x<10 ) return 1; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 456 | n = x%10; |
| 457 | x /= 10; |
| 458 | if( n>=5 ) n -= 2; |
| 459 | else if( n>=1 ) n -= 1; |
| 460 | if( x>=3 ) return (n+8)<<(x-3); |
| 461 | return (n+8)>>(3-x); |
| 462 | } |
| 463 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 464 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 465 | ** Return the estimated number of output rows from a WHERE clause |
| 466 | */ |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 467 | u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 468 | return whereCostToInt(pWInfo->nRowOut); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* |
| 472 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 473 | ** WHERE clause returns outputs for DISTINCT processing. |
| 474 | */ |
| 475 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 476 | return pWInfo->eDistinct; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 481 | ** Return FALSE if the output needs to be sorted. |
| 482 | */ |
| 483 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 484 | return pWInfo->bOBSat!=0; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* |
| 488 | ** Return the VDBE address or label to jump to in order to continue |
| 489 | ** immediately with the next row of a WHERE clause. |
| 490 | */ |
| 491 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 492 | return pWInfo->iContinue; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | ** Return the VDBE address or label to jump to in order to break |
| 497 | ** out of a WHERE loop. |
| 498 | */ |
| 499 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 500 | return pWInfo->iBreak; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 505 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 506 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
| 507 | */ |
| 508 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo){ |
| 509 | return pWInfo->okOnePass; |
| 510 | } |
| 511 | |
| 512 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 513 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 514 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 515 | static void whereClauseInit( |
| 516 | WhereClause *pWC, /* The WhereClause to be initialized */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 517 | WhereInfo *pWInfo /* The WHERE processing context */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 518 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 519 | pWC->pWInfo = pWInfo; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 520 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 521 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 522 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 523 | pWC->a = pWC->aStatic; |
| 524 | } |
| 525 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 526 | /* Forward reference */ |
| 527 | static void whereClauseClear(WhereClause*); |
| 528 | |
| 529 | /* |
| 530 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 531 | */ |
| 532 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 533 | whereClauseClear(&p->wc); |
| 534 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* |
| 538 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 539 | */ |
| 540 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 541 | whereClauseClear(&p->wc); |
| 542 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 543 | } |
| 544 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 545 | /* |
| 546 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 547 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 548 | */ |
| 549 | static void whereClauseClear(WhereClause *pWC){ |
| 550 | int i; |
| 551 | WhereTerm *a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 552 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 553 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 554 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 555 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 556 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 557 | if( a->wtFlags & TERM_ORINFO ){ |
| 558 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 559 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 560 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 561 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 562 | } |
| 563 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 564 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 569 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 570 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 571 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 572 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 573 | ** allocation error. The memory allocation failure will be recorded in |
| 574 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 575 | ** |
| 576 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 577 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 578 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 579 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 580 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 581 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 582 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 583 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 584 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 585 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 586 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 587 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 588 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 589 | int idx; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 590 | testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 591 | if( pWC->nTerm>=pWC->nSlot ){ |
| 592 | WhereTerm *pOld = pWC->a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 593 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 594 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 595 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 596 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 597 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 598 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 599 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 600 | return 0; |
| 601 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 602 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 603 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 604 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 605 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 606 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 607 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 608 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 609 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 610 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 611 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 612 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 613 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 614 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 615 | |
| 616 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 617 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 618 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 619 | ** operator specified in the op parameter. The WhereClause structure |
| 620 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 621 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 622 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 623 | ** \________/ \_______________/ \________________/ |
| 624 | ** slot[0] slot[1] slot[2] |
| 625 | ** |
| 626 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 627 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 628 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 629 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 630 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 631 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 632 | */ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 633 | static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 634 | pWC->op = op; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 635 | if( pExpr==0 ) return; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 636 | if( pExpr->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 637 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 638 | }else{ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 639 | whereSplit(pWC, pExpr->pLeft, op); |
| 640 | whereSplit(pWC, pExpr->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 641 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 645 | ** Initialize a WhereMaskSet object |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 646 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 647 | #define initMaskSet(P) (P)->n=0 |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 648 | |
| 649 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 650 | ** Return the bitmask for the given cursor number. Return 0 if |
| 651 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 652 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 653 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 654 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 655 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 656 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 657 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 658 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 659 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 660 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 665 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 666 | ** |
| 667 | ** There is one cursor per table in the FROM clause. The number of |
| 668 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 669 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 670 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 671 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 672 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 673 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 674 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 678 | ** These routine walk (recursively) an expression tree and generates |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 679 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 680 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 681 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 682 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 683 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 684 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 685 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 686 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 687 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 688 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 689 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 690 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 691 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 692 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 693 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 694 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 695 | }else{ |
| 696 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 697 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 698 | return mask; |
| 699 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 700 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 701 | int i; |
| 702 | Bitmask mask = 0; |
| 703 | if( pList ){ |
| 704 | for(i=0; i<pList->nExpr; i++){ |
| 705 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 708 | return mask; |
| 709 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 710 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 711 | Bitmask mask = 0; |
| 712 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 713 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 714 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 715 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 716 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 717 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 718 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 719 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 720 | int i; |
| 721 | for(i=0; i<pSrc->nSrc; i++){ |
| 722 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 723 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 724 | } |
| 725 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 726 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 727 | } |
| 728 | return mask; |
| 729 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 730 | |
| 731 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 732 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 733 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 734 | ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 735 | ** |
| 736 | ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be |
| 737 | ** of one of the following forms: column = expression column > expression |
| 738 | ** column >= expression column < expression column <= expression |
| 739 | ** expression = column expression > column expression >= column |
| 740 | ** expression < column expression <= column column IN |
| 741 | ** (expression-list) column IN (subquery) column IS NULL |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 742 | */ |
| 743 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 744 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 745 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 746 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 747 | assert( TK_GE==TK_EQ+4 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 748 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /* |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 752 | ** Swap two objects of type TYPE. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 753 | */ |
| 754 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 755 | |
| 756 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 757 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 758 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 759 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 760 | ** If left/right precedence rules come into play when determining the |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 761 | ** collating sequence, then COLLATE operators are adjusted to ensure |
| 762 | ** that the collating sequence does not change. For example: |
| 763 | ** "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] | 764 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 765 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 766 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 767 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 768 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 769 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 770 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 771 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 772 | if( expRight==expLeft ){ |
| 773 | /* Either X and Y both have COLLATE operator or neither do */ |
| 774 | if( expRight ){ |
| 775 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 776 | ** used by clearing the EP_Collate flag from Y. */ |
| 777 | pExpr->pRight->flags &= ~EP_Collate; |
| 778 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 779 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 780 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 781 | ** it to be searched first. */ |
| 782 | pExpr->pLeft->flags |= EP_Collate; |
| 783 | } |
| 784 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 785 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 786 | if( pExpr->op>=TK_GT ){ |
| 787 | assert( TK_LT==TK_GT+2 ); |
| 788 | assert( TK_GE==TK_LE+2 ); |
| 789 | assert( TK_GT>TK_EQ ); |
| 790 | assert( TK_GT<TK_LE ); |
| 791 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 792 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 793 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 797 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 798 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 799 | static u16 operatorMask(int op){ |
| 800 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 801 | assert( allowedOp(op) ); |
| 802 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 803 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 804 | }else if( op==TK_ISNULL ){ |
| 805 | c = WO_ISNULL; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 806 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 807 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 808 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 809 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 810 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 811 | assert( op!=TK_IN || c==WO_IN ); |
| 812 | assert( op!=TK_EQ || c==WO_EQ ); |
| 813 | assert( op!=TK_LT || c==WO_LT ); |
| 814 | assert( op!=TK_LE || c==WO_LE ); |
| 815 | assert( op!=TK_GT || c==WO_GT ); |
| 816 | assert( op!=TK_GE || c==WO_GE ); |
| 817 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 821 | ** Advance to the next WhereTerm that matches according to the criteria |
| 822 | ** established when the pScan object was initialized by whereScanInit(). |
| 823 | ** Return NULL if there are no more matching WhereTerms. |
| 824 | */ |
| 825 | WhereTerm *whereScanNext(WhereScan *pScan){ |
| 826 | int iCur; /* The cursor on the LHS of the term */ |
| 827 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 828 | Expr *pX; /* An expression being tested */ |
| 829 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 830 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 831 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 832 | |
| 833 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 834 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 835 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 836 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 837 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 838 | if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){ |
| 839 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 840 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 841 | ){ |
| 842 | int j; |
| 843 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 844 | assert( pX->op==TK_COLUMN ); |
| 845 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 846 | if( pScan->aEquiv[j]==pX->iTable |
| 847 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | if( j==pScan->nEquiv ){ |
| 852 | pScan->aEquiv[j] = pX->iTable; |
| 853 | pScan->aEquiv[j+1] = pX->iColumn; |
| 854 | pScan->nEquiv += 2; |
| 855 | } |
| 856 | } |
| 857 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 858 | /* Verify the affinity and collating sequence match */ |
| 859 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 860 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 861 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 862 | pX = pTerm->pExpr; |
| 863 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 864 | continue; |
| 865 | } |
| 866 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 867 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 868 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 869 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 870 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 871 | continue; |
| 872 | } |
| 873 | } |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 874 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 875 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 876 | && pX->iTable==pScan->aEquiv[0] |
| 877 | && pX->iColumn==pScan->aEquiv[1] |
| 878 | ){ |
| 879 | continue; |
| 880 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 881 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 882 | return pTerm; |
| 883 | } |
| 884 | } |
| 885 | } |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 886 | pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 887 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 888 | } |
| 889 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 890 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 891 | pScan->iEquiv += 2; |
| 892 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 898 | ** first match. Return NULL if there are no matches. |
| 899 | ** |
| 900 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 901 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 902 | ** iCur. The <op> must be one of the operators described by opMask. |
| 903 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 904 | ** If the search is for X and the WHERE clause contains terms of the |
| 905 | ** form X=Y then this routine might also return terms of the form |
| 906 | ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 907 | ** but is enough to handle most commonly occurring SQL statements. |
| 908 | ** |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 909 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 910 | ** index pIdx. |
| 911 | */ |
| 912 | WhereTerm *whereScanInit( |
| 913 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 914 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 915 | int iCur, /* Cursor to scan for */ |
| 916 | int iColumn, /* Column to scan for */ |
| 917 | u32 opMask, /* Operator(s) to scan for */ |
| 918 | Index *pIdx /* Must be compatible with this index */ |
| 919 | ){ |
| 920 | int j; |
| 921 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 922 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 923 | pScan->pOrigWC = pWC; |
| 924 | pScan->pWC = pWC; |
| 925 | if( pIdx && iColumn>=0 ){ |
| 926 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 927 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 928 | if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 929 | } |
| 930 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 931 | }else{ |
| 932 | pScan->idxaff = 0; |
| 933 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 934 | } |
| 935 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 936 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 937 | pScan->aEquiv[0] = iCur; |
| 938 | pScan->aEquiv[1] = iColumn; |
| 939 | pScan->nEquiv = 2; |
| 940 | pScan->iEquiv = 2; |
| 941 | return whereScanNext(pScan); |
| 942 | } |
| 943 | |
| 944 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 945 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 946 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 947 | ** the WO_xx operator codes specified by the op parameter. |
| 948 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 949 | ** |
| 950 | ** The term returned might by Y=<expr> if there is another constraint in |
| 951 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 952 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 953 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 954 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 955 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 956 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 957 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 958 | ** and ... and A9=A10 and A10=<expr>. |
| 959 | ** |
| 960 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 961 | ** then try for the one with no dependencies on <expr> - in other words where |
| 962 | ** <expr> is a constant expression of some kind. Only return entries of |
| 963 | ** 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] | 964 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 965 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 966 | */ |
| 967 | static WhereTerm *findTerm( |
| 968 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 969 | int iCur, /* Cursor number of LHS */ |
| 970 | int iColumn, /* Column number of LHS */ |
| 971 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 972 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 973 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 974 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 975 | WhereTerm *pResult = 0; |
| 976 | WhereTerm *p; |
| 977 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 978 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 979 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 980 | while( p ){ |
| 981 | if( (p->prereqRight & notReady)==0 ){ |
| 982 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 983 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 984 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 985 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 986 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 987 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 988 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 989 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 990 | } |
| 991 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 992 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 993 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | ** Call exprAnalyze on all terms in a WHERE clause. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 997 | */ |
| 998 | static void exprAnalyzeAll( |
| 999 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1000 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 1001 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1002 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1003 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1004 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1008 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1009 | /* |
| 1010 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 1011 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 1012 | ** so and false if not. |
| 1013 | ** |
| 1014 | ** In order for the operator to be optimizible, the RHS must be a string |
| 1015 | ** literal that does not begin with a wildcard. |
| 1016 | */ |
| 1017 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1018 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1019 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1020 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1021 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 1022 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1023 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1024 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 1025 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 1026 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 1027 | int c; /* One character in z[] */ |
| 1028 | int cnt; /* Number of non-wildcard prefix characters */ |
| 1029 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 1030 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1031 | sqlite3_value *pVal = 0; |
| 1032 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1033 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1034 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1035 | return 0; |
| 1036 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1037 | #ifdef SQLITE_EBCDIC |
| 1038 | if( *pnoCase ) return 0; |
| 1039 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1040 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1041 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 1042 | if( pLeft->op!=TK_COLUMN |
| 1043 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 1044 | || IsVirtual(pLeft->pTab) |
| 1045 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1046 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 1047 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1048 | return 0; |
| 1049 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1050 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1051 | |
| 1052 | pRight = pList->a[0].pExpr; |
| 1053 | op = pRight->op; |
| 1054 | if( op==TK_REGISTER ){ |
| 1055 | op = pRight->op2; |
| 1056 | } |
| 1057 | if( op==TK_VARIABLE ){ |
| 1058 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 1059 | int iCol = pRight->iColumn; |
| 1060 | pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1061 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 1062 | z = (char *)sqlite3_value_text(pVal); |
| 1063 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1064 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1065 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 1066 | }else if( op==TK_STRING ){ |
| 1067 | z = pRight->u.zToken; |
| 1068 | } |
| 1069 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1070 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1071 | 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] | 1072 | cnt++; |
| 1073 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1074 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1075 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1076 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1077 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 1078 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 1079 | *ppPrefix = pPrefix; |
| 1080 | if( op==TK_VARIABLE ){ |
| 1081 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1082 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1083 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 1084 | /* If the rhs of the LIKE expression is a variable, and the current |
| 1085 | ** value of the variable means there is no need to invoke the LIKE |
| 1086 | ** function, then no OP_Variable will be added to the program. |
| 1087 | ** This causes problems for the sqlite3_bind_parameter_name() |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1088 | ** API. To workaround them, add a dummy OP_Variable here. |
| 1089 | */ |
| 1090 | int r1 = sqlite3GetTempReg(pParse); |
| 1091 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1092 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1093 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | }else{ |
| 1097 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1098 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1099 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1100 | |
| 1101 | sqlite3ValueFree(pVal); |
| 1102 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1103 | } |
| 1104 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 1105 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1106 | |
| 1107 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1108 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1109 | ** Check to see if the given expression is of the form |
| 1110 | ** |
| 1111 | ** column MATCH expr |
| 1112 | ** |
| 1113 | ** If it is then return TRUE. If not, return FALSE. |
| 1114 | */ |
| 1115 | static int isMatchOfColumn( |
| 1116 | Expr *pExpr /* Test this expression */ |
| 1117 | ){ |
| 1118 | ExprList *pList; |
| 1119 | |
| 1120 | if( pExpr->op!=TK_FUNCTION ){ |
| 1121 | return 0; |
| 1122 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1123 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1124 | return 0; |
| 1125 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1126 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1127 | if( pList->nExpr!=2 ){ |
| 1128 | return 0; |
| 1129 | } |
| 1130 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 1131 | return 0; |
| 1132 | } |
| 1133 | return 1; |
| 1134 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1135 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1138 | ** If the pBase expression originated in the ON or USING clause of |
| 1139 | ** a join, then transfer the appropriate markings over to derived. |
| 1140 | */ |
| 1141 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
| 1142 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 1143 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 1144 | } |
| 1145 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1146 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 1147 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1148 | ** Analyze a term that consists of two or more OR-connected |
| 1149 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1150 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1151 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 1152 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1153 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1154 | ** This routine analyzes terms such as the middle term in the above example. |
| 1155 | ** A WhereOrTerm object is computed and attached to the term under |
| 1156 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1157 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1158 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 1159 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1160 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1161 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 1162 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1163 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1164 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1165 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 1166 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 1167 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 1168 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 1169 | ** (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] | 1170 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1171 | ** CASE 1: |
| 1172 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1173 | ** 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] | 1174 | ** a single table T (as shown in example B above) then create a new virtual |
| 1175 | ** term that is an equivalent IN expression. In other words, if the term |
| 1176 | ** being analyzed is: |
| 1177 | ** |
| 1178 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 1179 | ** |
| 1180 | ** then create a new virtual term like this: |
| 1181 | ** |
| 1182 | ** x IN (expr1,expr2,expr3) |
| 1183 | ** |
| 1184 | ** CASE 2: |
| 1185 | ** |
| 1186 | ** If all subterms are indexable by a single table T, then set |
| 1187 | ** |
| 1188 | ** WhereTerm.eOperator = WO_OR |
| 1189 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 1190 | ** |
| 1191 | ** A subterm is "indexable" if it is of the form |
| 1192 | ** "T.C <op> <expr>" where C is any column of table T and |
| 1193 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 1194 | ** A subterm is also indexable if it is an AND of two or more |
| 1195 | ** subsubterms at least one of which is indexable. Indexable AND |
| 1196 | ** subterms have their eOperator set to WO_AND and they have |
| 1197 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 1198 | ** |
| 1199 | ** From another point of view, "indexable" means that the subterm could |
| 1200 | ** potentially be used with an index if an appropriate index exists. |
| 1201 | ** This analysis does not consider whether or not the index exists; that |
| 1202 | ** is something the bestIndex() routine will determine. This analysis |
| 1203 | ** only looks at whether subterms appropriate for indexing exist. |
| 1204 | ** |
| 1205 | ** All examples A through E above all satisfy case 2. But if a term |
| 1206 | ** also statisfies case 1 (such as B) we know that the optimizer will |
| 1207 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 1208 | ** satisfied. |
| 1209 | ** |
| 1210 | ** It might be the case that multiple tables are indexable. For example, |
| 1211 | ** (E) above is indexable on tables P, Q, and R. |
| 1212 | ** |
| 1213 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 1214 | ** separate indices to find rowids for each subterm and composing |
| 1215 | ** the union of all rowids using a RowSet object. This is similar |
| 1216 | ** to "bitmap indices" in other database engines. |
| 1217 | ** |
| 1218 | ** OTHERWISE: |
| 1219 | ** |
| 1220 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 1221 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1222 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1223 | static void exprAnalyzeOrTerm( |
| 1224 | SrcList *pSrc, /* the FROM clause */ |
| 1225 | WhereClause *pWC, /* the complete WHERE clause */ |
| 1226 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 1227 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1228 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 1229 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1230 | sqlite3 *db = pParse->db; /* Database connection */ |
| 1231 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 1232 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1233 | int i; /* Loop counters */ |
| 1234 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 1235 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 1236 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 1237 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 1238 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1239 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1240 | /* |
| 1241 | ** Break the OR clause into its separate subterms. The subterms are |
| 1242 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 1243 | ** object that is attached to the original OR clause term. |
| 1244 | */ |
| 1245 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 1246 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 1247 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1248 | if( pOrInfo==0 ) return; |
| 1249 | pTerm->wtFlags |= TERM_ORINFO; |
| 1250 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1251 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1252 | whereSplit(pOrWc, pExpr, TK_OR); |
| 1253 | exprAnalyzeAll(pSrc, pOrWc); |
| 1254 | if( db->mallocFailed ) return; |
| 1255 | assert( pOrWc->nTerm>=2 ); |
| 1256 | |
| 1257 | /* |
| 1258 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 1259 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 1260 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1261 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1262 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 1263 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1264 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1265 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1266 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1267 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 1268 | if( pAndInfo ){ |
| 1269 | WhereClause *pAndWC; |
| 1270 | WhereTerm *pAndTerm; |
| 1271 | int j; |
| 1272 | Bitmask b = 0; |
| 1273 | pOrTerm->u.pAndInfo = pAndInfo; |
| 1274 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 1275 | pOrTerm->eOperator = WO_AND; |
| 1276 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1277 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1278 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 1279 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 1280 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 1281 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1282 | if( !db->mallocFailed ){ |
| 1283 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 1284 | assert( pAndTerm->pExpr ); |
| 1285 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1286 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1287 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | indexable &= b; |
| 1291 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1292 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 1293 | /* Skip this term for now. We revisit it when we process the |
| 1294 | ** corresponding TERM_VIRTUAL term */ |
| 1295 | }else{ |
| 1296 | Bitmask b; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1297 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1298 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 1299 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1300 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1301 | } |
| 1302 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1303 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1304 | chngToIN = 0; |
| 1305 | }else{ |
| 1306 | chngToIN &= b; |
| 1307 | } |
| 1308 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1309 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1310 | |
| 1311 | /* |
| 1312 | ** Record the set of tables that satisfy case 2. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1313 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1314 | */ |
| 1315 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1316 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1317 | |
| 1318 | /* |
| 1319 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 1320 | ** we have to do some additional checking to see if case 1 really |
| 1321 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1322 | ** |
| 1323 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 1324 | ** that there is no possibility of transforming the OR clause into an |
| 1325 | ** IN operator because one or more terms in the OR clause contain |
| 1326 | ** something other than == on a column in the single table. The 1-bit |
| 1327 | ** case means that every term of the OR clause is of the form |
| 1328 | ** "table.column=expr" for some single table. The one bit that is set |
| 1329 | ** will correspond to the common table. We still need to check to make |
| 1330 | ** sure the same column is used on all terms. The 2-bit case is when |
| 1331 | ** the all terms are of the form "table1.column=table2.column". It |
| 1332 | ** might be possible to form an IN operator with either table1.column |
| 1333 | ** or table2.column as the LHS if either is common to every term of |
| 1334 | ** the OR clause. |
| 1335 | ** |
| 1336 | ** Note that terms of the form "table.column1=table.column2" (the |
| 1337 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1338 | */ |
| 1339 | if( chngToIN ){ |
| 1340 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 1341 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 1342 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1343 | int j = 0; /* Loop counter */ |
| 1344 | |
| 1345 | /* Search for a table and column that appears on one side or the |
| 1346 | ** other of the == operator in every subterm. That table and column |
| 1347 | ** will be recorded in iCursor and iColumn. There might not be any |
| 1348 | ** such table and column. Set okToChngToIN if an appropriate table |
| 1349 | ** and column is found but leave okToChngToIN false if not found. |
| 1350 | */ |
| 1351 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 1352 | pOrTerm = pOrWc->a; |
| 1353 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1354 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1355 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1356 | if( pOrTerm->leftCursor==iCursor ){ |
| 1357 | /* This is the 2-bit case and we are on the second iteration and |
| 1358 | ** current term is from the first iteration. So skip this term. */ |
| 1359 | assert( j==1 ); |
| 1360 | continue; |
| 1361 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1362 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1363 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 1364 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 1365 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 1366 | ** and use its inversion. */ |
| 1367 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 1368 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 1369 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 1370 | continue; |
| 1371 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1372 | iColumn = pOrTerm->u.leftColumn; |
| 1373 | iCursor = pOrTerm->leftCursor; |
| 1374 | break; |
| 1375 | } |
| 1376 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1377 | /* No candidate table+column was found. This can only occur |
| 1378 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1379 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1380 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1381 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1382 | break; |
| 1383 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1384 | testcase( j==1 ); |
| 1385 | |
| 1386 | /* We have found a candidate table and column. Check to see if that |
| 1387 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1388 | okToChngToIN = 1; |
| 1389 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1390 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1391 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1392 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1393 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1394 | okToChngToIN = 0; |
| 1395 | }else{ |
| 1396 | int affLeft, affRight; |
| 1397 | /* If the right-hand side is also a column, then the affinities |
| 1398 | ** of both right and left sides must be such that no type |
| 1399 | ** conversions are required on the right. (Ticket #2249) |
| 1400 | */ |
| 1401 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1402 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1403 | if( affRight!=0 && affRight!=affLeft ){ |
| 1404 | okToChngToIN = 0; |
| 1405 | }else{ |
| 1406 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1413 | ** case 1. In that case, construct a new virtual term that is |
| 1414 | ** pTerm converted into an IN operator. |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 1415 | ** |
| 1416 | ** EV: R-00211-15100 |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1417 | */ |
| 1418 | if( okToChngToIN ){ |
| 1419 | Expr *pDup; /* A transient duplicate expression */ |
| 1420 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1421 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1422 | Expr *pNew; /* The complete IN operator */ |
| 1423 | |
| 1424 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1425 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1426 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1427 | assert( pOrTerm->leftCursor==iCursor ); |
| 1428 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1429 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1430 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1431 | pLeft = pOrTerm->pExpr->pLeft; |
| 1432 | } |
| 1433 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1434 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1435 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1436 | if( pNew ){ |
| 1437 | int idxNew; |
| 1438 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1439 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1440 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1441 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1442 | testcase( idxNew==0 ); |
| 1443 | exprAnalyze(pSrc, pWC, idxNew); |
| 1444 | pTerm = &pWC->a[idxTerm]; |
| 1445 | pWC->a[idxNew].iParent = idxTerm; |
| 1446 | pTerm->nChild = 1; |
| 1447 | }else{ |
| 1448 | sqlite3ExprListDelete(db, pList); |
| 1449 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1450 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1451 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1452 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1453 | } |
| 1454 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1455 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1456 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1457 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1458 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1459 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1460 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1461 | ** |
| 1462 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1463 | ** to the standard form of "X <op> <expr>". |
| 1464 | ** |
| 1465 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1466 | ** columns, then the original expression is unchanged and a new virtual |
| 1467 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1468 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1469 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1470 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1471 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1472 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1473 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1474 | static void exprAnalyze( |
| 1475 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1476 | WhereClause *pWC, /* the WHERE clause */ |
| 1477 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1478 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1479 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1480 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1481 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1482 | Expr *pExpr; /* The expression to be analyzed */ |
| 1483 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1484 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1485 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1486 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1487 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1488 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1489 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1490 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1491 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1492 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1493 | if( db->mallocFailed ){ |
| 1494 | return; |
| 1495 | } |
| 1496 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1497 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1498 | pExpr = pTerm->pExpr; |
| 1499 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1500 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1501 | op = pExpr->op; |
| 1502 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1503 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1504 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1505 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1506 | }else{ |
| 1507 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1508 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1509 | }else if( op==TK_ISNULL ){ |
| 1510 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1511 | }else{ |
| 1512 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1513 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1514 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1515 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1516 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1517 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1518 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1519 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1520 | } |
| 1521 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1522 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1523 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1524 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1525 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1526 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1527 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1528 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1529 | if( pLeft->op==TK_COLUMN ){ |
| 1530 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1531 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1532 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1533 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1534 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1535 | WhereTerm *pNew; |
| 1536 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1537 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1538 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1539 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1540 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1541 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1542 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1543 | return; |
| 1544 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1545 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1546 | if( idxNew==0 ) return; |
| 1547 | pNew = &pWC->a[idxNew]; |
| 1548 | pNew->iParent = idxTerm; |
| 1549 | pTerm = &pWC->a[idxTerm]; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1550 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1551 | pTerm->wtFlags |= TERM_COPIED; |
drh | eb5bc92 | 2013-01-17 16:43:33 +0000 | [diff] [blame] | 1552 | if( pExpr->op==TK_EQ |
| 1553 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1554 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 1555 | ){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1556 | pTerm->eOperator |= WO_EQUIV; |
| 1557 | eExtraOp = WO_EQUIV; |
| 1558 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1559 | }else{ |
| 1560 | pDup = pExpr; |
| 1561 | pNew = pTerm; |
| 1562 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1563 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1564 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1565 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1566 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1567 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1568 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1569 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1570 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1571 | } |
| 1572 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1573 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1574 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1575 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1576 | ** that define the range that the BETWEEN implements. For example: |
| 1577 | ** |
| 1578 | ** a BETWEEN b AND c |
| 1579 | ** |
| 1580 | ** is converted into: |
| 1581 | ** |
| 1582 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1583 | ** |
| 1584 | ** The two new terms are added onto the end of the WhereClause object. |
| 1585 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1586 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1587 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1588 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1589 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1590 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1591 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1592 | int i; |
| 1593 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1594 | assert( pList!=0 ); |
| 1595 | assert( pList->nExpr==2 ); |
| 1596 | for(i=0; i<2; i++){ |
| 1597 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1598 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1599 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1600 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1601 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1602 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1603 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1604 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1605 | pTerm = &pWC->a[idxTerm]; |
| 1606 | pWC->a[idxNew].iParent = idxTerm; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1607 | } |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1608 | pTerm->nChild = 2; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1609 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1610 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1611 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1612 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1613 | /* Analyze a term that is composed of two or more subterms connected by |
| 1614 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1615 | */ |
| 1616 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1617 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1618 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1619 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1620 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1621 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1622 | |
| 1623 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1624 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1625 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1626 | ** |
| 1627 | ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 1628 | ** |
| 1629 | ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 1630 | ** |
| 1631 | ** The last character of the prefix "abc" is incremented to form the |
shane | 7bc71e5 | 2008-05-28 18:01:44 +0000 | [diff] [blame] | 1632 | ** termination condition "abd". |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1633 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1634 | if( pWC->op==TK_AND |
| 1635 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1636 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1637 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1638 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1639 | Expr *pNewExpr1; |
| 1640 | Expr *pNewExpr2; |
| 1641 | int idxNew1; |
| 1642 | int idxNew2; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1643 | Token sCollSeqName; /* Name of collating sequence */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1644 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1645 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1646 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1647 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1648 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1649 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1650 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1651 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1652 | /* The point is to increment the last character before the first |
| 1653 | ** wildcard. But if we increment '@', that will push it into the |
| 1654 | ** alphabetic range where case conversions will mess up the |
| 1655 | ** inequality. To avoid this, make sure to also run the full |
| 1656 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1657 | */ |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 1658 | if( c=='A'-1 ) isComplete = 0; /* EV: R-64339-08207 */ |
| 1659 | |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1660 | |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1661 | c = sqlite3UpperToLower[c]; |
| 1662 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1663 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1664 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1665 | sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 1666 | sCollSeqName.n = 6; |
| 1667 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1668 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1669 | sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1670 | pStr1, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1671 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1672 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1673 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1674 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1675 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1676 | sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1677 | pStr2, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1678 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1679 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1680 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1681 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1682 | if( isComplete ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1683 | pWC->a[idxNew1].iParent = idxTerm; |
| 1684 | pWC->a[idxNew2].iParent = idxTerm; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1685 | pTerm->nChild = 2; |
| 1686 | } |
| 1687 | } |
| 1688 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1689 | |
| 1690 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1691 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1692 | ** current expression is of the form: column MATCH expr. |
| 1693 | ** This information is used by the xBestIndex methods of |
| 1694 | ** virtual tables. The native query optimizer does not attempt |
| 1695 | ** to do anything with MATCH functions. |
| 1696 | */ |
| 1697 | if( isMatchOfColumn(pExpr) ){ |
| 1698 | int idxNew; |
| 1699 | Expr *pRight, *pLeft; |
| 1700 | WhereTerm *pNewTerm; |
| 1701 | Bitmask prereqColumn, prereqExpr; |
| 1702 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1703 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1704 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1705 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1706 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1707 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1708 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1709 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1710 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1711 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1712 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1713 | pNewTerm = &pWC->a[idxNew]; |
| 1714 | pNewTerm->prereqRight = prereqExpr; |
| 1715 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1716 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1717 | pNewTerm->eOperator = WO_MATCH; |
| 1718 | pNewTerm->iParent = idxTerm; |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1719 | pTerm = &pWC->a[idxTerm]; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1720 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1721 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1722 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1723 | } |
| 1724 | } |
| 1725 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1726 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1727 | #ifdef SQLITE_ENABLE_STAT3 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1728 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1729 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1730 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1731 | ** virtual term of that form. |
| 1732 | ** |
| 1733 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 1734 | ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 1735 | ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 1736 | ** the start of the loop will prevent any results from being returned. |
| 1737 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1738 | if( pExpr->op==TK_NOTNULL |
| 1739 | && pExpr->pLeft->op==TK_COLUMN |
| 1740 | && pExpr->pLeft->iColumn>=0 |
| 1741 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1742 | Expr *pNewExpr; |
| 1743 | Expr *pLeft = pExpr->pLeft; |
| 1744 | int idxNew; |
| 1745 | WhereTerm *pNewTerm; |
| 1746 | |
| 1747 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1748 | sqlite3ExprDup(db, pLeft, 0), |
| 1749 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1750 | |
| 1751 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1752 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1753 | if( idxNew ){ |
| 1754 | pNewTerm = &pWC->a[idxNew]; |
| 1755 | pNewTerm->prereqRight = 0; |
| 1756 | pNewTerm->leftCursor = pLeft->iTable; |
| 1757 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1758 | pNewTerm->eOperator = WO_GT; |
| 1759 | pNewTerm->iParent = idxTerm; |
| 1760 | pTerm = &pWC->a[idxTerm]; |
| 1761 | pTerm->nChild = 1; |
| 1762 | pTerm->wtFlags |= TERM_COPIED; |
| 1763 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1764 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1765 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1766 | #endif /* SQLITE_ENABLE_STAT */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1767 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1768 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1769 | ** an index for tables to the left of the join. |
| 1770 | */ |
| 1771 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1774 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1775 | ** This function searches pList for a entry that matches the iCol-th column |
| 1776 | ** of index pIdx. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1777 | ** |
| 1778 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1779 | ** no expression is found, -1 is returned. |
| 1780 | */ |
| 1781 | static int findIndexCol( |
| 1782 | Parse *pParse, /* Parse context */ |
| 1783 | ExprList *pList, /* Expression list to search */ |
| 1784 | int iBase, /* Cursor for table associated with pIdx */ |
| 1785 | Index *pIdx, /* Index to match column of */ |
| 1786 | int iCol /* Column of index to match */ |
| 1787 | ){ |
| 1788 | int i; |
| 1789 | const char *zColl = pIdx->azColl[iCol]; |
| 1790 | |
| 1791 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1792 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1793 | if( p->op==TK_COLUMN |
| 1794 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1795 | && p->iTable==iBase |
| 1796 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1797 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1798 | if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1799 | return i; |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | return -1; |
| 1805 | } |
| 1806 | |
| 1807 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1808 | ** Return true if the DISTINCT expression-list passed as the third argument |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1809 | ** is redundant. |
| 1810 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1811 | ** A DISTINCT list is redundant if the database contains some subset of |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1812 | ** columns that are unique and non-null. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1813 | */ |
| 1814 | static int isDistinctRedundant( |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1815 | Parse *pParse, /* Parsing context */ |
| 1816 | SrcList *pTabList, /* The FROM clause */ |
| 1817 | WhereClause *pWC, /* The WHERE clause */ |
| 1818 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1819 | ){ |
| 1820 | Table *pTab; |
| 1821 | Index *pIdx; |
| 1822 | int i; |
| 1823 | int iBase; |
| 1824 | |
| 1825 | /* If there is more than one table or sub-select in the FROM clause of |
| 1826 | ** this query, then it will not be possible to show that the DISTINCT |
| 1827 | ** clause is redundant. */ |
| 1828 | if( pTabList->nSrc!=1 ) return 0; |
| 1829 | iBase = pTabList->a[0].iCursor; |
| 1830 | pTab = pTabList->a[0].pTab; |
| 1831 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1832 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1833 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1834 | ** current SELECT is a correlated sub-query. |
| 1835 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1836 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1837 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1838 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | /* Loop through all indices on the table, checking each to see if it makes |
| 1842 | ** the DISTINCT qualifier redundant. It does so if: |
| 1843 | ** |
| 1844 | ** 1. The index is itself UNIQUE, and |
| 1845 | ** |
| 1846 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1847 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1848 | ** where X is a constant value. The collation sequences of the |
| 1849 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1850 | ** |
| 1851 | ** 3. All of those index columns for which the WHERE clause does not |
| 1852 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1853 | */ |
| 1854 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1855 | if( pIdx->onError==OE_None ) continue; |
| 1856 | for(i=0; i<pIdx->nColumn; i++){ |
| 1857 | int iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1858 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1859 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
| 1860 | if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){ |
| 1861 | break; |
| 1862 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1863 | } |
| 1864 | } |
| 1865 | if( i==pIdx->nColumn ){ |
| 1866 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1867 | return 1; |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | return 0; |
| 1872 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1873 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1874 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1875 | ** The (an approximate) sum of two WhereCosts. This computation is |
| 1876 | ** not a simple "+" operator because WhereCost is stored as a logarithmic |
| 1877 | ** value. |
| 1878 | ** |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1879 | */ |
| 1880 | static WhereCost whereCostAdd(WhereCost a, WhereCost b){ |
| 1881 | static const unsigned char x[] = { |
| 1882 | 10, 10, /* 0,1 */ |
| 1883 | 9, 9, /* 2,3 */ |
| 1884 | 8, 8, /* 4,5 */ |
| 1885 | 7, 7, 7, /* 6,7,8 */ |
| 1886 | 6, 6, 6, /* 9,10,11 */ |
| 1887 | 5, 5, 5, /* 12-14 */ |
| 1888 | 4, 4, 4, 4, /* 15-18 */ |
| 1889 | 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 1890 | 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 1891 | }; |
| 1892 | if( a>=b ){ |
| 1893 | if( a>b+49 ) return a; |
| 1894 | if( a>b+31 ) return a+1; |
| 1895 | return a+x[a-b]; |
| 1896 | }else{ |
| 1897 | if( b>a+49 ) return b; |
| 1898 | if( b>a+31 ) return b+1; |
| 1899 | return b+x[b-a]; |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1904 | ** Convert an integer into a WhereCost. In other words, compute a |
| 1905 | ** good approximatation for 10*log2(x). |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1906 | */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 1907 | static WhereCost whereCost(tRowcnt x){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1908 | static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 1909 | WhereCost y = 40; |
| 1910 | if( x<8 ){ |
| 1911 | if( x<2 ) return 0; |
| 1912 | while( x<8 ){ y -= 10; x <<= 1; } |
| 1913 | }else{ |
| 1914 | while( x>255 ){ y += 40; x >>= 4; } |
| 1915 | while( x>15 ){ y += 10; x >>= 1; } |
| 1916 | } |
| 1917 | return a[x&7] + y - 10; |
| 1918 | } |
| 1919 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1920 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1921 | /* |
| 1922 | ** Convert a double (as received from xBestIndex of a virtual table) |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1923 | ** into a WhereCost. In other words, compute an approximation for |
| 1924 | ** 10*log2(x). |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1925 | */ |
| 1926 | static WhereCost whereCostFromDouble(double x){ |
| 1927 | u64 a; |
| 1928 | WhereCost e; |
| 1929 | assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 1930 | if( x<=1 ) return 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 1931 | if( x<=2000000000 ) return whereCost((tRowcnt)x); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1932 | memcpy(&a, &x, 8); |
| 1933 | e = (a>>52) - 1022; |
| 1934 | return e*10; |
| 1935 | } |
| 1936 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 1937 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1938 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1939 | ** Estimate the logarithm of the input value to base 2. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1940 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 1941 | static WhereCost estLog(WhereCost N){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 1942 | WhereCost x = whereCost(N); |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 1943 | return x>33 ? x - 33 : 0; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1946 | /* |
| 1947 | ** Two routines for printing the content of an sqlite3_index_info |
| 1948 | ** structure. Used for testing and debugging only. If neither |
| 1949 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 1950 | ** are no-ops. |
| 1951 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 1952 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1953 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 1954 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1955 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1956 | for(i=0; i<p->nConstraint; i++){ |
| 1957 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 1958 | i, |
| 1959 | p->aConstraint[i].iColumn, |
| 1960 | p->aConstraint[i].iTermOffset, |
| 1961 | p->aConstraint[i].op, |
| 1962 | p->aConstraint[i].usable); |
| 1963 | } |
| 1964 | for(i=0; i<p->nOrderBy; i++){ |
| 1965 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 1966 | i, |
| 1967 | p->aOrderBy[i].iColumn, |
| 1968 | p->aOrderBy[i].desc); |
| 1969 | } |
| 1970 | } |
| 1971 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 1972 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1973 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1974 | for(i=0; i<p->nConstraint; i++){ |
| 1975 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 1976 | i, |
| 1977 | p->aConstraintUsage[i].argvIndex, |
| 1978 | p->aConstraintUsage[i].omit); |
| 1979 | } |
| 1980 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 1981 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 1982 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 1983 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
| 1984 | } |
| 1985 | #else |
| 1986 | #define TRACE_IDX_INPUTS(A) |
| 1987 | #define TRACE_IDX_OUTPUTS(A) |
| 1988 | #endif |
| 1989 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1990 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1991 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1992 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 1993 | ** could be used with an index to access pSrc, assuming an appropriate |
| 1994 | ** index existed. |
| 1995 | */ |
| 1996 | static int termCanDriveIndex( |
| 1997 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 1998 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 1999 | Bitmask notReady /* Tables in outer loops of the join */ |
| 2000 | ){ |
| 2001 | char aff; |
| 2002 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2003 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2004 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 2005 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2006 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 2007 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 2008 | return 1; |
| 2009 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2010 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2011 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2012 | |
| 2013 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2014 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2015 | ** Generate code to construct the Index object for an automatic index |
| 2016 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 2017 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2018 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2019 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2020 | Parse *pParse, /* The parsing context */ |
| 2021 | WhereClause *pWC, /* The WHERE clause */ |
| 2022 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 2023 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 2024 | WhereLevel *pLevel /* Write new index here */ |
| 2025 | ){ |
| 2026 | int nColumn; /* Number of columns in the constructed index */ |
| 2027 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 2028 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 2029 | int nByte; /* Byte of memory needed for pIdx */ |
| 2030 | Index *pIdx; /* Object describing the transient index */ |
| 2031 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2032 | int addrInit; /* Address of the initialization bypass jump */ |
| 2033 | Table *pTable; /* The table being indexed */ |
| 2034 | KeyInfo *pKeyinfo; /* Key information for the index */ |
| 2035 | int addrTop; /* Top of the index fill loop */ |
| 2036 | int regRecord; /* Register holding an index record */ |
| 2037 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2038 | int i; /* Loop counter */ |
| 2039 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2040 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2041 | WhereLoop *pLoop; /* The Loop object */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2042 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 2043 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2044 | |
| 2045 | /* Generate code to skip over the creation and initialization of the |
| 2046 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 2047 | v = pParse->pVdbe; |
| 2048 | assert( v!=0 ); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2049 | addrInit = sqlite3CodeOnce(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2050 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2051 | /* Count the number of columns that will be added to the index |
| 2052 | ** and used to match WHERE clause constraints */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2053 | nColumn = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2054 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2055 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2056 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2057 | idxCols = 0; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 2058 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2059 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 2060 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2061 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2062 | testcase( iCol==BMS ); |
| 2063 | testcase( iCol==BMS-1 ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2064 | if( (idxCols & cMask)==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2065 | if( whereLoopResize(pParse->db, pLoop, nColumn+1) ) return; |
| 2066 | pLoop->aLTerm[nColumn++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2067 | idxCols |= cMask; |
| 2068 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | assert( nColumn>0 ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2072 | pLoop->u.btree.nEq = pLoop->nLTerm = nColumn; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 2073 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
| 2074 | | WHERE_TEMP_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2075 | |
| 2076 | /* Count the number of additional columns needed to create a |
| 2077 | ** covering index. A "covering index" is an index that contains all |
| 2078 | ** columns that are needed by the query. With a covering index, the |
| 2079 | ** original table never needs to be accessed. Automatic indices must |
| 2080 | ** be a covering index because the index will not be updated if the |
| 2081 | ** original table changes and the index and table cannot both be used |
| 2082 | ** if they go out of sync. |
| 2083 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2084 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2085 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2086 | testcase( pTable->nCol==BMS-1 ); |
| 2087 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2088 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2089 | if( extraCols & MASKBIT(i) ) nColumn++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2090 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2091 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2092 | nColumn += pTable->nCol - BMS + 1; |
| 2093 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2094 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2095 | |
| 2096 | /* Construct the Index object to describe this index */ |
| 2097 | nByte = sizeof(Index); |
| 2098 | nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 2099 | nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 2100 | nByte += nColumn; /* Index.aSortOrder */ |
| 2101 | pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 2102 | if( pIdx==0 ) return; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2103 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2104 | pIdx->azColl = (char**)&pIdx[1]; |
| 2105 | pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 2106 | pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 2107 | pIdx->zName = "auto-index"; |
| 2108 | pIdx->nColumn = nColumn; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2109 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2110 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2111 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2112 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2113 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2114 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2115 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 2116 | testcase( iCol==BMS-1 ); |
| 2117 | testcase( iCol==BMS ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2118 | if( (idxCols & cMask)==0 ){ |
| 2119 | Expr *pX = pTerm->pExpr; |
| 2120 | idxCols |= cMask; |
| 2121 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 2122 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 6f2e6c0 | 2011-02-17 13:33:15 +0000 | [diff] [blame] | 2123 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2124 | n++; |
| 2125 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2126 | } |
| 2127 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2128 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2129 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2130 | /* Add additional columns needed to make the automatic index into |
| 2131 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2132 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2133 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2134 | pIdx->aiColumn[n] = i; |
| 2135 | pIdx->azColl[n] = "BINARY"; |
| 2136 | n++; |
| 2137 | } |
| 2138 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2139 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2140 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 2141 | pIdx->aiColumn[n] = i; |
| 2142 | pIdx->azColl[n] = "BINARY"; |
| 2143 | n++; |
| 2144 | } |
| 2145 | } |
| 2146 | assert( n==nColumn ); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2147 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2148 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2149 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 2150 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 2151 | pLevel->iIdxCur = pParse->nTab++; |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2152 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2153 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2154 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2155 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2156 | /* Fill the automatic index with content */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2157 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); |
| 2158 | regRecord = sqlite3GetTempReg(pParse); |
| 2159 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1); |
| 2160 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 2161 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2162 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2163 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2164 | sqlite3VdbeJumpHere(v, addrTop); |
| 2165 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 2166 | |
| 2167 | /* Jump here when skipping the initialization */ |
| 2168 | sqlite3VdbeJumpHere(v, addrInit); |
| 2169 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2170 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2171 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 2172 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2173 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2174 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 2175 | ** responsibility of the caller to eventually release the structure |
| 2176 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 2177 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2178 | static sqlite3_index_info *allocateIndexInfo( |
| 2179 | Parse *pParse, |
| 2180 | WhereClause *pWC, |
| 2181 | struct SrcList_item *pSrc, |
| 2182 | ExprList *pOrderBy |
| 2183 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2184 | int i, j; |
| 2185 | int nTerm; |
| 2186 | struct sqlite3_index_constraint *pIdxCons; |
| 2187 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 2188 | struct sqlite3_index_constraint_usage *pUsage; |
| 2189 | WhereTerm *pTerm; |
| 2190 | int nOrderBy; |
| 2191 | sqlite3_index_info *pIdxInfo; |
| 2192 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2193 | /* Count the number of possible WHERE clause constraints referring |
| 2194 | ** to this virtual table */ |
| 2195 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 2196 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2197 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2198 | testcase( pTerm->eOperator & WO_IN ); |
| 2199 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2200 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2201 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2202 | nTerm++; |
| 2203 | } |
| 2204 | |
| 2205 | /* If the ORDER BY clause contains only columns in the current |
| 2206 | ** virtual table then allocate space for the aOrderBy part of |
| 2207 | ** the sqlite3_index_info structure. |
| 2208 | */ |
| 2209 | nOrderBy = 0; |
| 2210 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2211 | int n = pOrderBy->nExpr; |
| 2212 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2213 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2214 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 2215 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2216 | if( i==n){ |
| 2217 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | /* Allocate the sqlite3_index_info structure |
| 2222 | */ |
| 2223 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 2224 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 2225 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 2226 | if( pIdxInfo==0 ){ |
| 2227 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2228 | return 0; |
| 2229 | } |
| 2230 | |
| 2231 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 2232 | ** many fields that are declared "const" to prevent xBestIndex from |
| 2233 | ** changing them. We have to do some funky casting in order to |
| 2234 | ** initialize those fields. |
| 2235 | */ |
| 2236 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 2237 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 2238 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 2239 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 2240 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 2241 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 2242 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 2243 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 2244 | pUsage; |
| 2245 | |
| 2246 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2247 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2248 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2249 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2250 | testcase( pTerm->eOperator & WO_IN ); |
| 2251 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2252 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2253 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2254 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 2255 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2256 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2257 | if( op==WO_IN ) op = WO_EQ; |
| 2258 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2259 | /* The direct assignment in the previous line is possible only because |
| 2260 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 2261 | ** following asserts verify this fact. */ |
| 2262 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 2263 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 2264 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 2265 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 2266 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 2267 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2268 | 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] | 2269 | j++; |
| 2270 | } |
| 2271 | for(i=0; i<nOrderBy; i++){ |
| 2272 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2273 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 2274 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 2275 | } |
| 2276 | |
| 2277 | return pIdxInfo; |
| 2278 | } |
| 2279 | |
| 2280 | /* |
| 2281 | ** The table object reference passed as the second argument to this function |
| 2282 | ** must represent a virtual table. This function invokes the xBestIndex() |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2283 | ** method of the virtual table with the sqlite3_index_info object that |
| 2284 | ** comes in as the 3rd argument to this function. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2285 | ** |
| 2286 | ** If an error occurs, pParse is populated with an error message and a |
| 2287 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 2288 | ** part of the sqlite3_index_info structure is left populated. |
| 2289 | ** |
| 2290 | ** Whether or not an error is returned, it is the responsibility of the |
| 2291 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 2292 | ** that this is required. |
| 2293 | */ |
| 2294 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 2295 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2296 | int i; |
| 2297 | int rc; |
| 2298 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2299 | TRACE_IDX_INPUTS(p); |
| 2300 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 2301 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2302 | |
| 2303 | if( rc!=SQLITE_OK ){ |
| 2304 | if( rc==SQLITE_NOMEM ){ |
| 2305 | pParse->db->mallocFailed = 1; |
| 2306 | }else if( !pVtab->zErrMsg ){ |
| 2307 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 2308 | }else{ |
| 2309 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 2310 | } |
| 2311 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2312 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2313 | pVtab->zErrMsg = 0; |
| 2314 | |
| 2315 | for(i=0; i<p->nConstraint; i++){ |
| 2316 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 2317 | sqlite3ErrorMsg(pParse, |
| 2318 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | return pParse->nErr; |
| 2323 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2324 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2325 | |
| 2326 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2327 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2328 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2329 | ** Estimate the location of a particular key among all keys in an |
| 2330 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2331 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2332 | ** aStat[0] Est. number of rows less than pVal |
| 2333 | ** aStat[1] Est. number of rows equal to pVal |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2334 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2335 | ** Return SQLITE_OK on success. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2336 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2337 | static int whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2338 | Parse *pParse, /* Database connection */ |
| 2339 | Index *pIdx, /* Index to consider domain of */ |
| 2340 | sqlite3_value *pVal, /* Value to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2341 | int roundUp, /* Round up if true. Round down if false */ |
| 2342 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2343 | ){ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2344 | tRowcnt n; |
| 2345 | IndexSample *aSample; |
| 2346 | int i, eType; |
| 2347 | int isEq = 0; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2348 | i64 v; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2349 | double r, rS; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2350 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2351 | assert( roundUp==0 || roundUp==1 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2352 | assert( pIdx->nSample>0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2353 | if( pVal==0 ) return SQLITE_ERROR; |
| 2354 | n = pIdx->aiRowEst[0]; |
| 2355 | aSample = pIdx->aSample; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2356 | eType = sqlite3_value_type(pVal); |
| 2357 | |
| 2358 | if( eType==SQLITE_INTEGER ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2359 | v = sqlite3_value_int64(pVal); |
| 2360 | r = (i64)v; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2361 | for(i=0; i<pIdx->nSample; i++){ |
| 2362 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2363 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2364 | if( aSample[i].eType==SQLITE_INTEGER ){ |
| 2365 | if( aSample[i].u.i>=v ){ |
| 2366 | isEq = aSample[i].u.i==v; |
| 2367 | break; |
| 2368 | } |
| 2369 | }else{ |
| 2370 | assert( aSample[i].eType==SQLITE_FLOAT ); |
| 2371 | if( aSample[i].u.r>=r ){ |
| 2372 | isEq = aSample[i].u.r==r; |
| 2373 | break; |
| 2374 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2375 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2376 | } |
| 2377 | }else if( eType==SQLITE_FLOAT ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2378 | r = sqlite3_value_double(pVal); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2379 | for(i=0; i<pIdx->nSample; i++){ |
| 2380 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2381 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2382 | if( aSample[i].eType==SQLITE_FLOAT ){ |
| 2383 | rS = aSample[i].u.r; |
| 2384 | }else{ |
| 2385 | rS = aSample[i].u.i; |
| 2386 | } |
| 2387 | if( rS>=r ){ |
| 2388 | isEq = rS==r; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2389 | break; |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2390 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2391 | } |
| 2392 | }else if( eType==SQLITE_NULL ){ |
| 2393 | i = 0; |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2394 | if( aSample[0].eType==SQLITE_NULL ) isEq = 1; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2395 | }else{ |
| 2396 | assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 2397 | for(i=0; i<pIdx->nSample; i++){ |
| 2398 | if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){ |
| 2399 | break; |
| 2400 | } |
| 2401 | } |
| 2402 | if( i<pIdx->nSample ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2403 | sqlite3 *db = pParse->db; |
| 2404 | CollSeq *pColl; |
| 2405 | const u8 *z; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2406 | if( eType==SQLITE_BLOB ){ |
| 2407 | z = (const u8 *)sqlite3_value_blob(pVal); |
| 2408 | pColl = db->pDfltColl; |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2409 | assert( pColl->enc==SQLITE_UTF8 ); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2410 | }else{ |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 2411 | pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl); |
drh | 472eae8 | 2013-06-20 17:32:28 +0000 | [diff] [blame] | 2412 | /* If the collating sequence was unavailable, we should have failed |
| 2413 | ** long ago and never reached this point. But we'll check just to |
| 2414 | ** be doubly sure. */ |
| 2415 | if( NEVER(pColl==0) ) return SQLITE_ERROR; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2416 | z = (const u8 *)sqlite3ValueText(pVal, pColl->enc); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2417 | if( !z ){ |
| 2418 | return SQLITE_NOMEM; |
| 2419 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2420 | assert( z && pColl && pColl->xCmp ); |
| 2421 | } |
| 2422 | n = sqlite3ValueBytes(pVal, pColl->enc); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2423 | |
| 2424 | for(; i<pIdx->nSample; i++){ |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2425 | int c; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2426 | int eSampletype = aSample[i].eType; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2427 | if( eSampletype<eType ) continue; |
| 2428 | if( eSampletype!=eType ) break; |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2429 | #ifndef SQLITE_OMIT_UTF16 |
| 2430 | if( pColl->enc!=SQLITE_UTF8 ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2431 | int nSample; |
| 2432 | char *zSample = sqlite3Utf8to16( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2433 | db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample |
| 2434 | ); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2435 | if( !zSample ){ |
| 2436 | assert( db->mallocFailed ); |
| 2437 | return SQLITE_NOMEM; |
| 2438 | } |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2439 | c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2440 | sqlite3DbFree(db, zSample); |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2441 | }else |
| 2442 | #endif |
| 2443 | { |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2444 | 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] | 2445 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2446 | if( c>=0 ){ |
| 2447 | if( c==0 ) isEq = 1; |
| 2448 | break; |
| 2449 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2450 | } |
| 2451 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2452 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2453 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2454 | /* At this point, aSample[i] is the first sample that is greater than |
| 2455 | ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 2456 | ** than pVal. If aSample[i]==pVal, then isEq==1. |
| 2457 | */ |
| 2458 | if( isEq ){ |
| 2459 | assert( i<pIdx->nSample ); |
| 2460 | aStat[0] = aSample[i].nLt; |
| 2461 | aStat[1] = aSample[i].nEq; |
| 2462 | }else{ |
| 2463 | tRowcnt iLower, iUpper, iGap; |
| 2464 | if( i==0 ){ |
| 2465 | iLower = 0; |
| 2466 | iUpper = aSample[0].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2467 | }else{ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2468 | iUpper = i>=pIdx->nSample ? n : aSample[i].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2469 | iLower = aSample[i-1].nEq + aSample[i-1].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2470 | } |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2471 | aStat[1] = pIdx->avgEq; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2472 | if( iLower>=iUpper ){ |
| 2473 | iGap = 0; |
| 2474 | }else{ |
| 2475 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2476 | } |
| 2477 | if( roundUp ){ |
| 2478 | iGap = (iGap*2)/3; |
| 2479 | }else{ |
| 2480 | iGap = iGap/3; |
| 2481 | } |
| 2482 | aStat[0] = iLower + iGap; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2483 | } |
| 2484 | return SQLITE_OK; |
| 2485 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2486 | #endif /* SQLITE_ENABLE_STAT3 */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2487 | |
| 2488 | /* |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2489 | ** If expression pExpr represents a literal value, set *pp to point to |
| 2490 | ** an sqlite3_value structure containing the same value, with affinity |
| 2491 | ** aff applied to it, before returning. It is the responsibility of the |
| 2492 | ** caller to eventually release this structure by passing it to |
| 2493 | ** sqlite3ValueFree(). |
| 2494 | ** |
| 2495 | ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr |
| 2496 | ** is an SQL variable that currently has a non-NULL value bound to it, |
| 2497 | ** create an sqlite3_value structure containing this value, again with |
| 2498 | ** affinity aff applied to it, instead. |
| 2499 | ** |
| 2500 | ** If neither of the above apply, set *pp to NULL. |
| 2501 | ** |
| 2502 | ** If an error occurs, return an error code. Otherwise, SQLITE_OK. |
| 2503 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2504 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2505 | static int valueFromExpr( |
| 2506 | Parse *pParse, |
| 2507 | Expr *pExpr, |
| 2508 | u8 aff, |
| 2509 | sqlite3_value **pp |
| 2510 | ){ |
drh | 4278d53 | 2010-12-16 19:52:52 +0000 | [diff] [blame] | 2511 | if( pExpr->op==TK_VARIABLE |
| 2512 | || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) |
| 2513 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2514 | int iVar = pExpr->iColumn; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 2515 | sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2516 | *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); |
| 2517 | return SQLITE_OK; |
| 2518 | } |
| 2519 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 2520 | } |
dan | f7b0b0a | 2009-10-19 15:52:32 +0000 | [diff] [blame] | 2521 | #endif |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2522 | |
| 2523 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2524 | ** This function is used to estimate the number of rows that will be visited |
| 2525 | ** by scanning an index for a range of values. The range may have an upper |
| 2526 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2527 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2528 | ** example, assuming that index p is on t1(a): |
| 2529 | ** |
| 2530 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2531 | ** |_____| |_____| |
| 2532 | ** | | |
| 2533 | ** pLower pUpper |
| 2534 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2535 | ** 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] | 2536 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2537 | ** |
| 2538 | ** The nEq parameter is passed the index of the index column subject to the |
| 2539 | ** range constraint. Or, equivalently, the number of equality constraints |
| 2540 | ** optimized by the proposed index scan. For example, assuming index p is |
| 2541 | ** on t1(a, b), and the SQL query is: |
| 2542 | ** |
| 2543 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2544 | ** |
| 2545 | ** then nEq should be passed the value 1 (as the range restricted column, |
| 2546 | ** b, is the second left-most column of the index). Or, if the query is: |
| 2547 | ** |
| 2548 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2549 | ** |
| 2550 | ** then nEq should be passed 0. |
| 2551 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2552 | ** The returned value is an integer divisor to reduce the estimated |
| 2553 | ** search space. A return value of 1 means that range constraints are |
| 2554 | ** no help at all. A return value of 2 means range constraints are |
| 2555 | ** expected to reduce the search space by half. And so forth... |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2556 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2557 | ** In the absence of sqlite_stat3 ANALYZE data, each range inequality |
| 2558 | ** reduces the search space by a factor of 4. Hence a single constraint (x>?) |
| 2559 | ** results in a return of 4 and a range constraint (x>? AND x<?) results |
| 2560 | ** in a return of 16. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2561 | */ |
| 2562 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2563 | Parse *pParse, /* Parsing & code generating context */ |
| 2564 | Index *p, /* The index containing the range-compared column; "x" */ |
| 2565 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 2566 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2567 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2568 | WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2569 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2570 | int rc = SQLITE_OK; |
| 2571 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2572 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2573 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2574 | if( nEq==0 && p->nSample ){ |
| 2575 | sqlite3_value *pRangeVal; |
| 2576 | tRowcnt iLower = 0; |
| 2577 | tRowcnt iUpper = p->aiRowEst[0]; |
| 2578 | tRowcnt a[2]; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2579 | u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2580 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2581 | if( pLower ){ |
| 2582 | Expr *pExpr = pLower->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2583 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2584 | assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2585 | if( rc==SQLITE_OK |
| 2586 | && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK |
| 2587 | ){ |
| 2588 | iLower = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2589 | if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2590 | } |
| 2591 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2592 | } |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2593 | if( rc==SQLITE_OK && pUpper ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2594 | Expr *pExpr = pUpper->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2595 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2596 | assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2597 | if( rc==SQLITE_OK |
| 2598 | && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK |
| 2599 | ){ |
| 2600 | iUpper = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2601 | if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1]; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2602 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2603 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2604 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2605 | if( rc==SQLITE_OK ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2606 | WhereCost iBase = whereCost(p->aiRowEst[0]); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2607 | if( iUpper>iLower ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2608 | iBase -= whereCost(iUpper - iLower); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2609 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2610 | *pRangeDiv = iBase; |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2611 | WHERETRACE(0x100, ("range scan regions: %u..%u div=%d\n", |
| 2612 | (u32)iLower, (u32)iUpper, *pRangeDiv)); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2613 | return SQLITE_OK; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2614 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2615 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2616 | #else |
| 2617 | UNUSED_PARAMETER(pParse); |
| 2618 | UNUSED_PARAMETER(p); |
| 2619 | UNUSED_PARAMETER(nEq); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2620 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2621 | assert( pLower || pUpper ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2622 | *pRangeDiv = 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2623 | /* TUNING: Each inequality constraint reduces the search space 4-fold. |
| 2624 | ** A BETWEEN operator, therefore, reduces the search space 16-fold */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2625 | if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2626 | *pRangeDiv += 20; assert( 20==whereCost(4) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2627 | } |
| 2628 | if( pUpper ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 2629 | *pRangeDiv += 20; assert( 20==whereCost(4) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2630 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2631 | return rc; |
| 2632 | } |
| 2633 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2634 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2635 | /* |
| 2636 | ** Estimate the number of rows that will be returned based on |
| 2637 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2638 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2639 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2640 | ** for that index. When pExpr==NULL that means the constraint is |
| 2641 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2642 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2643 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2644 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2645 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2646 | ** |
| 2647 | ** This routine can fail if it is unable to load a collating sequence |
| 2648 | ** required for string comparison, or if unable to allocate memory |
| 2649 | ** for a UTF conversion required for comparison. The error is stored |
| 2650 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2651 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2652 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2653 | Parse *pParse, /* Parsing & code generating context */ |
| 2654 | Index *p, /* The index whose left-most column is pTerm */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2655 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2656 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2657 | ){ |
| 2658 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2659 | u8 aff; /* Column affinity */ |
| 2660 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2661 | tRowcnt a[2]; /* Statistics */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2662 | |
| 2663 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2664 | assert( p->nSample>0 ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2665 | aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2666 | if( pExpr ){ |
| 2667 | rc = valueFromExpr(pParse, pExpr, aff, &pRhs); |
| 2668 | if( rc ) goto whereEqualScanEst_cancel; |
| 2669 | }else{ |
| 2670 | pRhs = sqlite3ValueNew(pParse->db); |
| 2671 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2672 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2673 | rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 2674 | if( rc==SQLITE_OK ){ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2675 | WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1])); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2676 | *pnRow = a[1]; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2677 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2678 | whereEqualScanEst_cancel: |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2679 | sqlite3ValueFree(pRhs); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2680 | return rc; |
| 2681 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2682 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2683 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2684 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2685 | /* |
| 2686 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2687 | ** an IN constraint where the right-hand side of the IN operator |
| 2688 | ** is a list of values. Example: |
| 2689 | ** |
| 2690 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2691 | ** |
| 2692 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2693 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2694 | ** non-zero. |
| 2695 | ** |
| 2696 | ** This routine can fail if it is unable to load a collating sequence |
| 2697 | ** required for string comparison, or if unable to allocate memory |
| 2698 | ** for a UTF conversion required for comparison. The error is stored |
| 2699 | ** in the pParse structure. |
| 2700 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2701 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2702 | Parse *pParse, /* Parsing & code generating context */ |
| 2703 | Index *p, /* The index whose left-most column is pTerm */ |
| 2704 | 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] | 2705 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2706 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2707 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2708 | tRowcnt nEst; /* Number of rows for a single term */ |
| 2709 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 2710 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2711 | |
| 2712 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2713 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 2714 | nEst = p->aiRowEst[0]; |
| 2715 | rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| 2716 | nRowEst += nEst; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2717 | } |
| 2718 | if( rc==SQLITE_OK ){ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2719 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 2720 | *pnRow = nRowEst; |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2721 | WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst)); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2722 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2723 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2724 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2725 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2726 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2727 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2728 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2729 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2730 | ** or USING clause of that join. |
| 2731 | ** |
| 2732 | ** Consider the term t2.z='ok' in the following queries: |
| 2733 | ** |
| 2734 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2735 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2736 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2737 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2738 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2739 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2740 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2741 | ** |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 2742 | ** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are |
| 2743 | ** completely satisfied by indices. |
| 2744 | ** |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2745 | ** 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] | 2746 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2747 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2748 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2749 | ** but joins might run a little slower. The trick is to disable as much |
| 2750 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2751 | ** the wrong answer. See ticket #813. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2752 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2753 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 2754 | if( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2755 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2756 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 2757 | ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 2758 | pTerm->wtFlags |= TERM_CODED; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 2759 | if( pTerm->iParent>=0 ){ |
| 2760 | WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 2761 | if( (--pOther->nChild)==0 ){ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 2762 | disableTerm(pLevel, pOther); |
| 2763 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2764 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2769 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2770 | ** to the n registers starting at base. |
| 2771 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2772 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2773 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2774 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2775 | ** |
| 2776 | ** This routine makes its own copy of zAff so that the caller is free |
| 2777 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2778 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2779 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2780 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2781 | if( zAff==0 ){ |
| 2782 | assert( pParse->db->mallocFailed ); |
| 2783 | return; |
| 2784 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2785 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2786 | |
| 2787 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2788 | ** and end of the affinity string. |
| 2789 | */ |
| 2790 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2791 | n--; |
| 2792 | base++; |
| 2793 | zAff++; |
| 2794 | } |
| 2795 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2796 | n--; |
| 2797 | } |
| 2798 | |
| 2799 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2800 | if( n>0 ){ |
| 2801 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2802 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2803 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2804 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2807 | |
| 2808 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2809 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2810 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2811 | ** coded. |
| 2812 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2813 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2814 | ** |
| 2815 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2816 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2817 | ** 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] | 2818 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2819 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2820 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2821 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2822 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2823 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2824 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2825 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2826 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2827 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2828 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2829 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2830 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2831 | assert( iTarget>0 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2832 | if( pX->op==TK_EQ ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2833 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2834 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2835 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2836 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2837 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2838 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2839 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2840 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2841 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2842 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2843 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2844 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2845 | && pLoop->u.btree.pIndex!=0 |
| 2846 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2847 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2848 | testcase( iEq==0 ); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2849 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2850 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2851 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2852 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2853 | iReg = iTarget; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2854 | eType = sqlite3FindInIndex(pParse, pX, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2855 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2856 | testcase( bRev ); |
| 2857 | bRev = !bRev; |
| 2858 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2859 | iTab = pX->iTable; |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2860 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2861 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2862 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2863 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2864 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2865 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2866 | pLevel->u.in.nIn++; |
| 2867 | pLevel->u.in.aInLoop = |
| 2868 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2869 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2870 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2871 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2872 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2873 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2874 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2875 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2876 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2877 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2878 | } |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2879 | pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2880 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2881 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2882 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2883 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2884 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2885 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2886 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2887 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2890 | /* |
| 2891 | ** Generate code that will evaluate all == and IN constraints for an |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2892 | ** index. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2893 | ** |
| 2894 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2895 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2896 | ** The index has as many as three equality constraints, but in this |
| 2897 | ** example, the third "c" value is an inequality. So only two |
| 2898 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2899 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2900 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2901 | ** |
| 2902 | ** In the example above nEq==2. But this subroutine works for any value |
| 2903 | ** 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] | 2904 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2905 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2906 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2907 | ** This routine always allocates at least one memory cell and returns |
| 2908 | ** the index of that memory cell. The code that |
| 2909 | ** calls this routine will use that memory cell to store the termination |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2910 | ** key value of the loop. If one or more IN operators appear, then |
| 2911 | ** this routine allocates an additional nEq memory cells for internal |
| 2912 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2913 | ** |
| 2914 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2915 | ** copy of the column affinity string of the index allocated using |
| 2916 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2917 | ** with equality constraints that use NONE affinity are set to |
| 2918 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2919 | ** |
| 2920 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2921 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2922 | ** |
| 2923 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2924 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2925 | ** no conversion should be attempted before using a t2.b value as part of |
| 2926 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2927 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2928 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2929 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2930 | Parse *pParse, /* Parsing context */ |
| 2931 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2932 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2933 | int nExtraReg, /* Number of extra registers to allocate */ |
| 2934 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2935 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2936 | int nEq; /* The number of == or IN constraints to code */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2937 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 2938 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2939 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2940 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2941 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2942 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2943 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2944 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2945 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2946 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2947 | pLoop = pLevel->pWLoop; |
| 2948 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 2949 | nEq = pLoop->u.btree.nEq; |
| 2950 | pIdx = pLoop->u.btree.pIndex; |
| 2951 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2952 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2953 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2954 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2955 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2956 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2957 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2958 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2959 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 2960 | if( !zAff ){ |
| 2961 | pParse->db->mallocFailed = 1; |
| 2962 | } |
| 2963 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2964 | /* Evaluate the equality constraints |
| 2965 | */ |
drh | c49de5d | 2007-01-19 01:06:01 +0000 | [diff] [blame] | 2966 | assert( pIdx->nColumn>=nEq ); |
| 2967 | for(j=0; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2968 | int r1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2969 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2970 | assert( pTerm!=0 ); |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2971 | /* The following true for indices with redundant columns. |
| 2972 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 2973 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 2974 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2975 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2976 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2977 | if( nReg==1 ){ |
| 2978 | sqlite3ReleaseTempReg(pParse, regBase); |
| 2979 | regBase = r1; |
| 2980 | }else{ |
| 2981 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 2982 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2983 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 2984 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 2985 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2986 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2987 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 2f2855b | 2009-11-18 01:25:26 +0000 | [diff] [blame] | 2988 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2989 | if( zAff ){ |
| 2990 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 2991 | zAff[j] = SQLITE_AFF_NONE; |
| 2992 | } |
| 2993 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 2994 | zAff[j] = SQLITE_AFF_NONE; |
| 2995 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2996 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2997 | } |
| 2998 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2999 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3000 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3003 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3004 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3005 | ** This routine is a helper for explainIndexRange() below |
| 3006 | ** |
| 3007 | ** pStr holds the text of an expression that we are building up one term |
| 3008 | ** at a time. This routine adds a new term to the end of the expression. |
| 3009 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 3010 | ** terms only. |
| 3011 | */ |
| 3012 | static void explainAppendTerm( |
| 3013 | StrAccum *pStr, /* The text expression being built */ |
| 3014 | int iTerm, /* Index of this term. First is zero */ |
| 3015 | const char *zColumn, /* Name of the column */ |
| 3016 | const char *zOp /* Name of the operator */ |
| 3017 | ){ |
| 3018 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 3019 | sqlite3StrAccumAppend(pStr, zColumn, -1); |
| 3020 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 3021 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 3022 | } |
| 3023 | |
| 3024 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3025 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 3026 | ** function returns a pointer to a string buffer containing a description |
| 3027 | ** of the subset of table rows scanned by the strategy in the form of an |
| 3028 | ** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 3029 | ** |
| 3030 | ** For example, if the query: |
| 3031 | ** |
| 3032 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 3033 | ** |
| 3034 | ** is run and there is an index on (a, b), then this function returns a |
| 3035 | ** string similar to: |
| 3036 | ** |
| 3037 | ** "a=? AND b>?" |
| 3038 | ** |
| 3039 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 3040 | ** It is the responsibility of the caller to free the buffer when it is |
| 3041 | ** no longer required. |
| 3042 | */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3043 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 3044 | Index *pIndex = pLoop->u.btree.pIndex; |
| 3045 | int nEq = pLoop->u.btree.nEq; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3046 | int i, j; |
| 3047 | Column *aCol = pTab->aCol; |
| 3048 | int *aiColumn = pIndex->aiColumn; |
| 3049 | StrAccum txt; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3050 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3051 | if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3052 | return 0; |
| 3053 | } |
| 3054 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
drh | 03b6df1 | 2010-11-15 16:29:30 +0000 | [diff] [blame] | 3055 | txt.db = db; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3056 | sqlite3StrAccumAppend(&txt, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3057 | for(i=0; i<nEq; i++){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3058 | explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "="); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3061 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3062 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3063 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 3064 | explainAppendTerm(&txt, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3065 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3066 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3067 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 3068 | explainAppendTerm(&txt, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3069 | } |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3070 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 3071 | return sqlite3StrAccumFinish(&txt); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3074 | /* |
| 3075 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 3076 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 3077 | ** record is added to the output to describe the table scan strategy in |
| 3078 | ** pLevel. |
| 3079 | */ |
| 3080 | static void explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3081 | Parse *pParse, /* Parse context */ |
| 3082 | SrcList *pTabList, /* Table list this loop refers to */ |
| 3083 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 3084 | int iLevel, /* Value for "level" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3085 | int iFrom, /* Value for "from" column of output */ |
| 3086 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3087 | ){ |
| 3088 | if( pParse->explain==2 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3089 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3090 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 3091 | sqlite3 *db = pParse->db; /* Database handle */ |
| 3092 | char *zMsg; /* Text to add to EQP output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3093 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3094 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3095 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 3096 | u32 flags; /* Flags that describe this loop */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3097 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3098 | pLoop = pLevel->pWLoop; |
| 3099 | flags = pLoop->wsFlags; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3100 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3101 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3102 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 3103 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 3104 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3105 | |
| 3106 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3107 | if( pItem->pSelect ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3108 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3109 | }else{ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3110 | zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3113 | if( pItem->zAlias ){ |
| 3114 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 3115 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3116 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3117 | && ALWAYS(pLoop->u.btree.pIndex!=0) |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3118 | ){ |
| 3119 | char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3120 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3121 | ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""), |
| 3122 | ((flags & WHERE_IDX_ONLY)?"COVERING ":""), |
| 3123 | ((flags & WHERE_TEMP_INDEX)?"":" "), |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3124 | ((flags & WHERE_TEMP_INDEX)?"": pLoop->u.btree.pIndex->zName), |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3125 | zWhere |
| 3126 | ); |
| 3127 | sqlite3DbFree(db, zWhere); |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 3128 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3129 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3130 | |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 3131 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3132 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 3133 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3134 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 3135 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 3136 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3137 | }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3138 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 3139 | } |
| 3140 | } |
| 3141 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3142 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3143 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3144 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3145 | } |
| 3146 | #endif |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3147 | zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3148 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3149 | } |
| 3150 | } |
| 3151 | #else |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3152 | # define explainOneScan(u,v,w,x,y,z) |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3153 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 3154 | |
| 3155 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3156 | /* |
| 3157 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 3158 | ** implementation described by pWInfo. |
| 3159 | */ |
| 3160 | static Bitmask codeOneLoopStart( |
| 3161 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 3162 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3163 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3164 | ){ |
| 3165 | int j, k; /* Loop counters */ |
| 3166 | int iCur; /* The VDBE cursor for the table */ |
| 3167 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 3168 | int omitTable; /* True if we use the index only */ |
| 3169 | int bRev; /* True if we need to scan in reverse order */ |
| 3170 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3171 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3172 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 3173 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 3174 | Parse *pParse; /* Parsing context */ |
| 3175 | Vdbe *v; /* The prepared stmt under constructions */ |
| 3176 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3177 | int addrBrk; /* Jump here to break out of the loop */ |
| 3178 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3179 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 3180 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3181 | Bitmask newNotReady; /* Return value */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3182 | |
| 3183 | pParse = pWInfo->pParse; |
| 3184 | v = pParse->pVdbe; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3185 | pWC = &pWInfo->sWC; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3186 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3187 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3188 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 3189 | iCur = pTabItem->iCursor; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3190 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 3191 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3192 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3193 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3194 | |
| 3195 | /* Create labels for the "break" and "continue" instructions |
| 3196 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 3197 | ** Jump to cont to go immediately to the next iteration of the |
| 3198 | ** loop. |
| 3199 | ** |
| 3200 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 3201 | ** means to continue with the next IN value combination. When |
| 3202 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 3203 | ** is the same as "addrBrk". |
| 3204 | */ |
| 3205 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 3206 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 3207 | |
| 3208 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 3209 | ** initialize a memory cell that records if this table matches any |
| 3210 | ** row of the left table of the join. |
| 3211 | */ |
| 3212 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 3213 | pLevel->iLeftJoin = ++pParse->nMem; |
| 3214 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 3215 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 3216 | } |
| 3217 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 3218 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 3219 | if( pTabItem->viaCoroutine ){ |
| 3220 | int regYield = pTabItem->regReturn; |
| 3221 | sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield); |
| 3222 | pLevel->p2 = sqlite3VdbeAddOp1(v, OP_Yield, regYield); |
| 3223 | VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName)); |
| 3224 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 3225 | pLevel->op = OP_Goto; |
| 3226 | }else |
| 3227 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3228 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3229 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 3230 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3231 | ** to access the data. |
| 3232 | */ |
| 3233 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3234 | int addrNotFound; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3235 | int nConstraint = pLoop->nLTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3236 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3237 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3238 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3239 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3240 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 3241 | int iTarget = iReg+j+2; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3242 | pTerm = pLoop->aLTerm[j]; |
drh | 95ed68d | 2013-06-12 17:55:50 +0000 | [diff] [blame] | 3243 | if( pTerm==0 ) continue; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3244 | if( pTerm->eOperator & WO_IN ){ |
| 3245 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 3246 | addrNotFound = pLevel->addrNxt; |
| 3247 | }else{ |
| 3248 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 3249 | } |
| 3250 | } |
| 3251 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 3252 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3253 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 3254 | pLoop->u.vtab.idxStr, |
| 3255 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 3256 | pLoop->u.vtab.needFree = 0; |
| 3257 | for(j=0; j<nConstraint && j<16; j++){ |
| 3258 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3259 | disableTerm(pLevel, pLoop->aLTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3260 | } |
| 3261 | } |
| 3262 | pLevel->op = OP_VNext; |
| 3263 | pLevel->p1 = iCur; |
| 3264 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3265 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3266 | sqlite3ExprCachePop(pParse, 1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3267 | }else |
| 3268 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3269 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3270 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3271 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 3272 | ){ |
| 3273 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3274 | ** equality comparison against the ROWID field. Or |
| 3275 | ** we reference multiple rows using a "rowid IN (...)" |
| 3276 | ** construct. |
| 3277 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3278 | assert( pLoop->u.btree.nEq==1 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3279 | iReleaseReg = sqlite3GetTempReg(pParse); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3280 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3281 | assert( pTerm!=0 ); |
| 3282 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3283 | assert( omitTable==0 ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3284 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3285 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3286 | addrNxt = pLevel->addrNxt; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3287 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 3288 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 3289 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3290 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3291 | VdbeComment((v, "pk")); |
| 3292 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3293 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3294 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 3295 | ){ |
| 3296 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3297 | */ |
| 3298 | int testOp = OP_Noop; |
| 3299 | int start; |
| 3300 | int memEndValue = 0; |
| 3301 | WhereTerm *pStart, *pEnd; |
| 3302 | |
| 3303 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3304 | j = 0; |
| 3305 | pStart = pEnd = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3306 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 3307 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3308 | assert( pStart!=0 || pEnd!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3309 | if( bRev ){ |
| 3310 | pTerm = pStart; |
| 3311 | pStart = pEnd; |
| 3312 | pEnd = pTerm; |
| 3313 | } |
| 3314 | if( pStart ){ |
| 3315 | Expr *pX; /* The expression that defines the start bound */ |
| 3316 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 3317 | |
| 3318 | /* The following constant maps TK_xx codes into corresponding |
| 3319 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 3320 | */ |
| 3321 | const u8 aMoveOp[] = { |
| 3322 | /* TK_GT */ OP_SeekGt, |
| 3323 | /* TK_LE */ OP_SeekLe, |
| 3324 | /* TK_LT */ OP_SeekLt, |
| 3325 | /* TK_GE */ OP_SeekGe |
| 3326 | }; |
| 3327 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 3328 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 3329 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 3330 | |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3331 | testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3332 | pX = pStart->pExpr; |
| 3333 | assert( pX!=0 ); |
| 3334 | assert( pStart->leftCursor==iCur ); |
| 3335 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 3336 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| 3337 | VdbeComment((v, "pk")); |
| 3338 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 3339 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 3340 | disableTerm(pLevel, pStart); |
| 3341 | }else{ |
| 3342 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 3343 | } |
| 3344 | if( pEnd ){ |
| 3345 | Expr *pX; |
| 3346 | pX = pEnd->pExpr; |
| 3347 | assert( pX!=0 ); |
| 3348 | assert( pEnd->leftCursor==iCur ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3349 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3350 | memEndValue = ++pParse->nMem; |
| 3351 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 3352 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 3353 | testOp = bRev ? OP_Le : OP_Ge; |
| 3354 | }else{ |
| 3355 | testOp = bRev ? OP_Lt : OP_Gt; |
| 3356 | } |
| 3357 | disableTerm(pLevel, pEnd); |
| 3358 | } |
| 3359 | start = sqlite3VdbeCurrentAddr(v); |
| 3360 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 3361 | pLevel->p1 = iCur; |
| 3362 | pLevel->p2 = start; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3363 | assert( pLevel->p5==0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3364 | if( testOp!=OP_Noop ){ |
| 3365 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3366 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3367 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3368 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 3369 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3370 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3371 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3372 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3373 | ** |
| 3374 | ** The WHERE clause may contain zero or more equality |
| 3375 | ** terms ("==" or "IN" operators) that refer to the N |
| 3376 | ** left-most columns of the index. It may also contain |
| 3377 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3378 | ** column that immediately follows the N equalities. Only |
| 3379 | ** the right-most column can be an inequality - the rest must |
| 3380 | ** use the "==" and "IN" operators. For example, if the |
| 3381 | ** index is on (x,y,z), then the following clauses are all |
| 3382 | ** optimized: |
| 3383 | ** |
| 3384 | ** x=5 |
| 3385 | ** x=5 AND y=10 |
| 3386 | ** x=5 AND y<10 |
| 3387 | ** x=5 AND y>5 AND y<10 |
| 3388 | ** x=5 AND y=5 AND z<=10 |
| 3389 | ** |
| 3390 | ** The z<10 term of the following cannot be used, only |
| 3391 | ** the x=5 term: |
| 3392 | ** |
| 3393 | ** x=5 AND z<10 |
| 3394 | ** |
| 3395 | ** N may be zero if there are inequality constraints. |
| 3396 | ** If there are no inequality constraints, then N is at |
| 3397 | ** least one. |
| 3398 | ** |
| 3399 | ** This case is also used when there are no WHERE clause |
| 3400 | ** constraints but an index is selected anyway, in order |
| 3401 | ** to force the output order to conform to an ORDER BY. |
| 3402 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3403 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3404 | 0, |
| 3405 | 0, |
| 3406 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3407 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 3408 | OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3409 | OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ |
| 3410 | OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ |
| 3411 | OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ |
| 3412 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3413 | static const u8 aEndOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3414 | OP_Noop, /* 0: (!end_constraints) */ |
| 3415 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 3416 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 3417 | }; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3418 | int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 3419 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3420 | int regBase; /* Base register holding constraint values */ |
| 3421 | int r1; /* Temp register */ |
| 3422 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3423 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3424 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3425 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3426 | int start_constraints; /* Start of range is constrained */ |
| 3427 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3428 | Index *pIdx; /* The index we will be using */ |
| 3429 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3430 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3431 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3432 | char *zStartAff; /* Affinity for start of range constraint */ |
| 3433 | char *zEndAff; /* Affinity for end of range constraint */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3434 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3435 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3436 | iIdxCur = pLevel->iIdxCur; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3437 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3438 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3439 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3440 | ** query, then the caller will only allow the loop to run for |
| 3441 | ** a single iteration. This means that the first row returned |
| 3442 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3443 | ** the first one after the nEq equality constraints in the index, |
| 3444 | ** this requires some special handling. |
| 3445 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3446 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 3447 | && (pWInfo->bOBSat!=0) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3448 | && (pIdx->nColumn>nEq) |
| 3449 | ){ |
| 3450 | /* assert( pOrderBy->nExpr==1 ); */ |
| 3451 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 3452 | isMinQuery = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3453 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | /* Find any inequality constraint terms for the start and end |
| 3457 | ** of the range. |
| 3458 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3459 | j = nEq; |
| 3460 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3461 | pRangeStart = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3462 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3463 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3464 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3465 | pRangeEnd = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3466 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3469 | /* Generate code to evaluate all constraint terms using == or IN |
| 3470 | ** and store the values of those terms in an array of registers |
| 3471 | ** starting at regBase. |
| 3472 | */ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 3473 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3474 | zEndAff = sqlite3DbStrDup(pParse->db, zStartAff); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3475 | addrNxt = pLevel->addrNxt; |
| 3476 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3477 | /* If we are doing a reverse order scan on an ascending index, or |
| 3478 | ** a forward order scan on a descending index, interchange the |
| 3479 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3480 | */ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3481 | if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3482 | || (bRev && pIdx->nColumn==nEq) |
| 3483 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3484 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 3485 | } |
| 3486 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3487 | testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 3488 | testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 3489 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 3490 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3491 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3492 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3493 | start_constraints = pRangeStart || nEq>0; |
| 3494 | |
| 3495 | /* Seek the index cursor to the start of the range. */ |
| 3496 | nConstraint = nEq; |
| 3497 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3498 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3499 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3500 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){ |
| 3501 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3502 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3503 | if( zStartAff ){ |
| 3504 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3505 | /* Since the comparison is to be performed with no conversions |
| 3506 | ** applied to the operands, set the affinity to apply to pRight to |
| 3507 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3508 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3509 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3510 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3511 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3512 | } |
| 3513 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3514 | nConstraint++; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3515 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3516 | }else if( isMinQuery ){ |
| 3517 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3518 | nConstraint++; |
| 3519 | startEq = 0; |
| 3520 | start_constraints = 1; |
| 3521 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3522 | codeApplyAffinity(pParse, regBase, nConstraint, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3523 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3524 | assert( op!=0 ); |
| 3525 | testcase( op==OP_Rewind ); |
| 3526 | testcase( op==OP_Last ); |
| 3527 | testcase( op==OP_SeekGt ); |
| 3528 | testcase( op==OP_SeekGe ); |
| 3529 | testcase( op==OP_SeekLe ); |
| 3530 | testcase( op==OP_SeekLt ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3531 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3532 | |
| 3533 | /* Load the value for the inequality constraint at the end of the |
| 3534 | ** range (if any). |
| 3535 | */ |
| 3536 | nConstraint = nEq; |
| 3537 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3538 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3539 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3540 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3541 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ |
| 3542 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3543 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3544 | if( zEndAff ){ |
| 3545 | if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3546 | /* Since the comparison is to be performed with no conversions |
| 3547 | ** applied to the operands, set the affinity to apply to pRight to |
| 3548 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3549 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3550 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3551 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ |
| 3552 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3553 | } |
| 3554 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3555 | codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3556 | nConstraint++; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3557 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3558 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3559 | sqlite3DbFree(pParse->db, zStartAff); |
| 3560 | sqlite3DbFree(pParse->db, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3561 | |
| 3562 | /* Top of the loop body */ |
| 3563 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3564 | |
| 3565 | /* Check if the index cursor is past the end of the range. */ |
| 3566 | op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)]; |
| 3567 | testcase( op==OP_Noop ); |
| 3568 | testcase( op==OP_IdxGE ); |
| 3569 | testcase( op==OP_IdxLT ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3570 | if( op!=OP_Noop ){ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3571 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3572 | sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0); |
| 3573 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3574 | |
| 3575 | /* If there are inequality constraints, check that the value |
| 3576 | ** of the table column that the inequality contrains is not NULL. |
| 3577 | ** If it is, jump to the next iteration of the loop. |
| 3578 | */ |
| 3579 | r1 = sqlite3GetTempReg(pParse); |
drh | 37ca048 | 2013-06-12 17:17:45 +0000 | [diff] [blame] | 3580 | testcase( pLoop->wsFlags & WHERE_BTM_LIMIT ); |
| 3581 | testcase( pLoop->wsFlags & WHERE_TOP_LIMIT ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3582 | if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3583 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 3584 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 3585 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3586 | sqlite3ReleaseTempReg(pParse, r1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3587 | |
| 3588 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3589 | disableTerm(pLevel, pRangeStart); |
| 3590 | disableTerm(pLevel, pRangeEnd); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3591 | if( !omitTable ){ |
| 3592 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3593 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3594 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3595 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3596 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3597 | |
| 3598 | /* Record the instruction used to terminate the loop. Disable |
| 3599 | ** WHERE clause terms made redundant by the index range scan. |
| 3600 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3601 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3602 | pLevel->op = OP_Noop; |
| 3603 | }else if( bRev ){ |
| 3604 | pLevel->op = OP_Prev; |
| 3605 | }else{ |
| 3606 | pLevel->op = OP_Next; |
| 3607 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3608 | pLevel->p1 = iIdxCur; |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3609 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3610 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3611 | }else{ |
| 3612 | assert( pLevel->p5==0 ); |
| 3613 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3614 | }else |
| 3615 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3616 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3617 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3618 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3619 | ** |
| 3620 | ** Example: |
| 3621 | ** |
| 3622 | ** CREATE TABLE t1(a,b,c,d); |
| 3623 | ** CREATE INDEX i1 ON t1(a); |
| 3624 | ** CREATE INDEX i2 ON t1(b); |
| 3625 | ** CREATE INDEX i3 ON t1(c); |
| 3626 | ** |
| 3627 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3628 | ** |
| 3629 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3630 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3631 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3632 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3633 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3634 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3635 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3636 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3637 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3638 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3639 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3640 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3641 | ** Gosub 2 A |
| 3642 | ** sqlite3WhereEnd() |
| 3643 | ** |
| 3644 | ** Following the above, code to terminate the loop. Label A, the target |
| 3645 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3646 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3647 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3648 | ** Goto B # The loop is finished. |
| 3649 | ** |
| 3650 | ** A: <loop body> # Return data, whatever. |
| 3651 | ** |
| 3652 | ** Return 2 # Jump back to the Gosub |
| 3653 | ** |
| 3654 | ** B: <after the loop> |
| 3655 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3656 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3657 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3658 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3659 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3660 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3661 | |
| 3662 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3663 | int regRowset = 0; /* Register for RowSet object */ |
| 3664 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3665 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3666 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3667 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3668 | int ii; /* Loop counter */ |
| 3669 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3670 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3671 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3672 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3673 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3674 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3675 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3676 | pLevel->op = OP_Return; |
| 3677 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3678 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3679 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3680 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3681 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3682 | */ |
| 3683 | if( pWInfo->nLevel>1 ){ |
| 3684 | int nNotReady; /* The number of notReady tables */ |
| 3685 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3686 | nNotReady = pWInfo->nLevel - iLevel - 1; |
| 3687 | pOrTab = sqlite3StackAllocRaw(pParse->db, |
| 3688 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3689 | if( pOrTab==0 ) return notReady; |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 3690 | pOrTab->nAlloc = (u8)(nNotReady + 1); |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3691 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3692 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3693 | origSrc = pWInfo->pTabList->a; |
| 3694 | for(k=1; k<=nNotReady; k++){ |
| 3695 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3696 | } |
| 3697 | }else{ |
| 3698 | pOrTab = pWInfo->pTabList; |
| 3699 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3700 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3701 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 3702 | ** equivalent to an empty rowset. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3703 | ** |
| 3704 | ** Also initialize regReturn to contain the address of the instruction |
| 3705 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3706 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3707 | ** over the top of the loop into the body of it. In this case the |
| 3708 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3709 | ** fall through to the next instruction, just as an OP_Next does if |
| 3710 | ** called on an uninitialized cursor. |
| 3711 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3712 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3713 | regRowset = ++pParse->nMem; |
| 3714 | regRowid = ++pParse->nMem; |
| 3715 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3716 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3717 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3718 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3719 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3720 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3721 | ** That way, terms in y that are factored into the disjunction will |
| 3722 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3723 | ** |
| 3724 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3725 | ** the "interesting" terms of z - terms that did not originate in the |
| 3726 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3727 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3728 | ** |
| 3729 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3730 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3731 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3732 | */ |
| 3733 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3734 | int iTerm; |
| 3735 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3736 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3737 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3738 | if( pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_ORINFO) ) continue; |
| 3739 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3740 | pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 3741 | pAndExpr = sqlite3ExprAnd(pParse->db, pAndExpr, pExpr); |
| 3742 | } |
| 3743 | if( pAndExpr ){ |
| 3744 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3745 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3748 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3749 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3750 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3751 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3752 | Expr *pOrExpr = pOrTerm->pExpr; |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3753 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3754 | pAndExpr->pLeft = pOrExpr; |
| 3755 | pOrExpr = pAndExpr; |
| 3756 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3757 | /* Loop through table entries that match term pOrTerm. */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3758 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 3759 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3760 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3761 | assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3762 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3763 | WhereLoop *pSubLoop; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3764 | explainOneScan( |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3765 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3766 | ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3767 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3768 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3769 | int r; |
| 3770 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3771 | regRowid, 0); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3772 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| 3773 | sqlite3VdbeCurrentAddr(v)+2, r, iSet); |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3774 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3775 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 3776 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3777 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 3778 | ** contained one or more AND term from a notReady table. The |
| 3779 | ** terms from the notReady table could not be tested and will |
| 3780 | ** need to be tested later. |
| 3781 | */ |
| 3782 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 3783 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3784 | /* If all of the OR-connected terms are optimized using the same |
| 3785 | ** index, and the index is opened using the same cursor number |
| 3786 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 3787 | ** be possible to use that index as a covering index. |
| 3788 | ** |
| 3789 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 3790 | ** uses an index, and this is either the first OR-connected term |
| 3791 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3792 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 3793 | ** pCov to NULL to indicate that no candidate covering index will |
| 3794 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3795 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3796 | pSubLoop = pSubWInfo->a[0].pWLoop; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3797 | assert( (pSubLoop->wsFlags & WHERE_TEMP_INDEX)==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3798 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3799 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3800 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3801 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
drh | 907717f | 2013-06-04 18:03:22 +0000 | [diff] [blame] | 3802 | pCov = pSubLoop->u.btree.pIndex; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3803 | }else{ |
| 3804 | pCov = 0; |
| 3805 | } |
| 3806 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3807 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 3808 | sqlite3WhereEnd(pSubWInfo); |
| 3809 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 3812 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 3813 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3814 | if( pAndExpr ){ |
| 3815 | pAndExpr->pLeft = 0; |
| 3816 | sqlite3ExprDelete(pParse->db, pAndExpr); |
| 3817 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3818 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3819 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 3820 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 3821 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3822 | if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab); |
| 3823 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3824 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3825 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3826 | |
| 3827 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3828 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3829 | ** scan of the entire table. |
| 3830 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3831 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 3832 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 3833 | assert( bRev==0 || bRev==1 ); |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3834 | pLevel->op = aStep[bRev]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3835 | pLevel->p1 = iCur; |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3836 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3837 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3838 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3839 | newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3840 | |
| 3841 | /* Insert code to test every subexpression that can be completely |
| 3842 | ** computed using the current set of tables. |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3843 | ** |
| 3844 | ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through |
| 3845 | ** the use of indices become tests that are evaluated against each row of |
| 3846 | ** the relevant input tables. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3847 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3848 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3849 | Expr *pE; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3850 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3851 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3852 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3853 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3854 | testcase( pWInfo->untestedTerms==0 |
| 3855 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 3856 | pWInfo->untestedTerms = 1; |
| 3857 | continue; |
| 3858 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3859 | pE = pTerm->pExpr; |
| 3860 | assert( pE!=0 ); |
| 3861 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 3862 | continue; |
| 3863 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3864 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3865 | pTerm->wtFlags |= TERM_CODED; |
| 3866 | } |
| 3867 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3868 | /* Insert code to test for implied constraints based on transitivity |
| 3869 | ** of the "==" operator. |
| 3870 | ** |
| 3871 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 3872 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 3873 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 3874 | ** the implied "t1.a=123" constraint. |
| 3875 | */ |
| 3876 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3877 | Expr *pE; |
| 3878 | WhereTerm *pAlt; |
| 3879 | Expr sEq; |
| 3880 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 3881 | if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue; |
| 3882 | if( pTerm->leftCursor!=iCur ) continue; |
| 3883 | pE = pTerm->pExpr; |
| 3884 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 3885 | assert( (pTerm->prereqRight & newNotReady)!=0 ); |
| 3886 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 3887 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 3888 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3889 | testcase( pAlt->eOperator & WO_EQ ); |
| 3890 | testcase( pAlt->eOperator & WO_IN ); |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3891 | VdbeNoopComment((v, "begin transitive constraint")); |
| 3892 | sEq = *pAlt->pExpr; |
| 3893 | sEq.pLeft = pE->pLeft; |
| 3894 | sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL); |
| 3895 | } |
| 3896 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3897 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 3898 | ** at least one row of the right table has matched the left table. |
| 3899 | */ |
| 3900 | if( pLevel->iLeftJoin ){ |
| 3901 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 3902 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 3903 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3904 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3905 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3906 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3907 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3908 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3909 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 3910 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3911 | continue; |
| 3912 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3913 | assert( pTerm->pExpr ); |
| 3914 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 3915 | pTerm->wtFlags |= TERM_CODED; |
| 3916 | } |
| 3917 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3918 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3919 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3920 | return newNotReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3923 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3924 | /* |
| 3925 | ** Print a WhereLoop object for debugging purposes |
| 3926 | */ |
| 3927 | static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3928 | int nb = 1+(pTabList->nSrc+7)/8; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3929 | struct SrcList_item *pItem = pTabList->a + p->iTab; |
| 3930 | Table *pTab = pItem->pTab; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3931 | sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 3932 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3933 | sqlite3DebugPrintf(" %12s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3934 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3935 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 3936 | if( p->u.btree.pIndex ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3937 | const char *zName = p->u.btree.pIndex->zName; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3938 | if( zName==0 ) zName = "ipk"; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3939 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 3940 | int i = sqlite3Strlen30(zName) - 1; |
| 3941 | while( zName[i]!='_' ) i--; |
| 3942 | zName += i; |
| 3943 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3944 | sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3945 | }else{ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3946 | sqlite3DebugPrintf("%20s",""); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3947 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3948 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3949 | char *z; |
| 3950 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3951 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 3952 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3953 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3954 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3955 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3956 | sqlite3DebugPrintf(" %-19s", z); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3957 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3958 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 3959 | sqlite3DebugPrintf(" f %04x N %d", p->wsFlags, p->nLTerm); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3960 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3961 | } |
| 3962 | #endif |
| 3963 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3964 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3965 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 3966 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3967 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3968 | static void whereLoopInit(WhereLoop *p){ |
| 3969 | p->aLTerm = p->aLTermSpace; |
| 3970 | p->nLTerm = 0; |
| 3971 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 3972 | p->wsFlags = 0; |
| 3973 | } |
| 3974 | |
| 3975 | /* |
| 3976 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 3977 | */ |
| 3978 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3979 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_TEMP_INDEX) ){ |
| 3980 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 3981 | sqlite3_free(p->u.vtab.idxStr); |
| 3982 | p->u.vtab.needFree = 0; |
| 3983 | p->u.vtab.idxStr = 0; |
| 3984 | }else if( (p->wsFlags & WHERE_TEMP_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
| 3985 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 3986 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 3987 | p->u.btree.pIndex = 0; |
| 3988 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3989 | } |
| 3990 | } |
| 3991 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3992 | /* |
| 3993 | ** Deallocate internal memory used by a WhereLoop object |
| 3994 | */ |
| 3995 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 3996 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 3997 | whereLoopClearUnion(db, p); |
| 3998 | whereLoopInit(p); |
| 3999 | } |
| 4000 | |
| 4001 | /* |
| 4002 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 4003 | */ |
| 4004 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 4005 | WhereTerm **paNew; |
| 4006 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 4007 | n = (n+7)&~7; |
| 4008 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 4009 | if( paNew==0 ) return SQLITE_NOMEM; |
| 4010 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 4011 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 4012 | p->aLTerm = paNew; |
| 4013 | p->nLSlot = n; |
| 4014 | return SQLITE_OK; |
| 4015 | } |
| 4016 | |
| 4017 | /* |
| 4018 | ** Transfer content from the second pLoop into the first. |
| 4019 | */ |
| 4020 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
| 4021 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ) return SQLITE_NOMEM; |
| 4022 | whereLoopClearUnion(db, pTo); |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4023 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 4024 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4025 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 4026 | pFrom->u.vtab.needFree = 0; |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4027 | }else if( (pFrom->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4028 | pFrom->u.btree.pIndex = 0; |
| 4029 | } |
| 4030 | return SQLITE_OK; |
| 4031 | } |
| 4032 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4033 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4034 | ** Delete a WhereLoop object |
| 4035 | */ |
| 4036 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4037 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4038 | sqlite3DbFree(db, p); |
| 4039 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 4040 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4041 | /* |
| 4042 | ** Free a WhereInfo structure |
| 4043 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 4044 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 4045 | if( ALWAYS(pWInfo) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4046 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4047 | while( pWInfo->pLoops ){ |
| 4048 | WhereLoop *p = pWInfo->pLoops; |
| 4049 | pWInfo->pLoops = p->pNextLoop; |
| 4050 | whereLoopDelete(db, p); |
| 4051 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4052 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4053 | } |
| 4054 | } |
| 4055 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4056 | /* |
| 4057 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 4058 | ** |
| 4059 | ** An existing WhereLoop entry might be overwritten if the new template |
| 4060 | ** is better and has fewer dependencies. Or the template will be ignored |
| 4061 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 4062 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4063 | ** added based on the template. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4064 | ** |
| 4065 | ** If pBuilder->pBest is not NULL then we only care about the very |
| 4066 | ** best template and that template should be stored in pBuilder->pBest. |
| 4067 | ** If pBuilder->pBest is NULL then a list of the best templates are stored |
| 4068 | ** in pBuilder->pWInfo->pLoops. |
| 4069 | ** |
| 4070 | ** When accumulating multiple loops (when pBuilder->pBest is NULL) we |
| 4071 | ** still might overwrite similar loops with the new template if the |
| 4072 | ** template is better. Loops may be overwritten if the following |
| 4073 | ** conditions are met: |
| 4074 | ** |
| 4075 | ** (1) They have the same iTab. |
| 4076 | ** (2) They have the same iSortIdx. |
| 4077 | ** (3) The template has same or fewer dependencies than the current loop |
| 4078 | ** (4) The template has the same or lower cost than the current loop |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4079 | ** (5) The template uses more terms of the same index but has no additional |
| 4080 | ** dependencies |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4081 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4082 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4083 | WhereLoop **ppPrev, *p, *pNext = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4084 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4085 | sqlite3 *db = pWInfo->pParse->db; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4086 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4087 | /* If pBuilder->pBest is defined, then only keep track of the single |
| 4088 | ** best WhereLoop. pBuilder->pBest->maskSelf==0 indicates that no |
| 4089 | ** prior WhereLoops have been evaluated and that the current pTemplate |
| 4090 | ** is therefore the first and hence the best and should be retained. |
| 4091 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4092 | if( (p = pBuilder->pBest)!=0 ){ |
| 4093 | if( p->maskSelf!=0 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4094 | WhereCost rCost = whereCostAdd(p->rRun,p->rSetup); |
| 4095 | WhereCost rTemplate = whereCostAdd(pTemplate->rRun,pTemplate->rSetup); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4096 | if( rCost < rTemplate ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4097 | testcase( rCost==rTemplate-1 ); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4098 | goto whereLoopInsert_noop; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4099 | } |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4100 | if( rCost==rTemplate && (p->prereq & pTemplate->prereq)==p->prereq ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4101 | goto whereLoopInsert_noop; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4102 | } |
| 4103 | } |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4104 | #if WHERETRACE_ENABLED |
| 4105 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 4106 | sqlite3DebugPrintf(p->maskSelf==0 ? "ins-init: " : "ins-best: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4107 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4108 | } |
| 4109 | #endif |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 4110 | whereLoopXfer(db, p, pTemplate); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4111 | return SQLITE_OK; |
| 4112 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4113 | |
| 4114 | /* Search for an existing WhereLoop to overwrite, or which takes |
| 4115 | ** priority over pTemplate. |
| 4116 | */ |
| 4117 | for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4118 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 4119 | /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 4120 | ** then those WhereLoops need to be considered separately. Neither is |
| 4121 | ** a candidate to replace the other. */ |
| 4122 | continue; |
| 4123 | } |
| 4124 | /* In the current implementation, the rSetup value is either zero |
| 4125 | ** or the cost of building an automatic index (NlogN) and the NlogN |
| 4126 | ** is the same for compatible WhereLoops. */ |
| 4127 | assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 4128 | || p->rSetup==pTemplate->rSetup ); |
| 4129 | |
| 4130 | /* whereLoopAddBtree() always generates and inserts the automatic index |
| 4131 | ** case first. Hence compatible candidate WhereLoops never have a larger |
| 4132 | ** rSetup. Call this SETUP-INVARIANT */ |
| 4133 | assert( p->rSetup>=pTemplate->rSetup ); |
| 4134 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4135 | if( (p->prereq & pTemplate->prereq)==p->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4136 | && p->rSetup<=pTemplate->rSetup |
| 4137 | && p->rRun<=pTemplate->rRun |
| 4138 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4139 | /* This branch taken when p is equal or better than pTemplate in |
| 4140 | ** all of (1) dependences (2) setup-cost, and (3) run-cost. */ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4141 | assert( p->rSetup==pTemplate->rSetup ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4142 | if( p->nLTerm<pTemplate->nLTerm |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4143 | && (p->wsFlags & WHERE_INDEXED)!=0 |
| 4144 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 4145 | && p->u.btree.pIndex==pTemplate->u.btree.pIndex |
| 4146 | && p->prereq==pTemplate->prereq |
| 4147 | ){ |
| 4148 | /* Overwrite an existing WhereLoop with an similar one that uses |
| 4149 | ** more terms of the index */ |
| 4150 | pNext = p->pNextLoop; |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4151 | break; |
| 4152 | }else{ |
| 4153 | /* pTemplate is not helpful. |
| 4154 | ** Return without changing or adding anything */ |
| 4155 | goto whereLoopInsert_noop; |
| 4156 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4157 | } |
| 4158 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4159 | && p->rRun>=pTemplate->rRun |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4160 | && ALWAYS(p->rSetup>=pTemplate->rSetup) /* See SETUP-INVARIANT above */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4161 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4162 | /* Overwrite an existing WhereLoop with a better one: one that is |
| 4163 | ** better at one of (1) dependences, (2) setup-cost, or (3) run-cost |
| 4164 | ** and is no worse in any of those categories. */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4165 | pNext = p->pNextLoop; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4166 | break; |
| 4167 | } |
| 4168 | } |
| 4169 | |
| 4170 | /* If we reach this point it means that either p[] should be overwritten |
| 4171 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 4172 | ** WhereLoop and insert it. |
| 4173 | */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4174 | #if WHERETRACE_ENABLED |
| 4175 | if( sqlite3WhereTrace & 0x8 ){ |
| 4176 | if( p!=0 ){ |
| 4177 | sqlite3DebugPrintf("ins-del: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4178 | whereLoopPrint(p, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4179 | } |
| 4180 | sqlite3DebugPrintf("ins-new: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4181 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4182 | } |
| 4183 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4184 | if( p==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4185 | p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4186 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4187 | whereLoopInit(p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4188 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4189 | whereLoopXfer(db, p, pTemplate); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4190 | p->pNextLoop = pNext; |
| 4191 | *ppPrev = p; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4192 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4193 | Index *pIndex = p->u.btree.pIndex; |
| 4194 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4195 | p->u.btree.pIndex = 0; |
| 4196 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4197 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4198 | return SQLITE_OK; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4199 | |
| 4200 | /* Jump here if the insert is a no-op */ |
| 4201 | whereLoopInsert_noop: |
| 4202 | #if WHERETRACE_ENABLED |
| 4203 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 4204 | sqlite3DebugPrintf(pBuilder->pBest ? "ins-skip: " : "ins-noop: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4205 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4206 | } |
| 4207 | #endif |
| 4208 | return SQLITE_OK; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | /* |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4212 | ** 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] | 4213 | ** Try to match one more. |
| 4214 | ** |
| 4215 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 4216 | ** INTEGER PRIMARY KEY. |
| 4217 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4218 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4219 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 4220 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 4221 | Index *pProbe, /* An index on pSrc */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4222 | WhereCost nInMul /* log(Number of iterations due to IN) */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4223 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4224 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 4225 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 4226 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4227 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 4228 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4229 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4230 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4231 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 4232 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 4233 | int saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 4234 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 4235 | WhereCost saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4236 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4237 | int rc = SQLITE_OK; /* Return code */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4238 | WhereCost nRowEst; /* Estimated index selectivity */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4239 | WhereCost rLogSize; /* Logarithm of table size */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4240 | WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4241 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4242 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4243 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4244 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4245 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4246 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 4247 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 4248 | opMask = WO_LT|WO_LE; |
| 4249 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 4250 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4251 | }else{ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4252 | 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] | 4253 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4254 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4255 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4256 | assert( pNew->u.btree.nEq<=pProbe->nColumn ); |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4257 | if( pNew->u.btree.nEq < pProbe->nColumn ){ |
| 4258 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4259 | nRowEst = whereCost(pProbe->aiRowEst[pNew->u.btree.nEq+1]); |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4260 | if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4261 | }else{ |
| 4262 | iCol = -1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4263 | nRowEst = 0; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4264 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4265 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4266 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4267 | saved_nEq = pNew->u.btree.nEq; |
| 4268 | saved_nLTerm = pNew->nLTerm; |
| 4269 | saved_wsFlags = pNew->wsFlags; |
| 4270 | saved_prereq = pNew->prereq; |
| 4271 | saved_nOut = pNew->nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4272 | pNew->rSetup = 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4273 | rLogSize = estLog(whereCost(pProbe->aiRowEst[0])); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4274 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4275 | int nIn = 0; |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4276 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4277 | pNew->wsFlags = saved_wsFlags; |
| 4278 | pNew->u.btree.nEq = saved_nEq; |
| 4279 | pNew->nLTerm = saved_nLTerm; |
| 4280 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4281 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 4282 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4283 | pNew->rRun = rLogSize; /* Baseline cost is log2(N). Adjustments below */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4284 | if( pTerm->eOperator & WO_IN ){ |
| 4285 | Expr *pExpr = pTerm->pExpr; |
| 4286 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4287 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4288 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
| 4289 | nIn = 46; assert( 46==whereCost(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4290 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4291 | /* "x IN (value, value, ...)" */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4292 | nIn = whereCost(pExpr->x.pList->nExpr); |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4293 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4294 | pNew->rRun += nIn; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4295 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4296 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4297 | }else if( pTerm->eOperator & (WO_EQ) ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4298 | assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4299 | || nInMul==0 ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4300 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4301 | if( iCol<0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4302 | || (pProbe->onError!=OE_None && nInMul==0 |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4303 | && pNew->u.btree.nEq==pProbe->nColumn-1) |
| 4304 | ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 4305 | assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4306 | pNew->wsFlags |= WHERE_ONEROW; |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4307 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4308 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4309 | pNew->nOut = nRowEst + nInMul; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4310 | }else if( pTerm->eOperator & (WO_ISNULL) ){ |
| 4311 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 4312 | pNew->u.btree.nEq++; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4313 | /* TUNING: IS NULL selects 2 rows */ |
| 4314 | nIn = 10; assert( 10==whereCost(2) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4315 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4316 | }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4317 | testcase( pTerm->eOperator & WO_GT ); |
| 4318 | testcase( pTerm->eOperator & WO_GE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4319 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4320 | pBtm = pTerm; |
| 4321 | pTop = 0; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4322 | }else{ |
| 4323 | assert( pTerm->eOperator & (WO_LT|WO_LE) ); |
| 4324 | testcase( pTerm->eOperator & WO_LT ); |
| 4325 | testcase( pTerm->eOperator & WO_LE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4326 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4327 | pTop = pTerm; |
| 4328 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4329 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4330 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4331 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4332 | /* Adjust nOut and rRun for STAT3 range values */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4333 | WhereCost rDiv; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4334 | whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq, |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4335 | pBtm, pTop, &rDiv); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4336 | pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4337 | } |
| 4338 | #ifdef SQLITE_ENABLE_STAT3 |
| 4339 | if( pNew->u.btree.nEq==1 && pProbe->nSample ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4340 | tRowcnt nOut = 0; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4341 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 4342 | testcase( pTerm->eOperator & WO_EQ ); |
| 4343 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4344 | rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4345 | }else if( (pTerm->eOperator & WO_IN) |
| 4346 | && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4347 | rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4348 | } |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 4349 | if( rc==SQLITE_OK ) pNew->nOut = whereCost(nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4350 | } |
| 4351 | #endif |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4352 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4353 | /* Each row involves a step of the index, then a binary search of |
| 4354 | ** the main table */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4355 | pNew->rRun = whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4356 | } |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4357 | /* Step cost for each output row */ |
| 4358 | pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4359 | /* TBD: Adjust nOut for additional constraints */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4360 | rc = whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4361 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
drh | bbe8b24 | 2013-06-13 15:50:59 +0000 | [diff] [blame] | 4362 | && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0)) |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4363 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4364 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4365 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4366 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4367 | pNew->prereq = saved_prereq; |
| 4368 | pNew->u.btree.nEq = saved_nEq; |
| 4369 | pNew->wsFlags = saved_wsFlags; |
| 4370 | pNew->nOut = saved_nOut; |
| 4371 | pNew->nLTerm = saved_nLTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4372 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4376 | ** Return True if it is possible that pIndex might be useful in |
| 4377 | ** implementing the ORDER BY clause in pBuilder. |
| 4378 | ** |
| 4379 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 4380 | ** if there is no way for pIndex to be useful in implementing that |
| 4381 | ** ORDER BY clause. |
| 4382 | */ |
| 4383 | static int indexMightHelpWithOrderBy( |
| 4384 | WhereLoopBuilder *pBuilder, |
| 4385 | Index *pIndex, |
| 4386 | int iCursor |
| 4387 | ){ |
| 4388 | ExprList *pOB; |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4389 | int ii, jj; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4390 | |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4391 | if( pIndex->bUnordered ) return 0; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4392 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4393 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 4394 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4395 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 4396 | if( pExpr->iTable==iCursor ){ |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 4397 | for(jj=0; jj<pIndex->nColumn; jj++){ |
| 4398 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 4399 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4400 | } |
| 4401 | } |
| 4402 | return 0; |
| 4403 | } |
| 4404 | |
| 4405 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4406 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 4407 | ** the table is used by an index. Only the first 63 columns are considered. |
| 4408 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4409 | static Bitmask columnsInIndex(Index *pIdx){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4410 | Bitmask m = 0; |
| 4411 | int j; |
| 4412 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 4413 | int x = pIdx->aiColumn[j]; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4414 | testcase( x==BMS-1 ); |
| 4415 | testcase( x==BMS-2 ); |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4416 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 4417 | } |
| 4418 | return m; |
| 4419 | } |
| 4420 | |
| 4421 | |
| 4422 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4423 | ** Add all WhereLoop objects a single table of the join were the table |
| 4424 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 4425 | ** a b-tree table, not a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4426 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4427 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4428 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4429 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4430 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4431 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4432 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4433 | Index sPk; /* A fake index object for the primary key */ |
| 4434 | tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 4435 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4436 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4437 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4438 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4439 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4440 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4441 | int b; /* A boolean value */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4442 | WhereCost rSize; /* number of rows in the table */ |
| 4443 | WhereCost rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4444 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4445 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4446 | pWInfo = pBuilder->pWInfo; |
| 4447 | pTabList = pWInfo->pTabList; |
| 4448 | pSrc = pTabList->a + pNew->iTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4449 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4450 | |
| 4451 | if( pSrc->pIndex ){ |
| 4452 | /* An INDEXED BY clause specifies a particular index to use */ |
| 4453 | pProbe = pSrc->pIndex; |
| 4454 | }else{ |
| 4455 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 4456 | ** variable sPk to represent the rowid primary key index. Make this |
| 4457 | ** fake index the first in a chain of Index objects with all of the real |
| 4458 | ** indices to follow */ |
| 4459 | Index *pFirst; /* First of real indices on the table */ |
| 4460 | memset(&sPk, 0, sizeof(Index)); |
| 4461 | sPk.nColumn = 1; |
| 4462 | sPk.aiColumn = &aiColumnPk; |
| 4463 | sPk.aiRowEst = aiRowEstPk; |
| 4464 | sPk.onError = OE_Replace; |
| 4465 | sPk.pTable = pSrc->pTab; |
| 4466 | aiRowEstPk[0] = pSrc->pTab->nRowEst; |
| 4467 | aiRowEstPk[1] = 1; |
| 4468 | pFirst = pSrc->pTab->pIndex; |
| 4469 | if( pSrc->notIndexed==0 ){ |
| 4470 | /* The real indices of the table are only considered if the |
| 4471 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 4472 | sPk.pNext = pFirst; |
| 4473 | } |
| 4474 | pProbe = &sPk; |
| 4475 | } |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4476 | rSize = whereCost(pSrc->pTab->nRowEst); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4477 | rLogSize = estLog(rSize); |
| 4478 | |
| 4479 | /* Automatic indexes */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4480 | if( !pBuilder->pBest |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 4481 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 4482 | && pSrc->pIndex==0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4483 | && !pSrc->viaCoroutine |
| 4484 | && !pSrc->notIndexed |
| 4485 | && !pSrc->isCorrelated |
| 4486 | ){ |
| 4487 | /* Generate auto-index WhereLoops */ |
| 4488 | WhereClause *pWC = pBuilder->pWC; |
| 4489 | WhereTerm *pTerm; |
| 4490 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 4491 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4492 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4493 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 4494 | pNew->u.btree.nEq = 1; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4495 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4496 | pNew->nLTerm = 1; |
| 4497 | pNew->aLTerm[0] = pTerm; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4498 | /* TUNING: One-time cost for computing the automatic index is |
| 4499 | ** approximately 6*N*log2(N) where N is the number of rows in |
| 4500 | ** the table being indexed. */ |
| 4501 | pNew->rSetup = rLogSize + rSize + 26; assert( 26==whereCost(6) ); |
| 4502 | /* TUNING: Each index lookup yields 10 rows in the table */ |
| 4503 | pNew->nOut = 33; assert( 33==whereCost(10) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4504 | pNew->rRun = whereCostAdd(rLogSize,pNew->nOut); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4505 | pNew->wsFlags = WHERE_TEMP_INDEX; |
| 4506 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4507 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4508 | } |
| 4509 | } |
| 4510 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4511 | |
| 4512 | /* Loop over all indices |
| 4513 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4514 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4515 | pNew->u.btree.nEq = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4516 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4517 | pNew->iSortIdx = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4518 | pNew->rSetup = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4519 | pNew->prereq = mExtra; |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4520 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4521 | pNew->u.btree.pIndex = pProbe; |
| 4522 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4523 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 4524 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4525 | if( pProbe->tnum<=0 ){ |
| 4526 | /* Integer primary key index */ |
| 4527 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4528 | |
| 4529 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4530 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4531 | /* TUNING: Cost of full table scan is 3*(N + log2(N)). |
| 4532 | ** + The extra 3 factor is to encourage the use of indexed lookups |
| 4533 | ** over full scans. A smaller constant 2 is used for covering |
| 4534 | ** index scans so that a covering index scan will be favored over |
| 4535 | ** a table scan. */ |
| 4536 | pNew->rRun = whereCostAdd(rSize,rLogSize) + 16; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4537 | rc = whereLoopInsert(pBuilder, pNew); |
| 4538 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4539 | }else{ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4540 | Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4541 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4542 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4543 | /* Full scan via index */ |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 4544 | if( b |
| 4545 | || ( m==0 |
| 4546 | && pProbe->bUnordered==0 |
| 4547 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 4548 | && sqlite3GlobalConfig.bUseCis |
| 4549 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 4550 | ) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4551 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4552 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4553 | if( m==0 ){ |
| 4554 | /* TUNING: Cost of a covering index scan is 2*(N + log2(N)). |
| 4555 | ** + The extra 2 factor is to encourage the use of indexed lookups |
| 4556 | ** over index scans. A table scan uses a factor of 3 so that |
| 4557 | ** index scans are favored over table scans. |
| 4558 | ** + If this covering index might also help satisfy the ORDER BY |
| 4559 | ** clause, then the cost is fudged down slightly so that this |
| 4560 | ** index is favored above other indices that have no hope of |
| 4561 | ** helping with the ORDER BY. */ |
| 4562 | pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b; |
| 4563 | }else{ |
| 4564 | assert( b!=0 ); |
| 4565 | /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N) |
| 4566 | ** which we will simplify to just N*log2(N) */ |
| 4567 | pNew->rRun = rSize + rLogSize; |
| 4568 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4569 | rc = whereLoopInsert(pBuilder, pNew); |
| 4570 | if( rc ) break; |
| 4571 | } |
| 4572 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4573 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4574 | |
| 4575 | /* If there was an INDEXED BY clause, then only that one index is |
| 4576 | ** considered. */ |
| 4577 | if( pSrc->pIndex ) break; |
| 4578 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4579 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4582 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4583 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4584 | ** Add all WhereLoop objects for a table of the join identified by |
| 4585 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4586 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4587 | static int whereLoopAddVirtual( |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4588 | WhereLoopBuilder *pBuilder /* WHERE clause information */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4589 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4590 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4591 | Parse *pParse; /* The parsing context */ |
| 4592 | WhereClause *pWC; /* The WHERE clause */ |
| 4593 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 4594 | Table *pTab; |
| 4595 | sqlite3 *db; |
| 4596 | sqlite3_index_info *pIdxInfo; |
| 4597 | struct sqlite3_index_constraint *pIdxCons; |
| 4598 | struct sqlite3_index_constraint_usage *pUsage; |
| 4599 | WhereTerm *pTerm; |
| 4600 | int i, j; |
| 4601 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4602 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4603 | int seenIn = 0; /* True if an IN operator is seen */ |
| 4604 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 4605 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 4606 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4607 | int rc = SQLITE_OK; |
| 4608 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4609 | pWInfo = pBuilder->pWInfo; |
| 4610 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4611 | db = pParse->db; |
| 4612 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4613 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4614 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4615 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4616 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4617 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4618 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4619 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4620 | pNew->rSetup = 0; |
| 4621 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4622 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4623 | pNew->u.vtab.needFree = 0; |
| 4624 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4625 | nConstraint = pIdxInfo->nConstraint; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4626 | if( whereLoopResize(db, pNew, nConstraint) ){ |
| 4627 | sqlite3DbFree(db, pIdxInfo); |
| 4628 | return SQLITE_NOMEM; |
| 4629 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4630 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4631 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4632 | if( !seenIn && (iPhase&1)!=0 ){ |
| 4633 | iPhase++; |
| 4634 | if( iPhase>3 ) break; |
| 4635 | } |
| 4636 | if( !seenVar && iPhase>1 ) break; |
| 4637 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4638 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4639 | j = pIdxCons->iTermOffset; |
| 4640 | pTerm = &pWC->a[j]; |
| 4641 | switch( iPhase ){ |
| 4642 | case 0: /* Constants without IN operator */ |
| 4643 | pIdxCons->usable = 0; |
| 4644 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4645 | seenIn = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4646 | } |
| 4647 | if( pTerm->prereqRight!=0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4648 | seenVar = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4649 | }else if( (pTerm->eOperator & WO_IN)==0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4650 | pIdxCons->usable = 1; |
| 4651 | } |
| 4652 | break; |
| 4653 | case 1: /* Constants with IN operators */ |
| 4654 | assert( seenIn ); |
| 4655 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 4656 | break; |
| 4657 | case 2: /* Variables without IN */ |
| 4658 | assert( seenVar ); |
| 4659 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 4660 | break; |
| 4661 | default: /* Variables with IN */ |
| 4662 | assert( seenVar && seenIn ); |
| 4663 | pIdxCons->usable = 1; |
| 4664 | break; |
| 4665 | } |
| 4666 | } |
| 4667 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 4668 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4669 | pIdxInfo->idxStr = 0; |
| 4670 | pIdxInfo->idxNum = 0; |
| 4671 | pIdxInfo->needToFreeIdxStr = 0; |
| 4672 | pIdxInfo->orderByConsumed = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4673 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4674 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 4675 | if( rc ) goto whereLoopAddVtab_exit; |
| 4676 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4677 | pNew->prereq = 0; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4678 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4679 | assert( pNew->nLSlot>=nConstraint ); |
| 4680 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4681 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4682 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4683 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 4684 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4685 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4686 | || j<0 |
| 4687 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4688 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4689 | ){ |
| 4690 | rc = SQLITE_ERROR; |
| 4691 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4692 | goto whereLoopAddVtab_exit; |
| 4693 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4694 | testcase( iTerm==nConstraint-1 ); |
| 4695 | testcase( j==0 ); |
| 4696 | testcase( j==pWC->nTerm-1 ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4697 | pTerm = &pWC->a[j]; |
| 4698 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4699 | assert( iTerm<pNew->nLSlot ); |
| 4700 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4701 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4702 | testcase( iTerm==15 ); |
| 4703 | testcase( iTerm==16 ); |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4704 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4705 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4706 | if( pUsage[i].omit==0 ){ |
| 4707 | /* Do not attempt to use an IN constraint if the virtual table |
| 4708 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4709 | ** If we do attempt to use such a constraint, some rows might be |
| 4710 | ** repeated in the output. */ |
| 4711 | break; |
| 4712 | } |
| 4713 | /* A virtual table that is constrained by an IN clause may not |
| 4714 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4715 | ** is not necessarily related to the order of output terms and |
| 4716 | ** (2) Multiple outputs from a single IN value will not merge |
| 4717 | ** together. */ |
| 4718 | pIdxInfo->orderByConsumed = 0; |
| 4719 | } |
| 4720 | } |
| 4721 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4722 | if( i>=nConstraint ){ |
| 4723 | pNew->nLTerm = mxTerm+1; |
| 4724 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4725 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4726 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 4727 | pIdxInfo->needToFreeIdxStr = 0; |
| 4728 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 3b1d808 | 2013-06-03 16:56:37 +0000 | [diff] [blame] | 4729 | pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) |
| 4730 | && pIdxInfo->orderByConsumed); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4731 | pNew->rSetup = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4732 | pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4733 | /* TUNING: Every virtual table query returns 25 rows */ |
| 4734 | pNew->nOut = 46; assert( 46==whereCost(25) ); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4735 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4736 | if( pNew->u.vtab.needFree ){ |
| 4737 | sqlite3_free(pNew->u.vtab.idxStr); |
| 4738 | pNew->u.vtab.needFree = 0; |
| 4739 | } |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | whereLoopAddVtab_exit: |
| 4744 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4745 | sqlite3DbFree(db, pIdxInfo); |
| 4746 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4747 | } |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4748 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4749 | |
| 4750 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4751 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 4752 | ** btrees or virtual tables. |
| 4753 | */ |
| 4754 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4755 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4756 | WhereClause *pWC; |
| 4757 | WhereLoop *pNew; |
| 4758 | WhereTerm *pTerm, *pWCEnd; |
| 4759 | int rc = SQLITE_OK; |
| 4760 | int iCur; |
| 4761 | WhereClause tempWC; |
| 4762 | WhereLoopBuilder sSubBuild; |
| 4763 | WhereLoop sBest; |
| 4764 | struct SrcList_item *pItem; |
| 4765 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4766 | pWC = pBuilder->pWC; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4767 | if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4768 | pWCEnd = pWC->a + pWC->nTerm; |
| 4769 | pNew = pBuilder->pNew; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4770 | |
| 4771 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 4772 | if( (pTerm->eOperator & WO_OR)!=0 |
| 4773 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 4774 | ){ |
| 4775 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 4776 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 4777 | WhereTerm *pOrTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4778 | WhereCost rTotal = 0; |
| 4779 | WhereCost nRow = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4780 | Bitmask prereq = mExtra; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4781 | |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4782 | whereLoopInit(&sBest); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4783 | pItem = pWInfo->pTabList->a + pNew->iTab; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4784 | iCur = pItem->iCursor; |
| 4785 | sSubBuild = *pBuilder; |
| 4786 | sSubBuild.pOrderBy = 0; |
| 4787 | sSubBuild.pBest = &sBest; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4788 | |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4789 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4790 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4791 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 4792 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4793 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4794 | tempWC.pOuter = pWC; |
| 4795 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4796 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4797 | tempWC.a = pOrTerm; |
| 4798 | sSubBuild.pWC = &tempWC; |
| 4799 | }else{ |
| 4800 | continue; |
| 4801 | } |
| 4802 | sBest.maskSelf = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4803 | sBest.rSetup = 0; |
| 4804 | sBest.rRun = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4805 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4806 | if( IsVirtual(pItem->pTab) ){ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4807 | rc = whereLoopAddVirtual(&sSubBuild); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4808 | }else |
| 4809 | #endif |
| 4810 | { |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4811 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 4812 | } |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4813 | /* sBest.maskSelf is always zero if an error occurs */ |
| 4814 | assert( rc==SQLITE_OK || sBest.maskSelf==0 ); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4815 | if( sBest.maskSelf==0 ) break; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4816 | assert( sBest.rSetup==0 ); |
| 4817 | rTotal = whereCostAdd(rTotal, sBest.rRun); |
| 4818 | nRow = whereCostAdd(nRow, sBest.nOut); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4819 | prereq |= sBest.prereq; |
| 4820 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4821 | assert( pNew->nLSlot>=1 ); |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4822 | if( sBest.maskSelf ){ |
| 4823 | pNew->nLTerm = 1; |
| 4824 | pNew->aLTerm[0] = pTerm; |
| 4825 | pNew->wsFlags = WHERE_MULTI_OR; |
| 4826 | pNew->rSetup = 0; |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 4827 | /* TUNING: Multiple by 3.5 for the secondary table lookup */ |
| 4828 | pNew->rRun = rTotal + 18; assert( 18==whereCost(7)-whereCost(2) ); |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 4829 | pNew->nOut = nRow; |
| 4830 | pNew->prereq = prereq; |
| 4831 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 4832 | rc = whereLoopInsert(pBuilder, pNew); |
| 4833 | } |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4834 | whereLoopClear(pWInfo->pParse->db, &sBest); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4835 | } |
| 4836 | } |
| 4837 | return rc; |
| 4838 | } |
| 4839 | |
| 4840 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4841 | ** Add all WhereLoop objects for all tables |
| 4842 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4843 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4844 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4845 | Bitmask mExtra = 0; |
| 4846 | Bitmask mPrior = 0; |
| 4847 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4848 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4849 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4850 | sqlite3 *db = pWInfo->pParse->db; |
| 4851 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4852 | int rc = SQLITE_OK; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4853 | u8 priorJoinType = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4854 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4855 | |
| 4856 | /* Loop over the tables in the join, from left to right */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4857 | pNew = pBuilder->pNew; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4858 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4859 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4860 | pNew->iTab = iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4861 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4862 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4863 | mExtra = mPrior; |
| 4864 | } |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4865 | priorJoinType = pItem->jointype; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4866 | if( IsVirtual(pItem->pTab) ){ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 4867 | rc = whereLoopAddVirtual(pBuilder); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4868 | }else{ |
| 4869 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 4870 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4871 | if( rc==SQLITE_OK ){ |
| 4872 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 4873 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4874 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4875 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4876 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4877 | whereLoopClear(db, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4878 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4881 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4882 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4883 | ** parameters) to see if it outputs rows in the requested ORDER BY |
| 4884 | ** (or GROUP BY) without requiring a separate source operation. Return: |
| 4885 | ** |
| 4886 | ** 0: ORDER BY is not satisfied. Sorting required |
| 4887 | ** 1: ORDER BY is satisfied. Omit sorting |
| 4888 | ** -1: Unknown at this time |
| 4889 | ** |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4890 | */ |
| 4891 | static int wherePathSatisfiesOrderBy( |
| 4892 | WhereInfo *pWInfo, /* The WHERE clause */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4893 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4894 | WherePath *pPath, /* The WherePath to check */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4895 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 4896 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4897 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4898 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4899 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4900 | u8 revSet; /* True if rev is known */ |
| 4901 | u8 rev; /* Composite sort order */ |
| 4902 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4903 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 4904 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 4905 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4906 | u16 nColumn; /* Number of columns in pIndex */ |
| 4907 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 4908 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 4909 | int i, j; /* Loop counters */ |
| 4910 | int iCur; /* Cursor number for current WhereLoop */ |
| 4911 | int iColumn; /* A column number within table iCur */ |
drh | e8ae583 | 2013-06-19 13:32:46 +0000 | [diff] [blame] | 4912 | WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4913 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 4914 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 4915 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 4916 | Index *pIndex; /* The index associated with pLoop */ |
| 4917 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 4918 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 4919 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4920 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4921 | Bitmask ready; /* Mask of inner loops */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4922 | |
| 4923 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4924 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 4925 | ** 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] | 4926 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 4927 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4928 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 4929 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4930 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4931 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 4932 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 4933 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 4934 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 4935 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 4936 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 4937 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 4938 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 4939 | ** |
| 4940 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 4941 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 4942 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4943 | */ |
| 4944 | |
| 4945 | assert( pOrderBy!=0 ); |
| 4946 | |
| 4947 | /* Sortability of virtual tables is determined by the xBestIndex method |
| 4948 | ** of the virtual table itself */ |
| 4949 | if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4950 | testcase( nLoop>0 ); /* True when outer loops are one-row and match |
| 4951 | ** no ORDER BY terms */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4952 | return pLast->u.vtab.isOrdered; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4953 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4954 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4955 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4956 | nOrderBy = pOrderBy->nExpr; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4957 | testcase( nOrderBy==BMS-1 ); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4958 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 4959 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4960 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4961 | orderDistinctMask = 0; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4962 | ready = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4963 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4964 | if( iLoop>0 ) ready |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4965 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4966 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4967 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4968 | |
| 4969 | /* Mark off any ORDER BY term X that is a column in the table of |
| 4970 | ** the current loop for which there is term in the WHERE |
| 4971 | ** clause of the form X IS NULL or X=? that reference only outer |
| 4972 | ** loops. |
| 4973 | */ |
| 4974 | for(i=0; i<nOrderBy; i++){ |
| 4975 | if( MASKBIT(i) & obSat ) continue; |
| 4976 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 4977 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 4978 | if( pOBExpr->iTable!=iCur ) continue; |
| 4979 | pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 4980 | ~ready, WO_EQ|WO_ISNULL, 0); |
| 4981 | if( pTerm==0 ) continue; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4982 | if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 4983 | const char *z1, *z2; |
| 4984 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 4985 | if( !pColl ) pColl = db->pDfltColl; |
| 4986 | z1 = pColl->zName; |
| 4987 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 4988 | if( !pColl ) pColl = db->pDfltColl; |
| 4989 | z2 = pColl->zName; |
| 4990 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 4991 | } |
| 4992 | obSat |= MASKBIT(i); |
| 4993 | } |
| 4994 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4995 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 4996 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 4997 | pIndex = 0; |
| 4998 | nColumn = 0; |
| 4999 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5000 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5001 | }else{ |
| 5002 | nColumn = pIndex->nColumn; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5003 | isOrderDistinct = pIndex->onError!=OE_None; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5004 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5005 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5006 | /* Loop through all columns of the index and deal with the ones |
| 5007 | ** that are not constrained by == or IN. |
| 5008 | */ |
| 5009 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5010 | distinctColumns = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5011 | for(j=0; j<=nColumn; j++){ |
| 5012 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 5013 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5014 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5015 | if( j<pLoop->u.btree.nEq |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5016 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5017 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5018 | if( i & WO_ISNULL ){ |
| 5019 | testcase( isOrderDistinct ); |
| 5020 | isOrderDistinct = 0; |
| 5021 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5022 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5025 | /* Get the column number in the table (iColumn) and sort order |
| 5026 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5027 | */ |
| 5028 | if( j<nColumn ){ |
| 5029 | /* Normal index columns */ |
| 5030 | iColumn = pIndex->aiColumn[j]; |
| 5031 | revIdx = pIndex->aSortOrder[j]; |
| 5032 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5033 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5034 | /* The ROWID column at the end */ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5035 | assert( j==nColumn ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5036 | iColumn = -1; |
| 5037 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5038 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5039 | |
| 5040 | /* An unconstrained column that might be NULL means that this |
| 5041 | ** WhereLoop is not well-ordered |
| 5042 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5043 | if( isOrderDistinct |
| 5044 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5045 | && j>=pLoop->u.btree.nEq |
| 5046 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 5047 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5048 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | /* Find the ORDER BY term that corresponds to the j-th column |
| 5052 | ** of the index and and mark that ORDER BY term off |
| 5053 | */ |
| 5054 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5055 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5056 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 5057 | if( MASKBIT(i) & obSat ) continue; |
| 5058 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5059 | testcase( wctrlFlags & WHERE_GROUPBY ); |
| 5060 | testcase( wctrlFlags & WHERE_DISTINCTBY ); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5061 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5062 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5063 | if( pOBExpr->iTable!=iCur ) continue; |
| 5064 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 5065 | if( iColumn>=0 ){ |
| 5066 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5067 | if( !pColl ) pColl = db->pDfltColl; |
| 5068 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 5069 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5070 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5071 | break; |
| 5072 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5073 | if( isMatch ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5074 | if( iColumn<0 ){ |
| 5075 | testcase( distinctColumns==0 ); |
| 5076 | distinctColumns = 1; |
| 5077 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5078 | obSat |= MASKBIT(i); |
| 5079 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5080 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 5081 | ** Sort order is irrelevant for a GROUP BY clause. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5082 | if( revSet ){ |
| 5083 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; |
| 5084 | }else{ |
| 5085 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 5086 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 5087 | revSet = 1; |
| 5088 | } |
| 5089 | } |
| 5090 | }else{ |
| 5091 | /* No match found */ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5092 | if( j==0 || j<nColumn ){ |
| 5093 | testcase( isOrderDistinct!=0 ); |
| 5094 | isOrderDistinct = 0; |
| 5095 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5096 | break; |
| 5097 | } |
| 5098 | } /* end Loop over all index columns */ |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5099 | if( distinctColumns ){ |
| 5100 | testcase( isOrderDistinct==0 ); |
| 5101 | isOrderDistinct = 1; |
| 5102 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5103 | } /* end-if not one-row */ |
| 5104 | |
| 5105 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5106 | if( isOrderDistinct ){ |
| 5107 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5108 | for(i=0; i<nOrderBy; i++){ |
| 5109 | Expr *p; |
| 5110 | if( MASKBIT(i) & obSat ) continue; |
| 5111 | p = pOrderBy->a[i].pExpr; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5112 | if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5113 | obSat |= MASKBIT(i); |
| 5114 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 5115 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5116 | } |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5117 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5118 | if( obSat==obDone ) return 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5119 | if( !isOrderDistinct ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5120 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5123 | #ifdef WHERETRACE_ENABLED |
| 5124 | /* For debugging use only: */ |
| 5125 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 5126 | static char zName[65]; |
| 5127 | int i; |
| 5128 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 5129 | if( pLast ) zName[i++] = pLast->cId; |
| 5130 | zName[i] = 0; |
| 5131 | return zName; |
| 5132 | } |
| 5133 | #endif |
| 5134 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5135 | |
| 5136 | /* |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5137 | ** Given the list of WhereLoop objects on pWInfo->pLoops, this routine |
| 5138 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 5139 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 5140 | ** |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5141 | ** Assume that the total number of output rows that will need to be sorted |
| 5142 | ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 5143 | ** costs if nRowEst==0. |
| 5144 | ** |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5145 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 5146 | ** error occurs. |
| 5147 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5148 | static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5149 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5150 | int nLoop; /* Number of terms in the join */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5151 | Parse *pParse; /* Parsing context */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5152 | sqlite3 *db; /* The database connection */ |
| 5153 | int iLoop; /* Loop counter over the terms of the join */ |
| 5154 | int ii, jj; /* Loop counters */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5155 | WhereCost rCost; /* Cost of a path */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5156 | WhereCost mxCost = 0; /* Maximum cost of a set of paths */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5157 | WhereCost rSortCost; /* Cost to do a sort */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5158 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 5159 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 5160 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 5161 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 5162 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 5163 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 5164 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 5165 | char *pSpace; /* Temporary memory used by this routine */ |
| 5166 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5167 | pParse = pWInfo->pParse; |
| 5168 | db = pParse->db; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5169 | nLoop = pWInfo->nLevel; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5170 | /* TUNING: For simple queries, only the best path is tracked. |
| 5171 | ** For 2-way joins, the 5 best paths are followed. |
| 5172 | ** For joins of 3 or more tables, track the 10 best paths */ |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5173 | mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5174 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5175 | WHERETRACE(0x002, ("---- begin solver\n")); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5176 | |
| 5177 | /* Allocate and initialize space for aTo and aFrom */ |
| 5178 | ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 5179 | pSpace = sqlite3DbMallocRaw(db, ii); |
| 5180 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 5181 | aTo = (WherePath*)pSpace; |
| 5182 | aFrom = aTo+mxChoice; |
| 5183 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 5184 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5185 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5186 | pFrom->aLoop = pX; |
| 5187 | } |
| 5188 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5189 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 5190 | ** |
| 5191 | ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 5192 | ** of computing an automatic index is not paid back within the first 25 |
| 5193 | ** rows, then do not use the automatic index. */ |
| 5194 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==whereCost(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5195 | nFrom = 1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5196 | |
| 5197 | /* Precompute the cost of sorting the final result set, if the caller |
| 5198 | ** to sqlite3WhereBegin() was concerned about sorting */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5199 | rSortCost = 0; |
| 5200 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5201 | aFrom[0].isOrderedValid = 1; |
| 5202 | }else{ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5203 | /* TUNING: Estimated cost of sorting is N*log2(N) where N is the |
| 5204 | ** number of output rows. */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5205 | rSortCost = nRowEst + estLog(nRowEst); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5206 | WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost)); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5207 | } |
| 5208 | |
| 5209 | /* Compute successively longer WherePaths using the previous generation |
| 5210 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 5211 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5212 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 5213 | nTo = 0; |
| 5214 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 5215 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 5216 | Bitmask maskNew; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5217 | Bitmask revMask = 0; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5218 | u8 isOrderedValid = pFrom->isOrderedValid; |
| 5219 | u8 isOrdered = pFrom->isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5220 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 5221 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5222 | /* At this point, pWLoop is a candidate to be the next loop. |
| 5223 | ** Compute its cost */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5224 | rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 5225 | rCost = whereCostAdd(rCost, pFrom->rCost); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5226 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5227 | if( !isOrderedValid ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5228 | switch( wherePathSatisfiesOrderBy(pWInfo, |
| 5229 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5230 | iLoop, pWLoop, &revMask) ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5231 | case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ |
| 5232 | isOrdered = 1; |
| 5233 | isOrderedValid = 1; |
| 5234 | break; |
| 5235 | case 0: /* No. pFrom+pWLoop will require a separate sort */ |
| 5236 | isOrdered = 0; |
| 5237 | isOrderedValid = 1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5238 | rCost = whereCostAdd(rCost, rSortCost); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5239 | break; |
| 5240 | default: /* Cannot tell yet. Try again on the next iteration */ |
| 5241 | break; |
| 5242 | } |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 5243 | }else{ |
| 5244 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5245 | } |
| 5246 | /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 5247 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
| 5248 | if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5249 | testcase( jj==nTo-1 ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5250 | break; |
| 5251 | } |
| 5252 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5253 | if( jj>=nTo ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5254 | if( nTo>=mxChoice && rCost>=mxCost ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5255 | #ifdef WHERETRACE_ENABLED |
| 5256 | if( sqlite3WhereTrace&0x4 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5257 | sqlite3DebugPrintf("Skip %s cost=%3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5258 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5259 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5260 | } |
| 5261 | #endif |
| 5262 | continue; |
| 5263 | } |
| 5264 | /* Add a new Path to the aTo[] set */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5265 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5266 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5267 | jj = nTo++; |
| 5268 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5269 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 5270 | for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5271 | } |
| 5272 | pTo = &aTo[jj]; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5273 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5274 | if( sqlite3WhereTrace&0x4 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5275 | sqlite3DebugPrintf("New %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5276 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5277 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5278 | } |
| 5279 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5280 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5281 | if( pTo->rCost<=rCost ){ |
| 5282 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5283 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5284 | sqlite3DebugPrintf( |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5285 | "Skip %s cost=%-3d order=%c", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5286 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5287 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5288 | sqlite3DebugPrintf(" vs %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5289 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5290 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5291 | } |
| 5292 | #endif |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5293 | testcase( pTo->rCost==rCost ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5294 | continue; |
| 5295 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5296 | testcase( pTo->rCost==rCost+1 ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5297 | /* A new and better score for a previously created equivalent path */ |
| 5298 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5299 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5300 | sqlite3DebugPrintf( |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5301 | "Update %s cost=%-3d order=%c", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5302 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5303 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5304 | sqlite3DebugPrintf(" was %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5305 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5306 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5307 | } |
| 5308 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5309 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5310 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5311 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5312 | pTo->revLoop = revMask; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5313 | pTo->nRow = pFrom->nRow + pWLoop->nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5314 | pTo->rCost = rCost; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5315 | pTo->isOrderedValid = isOrderedValid; |
| 5316 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5317 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 5318 | pTo->aLoop[iLoop] = pWLoop; |
| 5319 | if( nTo>=mxChoice ){ |
| 5320 | mxCost = aTo[0].rCost; |
| 5321 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
| 5322 | if( pTo->rCost>mxCost ) mxCost = pTo->rCost; |
| 5323 | } |
| 5324 | } |
| 5325 | } |
| 5326 | } |
| 5327 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5328 | #ifdef WHERETRACE_ENABLED |
| 5329 | if( sqlite3WhereTrace>=2 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5330 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5331 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5332 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5333 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5334 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5335 | if( pTo->isOrderedValid && pTo->isOrdered ){ |
| 5336 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 5337 | }else{ |
| 5338 | sqlite3DebugPrintf("\n"); |
| 5339 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5340 | } |
| 5341 | } |
| 5342 | #endif |
| 5343 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5344 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5345 | pFrom = aTo; |
| 5346 | aTo = aFrom; |
| 5347 | aFrom = pFrom; |
| 5348 | nFrom = nTo; |
| 5349 | } |
| 5350 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5351 | if( nFrom==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5352 | sqlite3ErrorMsg(pParse, "no query solution"); |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5353 | sqlite3DbFree(db, pSpace); |
| 5354 | return SQLITE_ERROR; |
| 5355 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5356 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5357 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5358 | pFrom = aFrom; |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5359 | assert( nFrom==1 ); |
| 5360 | #if 0 /* The following is needed if nFrom is ever more than 1 */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5361 | for(ii=1; ii<nFrom; ii++){ |
| 5362 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 5363 | } |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5364 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5365 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5366 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5367 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5368 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 5369 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5370 | pLevel->iFrom = pWLoop->iTab; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5371 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5372 | } |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5373 | if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 5374 | && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 5375 | && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5376 | && nRowEst |
| 5377 | ){ |
| 5378 | Bitmask notUsed; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5379 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5380 | WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5381 | if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5382 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5383 | if( pFrom->isOrdered ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5384 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 5385 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5386 | }else{ |
| 5387 | pWInfo->bOBSat = 1; |
| 5388 | pWInfo->revMask = pFrom->revLoop; |
| 5389 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5390 | } |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5391 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5392 | |
| 5393 | /* Free temporary memory and return success */ |
| 5394 | sqlite3DbFree(db, pSpace); |
| 5395 | return SQLITE_OK; |
| 5396 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 5397 | |
| 5398 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5399 | ** Most queries use only a single table (they are not joins) and have |
| 5400 | ** simple == constraints against indexed fields. This routine attempts |
| 5401 | ** to plan those simple cases using much less ceremony than the |
| 5402 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 5403 | ** times for the common case. |
| 5404 | ** |
| 5405 | ** Return non-zero on success, if this query can be handled by this |
| 5406 | ** no-frills query planner. Return zero if this query needs the |
| 5407 | ** general-purpose query planner. |
| 5408 | */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5409 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5410 | WhereInfo *pWInfo; |
| 5411 | struct SrcList_item *pItem; |
| 5412 | WhereClause *pWC; |
| 5413 | WhereTerm *pTerm; |
| 5414 | WhereLoop *pLoop; |
| 5415 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5416 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5417 | Table *pTab; |
| 5418 | Index *pIdx; |
| 5419 | |
| 5420 | pWInfo = pBuilder->pWInfo; |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 5421 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5422 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 5423 | pItem = pWInfo->pTabList->a; |
| 5424 | pTab = pItem->pTab; |
| 5425 | if( IsVirtual(pTab) ) return 0; |
| 5426 | if( pItem->zIndex ) return 0; |
| 5427 | iCur = pItem->iCursor; |
| 5428 | pWC = &pWInfo->sWC; |
| 5429 | pLoop = pBuilder->pNew; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5430 | pLoop->wsFlags = 0; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5431 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5432 | if( pTerm ){ |
| 5433 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 5434 | pLoop->aLTerm[0] = pTerm; |
| 5435 | pLoop->nLTerm = 1; |
| 5436 | pLoop->u.btree.nEq = 1; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5437 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 5438 | pLoop->rRun = 33; /* 33==whereCost(10) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5439 | }else{ |
| 5440 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 5441 | if( pIdx->onError==OE_None ) continue; |
| 5442 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5443 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5444 | if( pTerm==0 ) break; |
| 5445 | whereLoopResize(pWInfo->pParse->db, pLoop, j); |
| 5446 | pLoop->aLTerm[j] = pTerm; |
| 5447 | } |
| 5448 | if( j!=pIdx->nColumn ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5449 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 5450 | if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5451 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 5452 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5453 | pLoop->nLTerm = j; |
| 5454 | pLoop->u.btree.nEq = j; |
| 5455 | pLoop->u.btree.pIndex = pIdx; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5456 | /* TUNING: Cost of a unique index lookup is 15 */ |
| 5457 | pLoop->rRun = 39; /* 39==whereCost(15) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5458 | break; |
| 5459 | } |
| 5460 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5461 | if( pLoop->wsFlags ){ |
| 5462 | pLoop->nOut = (WhereCost)1; |
| 5463 | pWInfo->a[0].pWLoop = pLoop; |
| 5464 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 5465 | pWInfo->a[0].iTabCur = iCur; |
| 5466 | pWInfo->nRowOut = 1; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5467 | if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5468 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5469 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5470 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5471 | #ifdef SQLITE_DEBUG |
| 5472 | pLoop->cId = '0'; |
| 5473 | #endif |
| 5474 | return 1; |
| 5475 | } |
| 5476 | return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5480 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 5481 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5482 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5483 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5484 | ** in order to complete the WHERE clause processing. |
| 5485 | ** |
| 5486 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5487 | ** |
| 5488 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5489 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5490 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5491 | ** example, if the SQL is this: |
| 5492 | ** |
| 5493 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5494 | ** |
| 5495 | ** Then the code generated is conceptually like the following: |
| 5496 | ** |
| 5497 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5498 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5499 | ** foreach row3 in t3 do / |
| 5500 | ** ... |
| 5501 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5502 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5503 | ** end / |
| 5504 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5505 | ** Note that the loops might not be nested in the order in which they |
| 5506 | ** 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] | 5507 | ** use of indices. Note also that when the IN operator appears in |
| 5508 | ** the WHERE clause, it might result in additional nested loops for |
| 5509 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5510 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5511 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5512 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5513 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5514 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5515 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5516 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5517 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5518 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5519 | ** data from the various tables of the loop. |
| 5520 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5521 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5522 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5523 | ** the tables have indices and there are terms in the WHERE clause that |
| 5524 | ** refer to those indices, a complete table scan can be avoided and the |
| 5525 | ** code will run much faster. Most of the work of this routine is checking |
| 5526 | ** to see if there are indices that can be used to speed up the loop. |
| 5527 | ** |
| 5528 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 5529 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 5530 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 5531 | ** loops are evaluated and if false a jump is made around all subsequent |
| 5532 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 5533 | ** most loop) |
| 5534 | ** |
| 5535 | ** OUTER JOINS |
| 5536 | ** |
| 5537 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 5538 | ** |
| 5539 | ** foreach row1 in t1 do |
| 5540 | ** flag = 0 |
| 5541 | ** foreach row2 in t2 do |
| 5542 | ** start: |
| 5543 | ** ... |
| 5544 | ** flag = 1 |
| 5545 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5546 | ** if flag==0 then |
| 5547 | ** move the row2 cursor to a null row |
| 5548 | ** goto start |
| 5549 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5550 | ** end |
| 5551 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5552 | ** ORDER BY CLAUSE PROCESSING |
| 5553 | ** |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5554 | ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5555 | ** 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] | 5556 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5557 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5558 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5559 | Parse *pParse, /* The parser context */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5560 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5561 | Expr *pWhere, /* The WHERE clause */ |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5562 | ExprList *pOrderBy, /* An ORDER BY clause, or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5563 | ExprList *pResultSet, /* Result set of the query */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 5564 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 5565 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5566 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5567 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5568 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5569 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 5570 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5571 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5572 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5573 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5574 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5575 | WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5576 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5577 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5578 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5579 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5580 | |
| 5581 | /* Variable initialization */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5582 | db = pParse->db; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5583 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5584 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5585 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5586 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 5587 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 5588 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 5589 | wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 5590 | } |
| 5591 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5592 | /* 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] | 5593 | ** bits in a Bitmask |
| 5594 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5595 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5596 | if( pTabList->nSrc>BMS ){ |
| 5597 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5598 | return 0; |
| 5599 | } |
| 5600 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5601 | /* This function normally generates a nested loop for all tables in |
| 5602 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 5603 | ** only generate code for the first table in pTabList and assume that |
| 5604 | ** any cursors associated with subsequent tables are uninitialized. |
| 5605 | */ |
| 5606 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 5607 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5608 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5609 | ** return value. A single allocation is used to store the WhereInfo |
| 5610 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 5611 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 5612 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 5613 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5614 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5615 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5616 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5617 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5618 | sqlite3DbFree(db, pWInfo); |
| 5619 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5620 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5621 | } |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5622 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5623 | pWInfo->pParse = pParse; |
| 5624 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5625 | pWInfo->pOrderBy = pOrderBy; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5626 | pWInfo->pResultSet = pResultSet; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5627 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5628 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5629 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5630 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5631 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5632 | sWLB.pWC = &pWInfo->sWC; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5633 | sWLB.pNew = (WhereLoop*)&pWInfo->a[nTabList]; |
| 5634 | whereLoopInit(sWLB.pNew); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5635 | #ifdef SQLITE_DEBUG |
| 5636 | sWLB.pNew->cId = '*'; |
| 5637 | #endif |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5638 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5639 | /* Split the WHERE clause into separate subexpressions where each |
| 5640 | ** subexpression is separated by an AND operator. |
| 5641 | */ |
| 5642 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5643 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5644 | sqlite3ExprCodeConstants(pParse, pWhere); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5645 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5646 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5647 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 5648 | ** expression and either jump over all of the code or fall thru. |
| 5649 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5650 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 5651 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 5652 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5653 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5654 | |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5655 | /* Special case: No FROM clause |
| 5656 | */ |
| 5657 | if( nTabList==0 ){ |
| 5658 | if( pOrderBy ) pWInfo->bOBSat = 1; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5659 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5660 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5661 | } |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5662 | } |
| 5663 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5664 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 5665 | ** |
| 5666 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 5667 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 5668 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 5669 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 5670 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 5671 | ** of the join. Subtracting one from the right table bitmask gives a |
| 5672 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 5673 | ** 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] | 5674 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5675 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 5676 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 5677 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 5678 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5679 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5680 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5681 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5682 | } |
| 5683 | #ifndef NDEBUG |
| 5684 | { |
| 5685 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5686 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5687 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5688 | assert( (m-1)==toTheLeft ); |
| 5689 | toTheLeft |= m; |
| 5690 | } |
| 5691 | } |
| 5692 | #endif |
| 5693 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5694 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 5695 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 5696 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 5697 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5698 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5699 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5700 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5701 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 5702 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5703 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5704 | /* If the ORDER BY (or GROUP BY) clause contains references to general |
| 5705 | ** expressions, then we won't be able to satisfy it using indices, so |
| 5706 | ** go ahead and disable it now. |
| 5707 | */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5708 | if( pOrderBy && (wctrlFlags & WHERE_WANT_DISTINCT)!=0 ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5709 | for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 5710 | Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr); |
| 5711 | if( pExpr->op!=TK_COLUMN ){ |
| 5712 | pWInfo->pOrderBy = pOrderBy = 0; |
| 5713 | break; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5714 | }else if( pExpr->iColumn<0 ){ |
| 5715 | break; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5716 | } |
| 5717 | } |
| 5718 | } |
| 5719 | |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5720 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5721 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ |
| 5722 | /* The DISTINCT marking is pointless. Ignore it. */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5723 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5724 | }else if( pOrderBy==0 ){ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5725 | /* Try to ORDER BY the result set to make distinct processing easier */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5726 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 5727 | pWInfo->pOrderBy = pResultSet; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5728 | } |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5731 | /* Construct the WhereLoop objects */ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5732 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5733 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5734 | rc = whereLoopAddAll(&sWLB); |
| 5735 | if( rc ) goto whereBeginError; |
| 5736 | |
| 5737 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5738 | #ifdef WHERETRACE_ENABLED |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5739 | if( sqlite3WhereTrace ){ |
| 5740 | WhereLoop *p; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5741 | int i; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5742 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 5743 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5744 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 5745 | p->cId = zLabel[i%sizeof(zLabel)]; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5746 | whereLoopPrint(p, pTabList); |
| 5747 | } |
| 5748 | } |
| 5749 | #endif |
| 5750 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5751 | wherePathSolver(pWInfo, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5752 | if( db->mallocFailed ) goto whereBeginError; |
| 5753 | if( pWInfo->pOrderBy ){ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5754 | wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5755 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5756 | } |
| 5757 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5758 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 5759 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5760 | } |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5761 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5762 | goto whereBeginError; |
| 5763 | } |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5764 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5765 | if( sqlite3WhereTrace ){ |
| 5766 | int ii; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5767 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 5768 | if( pWInfo->bOBSat ){ |
| 5769 | sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5770 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5771 | switch( pWInfo->eDistinct ){ |
| 5772 | case WHERE_DISTINCT_UNIQUE: { |
| 5773 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 5774 | break; |
| 5775 | } |
| 5776 | case WHERE_DISTINCT_ORDERED: { |
| 5777 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 5778 | break; |
| 5779 | } |
| 5780 | case WHERE_DISTINCT_UNORDERED: { |
| 5781 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 5782 | break; |
| 5783 | } |
| 5784 | } |
| 5785 | sqlite3DebugPrintf("\n"); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5786 | for(ii=0; ii<pWInfo->nLevel; ii++){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5787 | whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5788 | } |
| 5789 | } |
| 5790 | #endif |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5791 | /* Attempt to omit tables from the join that do not effect the result */ |
drh | 1031bd9 | 2013-06-22 15:44:26 +0000 | [diff] [blame^] | 5792 | if( pWInfo->nLevel>=2 |
| 5793 | && pResultSet!=0 |
| 5794 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 5795 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5796 | Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet); |
| 5797 | if( pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, pOrderBy); |
| 5798 | while( pWInfo->nLevel>=2 ){ |
| 5799 | pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5800 | if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break; |
| 5801 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 5802 | && (pLoop->wsFlags & WHERE_ONEROW)==0 |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5803 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5804 | break; |
| 5805 | } |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 5806 | if( (tabUsed & pLoop->maskSelf)!=0 ) break; |
| 5807 | WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 5808 | pWInfo->nLevel--; |
| 5809 | nTabList--; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5810 | } |
| 5811 | } |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5812 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 5813 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5814 | |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5815 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 5816 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 5817 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 5818 | ** the statement to update a single row. |
| 5819 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 5820 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5821 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 5822 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5823 | pWInfo->okOnePass = 1; |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 5824 | pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5825 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5826 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5827 | /* Open all tables in the pTabList and any indices selected for |
| 5828 | ** searching those tables. |
| 5829 | */ |
| 5830 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5831 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5832 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5833 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5834 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5835 | struct SrcList_item *pTabItem; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5836 | WhereLoop *pLoop; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5837 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5838 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5839 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5840 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5841 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 5842 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 5843 | /* Do nothing */ |
| 5844 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5845 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5846 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5847 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 5848 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5849 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 5850 | }else if( IsVirtual(pTab) ){ |
| 5851 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5852 | }else |
| 5853 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5854 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5855 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5856 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 5857 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5858 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); |
| 5859 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 5860 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5861 | Bitmask b = pTabItem->colUsed; |
| 5862 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5863 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5864 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 5865 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5866 | assert( n<=pTab->nCol ); |
| 5867 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5868 | }else{ |
| 5869 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5870 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5871 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5872 | if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5873 | constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel); |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5874 | }else |
| 5875 | #endif |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 5876 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5877 | Index *pIx = pLoop->u.btree.pIndex; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5878 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5879 | /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */ |
| 5880 | int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5881 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 5882 | assert( iIndexCur>=0 ); |
| 5883 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5884 | (char*)pKey, P4_KEYINFO_HANDOFF); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 5885 | VdbeComment((v, "%s", pIx->zName)); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5886 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5887 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5888 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5889 | } |
| 5890 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 5891 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5892 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5893 | /* Generate the code to do the search. Each iteration of the for |
| 5894 | ** loop below generates code for a single nested loop of the VM |
| 5895 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5896 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5897 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5898 | for(ii=0; ii<nTabList; ii++){ |
| 5899 | pLevel = &pWInfo->a[ii]; |
| 5900 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5901 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 5902 | pWInfo->iContinue = pLevel->addrCont; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5903 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 5904 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 5905 | /* Done. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5906 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5907 | |
| 5908 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5909 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5910 | if( pWInfo ){ |
| 5911 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 5912 | whereInfoFree(db, pWInfo); |
| 5913 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5914 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
| 5917 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5918 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5919 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5920 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5921 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5922 | Parse *pParse = pWInfo->pParse; |
| 5923 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5924 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5925 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5926 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 5927 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5928 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5929 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5930 | /* Generate loop termination code. |
| 5931 | */ |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 5932 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5933 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5934 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5935 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5936 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5937 | if( pLevel->op!=OP_Noop ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5938 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 5939 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5940 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5941 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 5942 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5943 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5944 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5945 | 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] | 5946 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 5947 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5948 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5949 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5950 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 5951 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5952 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5953 | if( pLevel->iLeftJoin ){ |
| 5954 | int addr; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5955 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5956 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 5957 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 5958 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 5959 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 5960 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 5961 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5962 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 5963 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 5964 | if( pLevel->op==OP_Return ){ |
| 5965 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 5966 | }else{ |
| 5967 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 5968 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 5969 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5970 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5971 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5972 | |
| 5973 | /* The "break" point is here, just past the end of the outer loop. |
| 5974 | ** Set it. |
| 5975 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5976 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5977 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5978 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5979 | */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 5980 | assert( pWInfo->nLevel<=pTabList->nSrc ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5981 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 5982 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5983 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5984 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 5985 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5986 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5987 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 5988 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5989 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5990 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5991 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5992 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5993 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 5994 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5995 | if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_TEMP_INDEX))==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5996 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 5997 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5998 | } |
| 5999 | |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 6000 | /* If this scan uses an index, make VDBE code substitutions to read data |
| 6001 | ** from the index instead of from the table where possible. In some cases |
| 6002 | ** this optimization prevents the table from ever being read, which can |
| 6003 | ** yield a significant performance boost. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6004 | ** |
| 6005 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 6006 | ** sqlite3WhereEnd will have created code that references the table |
| 6007 | ** directly. This loop scans all that code looking for opcodes |
| 6008 | ** that reference the table and converts them into opcodes that |
| 6009 | ** reference the index. |
| 6010 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6011 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 6012 | pIdx = pLoop->u.btree.pIndex; |
| 6013 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 6014 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 6015 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6016 | if( pIdx && !db->mallocFailed ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 6017 | int k, j, last; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6018 | VdbeOp *pOp; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6019 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6020 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 6021 | last = sqlite3VdbeCurrentAddr(v); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 6022 | for(k=pWInfo->iTop; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6023 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 6024 | if( pOp->opcode==OP_Column ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6025 | for(j=0; j<pIdx->nColumn; j++){ |
| 6026 | if( pOp->p2==pIdx->aiColumn[j] ){ |
| 6027 | pOp->p2 = j; |
danielk1977 | 21de2e7 | 2007-11-29 17:43:27 +0000 | [diff] [blame] | 6028 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6029 | break; |
| 6030 | } |
| 6031 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6032 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6033 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6034 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6035 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6036 | } |
| 6037 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6038 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6039 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6040 | |
| 6041 | /* Final cleanup |
| 6042 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 6043 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6044 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6045 | return; |
| 6046 | } |