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)) |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 30 | # define WHERETRACE(X) if(sqlite3WhereTrace) 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 | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 33 | # define WHERETRACE(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 | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 48 | typedef float WhereCost; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 49 | |
| 50 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 51 | ** For each nested loop in a WHERE clause implementation, the WhereInfo |
| 52 | ** structure contains a single instance of this structure. This structure |
| 53 | ** is intended to be private to the where.c module and should not be |
| 54 | ** access or modified by other modules. |
| 55 | ** |
| 56 | ** The pIdxInfo field is used to help pick the best index on a |
| 57 | ** virtual table. The pIdxInfo pointer contains indexing |
| 58 | ** information for the i-th table in the FROM clause before reordering. |
| 59 | ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c. |
| 60 | ** All other information in the i-th WhereLevel object for the i-th table |
| 61 | ** after FROM clause ordering. |
| 62 | */ |
| 63 | struct WhereLevel { |
| 64 | int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 65 | int iTabCur; /* The VDBE cursor used to access the table */ |
| 66 | int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 67 | int addrBrk; /* Jump here to break out of the loop */ |
| 68 | int addrNxt; /* Jump here to start the next IN combination */ |
| 69 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 70 | int addrFirst; /* First instruction of interior of the loop */ |
| 71 | u8 iFrom; /* FIXME: Which entry in the FROM clause */ |
| 72 | u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ |
| 73 | int p1, p2; /* Operands of the opcode used to ends the loop */ |
| 74 | union { /* Information that depends on plan.wsFlags */ |
| 75 | struct { |
| 76 | int nIn; /* Number of entries in aInLoop[] */ |
| 77 | struct InLoop { |
| 78 | int iCur; /* The VDBE cursor used by this IN operator */ |
| 79 | int addrInTop; /* Top of the IN loop */ |
| 80 | u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 81 | } *aInLoop; /* Information about each nested IN operator */ |
| 82 | } in; /* Used when plan.wsFlags&WHERE_IN_ABLE */ |
| 83 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 84 | } u; |
| 85 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 86 | }; |
| 87 | |
| 88 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 89 | ** Each instance of this object represents a way of evaluating one |
| 90 | ** term of a join. The WhereClause object holds a table of these |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 91 | ** objects using (maskSelf,prereq,) as the primary key. Note that the |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 92 | ** same join term might have multiple associated WhereLoop objects. |
| 93 | */ |
| 94 | struct WhereLoop { |
| 95 | Bitmask prereq; /* Bitmask of other loops that must run first */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 96 | Bitmask maskSelf; /* Bitmask identifying table iTab */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 97 | #ifdef SQLITE_DEBUG |
| 98 | char cId; /* Symbolic ID of this loop for debugging use */ |
| 99 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 100 | u8 iTab; /* Position in FROM clause of table for this loop */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 101 | u8 iSortIdx; /* Sorting index number. 0==None */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 102 | WhereCost rSetup; /* One-time setup cost (ex: create transient index) */ |
| 103 | WhereCost rRun; /* Cost of running each loop */ |
| 104 | WhereCost nOut; /* Estimated number of output rows */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 105 | union { |
| 106 | struct { /* Information for internal btree tables */ |
| 107 | int nEq; /* Number of equality constraints */ |
| 108 | Index *pIndex; /* Index used, or NULL */ |
| 109 | } btree; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 110 | struct { /* Information for virtual tables */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 111 | int idxNum; /* Index number */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 112 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 113 | u8 isOrdered; /* True if satisfies ORDER BY */ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 114 | u16 omitMask; /* Terms that may be omitted */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 115 | char *idxStr; /* Index identifier string */ |
| 116 | } vtab; |
| 117 | } u; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 118 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 119 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 120 | /**** whereLoopXfer() copies fields above ***********************/ |
| 121 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 122 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 123 | WhereTerm **aLTerm; /* WhereTerms used */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 124 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 125 | WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 128 | /* Forward declaration of methods */ |
| 129 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 130 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 131 | /* |
| 132 | ** Each instance of this object holds a sequence of WhereLoop objects |
| 133 | ** that implement some or all of the entire query plan. |
| 134 | */ |
| 135 | struct WherePath { |
| 136 | Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 137 | Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 138 | WhereCost nRow; /* Estimated number of rows generated by this path */ |
| 139 | WhereCost rCost; /* Total cost of this path */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 140 | u8 isOrdered; /* True if this path satisfies ORDER BY */ |
| 141 | u8 isOrderedValid; /* True if the isOrdered field is valid */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 142 | WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 143 | }; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 144 | |
| 145 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 146 | ** The query generator uses an array of instances of this structure to |
| 147 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 148 | ** clause subexpression is separated from the others by AND operators, |
| 149 | ** usually, or sometimes subexpressions separated by OR. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 150 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 151 | ** All WhereTerms are collected into a single WhereClause structure. |
| 152 | ** The following identity holds: |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 153 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 154 | ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 155 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 156 | ** When a term is of the form: |
| 157 | ** |
| 158 | ** X <op> <expr> |
| 159 | ** |
| 160 | ** where X is a column name and <op> is one of certain operators, |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 161 | ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the |
| 162 | ** cursor number and column number for X. WhereTerm.eOperator records |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 163 | ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 164 | ** use of a bitmask encoding for the operator allows us to search |
| 165 | ** quickly for terms that match any of several different operators. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 166 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 167 | ** A WhereTerm might also be two or more subterms connected by OR: |
| 168 | ** |
| 169 | ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... |
| 170 | ** |
| 171 | ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR |
| 172 | ** and the WhereTerm.u.pOrInfo field points to auxiliary information that |
| 173 | ** is collected about the |
| 174 | ** |
| 175 | ** If a term in the WHERE clause does not match either of the two previous |
| 176 | ** categories, then eOperator==0. The WhereTerm.pExpr field is still set |
| 177 | ** to the original subexpression content and wtFlags is set up appropriately |
| 178 | ** but no other fields in the WhereTerm object are meaningful. |
| 179 | ** |
| 180 | ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 181 | ** but they do so indirectly. A single WhereMaskSet structure translates |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 182 | ** cursor number into bits and the translated bit is stored in the prereq |
| 183 | ** fields. The translation is used in order to maximize the number of |
| 184 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 185 | ** spread out over the non-negative integers. For example, the cursor |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 186 | ** 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] | 187 | ** translates these sparse cursor numbers into consecutive integers |
| 188 | ** beginning with 0 in order to make the best possible use of the available |
| 189 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 190 | ** would be mapped into integers 0 through 7. |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 191 | ** |
| 192 | ** The number of terms in a join is limited by the number of bits |
| 193 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 194 | ** is only able to process joins with 64 or fewer tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 195 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 196 | struct WhereTerm { |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 197 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 198 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 199 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 200 | union { |
| 201 | int leftColumn; /* Column number of X in "X <op> <expr>" */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 202 | WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 203 | WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 204 | } u; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 205 | u16 eOperator; /* A WO_xx value describing <op> */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 206 | u8 wtFlags; /* TERM_xxx bit flags. See below */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 207 | u8 nChild; /* Number of children that must disable us */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 208 | WhereClause *pWC; /* The clause this term is part of */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 209 | Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 210 | Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | /* |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 214 | ** Allowed values of WhereTerm.wtFlags |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 215 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 216 | #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 217 | #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
| 218 | #define TERM_CODED 0x04 /* This term is already coded */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 219 | #define TERM_COPIED 0x08 /* Has a child */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 220 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 221 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 222 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 223 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 224 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 225 | #else |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 226 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 227 | #endif |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 228 | |
| 229 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 230 | ** An instance of the WhereScan object is used as an iterator for locating |
| 231 | ** terms in the WHERE clause that are useful to the query planner. |
| 232 | */ |
| 233 | struct WhereScan { |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 234 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 235 | WhereClause *pWC; /* WhereClause currently being scanned */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 236 | char *zCollName; /* Required collating sequence, if not NULL */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 237 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 238 | unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 239 | unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 240 | u32 opMask; /* Acceptable operators */ |
| 241 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 242 | int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ |
| 243 | }; |
| 244 | |
| 245 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 246 | ** An instance of the following structure holds all information about a |
| 247 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 248 | ** |
| 249 | ** Explanation of pOuter: For a WHERE clause of the form |
| 250 | ** |
| 251 | ** a AND ((b AND c) OR (d AND e)) AND f |
| 252 | ** |
| 253 | ** There are separate WhereClause objects for the whole clause and for |
| 254 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 255 | ** subclauses points to the WhereClause object for the whole clause. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 256 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 257 | struct WhereClause { |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 258 | WhereInfo *pWInfo; /* WHERE clause processing context */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 259 | WhereClause *pOuter; /* Outer conjunction */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 260 | u8 op; /* Split operator. TK_AND or TK_OR */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 261 | int nTerm; /* Number of terms */ |
| 262 | int nSlot; /* Number of entries in a[] */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 263 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 264 | #if defined(SQLITE_SMALL_STACK) |
| 265 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| 266 | #else |
| 267 | WhereTerm aStatic[8]; /* Initial static space for a[] */ |
| 268 | #endif |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 272 | ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to |
| 273 | ** a dynamically allocated instance of the following structure. |
| 274 | */ |
| 275 | struct WhereOrInfo { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 276 | WhereClause wc; /* Decomposition into subterms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 277 | Bitmask indexable; /* Bitmask of all indexable tables in the clause */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | /* |
| 281 | ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to |
| 282 | ** a dynamically allocated instance of the following structure. |
| 283 | */ |
| 284 | struct WhereAndInfo { |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 285 | WhereClause wc; /* The subexpression broken out */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 289 | ** An instance of the following structure keeps track of a mapping |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 290 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 291 | ** |
| 292 | ** The VDBE cursor numbers are small integers contained in |
| 293 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 294 | ** clause, the cursor numbers might not begin with 0 and they might |
| 295 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 296 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 297 | ** from the sparse cursor numbers into consecutive integers beginning |
| 298 | ** with 0. |
| 299 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 300 | ** 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] | 301 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 302 | ** |
| 303 | ** For example, if the WHERE clause expression used these VDBE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 304 | ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 305 | ** would map those cursor numbers into bits 0 through 5. |
| 306 | ** |
| 307 | ** Note that the mapping is not necessarily ordered. In the example |
| 308 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 309 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 310 | ** does not really matter. What is important is that sparse cursor |
| 311 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 312 | ** no gaps. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 313 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 314 | struct WhereMaskSet { |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 315 | int n; /* Number of assigned cursor values */ |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 316 | int ix[BMS]; /* Cursor assigned to each bit */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 319 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 320 | ** This object is a factory for WhereLoop objects for a particular query. |
| 321 | */ |
| 322 | struct WhereLoopBuilder { |
| 323 | WhereInfo *pWInfo; /* Information about this WHERE */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 324 | WhereClause *pWC; /* WHERE clause terms */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 325 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 326 | WhereLoop *pNew; /* Template WhereLoop */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 327 | WhereLoop *pBest; /* If non-NULL, store single best loop here */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | /* |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 331 | ** The WHERE clause processing routine has two halves. The |
| 332 | ** first part does the start of the WHERE loop and the second |
| 333 | ** half does the tail of the WHERE loop. An instance of |
| 334 | ** this structure is returned by the first half and passed |
| 335 | ** into the second half to give some continuity. |
| 336 | */ |
| 337 | struct WhereInfo { |
| 338 | Parse *pParse; /* Parsing and code generating context */ |
| 339 | SrcList *pTabList; /* List of tables in the join */ |
| 340 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
| 341 | ExprList *pDistinct; /* DISTINCT ON values, or NULL */ |
| 342 | Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
| 343 | u16 nOBSat; /* Number of ORDER BY terms satisfied by indices */ |
| 344 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
| 345 | u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ |
| 346 | u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
| 347 | u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ |
| 348 | int iTop; /* The very beginning of the WHERE loop */ |
| 349 | int iContinue; /* Jump here to continue with next record */ |
| 350 | int iBreak; /* Jump here to break out of the loop */ |
| 351 | int nLevel; /* Number of nested loop */ |
| 352 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 353 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
| 354 | WhereLoop *pLoops; /* List of all WhereLoop objects */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 355 | WhereCost savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 356 | WhereCost nRowOut; /* Estimated number of output rows */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 357 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 358 | }; |
| 359 | |
| 360 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 361 | ** Bitmasks for the operators that indices are able to exploit. An |
| 362 | ** OR-ed combination of these values can be used when searching for |
| 363 | ** terms in the where clause. |
| 364 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 365 | #define WO_IN 0x001 |
| 366 | #define WO_EQ 0x002 |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 367 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 368 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 369 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 370 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 371 | #define WO_MATCH 0x040 |
| 372 | #define WO_ISNULL 0x080 |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 373 | #define WO_OR 0x100 /* Two or more OR-connected terms */ |
| 374 | #define WO_AND 0x200 /* Two or more AND-connected terms */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 375 | #define WO_EQUIV 0x400 /* Of the form A==B, both columns */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 376 | #define WO_NOOP 0x800 /* This term does not restrict search space */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 377 | |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 378 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 379 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 380 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 381 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 382 | ** Value for wsFlags returned by bestIndex() and stored in |
| 383 | ** WhereLevel.wsFlags. These flags determine which search |
| 384 | ** strategies are appropriate. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 385 | */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 386 | #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR or x IN (...) or x IS NULL */ |
| 387 | #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 388 | #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 389 | #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 390 | #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 391 | #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 392 | #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 393 | #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 394 | #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 395 | #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 396 | #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 397 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 398 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 399 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 400 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 401 | #define WHERE_TEMP_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 402 | #define WHERE_COVER_SCAN 0x00008000 /* Full scan of a covering index */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 403 | |
| 404 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 405 | ** Return the estimated number of output rows from a WHERE clause |
| 406 | */ |
| 407 | double sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 408 | return (double)pWInfo->nRowOut; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
| 412 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 413 | ** WHERE clause returns outputs for DISTINCT processing. |
| 414 | */ |
| 415 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 416 | return pWInfo->eDistinct; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 421 | ** Return FALSE if the output needs to be sorted. |
| 422 | */ |
| 423 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
| 424 | return pWInfo->nOBSat>0; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | ** Return the VDBE address or label to jump to in order to continue |
| 429 | ** immediately with the next row of a WHERE clause. |
| 430 | */ |
| 431 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 432 | return pWInfo->iContinue; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | ** Return the VDBE address or label to jump to in order to break |
| 437 | ** out of a WHERE loop. |
| 438 | */ |
| 439 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 440 | return pWInfo->iBreak; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 445 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 446 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
| 447 | */ |
| 448 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo){ |
| 449 | return pWInfo->okOnePass; |
| 450 | } |
| 451 | |
| 452 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 453 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 454 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 455 | static void whereClauseInit( |
| 456 | WhereClause *pWC, /* The WhereClause to be initialized */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 457 | WhereInfo *pWInfo /* The WHERE processing context */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 458 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 459 | pWC->pWInfo = pWInfo; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 460 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 461 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 462 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 463 | pWC->a = pWC->aStatic; |
| 464 | } |
| 465 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 466 | /* Forward reference */ |
| 467 | static void whereClauseClear(WhereClause*); |
| 468 | |
| 469 | /* |
| 470 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 471 | */ |
| 472 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 473 | whereClauseClear(&p->wc); |
| 474 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* |
| 478 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 479 | */ |
| 480 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 481 | whereClauseClear(&p->wc); |
| 482 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 483 | } |
| 484 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 485 | /* |
| 486 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 487 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 488 | */ |
| 489 | static void whereClauseClear(WhereClause *pWC){ |
| 490 | int i; |
| 491 | WhereTerm *a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 492 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 493 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 494 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 495 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 496 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 497 | if( a->wtFlags & TERM_ORINFO ){ |
| 498 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 499 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 500 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 501 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 502 | } |
| 503 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 504 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 509 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 510 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 511 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 512 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 513 | ** allocation error. The memory allocation failure will be recorded in |
| 514 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 515 | ** |
| 516 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 517 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 518 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 519 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 520 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 521 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 522 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 523 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 524 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 525 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 526 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 527 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 528 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 529 | int idx; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 530 | testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 531 | if( pWC->nTerm>=pWC->nSlot ){ |
| 532 | WhereTerm *pOld = pWC->a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 533 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 534 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 535 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 536 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 537 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 538 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 539 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 540 | return 0; |
| 541 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 542 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 543 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 544 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 545 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 546 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 547 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 548 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 549 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 550 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 551 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 552 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 553 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 554 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 555 | |
| 556 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 557 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 558 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 559 | ** operator specified in the op parameter. The WhereClause structure |
| 560 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 561 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 562 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 563 | ** \________/ \_______________/ \________________/ |
| 564 | ** slot[0] slot[1] slot[2] |
| 565 | ** |
| 566 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 567 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 568 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 569 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 570 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 571 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 572 | */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 573 | static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 574 | pWC->op = (u8)op; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 575 | if( pExpr==0 ) return; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 576 | if( pExpr->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 577 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 578 | }else{ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 579 | whereSplit(pWC, pExpr->pLeft, op); |
| 580 | whereSplit(pWC, pExpr->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 581 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 585 | ** Initialize an expression mask set (a WhereMaskSet object) |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 586 | */ |
| 587 | #define initMaskSet(P) memset(P, 0, sizeof(*P)) |
| 588 | |
| 589 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 590 | ** Return the bitmask for the given cursor number. Return 0 if |
| 591 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 592 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 593 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 594 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 595 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 596 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 597 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 598 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 599 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 600 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 605 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 606 | ** |
| 607 | ** There is one cursor per table in the FROM clause. The number of |
| 608 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 609 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 610 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 611 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 612 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 613 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 614 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 618 | ** This routine walks (recursively) an expression tree and generates |
| 619 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 620 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 621 | ** |
| 622 | ** In order for this routine to work, the calling function must have |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 623 | ** previously invoked sqlite3ResolveExprNames() on the expression. See |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 624 | ** the header comment on that routine for additional information. |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 625 | ** The sqlite3ResolveExprNames() routines looks for column names and |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 626 | ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 627 | ** the VDBE cursor number of the table. This routine just has to |
| 628 | ** translate the cursor numbers into bitmask values and OR all |
| 629 | ** the bitmasks together. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 630 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 631 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 632 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 633 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 634 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 635 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 636 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 637 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 638 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 639 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 640 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 641 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 642 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 643 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 644 | }else{ |
| 645 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 646 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 647 | return mask; |
| 648 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 649 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 650 | int i; |
| 651 | Bitmask mask = 0; |
| 652 | if( pList ){ |
| 653 | for(i=0; i<pList->nExpr; i++){ |
| 654 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 657 | return mask; |
| 658 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 659 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 660 | Bitmask mask = 0; |
| 661 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 662 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 663 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 664 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 665 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 666 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 667 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 668 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 669 | int i; |
| 670 | for(i=0; i<pSrc->nSrc; i++){ |
| 671 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 672 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 673 | } |
| 674 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 675 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 676 | } |
| 677 | return mask; |
| 678 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 679 | |
| 680 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 681 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 682 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 683 | ** "=", "<", ">", "<=", ">=", and "IN". |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 684 | ** |
| 685 | ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be |
| 686 | ** of one of the following forms: column = expression column > expression |
| 687 | ** column >= expression column < expression column <= expression |
| 688 | ** expression = column expression > column expression >= column |
| 689 | ** expression < column expression <= column column IN |
| 690 | ** (expression-list) column IN (subquery) column IS NULL |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 691 | */ |
| 692 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 693 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 694 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 695 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 696 | assert( TK_GE==TK_EQ+4 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 697 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 701 | ** Swap two objects of type TYPE. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 702 | */ |
| 703 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 704 | |
| 705 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 706 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 707 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 708 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 709 | ** If left/right precedence rules come into play when determining the |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 710 | ** collating |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 711 | ** side of the comparison, it remains associated with the same side after |
| 712 | ** the commutation. So "Y collate NOCASE op X" becomes |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 713 | ** "X op Y". This is because any collation sequence on |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 714 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 715 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 716 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 717 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 718 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 719 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 720 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 721 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 722 | if( expRight==expLeft ){ |
| 723 | /* Either X and Y both have COLLATE operator or neither do */ |
| 724 | if( expRight ){ |
| 725 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 726 | ** used by clearing the EP_Collate flag from Y. */ |
| 727 | pExpr->pRight->flags &= ~EP_Collate; |
| 728 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 729 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 730 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 731 | ** it to be searched first. */ |
| 732 | pExpr->pLeft->flags |= EP_Collate; |
| 733 | } |
| 734 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 735 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 736 | if( pExpr->op>=TK_GT ){ |
| 737 | assert( TK_LT==TK_GT+2 ); |
| 738 | assert( TK_GE==TK_LE+2 ); |
| 739 | assert( TK_GT>TK_EQ ); |
| 740 | assert( TK_GT<TK_LE ); |
| 741 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 742 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 743 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 747 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 748 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 749 | static u16 operatorMask(int op){ |
| 750 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 751 | assert( allowedOp(op) ); |
| 752 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 753 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 754 | }else if( op==TK_ISNULL ){ |
| 755 | c = WO_ISNULL; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 756 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 757 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 758 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 759 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 760 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 761 | assert( op!=TK_IN || c==WO_IN ); |
| 762 | assert( op!=TK_EQ || c==WO_EQ ); |
| 763 | assert( op!=TK_LT || c==WO_LT ); |
| 764 | assert( op!=TK_LE || c==WO_LE ); |
| 765 | assert( op!=TK_GT || c==WO_GT ); |
| 766 | assert( op!=TK_GE || c==WO_GE ); |
| 767 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 771 | ** Advance to the next WhereTerm that matches according to the criteria |
| 772 | ** established when the pScan object was initialized by whereScanInit(). |
| 773 | ** Return NULL if there are no more matching WhereTerms. |
| 774 | */ |
| 775 | WhereTerm *whereScanNext(WhereScan *pScan){ |
| 776 | int iCur; /* The cursor on the LHS of the term */ |
| 777 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 778 | Expr *pX; /* An expression being tested */ |
| 779 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 780 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame^] | 781 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 782 | |
| 783 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 784 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 785 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 786 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame^] | 787 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 788 | if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){ |
| 789 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 790 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 791 | ){ |
| 792 | int j; |
| 793 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 794 | assert( pX->op==TK_COLUMN ); |
| 795 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 796 | if( pScan->aEquiv[j]==pX->iTable |
| 797 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | if( j==pScan->nEquiv ){ |
| 802 | pScan->aEquiv[j] = pX->iTable; |
| 803 | pScan->aEquiv[j+1] = pX->iColumn; |
| 804 | pScan->nEquiv += 2; |
| 805 | } |
| 806 | } |
| 807 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 808 | /* Verify the affinity and collating sequence match */ |
| 809 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 810 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 811 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 812 | pX = pTerm->pExpr; |
| 813 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 814 | continue; |
| 815 | } |
| 816 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 817 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 818 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 819 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 820 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 821 | continue; |
| 822 | } |
| 823 | } |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 824 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 825 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 826 | && pX->iTable==pScan->aEquiv[0] |
| 827 | && pX->iColumn==pScan->aEquiv[1] |
| 828 | ){ |
| 829 | continue; |
| 830 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame^] | 831 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 832 | return pTerm; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | pWC = pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame^] | 837 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 838 | } |
| 839 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame^] | 840 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 841 | pScan->iEquiv += 2; |
| 842 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 848 | ** first match. Return NULL if there are no matches. |
| 849 | ** |
| 850 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 851 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 852 | ** iCur. The <op> must be one of the operators described by opMask. |
| 853 | ** |
| 854 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 855 | ** index pIdx. |
| 856 | */ |
| 857 | WhereTerm *whereScanInit( |
| 858 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 859 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 860 | int iCur, /* Cursor to scan for */ |
| 861 | int iColumn, /* Column to scan for */ |
| 862 | u32 opMask, /* Operator(s) to scan for */ |
| 863 | Index *pIdx /* Must be compatible with this index */ |
| 864 | ){ |
| 865 | int j; |
| 866 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 867 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 868 | pScan->pOrigWC = pWC; |
| 869 | pScan->pWC = pWC; |
| 870 | if( pIdx && iColumn>=0 ){ |
| 871 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 872 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 873 | if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 874 | } |
| 875 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 876 | }else{ |
| 877 | pScan->idxaff = 0; |
| 878 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 879 | } |
| 880 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 881 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 882 | pScan->aEquiv[0] = iCur; |
| 883 | pScan->aEquiv[1] = iColumn; |
| 884 | pScan->nEquiv = 2; |
| 885 | pScan->iEquiv = 2; |
| 886 | return whereScanNext(pScan); |
| 887 | } |
| 888 | |
| 889 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 890 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 891 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 892 | ** the WO_xx operator codes specified by the op parameter. |
| 893 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 894 | ** |
| 895 | ** The term returned might by Y=<expr> if there is another constraint in |
| 896 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 897 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 898 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 899 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 900 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 901 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 902 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 903 | ** and ... and A9=A10 and A10=<expr>. |
| 904 | ** |
| 905 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 906 | ** then try for the one with no dependencies on <expr> - in other words where |
| 907 | ** <expr> is a constant expression of some kind. Only return entries of |
| 908 | ** 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] | 909 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 910 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 911 | */ |
| 912 | static WhereTerm *findTerm( |
| 913 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 914 | int iCur, /* Cursor number of LHS */ |
| 915 | int iColumn, /* Column number of LHS */ |
| 916 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 917 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 918 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 919 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 920 | WhereTerm *pResult = 0; |
| 921 | WhereTerm *p; |
| 922 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 923 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 924 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 925 | while( p ){ |
| 926 | if( (p->prereqRight & notReady)==0 ){ |
| 927 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 928 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 929 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 930 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 931 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 932 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 933 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 934 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 935 | } |
| 936 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 937 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 938 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 939 | |
| 940 | /* |
| 941 | ** Call exprAnalyze on all terms in a WHERE clause. |
| 942 | ** |
| 943 | ** |
| 944 | */ |
| 945 | static void exprAnalyzeAll( |
| 946 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 947 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 948 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 949 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 950 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 951 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 955 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 956 | /* |
| 957 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 958 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 959 | ** so and false if not. |
| 960 | ** |
| 961 | ** In order for the operator to be optimizible, the RHS must be a string |
| 962 | ** literal that does not begin with a wildcard. |
| 963 | */ |
| 964 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 965 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 966 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 967 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 968 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 969 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 970 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 971 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 972 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 973 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 974 | int c; /* One character in z[] */ |
| 975 | int cnt; /* Number of non-wildcard prefix characters */ |
| 976 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 977 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 978 | sqlite3_value *pVal = 0; |
| 979 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 980 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 981 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 982 | return 0; |
| 983 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 984 | #ifdef SQLITE_EBCDIC |
| 985 | if( *pnoCase ) return 0; |
| 986 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 987 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 988 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 989 | if( pLeft->op!=TK_COLUMN |
| 990 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 991 | || IsVirtual(pLeft->pTab) |
| 992 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 993 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 994 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 995 | return 0; |
| 996 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 997 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 998 | |
| 999 | pRight = pList->a[0].pExpr; |
| 1000 | op = pRight->op; |
| 1001 | if( op==TK_REGISTER ){ |
| 1002 | op = pRight->op2; |
| 1003 | } |
| 1004 | if( op==TK_VARIABLE ){ |
| 1005 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 1006 | int iCol = pRight->iColumn; |
| 1007 | pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1008 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 1009 | z = (char *)sqlite3_value_text(pVal); |
| 1010 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1011 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1012 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 1013 | }else if( op==TK_STRING ){ |
| 1014 | z = pRight->u.zToken; |
| 1015 | } |
| 1016 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1017 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1018 | 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] | 1019 | cnt++; |
| 1020 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1021 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1022 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1023 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1024 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 1025 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 1026 | *ppPrefix = pPrefix; |
| 1027 | if( op==TK_VARIABLE ){ |
| 1028 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1029 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1030 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 1031 | /* If the rhs of the LIKE expression is a variable, and the current |
| 1032 | ** value of the variable means there is no need to invoke the LIKE |
| 1033 | ** function, then no OP_Variable will be added to the program. |
| 1034 | ** This causes problems for the sqlite3_bind_parameter_name() |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1035 | ** API. To workaround them, add a dummy OP_Variable here. |
| 1036 | */ |
| 1037 | int r1 = sqlite3GetTempReg(pParse); |
| 1038 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1039 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1040 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | }else{ |
| 1044 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1045 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1046 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1047 | |
| 1048 | sqlite3ValueFree(pVal); |
| 1049 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1050 | } |
| 1051 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 1052 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1053 | |
| 1054 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1055 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1056 | ** Check to see if the given expression is of the form |
| 1057 | ** |
| 1058 | ** column MATCH expr |
| 1059 | ** |
| 1060 | ** If it is then return TRUE. If not, return FALSE. |
| 1061 | */ |
| 1062 | static int isMatchOfColumn( |
| 1063 | Expr *pExpr /* Test this expression */ |
| 1064 | ){ |
| 1065 | ExprList *pList; |
| 1066 | |
| 1067 | if( pExpr->op!=TK_FUNCTION ){ |
| 1068 | return 0; |
| 1069 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1070 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1071 | return 0; |
| 1072 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1073 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1074 | if( pList->nExpr!=2 ){ |
| 1075 | return 0; |
| 1076 | } |
| 1077 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 1078 | return 0; |
| 1079 | } |
| 1080 | return 1; |
| 1081 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1082 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1083 | |
| 1084 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1085 | ** If the pBase expression originated in the ON or USING clause of |
| 1086 | ** a join, then transfer the appropriate markings over to derived. |
| 1087 | */ |
| 1088 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
| 1089 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 1090 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 1091 | } |
| 1092 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1093 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 1094 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1095 | ** Analyze a term that consists of two or more OR-connected |
| 1096 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1097 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1098 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 1099 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1100 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1101 | ** This routine analyzes terms such as the middle term in the above example. |
| 1102 | ** A WhereOrTerm object is computed and attached to the term under |
| 1103 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1104 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1105 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 1106 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1107 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1108 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 1109 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1110 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1111 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1112 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 1113 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 1114 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 1115 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 1116 | ** (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] | 1117 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1118 | ** CASE 1: |
| 1119 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1120 | ** 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] | 1121 | ** a single table T (as shown in example B above) then create a new virtual |
| 1122 | ** term that is an equivalent IN expression. In other words, if the term |
| 1123 | ** being analyzed is: |
| 1124 | ** |
| 1125 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 1126 | ** |
| 1127 | ** then create a new virtual term like this: |
| 1128 | ** |
| 1129 | ** x IN (expr1,expr2,expr3) |
| 1130 | ** |
| 1131 | ** CASE 2: |
| 1132 | ** |
| 1133 | ** If all subterms are indexable by a single table T, then set |
| 1134 | ** |
| 1135 | ** WhereTerm.eOperator = WO_OR |
| 1136 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 1137 | ** |
| 1138 | ** A subterm is "indexable" if it is of the form |
| 1139 | ** "T.C <op> <expr>" where C is any column of table T and |
| 1140 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 1141 | ** A subterm is also indexable if it is an AND of two or more |
| 1142 | ** subsubterms at least one of which is indexable. Indexable AND |
| 1143 | ** subterms have their eOperator set to WO_AND and they have |
| 1144 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 1145 | ** |
| 1146 | ** From another point of view, "indexable" means that the subterm could |
| 1147 | ** potentially be used with an index if an appropriate index exists. |
| 1148 | ** This analysis does not consider whether or not the index exists; that |
| 1149 | ** is something the bestIndex() routine will determine. This analysis |
| 1150 | ** only looks at whether subterms appropriate for indexing exist. |
| 1151 | ** |
| 1152 | ** All examples A through E above all satisfy case 2. But if a term |
| 1153 | ** also statisfies case 1 (such as B) we know that the optimizer will |
| 1154 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 1155 | ** satisfied. |
| 1156 | ** |
| 1157 | ** It might be the case that multiple tables are indexable. For example, |
| 1158 | ** (E) above is indexable on tables P, Q, and R. |
| 1159 | ** |
| 1160 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 1161 | ** separate indices to find rowids for each subterm and composing |
| 1162 | ** the union of all rowids using a RowSet object. This is similar |
| 1163 | ** to "bitmap indices" in other database engines. |
| 1164 | ** |
| 1165 | ** OTHERWISE: |
| 1166 | ** |
| 1167 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 1168 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1169 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1170 | static void exprAnalyzeOrTerm( |
| 1171 | SrcList *pSrc, /* the FROM clause */ |
| 1172 | WhereClause *pWC, /* the complete WHERE clause */ |
| 1173 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 1174 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1175 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 1176 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1177 | sqlite3 *db = pParse->db; /* Database connection */ |
| 1178 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 1179 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1180 | int i; /* Loop counters */ |
| 1181 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 1182 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 1183 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 1184 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 1185 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1186 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1187 | /* |
| 1188 | ** Break the OR clause into its separate subterms. The subterms are |
| 1189 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 1190 | ** object that is attached to the original OR clause term. |
| 1191 | */ |
| 1192 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 1193 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 1194 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1195 | if( pOrInfo==0 ) return; |
| 1196 | pTerm->wtFlags |= TERM_ORINFO; |
| 1197 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1198 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1199 | whereSplit(pOrWc, pExpr, TK_OR); |
| 1200 | exprAnalyzeAll(pSrc, pOrWc); |
| 1201 | if( db->mallocFailed ) return; |
| 1202 | assert( pOrWc->nTerm>=2 ); |
| 1203 | |
| 1204 | /* |
| 1205 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 1206 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 1207 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1208 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1209 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 1210 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1211 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1212 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1213 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1214 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 1215 | if( pAndInfo ){ |
| 1216 | WhereClause *pAndWC; |
| 1217 | WhereTerm *pAndTerm; |
| 1218 | int j; |
| 1219 | Bitmask b = 0; |
| 1220 | pOrTerm->u.pAndInfo = pAndInfo; |
| 1221 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 1222 | pOrTerm->eOperator = WO_AND; |
| 1223 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1224 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1225 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 1226 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 1227 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 1228 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1229 | if( !db->mallocFailed ){ |
| 1230 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 1231 | assert( pAndTerm->pExpr ); |
| 1232 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1233 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1234 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | indexable &= b; |
| 1238 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1239 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 1240 | /* Skip this term for now. We revisit it when we process the |
| 1241 | ** corresponding TERM_VIRTUAL term */ |
| 1242 | }else{ |
| 1243 | Bitmask b; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1244 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1245 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 1246 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1247 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1248 | } |
| 1249 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1250 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1251 | chngToIN = 0; |
| 1252 | }else{ |
| 1253 | chngToIN &= b; |
| 1254 | } |
| 1255 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1256 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* |
| 1259 | ** Record the set of tables that satisfy case 2. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1260 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1261 | */ |
| 1262 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1263 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1264 | |
| 1265 | /* |
| 1266 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 1267 | ** we have to do some additional checking to see if case 1 really |
| 1268 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1269 | ** |
| 1270 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 1271 | ** that there is no possibility of transforming the OR clause into an |
| 1272 | ** IN operator because one or more terms in the OR clause contain |
| 1273 | ** something other than == on a column in the single table. The 1-bit |
| 1274 | ** case means that every term of the OR clause is of the form |
| 1275 | ** "table.column=expr" for some single table. The one bit that is set |
| 1276 | ** will correspond to the common table. We still need to check to make |
| 1277 | ** sure the same column is used on all terms. The 2-bit case is when |
| 1278 | ** the all terms are of the form "table1.column=table2.column". It |
| 1279 | ** might be possible to form an IN operator with either table1.column |
| 1280 | ** or table2.column as the LHS if either is common to every term of |
| 1281 | ** the OR clause. |
| 1282 | ** |
| 1283 | ** Note that terms of the form "table.column1=table.column2" (the |
| 1284 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1285 | */ |
| 1286 | if( chngToIN ){ |
| 1287 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 1288 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 1289 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1290 | int j = 0; /* Loop counter */ |
| 1291 | |
| 1292 | /* Search for a table and column that appears on one side or the |
| 1293 | ** other of the == operator in every subterm. That table and column |
| 1294 | ** will be recorded in iCursor and iColumn. There might not be any |
| 1295 | ** such table and column. Set okToChngToIN if an appropriate table |
| 1296 | ** and column is found but leave okToChngToIN false if not found. |
| 1297 | */ |
| 1298 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 1299 | pOrTerm = pOrWc->a; |
| 1300 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1301 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1302 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1303 | if( pOrTerm->leftCursor==iCursor ){ |
| 1304 | /* This is the 2-bit case and we are on the second iteration and |
| 1305 | ** current term is from the first iteration. So skip this term. */ |
| 1306 | assert( j==1 ); |
| 1307 | continue; |
| 1308 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1309 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1310 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 1311 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 1312 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 1313 | ** and use its inversion. */ |
| 1314 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 1315 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 1316 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 1317 | continue; |
| 1318 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1319 | iColumn = pOrTerm->u.leftColumn; |
| 1320 | iCursor = pOrTerm->leftCursor; |
| 1321 | break; |
| 1322 | } |
| 1323 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1324 | /* No candidate table+column was found. This can only occur |
| 1325 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1326 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1327 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1328 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1331 | testcase( j==1 ); |
| 1332 | |
| 1333 | /* We have found a candidate table and column. Check to see if that |
| 1334 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1335 | okToChngToIN = 1; |
| 1336 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1337 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1338 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1339 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1340 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1341 | okToChngToIN = 0; |
| 1342 | }else{ |
| 1343 | int affLeft, affRight; |
| 1344 | /* If the right-hand side is also a column, then the affinities |
| 1345 | ** of both right and left sides must be such that no type |
| 1346 | ** conversions are required on the right. (Ticket #2249) |
| 1347 | */ |
| 1348 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1349 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1350 | if( affRight!=0 && affRight!=affLeft ){ |
| 1351 | okToChngToIN = 0; |
| 1352 | }else{ |
| 1353 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1360 | ** case 1. In that case, construct a new virtual term that is |
| 1361 | ** pTerm converted into an IN operator. |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 1362 | ** |
| 1363 | ** EV: R-00211-15100 |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1364 | */ |
| 1365 | if( okToChngToIN ){ |
| 1366 | Expr *pDup; /* A transient duplicate expression */ |
| 1367 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1368 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1369 | Expr *pNew; /* The complete IN operator */ |
| 1370 | |
| 1371 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1372 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1373 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1374 | assert( pOrTerm->leftCursor==iCursor ); |
| 1375 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1376 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1377 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1378 | pLeft = pOrTerm->pExpr->pLeft; |
| 1379 | } |
| 1380 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1381 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1382 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1383 | if( pNew ){ |
| 1384 | int idxNew; |
| 1385 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1386 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1387 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1388 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1389 | testcase( idxNew==0 ); |
| 1390 | exprAnalyze(pSrc, pWC, idxNew); |
| 1391 | pTerm = &pWC->a[idxTerm]; |
| 1392 | pWC->a[idxNew].iParent = idxTerm; |
| 1393 | pTerm->nChild = 1; |
| 1394 | }else{ |
| 1395 | sqlite3ExprListDelete(db, pList); |
| 1396 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1397 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1398 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1399 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1400 | } |
| 1401 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1402 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1403 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1404 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1405 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1406 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1407 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1408 | ** |
| 1409 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1410 | ** to the standard form of "X <op> <expr>". |
| 1411 | ** |
| 1412 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1413 | ** columns, then the original expression is unchanged and a new virtual |
| 1414 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1415 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1416 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1417 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1418 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1419 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1420 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1421 | static void exprAnalyze( |
| 1422 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1423 | WhereClause *pWC, /* the WHERE clause */ |
| 1424 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1425 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1426 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1427 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1428 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1429 | Expr *pExpr; /* The expression to be analyzed */ |
| 1430 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1431 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1432 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1433 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1434 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1435 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1436 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1437 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1438 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1439 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1440 | if( db->mallocFailed ){ |
| 1441 | return; |
| 1442 | } |
| 1443 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1444 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1445 | pExpr = pTerm->pExpr; |
| 1446 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1447 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1448 | op = pExpr->op; |
| 1449 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1450 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1451 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1452 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1453 | }else{ |
| 1454 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1455 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1456 | }else if( op==TK_ISNULL ){ |
| 1457 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1458 | }else{ |
| 1459 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1460 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1461 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1462 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1463 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1464 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1465 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1466 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1467 | } |
| 1468 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1469 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1470 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1471 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1472 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1473 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1474 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1475 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1476 | if( pLeft->op==TK_COLUMN ){ |
| 1477 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1478 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1479 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1480 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1481 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1482 | WhereTerm *pNew; |
| 1483 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1484 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1485 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1486 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1487 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1488 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1489 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1490 | return; |
| 1491 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1492 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1493 | if( idxNew==0 ) return; |
| 1494 | pNew = &pWC->a[idxNew]; |
| 1495 | pNew->iParent = idxTerm; |
| 1496 | pTerm = &pWC->a[idxTerm]; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1497 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1498 | pTerm->wtFlags |= TERM_COPIED; |
drh | eb5bc92 | 2013-01-17 16:43:33 +0000 | [diff] [blame] | 1499 | if( pExpr->op==TK_EQ |
| 1500 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1501 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 1502 | ){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1503 | pTerm->eOperator |= WO_EQUIV; |
| 1504 | eExtraOp = WO_EQUIV; |
| 1505 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1506 | }else{ |
| 1507 | pDup = pExpr; |
| 1508 | pNew = pTerm; |
| 1509 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1510 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1511 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1512 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1513 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1514 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1515 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1516 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1517 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1518 | } |
| 1519 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1520 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1521 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1522 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1523 | ** that define the range that the BETWEEN implements. For example: |
| 1524 | ** |
| 1525 | ** a BETWEEN b AND c |
| 1526 | ** |
| 1527 | ** is converted into: |
| 1528 | ** |
| 1529 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1530 | ** |
| 1531 | ** The two new terms are added onto the end of the WhereClause object. |
| 1532 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1533 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1534 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1535 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1536 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1537 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1538 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1539 | int i; |
| 1540 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1541 | assert( pList!=0 ); |
| 1542 | assert( pList->nExpr==2 ); |
| 1543 | for(i=0; i<2; i++){ |
| 1544 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1545 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1546 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1547 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1548 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1549 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1550 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1551 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1552 | pTerm = &pWC->a[idxTerm]; |
| 1553 | pWC->a[idxNew].iParent = idxTerm; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1554 | } |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1555 | pTerm->nChild = 2; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1556 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1557 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1558 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1559 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1560 | /* Analyze a term that is composed of two or more subterms connected by |
| 1561 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1562 | */ |
| 1563 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1564 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1565 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1566 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1567 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1568 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1569 | |
| 1570 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1571 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1572 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1573 | ** |
| 1574 | ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 1575 | ** |
| 1576 | ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 1577 | ** |
| 1578 | ** The last character of the prefix "abc" is incremented to form the |
shane | 7bc71e5 | 2008-05-28 18:01:44 +0000 | [diff] [blame] | 1579 | ** termination condition "abd". |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1580 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1581 | if( pWC->op==TK_AND |
| 1582 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1583 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1584 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1585 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1586 | Expr *pNewExpr1; |
| 1587 | Expr *pNewExpr2; |
| 1588 | int idxNew1; |
| 1589 | int idxNew2; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1590 | Token sCollSeqName; /* Name of collating sequence */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1591 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1592 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1593 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1594 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1595 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1596 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1597 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1598 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1599 | /* The point is to increment the last character before the first |
| 1600 | ** wildcard. But if we increment '@', that will push it into the |
| 1601 | ** alphabetic range where case conversions will mess up the |
| 1602 | ** inequality. To avoid this, make sure to also run the full |
| 1603 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1604 | */ |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 1605 | if( c=='A'-1 ) isComplete = 0; /* EV: R-64339-08207 */ |
| 1606 | |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1607 | |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1608 | c = sqlite3UpperToLower[c]; |
| 1609 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1610 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1611 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1612 | sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 1613 | sCollSeqName.n = 6; |
| 1614 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1615 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1616 | sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1617 | pStr1, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1618 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1619 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1620 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1621 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1622 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1623 | sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1624 | pStr2, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1625 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1626 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1627 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1628 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1629 | if( isComplete ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1630 | pWC->a[idxNew1].iParent = idxTerm; |
| 1631 | pWC->a[idxNew2].iParent = idxTerm; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1632 | pTerm->nChild = 2; |
| 1633 | } |
| 1634 | } |
| 1635 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1636 | |
| 1637 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1638 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1639 | ** current expression is of the form: column MATCH expr. |
| 1640 | ** This information is used by the xBestIndex methods of |
| 1641 | ** virtual tables. The native query optimizer does not attempt |
| 1642 | ** to do anything with MATCH functions. |
| 1643 | */ |
| 1644 | if( isMatchOfColumn(pExpr) ){ |
| 1645 | int idxNew; |
| 1646 | Expr *pRight, *pLeft; |
| 1647 | WhereTerm *pNewTerm; |
| 1648 | Bitmask prereqColumn, prereqExpr; |
| 1649 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1650 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1651 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1652 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1653 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1654 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1655 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1656 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1657 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1658 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1659 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1660 | pNewTerm = &pWC->a[idxNew]; |
| 1661 | pNewTerm->prereqRight = prereqExpr; |
| 1662 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1663 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1664 | pNewTerm->eOperator = WO_MATCH; |
| 1665 | pNewTerm->iParent = idxTerm; |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1666 | pTerm = &pWC->a[idxTerm]; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1667 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1668 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1669 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1670 | } |
| 1671 | } |
| 1672 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1673 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1674 | #ifdef SQLITE_ENABLE_STAT3 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1675 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1676 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1677 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1678 | ** virtual term of that form. |
| 1679 | ** |
| 1680 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 1681 | ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 1682 | ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 1683 | ** the start of the loop will prevent any results from being returned. |
| 1684 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1685 | if( pExpr->op==TK_NOTNULL |
| 1686 | && pExpr->pLeft->op==TK_COLUMN |
| 1687 | && pExpr->pLeft->iColumn>=0 |
| 1688 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1689 | Expr *pNewExpr; |
| 1690 | Expr *pLeft = pExpr->pLeft; |
| 1691 | int idxNew; |
| 1692 | WhereTerm *pNewTerm; |
| 1693 | |
| 1694 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1695 | sqlite3ExprDup(db, pLeft, 0), |
| 1696 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1697 | |
| 1698 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1699 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1700 | if( idxNew ){ |
| 1701 | pNewTerm = &pWC->a[idxNew]; |
| 1702 | pNewTerm->prereqRight = 0; |
| 1703 | pNewTerm->leftCursor = pLeft->iTable; |
| 1704 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1705 | pNewTerm->eOperator = WO_GT; |
| 1706 | pNewTerm->iParent = idxTerm; |
| 1707 | pTerm = &pWC->a[idxTerm]; |
| 1708 | pTerm->nChild = 1; |
| 1709 | pTerm->wtFlags |= TERM_COPIED; |
| 1710 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1711 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1712 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1713 | #endif /* SQLITE_ENABLE_STAT */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1714 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1715 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1716 | ** an index for tables to the left of the join. |
| 1717 | */ |
| 1718 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1721 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1722 | ** This function searches the expression list passed as the second argument |
| 1723 | ** for an expression of type TK_COLUMN that refers to the same column and |
| 1724 | ** uses the same collation sequence as the iCol'th column of index pIdx. |
| 1725 | ** Argument iBase is the cursor number used for the table that pIdx refers |
| 1726 | ** to. |
| 1727 | ** |
| 1728 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1729 | ** no expression is found, -1 is returned. |
| 1730 | */ |
| 1731 | static int findIndexCol( |
| 1732 | Parse *pParse, /* Parse context */ |
| 1733 | ExprList *pList, /* Expression list to search */ |
| 1734 | int iBase, /* Cursor for table associated with pIdx */ |
| 1735 | Index *pIdx, /* Index to match column of */ |
| 1736 | int iCol /* Column of index to match */ |
| 1737 | ){ |
| 1738 | int i; |
| 1739 | const char *zColl = pIdx->azColl[iCol]; |
| 1740 | |
| 1741 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1742 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1743 | if( p->op==TK_COLUMN |
| 1744 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1745 | && p->iTable==iBase |
| 1746 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1747 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1748 | if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1749 | return i; |
| 1750 | } |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | return -1; |
| 1755 | } |
| 1756 | |
| 1757 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1758 | ** Return true if the DISTINCT expression-list passed as the third argument |
| 1759 | ** is redundant. A DISTINCT list is redundant if the database contains a |
| 1760 | ** UNIQUE index that guarantees that the result of the query will be distinct |
| 1761 | ** anyway. |
| 1762 | */ |
| 1763 | static int isDistinctRedundant( |
| 1764 | Parse *pParse, |
| 1765 | SrcList *pTabList, |
| 1766 | WhereClause *pWC, |
| 1767 | ExprList *pDistinct |
| 1768 | ){ |
| 1769 | Table *pTab; |
| 1770 | Index *pIdx; |
| 1771 | int i; |
| 1772 | int iBase; |
| 1773 | |
| 1774 | /* If there is more than one table or sub-select in the FROM clause of |
| 1775 | ** this query, then it will not be possible to show that the DISTINCT |
| 1776 | ** clause is redundant. */ |
| 1777 | if( pTabList->nSrc!=1 ) return 0; |
| 1778 | iBase = pTabList->a[0].iCursor; |
| 1779 | pTab = pTabList->a[0].pTab; |
| 1780 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1781 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1782 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1783 | ** current SELECT is a correlated sub-query. |
| 1784 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1785 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1786 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1787 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | /* Loop through all indices on the table, checking each to see if it makes |
| 1791 | ** the DISTINCT qualifier redundant. It does so if: |
| 1792 | ** |
| 1793 | ** 1. The index is itself UNIQUE, and |
| 1794 | ** |
| 1795 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1796 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1797 | ** where X is a constant value. The collation sequences of the |
| 1798 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1799 | ** |
| 1800 | ** 3. All of those index columns for which the WHERE clause does not |
| 1801 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1802 | */ |
| 1803 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1804 | if( pIdx->onError==OE_None ) continue; |
| 1805 | for(i=0; i<pIdx->nColumn; i++){ |
| 1806 | int iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1807 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1808 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
| 1809 | if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){ |
| 1810 | break; |
| 1811 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | if( i==pIdx->nColumn ){ |
| 1815 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1816 | return 1; |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | return 0; |
| 1821 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1822 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1823 | /* |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 1824 | ** Prepare a crude estimate of the logarithm of the input value. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1825 | ** The results need not be exact. This is only used for estimating |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 1826 | ** the total cost of performing operations with O(logN) or O(NlogN) |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1827 | ** complexity. Because N is just a guess, it is no great tragedy if |
| 1828 | ** logN is a little off. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1829 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 1830 | static WhereCost estLog(WhereCost N){ |
drh | 23fec45 | 2013-06-07 02:04:19 +0000 | [diff] [blame] | 1831 | u32 a; |
| 1832 | assert( sizeof(WhereCost)==4 ); /* 32-bit float input */ |
| 1833 | if( N<=0.0 ) return 0.0; |
| 1834 | memcpy(&a, &N, 4); |
| 1835 | return ((a >>= 23)-127)*0.3; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1838 | /* |
| 1839 | ** Two routines for printing the content of an sqlite3_index_info |
| 1840 | ** structure. Used for testing and debugging only. If neither |
| 1841 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 1842 | ** are no-ops. |
| 1843 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 1844 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1845 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 1846 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1847 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1848 | for(i=0; i<p->nConstraint; i++){ |
| 1849 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 1850 | i, |
| 1851 | p->aConstraint[i].iColumn, |
| 1852 | p->aConstraint[i].iTermOffset, |
| 1853 | p->aConstraint[i].op, |
| 1854 | p->aConstraint[i].usable); |
| 1855 | } |
| 1856 | for(i=0; i<p->nOrderBy; i++){ |
| 1857 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 1858 | i, |
| 1859 | p->aOrderBy[i].iColumn, |
| 1860 | p->aOrderBy[i].desc); |
| 1861 | } |
| 1862 | } |
| 1863 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 1864 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1865 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1866 | for(i=0; i<p->nConstraint; i++){ |
| 1867 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 1868 | i, |
| 1869 | p->aConstraintUsage[i].argvIndex, |
| 1870 | p->aConstraintUsage[i].omit); |
| 1871 | } |
| 1872 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 1873 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 1874 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 1875 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
| 1876 | } |
| 1877 | #else |
| 1878 | #define TRACE_IDX_INPUTS(A) |
| 1879 | #define TRACE_IDX_OUTPUTS(A) |
| 1880 | #endif |
| 1881 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1882 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1883 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1884 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 1885 | ** could be used with an index to access pSrc, assuming an appropriate |
| 1886 | ** index existed. |
| 1887 | */ |
| 1888 | static int termCanDriveIndex( |
| 1889 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 1890 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 1891 | Bitmask notReady /* Tables in outer loops of the join */ |
| 1892 | ){ |
| 1893 | char aff; |
| 1894 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1895 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1896 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 1897 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1898 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 1899 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 1900 | return 1; |
| 1901 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1902 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1903 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1904 | |
| 1905 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1906 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1907 | ** Generate code to construct the Index object for an automatic index |
| 1908 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 1909 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1910 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1911 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1912 | Parse *pParse, /* The parsing context */ |
| 1913 | WhereClause *pWC, /* The WHERE clause */ |
| 1914 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 1915 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 1916 | WhereLevel *pLevel /* Write new index here */ |
| 1917 | ){ |
| 1918 | int nColumn; /* Number of columns in the constructed index */ |
| 1919 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 1920 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 1921 | int nByte; /* Byte of memory needed for pIdx */ |
| 1922 | Index *pIdx; /* Object describing the transient index */ |
| 1923 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1924 | int addrInit; /* Address of the initialization bypass jump */ |
| 1925 | Table *pTable; /* The table being indexed */ |
| 1926 | KeyInfo *pKeyinfo; /* Key information for the index */ |
| 1927 | int addrTop; /* Top of the index fill loop */ |
| 1928 | int regRecord; /* Register holding an index record */ |
| 1929 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1930 | int i; /* Loop counter */ |
| 1931 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1932 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1933 | WhereLoop *pLoop; /* The Loop object */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1934 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 1935 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1936 | const int mxConstraint = 10; /* Maximum number of constraints */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1937 | |
| 1938 | /* Generate code to skip over the creation and initialization of the |
| 1939 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 1940 | v = pParse->pVdbe; |
| 1941 | assert( v!=0 ); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 1942 | addrInit = sqlite3CodeOnce(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1943 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1944 | /* Count the number of columns that will be added to the index |
| 1945 | ** and used to match WHERE clause constraints */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1946 | nColumn = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1947 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1948 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1949 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1950 | idxCols = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 1951 | for(pTerm=pWC->a; pTerm<pWCEnd && pLoop->nLTerm<mxConstraint; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1952 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 1953 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1954 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1955 | testcase( iCol==BMS ); |
| 1956 | testcase( iCol==BMS-1 ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1957 | if( (idxCols & cMask)==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 1958 | if( whereLoopResize(pParse->db, pLoop, nColumn+1) ) return; |
| 1959 | pLoop->aLTerm[nColumn++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1960 | idxCols |= cMask; |
| 1961 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1962 | } |
| 1963 | } |
| 1964 | assert( nColumn>0 ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 1965 | pLoop->u.btree.nEq = pLoop->nLTerm = nColumn; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1966 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
| 1967 | | WHERE_TEMP_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1968 | |
| 1969 | /* Count the number of additional columns needed to create a |
| 1970 | ** covering index. A "covering index" is an index that contains all |
| 1971 | ** columns that are needed by the query. With a covering index, the |
| 1972 | ** original table never needs to be accessed. Automatic indices must |
| 1973 | ** be a covering index because the index will not be updated if the |
| 1974 | ** original table changes and the index and table cannot both be used |
| 1975 | ** if they go out of sync. |
| 1976 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1977 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1978 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1979 | testcase( pTable->nCol==BMS-1 ); |
| 1980 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1981 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1982 | if( extraCols & MASKBIT(i) ) nColumn++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1983 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1984 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1985 | nColumn += pTable->nCol - BMS + 1; |
| 1986 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1987 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1988 | |
| 1989 | /* Construct the Index object to describe this index */ |
| 1990 | nByte = sizeof(Index); |
| 1991 | nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 1992 | nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 1993 | nByte += nColumn; /* Index.aSortOrder */ |
| 1994 | pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 1995 | if( pIdx==0 ) return; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1996 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1997 | pIdx->azColl = (char**)&pIdx[1]; |
| 1998 | pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 1999 | pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 2000 | pIdx->zName = "auto-index"; |
| 2001 | pIdx->nColumn = nColumn; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2002 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2003 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2004 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2005 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2006 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2007 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2008 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2009 | if( (idxCols & cMask)==0 ){ |
| 2010 | Expr *pX = pTerm->pExpr; |
| 2011 | idxCols |= cMask; |
| 2012 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 2013 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 6f2e6c0 | 2011-02-17 13:33:15 +0000 | [diff] [blame] | 2014 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2015 | n++; |
| 2016 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2019 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2020 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2021 | /* Add additional columns needed to make the automatic index into |
| 2022 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2023 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2024 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2025 | pIdx->aiColumn[n] = i; |
| 2026 | pIdx->azColl[n] = "BINARY"; |
| 2027 | n++; |
| 2028 | } |
| 2029 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2030 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2031 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 2032 | pIdx->aiColumn[n] = i; |
| 2033 | pIdx->azColl[n] = "BINARY"; |
| 2034 | n++; |
| 2035 | } |
| 2036 | } |
| 2037 | assert( n==nColumn ); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2038 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2039 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2040 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 2041 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 2042 | pLevel->iIdxCur = pParse->nTab++; |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2043 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2044 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2045 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2046 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2047 | /* Fill the automatic index with content */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2048 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); |
| 2049 | regRecord = sqlite3GetTempReg(pParse); |
| 2050 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1); |
| 2051 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 2052 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2053 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2054 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2055 | sqlite3VdbeJumpHere(v, addrTop); |
| 2056 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 2057 | |
| 2058 | /* Jump here when skipping the initialization */ |
| 2059 | sqlite3VdbeJumpHere(v, addrInit); |
| 2060 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2061 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2062 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 2063 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2064 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2065 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 2066 | ** responsibility of the caller to eventually release the structure |
| 2067 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 2068 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2069 | static sqlite3_index_info *allocateIndexInfo( |
| 2070 | Parse *pParse, |
| 2071 | WhereClause *pWC, |
| 2072 | struct SrcList_item *pSrc, |
| 2073 | ExprList *pOrderBy |
| 2074 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2075 | int i, j; |
| 2076 | int nTerm; |
| 2077 | struct sqlite3_index_constraint *pIdxCons; |
| 2078 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 2079 | struct sqlite3_index_constraint_usage *pUsage; |
| 2080 | WhereTerm *pTerm; |
| 2081 | int nOrderBy; |
| 2082 | sqlite3_index_info *pIdxInfo; |
| 2083 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2084 | /*WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));*/ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2085 | |
| 2086 | /* Count the number of possible WHERE clause constraints referring |
| 2087 | ** to this virtual table */ |
| 2088 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 2089 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2090 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2091 | testcase( pTerm->eOperator & WO_IN ); |
| 2092 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2093 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2094 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2095 | nTerm++; |
| 2096 | } |
| 2097 | |
| 2098 | /* If the ORDER BY clause contains only columns in the current |
| 2099 | ** virtual table then allocate space for the aOrderBy part of |
| 2100 | ** the sqlite3_index_info structure. |
| 2101 | */ |
| 2102 | nOrderBy = 0; |
| 2103 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2104 | int n = pOrderBy->nExpr; |
| 2105 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2106 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2107 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 2108 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2109 | if( i==n){ |
| 2110 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | /* Allocate the sqlite3_index_info structure |
| 2115 | */ |
| 2116 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 2117 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 2118 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 2119 | if( pIdxInfo==0 ){ |
| 2120 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2121 | return 0; |
| 2122 | } |
| 2123 | |
| 2124 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 2125 | ** many fields that are declared "const" to prevent xBestIndex from |
| 2126 | ** changing them. We have to do some funky casting in order to |
| 2127 | ** initialize those fields. |
| 2128 | */ |
| 2129 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 2130 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 2131 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 2132 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 2133 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 2134 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 2135 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 2136 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 2137 | pUsage; |
| 2138 | |
| 2139 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2140 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2141 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2142 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2143 | testcase( pTerm->eOperator & WO_IN ); |
| 2144 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2145 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2146 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2147 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 2148 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2149 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2150 | if( op==WO_IN ) op = WO_EQ; |
| 2151 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2152 | /* The direct assignment in the previous line is possible only because |
| 2153 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 2154 | ** following asserts verify this fact. */ |
| 2155 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 2156 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 2157 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 2158 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 2159 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 2160 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2161 | 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] | 2162 | j++; |
| 2163 | } |
| 2164 | for(i=0; i<nOrderBy; i++){ |
| 2165 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2166 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 2167 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 2168 | } |
| 2169 | |
| 2170 | return pIdxInfo; |
| 2171 | } |
| 2172 | |
| 2173 | /* |
| 2174 | ** The table object reference passed as the second argument to this function |
| 2175 | ** must represent a virtual table. This function invokes the xBestIndex() |
| 2176 | ** method of the virtual table with the sqlite3_index_info pointer passed |
| 2177 | ** as the argument. |
| 2178 | ** |
| 2179 | ** If an error occurs, pParse is populated with an error message and a |
| 2180 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 2181 | ** part of the sqlite3_index_info structure is left populated. |
| 2182 | ** |
| 2183 | ** Whether or not an error is returned, it is the responsibility of the |
| 2184 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 2185 | ** that this is required. |
| 2186 | */ |
| 2187 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 2188 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2189 | int i; |
| 2190 | int rc; |
| 2191 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2192 | /*WHERETRACE(("xBestIndex for %s\n", pTab->zName));*/ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2193 | TRACE_IDX_INPUTS(p); |
| 2194 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 2195 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2196 | |
| 2197 | if( rc!=SQLITE_OK ){ |
| 2198 | if( rc==SQLITE_NOMEM ){ |
| 2199 | pParse->db->mallocFailed = 1; |
| 2200 | }else if( !pVtab->zErrMsg ){ |
| 2201 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 2202 | }else{ |
| 2203 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 2204 | } |
| 2205 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2206 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2207 | pVtab->zErrMsg = 0; |
| 2208 | |
| 2209 | for(i=0; i<p->nConstraint; i++){ |
| 2210 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 2211 | sqlite3ErrorMsg(pParse, |
| 2212 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | return pParse->nErr; |
| 2217 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2218 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2219 | |
| 2220 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2221 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2222 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2223 | ** Estimate the location of a particular key among all keys in an |
| 2224 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2225 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2226 | ** aStat[0] Est. number of rows less than pVal |
| 2227 | ** aStat[1] Est. number of rows equal to pVal |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2228 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2229 | ** Return SQLITE_OK on success. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2230 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2231 | static int whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2232 | Parse *pParse, /* Database connection */ |
| 2233 | Index *pIdx, /* Index to consider domain of */ |
| 2234 | sqlite3_value *pVal, /* Value to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2235 | int roundUp, /* Round up if true. Round down if false */ |
| 2236 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2237 | ){ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2238 | tRowcnt n; |
| 2239 | IndexSample *aSample; |
| 2240 | int i, eType; |
| 2241 | int isEq = 0; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2242 | i64 v; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2243 | WhereCost r, rS; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2244 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2245 | assert( roundUp==0 || roundUp==1 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2246 | assert( pIdx->nSample>0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2247 | if( pVal==0 ) return SQLITE_ERROR; |
| 2248 | n = pIdx->aiRowEst[0]; |
| 2249 | aSample = pIdx->aSample; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2250 | eType = sqlite3_value_type(pVal); |
| 2251 | |
| 2252 | if( eType==SQLITE_INTEGER ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2253 | v = sqlite3_value_int64(pVal); |
| 2254 | r = (i64)v; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2255 | for(i=0; i<pIdx->nSample; i++){ |
| 2256 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2257 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2258 | if( aSample[i].eType==SQLITE_INTEGER ){ |
| 2259 | if( aSample[i].u.i>=v ){ |
| 2260 | isEq = aSample[i].u.i==v; |
| 2261 | break; |
| 2262 | } |
| 2263 | }else{ |
| 2264 | assert( aSample[i].eType==SQLITE_FLOAT ); |
| 2265 | if( aSample[i].u.r>=r ){ |
| 2266 | isEq = aSample[i].u.r==r; |
| 2267 | break; |
| 2268 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2269 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2270 | } |
| 2271 | }else if( eType==SQLITE_FLOAT ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2272 | r = sqlite3_value_double(pVal); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2273 | for(i=0; i<pIdx->nSample; i++){ |
| 2274 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2275 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2276 | if( aSample[i].eType==SQLITE_FLOAT ){ |
| 2277 | rS = aSample[i].u.r; |
| 2278 | }else{ |
| 2279 | rS = aSample[i].u.i; |
| 2280 | } |
| 2281 | if( rS>=r ){ |
| 2282 | isEq = rS==r; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2283 | break; |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2284 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2285 | } |
| 2286 | }else if( eType==SQLITE_NULL ){ |
| 2287 | i = 0; |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2288 | if( aSample[0].eType==SQLITE_NULL ) isEq = 1; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2289 | }else{ |
| 2290 | assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 2291 | for(i=0; i<pIdx->nSample; i++){ |
| 2292 | if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){ |
| 2293 | break; |
| 2294 | } |
| 2295 | } |
| 2296 | if( i<pIdx->nSample ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2297 | sqlite3 *db = pParse->db; |
| 2298 | CollSeq *pColl; |
| 2299 | const u8 *z; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2300 | if( eType==SQLITE_BLOB ){ |
| 2301 | z = (const u8 *)sqlite3_value_blob(pVal); |
| 2302 | pColl = db->pDfltColl; |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2303 | assert( pColl->enc==SQLITE_UTF8 ); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2304 | }else{ |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 2305 | pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl); |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 2306 | if( pColl==0 ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2307 | return SQLITE_ERROR; |
| 2308 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2309 | z = (const u8 *)sqlite3ValueText(pVal, pColl->enc); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2310 | if( !z ){ |
| 2311 | return SQLITE_NOMEM; |
| 2312 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2313 | assert( z && pColl && pColl->xCmp ); |
| 2314 | } |
| 2315 | n = sqlite3ValueBytes(pVal, pColl->enc); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2316 | |
| 2317 | for(; i<pIdx->nSample; i++){ |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2318 | int c; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2319 | int eSampletype = aSample[i].eType; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2320 | if( eSampletype<eType ) continue; |
| 2321 | if( eSampletype!=eType ) break; |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2322 | #ifndef SQLITE_OMIT_UTF16 |
| 2323 | if( pColl->enc!=SQLITE_UTF8 ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2324 | int nSample; |
| 2325 | char *zSample = sqlite3Utf8to16( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2326 | db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample |
| 2327 | ); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2328 | if( !zSample ){ |
| 2329 | assert( db->mallocFailed ); |
| 2330 | return SQLITE_NOMEM; |
| 2331 | } |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2332 | c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2333 | sqlite3DbFree(db, zSample); |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2334 | }else |
| 2335 | #endif |
| 2336 | { |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2337 | 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] | 2338 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2339 | if( c>=0 ){ |
| 2340 | if( c==0 ) isEq = 1; |
| 2341 | break; |
| 2342 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2343 | } |
| 2344 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2345 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2346 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2347 | /* At this point, aSample[i] is the first sample that is greater than |
| 2348 | ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 2349 | ** than pVal. If aSample[i]==pVal, then isEq==1. |
| 2350 | */ |
| 2351 | if( isEq ){ |
| 2352 | assert( i<pIdx->nSample ); |
| 2353 | aStat[0] = aSample[i].nLt; |
| 2354 | aStat[1] = aSample[i].nEq; |
| 2355 | }else{ |
| 2356 | tRowcnt iLower, iUpper, iGap; |
| 2357 | if( i==0 ){ |
| 2358 | iLower = 0; |
| 2359 | iUpper = aSample[0].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2360 | }else{ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2361 | iUpper = i>=pIdx->nSample ? n : aSample[i].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2362 | iLower = aSample[i-1].nEq + aSample[i-1].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2363 | } |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2364 | aStat[1] = pIdx->avgEq; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2365 | if( iLower>=iUpper ){ |
| 2366 | iGap = 0; |
| 2367 | }else{ |
| 2368 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2369 | } |
| 2370 | if( roundUp ){ |
| 2371 | iGap = (iGap*2)/3; |
| 2372 | }else{ |
| 2373 | iGap = iGap/3; |
| 2374 | } |
| 2375 | aStat[0] = iLower + iGap; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2376 | } |
| 2377 | return SQLITE_OK; |
| 2378 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2379 | #endif /* SQLITE_ENABLE_STAT3 */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2380 | |
| 2381 | /* |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2382 | ** If expression pExpr represents a literal value, set *pp to point to |
| 2383 | ** an sqlite3_value structure containing the same value, with affinity |
| 2384 | ** aff applied to it, before returning. It is the responsibility of the |
| 2385 | ** caller to eventually release this structure by passing it to |
| 2386 | ** sqlite3ValueFree(). |
| 2387 | ** |
| 2388 | ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr |
| 2389 | ** is an SQL variable that currently has a non-NULL value bound to it, |
| 2390 | ** create an sqlite3_value structure containing this value, again with |
| 2391 | ** affinity aff applied to it, instead. |
| 2392 | ** |
| 2393 | ** If neither of the above apply, set *pp to NULL. |
| 2394 | ** |
| 2395 | ** If an error occurs, return an error code. Otherwise, SQLITE_OK. |
| 2396 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2397 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2398 | static int valueFromExpr( |
| 2399 | Parse *pParse, |
| 2400 | Expr *pExpr, |
| 2401 | u8 aff, |
| 2402 | sqlite3_value **pp |
| 2403 | ){ |
drh | 4278d53 | 2010-12-16 19:52:52 +0000 | [diff] [blame] | 2404 | if( pExpr->op==TK_VARIABLE |
| 2405 | || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) |
| 2406 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2407 | int iVar = pExpr->iColumn; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 2408 | sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2409 | *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); |
| 2410 | return SQLITE_OK; |
| 2411 | } |
| 2412 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 2413 | } |
dan | f7b0b0a | 2009-10-19 15:52:32 +0000 | [diff] [blame] | 2414 | #endif |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2415 | |
| 2416 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2417 | ** This function is used to estimate the number of rows that will be visited |
| 2418 | ** by scanning an index for a range of values. The range may have an upper |
| 2419 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2420 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2421 | ** example, assuming that index p is on t1(a): |
| 2422 | ** |
| 2423 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2424 | ** |_____| |_____| |
| 2425 | ** | | |
| 2426 | ** pLower pUpper |
| 2427 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2428 | ** 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] | 2429 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2430 | ** |
| 2431 | ** The nEq parameter is passed the index of the index column subject to the |
| 2432 | ** range constraint. Or, equivalently, the number of equality constraints |
| 2433 | ** optimized by the proposed index scan. For example, assuming index p is |
| 2434 | ** on t1(a, b), and the SQL query is: |
| 2435 | ** |
| 2436 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2437 | ** |
| 2438 | ** then nEq should be passed the value 1 (as the range restricted column, |
| 2439 | ** b, is the second left-most column of the index). Or, if the query is: |
| 2440 | ** |
| 2441 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2442 | ** |
| 2443 | ** then nEq should be passed 0. |
| 2444 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2445 | ** The returned value is an integer divisor to reduce the estimated |
| 2446 | ** search space. A return value of 1 means that range constraints are |
| 2447 | ** no help at all. A return value of 2 means range constraints are |
| 2448 | ** expected to reduce the search space by half. And so forth... |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2449 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2450 | ** In the absence of sqlite_stat3 ANALYZE data, each range inequality |
| 2451 | ** reduces the search space by a factor of 4. Hence a single constraint (x>?) |
| 2452 | ** results in a return of 4 and a range constraint (x>? AND x<?) results |
| 2453 | ** in a return of 16. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2454 | */ |
| 2455 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2456 | Parse *pParse, /* Parsing & code generating context */ |
| 2457 | Index *p, /* The index containing the range-compared column; "x" */ |
| 2458 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 2459 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2460 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2461 | WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2462 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2463 | int rc = SQLITE_OK; |
| 2464 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2465 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2466 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2467 | if( nEq==0 && p->nSample ){ |
| 2468 | sqlite3_value *pRangeVal; |
| 2469 | tRowcnt iLower = 0; |
| 2470 | tRowcnt iUpper = p->aiRowEst[0]; |
| 2471 | tRowcnt a[2]; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2472 | u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2473 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2474 | if( pLower ){ |
| 2475 | Expr *pExpr = pLower->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2476 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2477 | assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2478 | if( rc==SQLITE_OK |
| 2479 | && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK |
| 2480 | ){ |
| 2481 | iLower = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2482 | if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2483 | } |
| 2484 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2485 | } |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2486 | if( rc==SQLITE_OK && pUpper ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2487 | Expr *pExpr = pUpper->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2488 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2489 | assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2490 | if( rc==SQLITE_OK |
| 2491 | && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK |
| 2492 | ){ |
| 2493 | iUpper = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2494 | if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1]; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2495 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2496 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2497 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2498 | if( rc==SQLITE_OK ){ |
| 2499 | if( iUpper<=iLower ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2500 | *pRangeDiv = (WhereCost)p->aiRowEst[0]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2501 | }else{ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2502 | *pRangeDiv = (WhereCost)p->aiRowEst[0]/(WhereCost)(iUpper - iLower); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2503 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2504 | /*WHERETRACE(("range scan regions: %u..%u div=%g\n", |
| 2505 | (u32)iLower, (u32)iUpper, *pRangeDiv));*/ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2506 | return SQLITE_OK; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2507 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2508 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2509 | #else |
| 2510 | UNUSED_PARAMETER(pParse); |
| 2511 | UNUSED_PARAMETER(p); |
| 2512 | UNUSED_PARAMETER(nEq); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2513 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2514 | assert( pLower || pUpper ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2515 | *pRangeDiv = (WhereCost)1; |
| 2516 | if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (WhereCost)4; |
| 2517 | if( pUpper ) *pRangeDiv *= (WhereCost)4; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2518 | return rc; |
| 2519 | } |
| 2520 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2521 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2522 | /* |
| 2523 | ** Estimate the number of rows that will be returned based on |
| 2524 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2525 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2526 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2527 | ** for that index. When pExpr==NULL that means the constraint is |
| 2528 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2529 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2530 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2531 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2532 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2533 | ** |
| 2534 | ** This routine can fail if it is unable to load a collating sequence |
| 2535 | ** required for string comparison, or if unable to allocate memory |
| 2536 | ** for a UTF conversion required for comparison. The error is stored |
| 2537 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2538 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2539 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2540 | Parse *pParse, /* Parsing & code generating context */ |
| 2541 | Index *p, /* The index whose left-most column is pTerm */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2542 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2543 | WhereCost *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2544 | ){ |
| 2545 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2546 | u8 aff; /* Column affinity */ |
| 2547 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2548 | tRowcnt a[2]; /* Statistics */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2549 | |
| 2550 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2551 | assert( p->nSample>0 ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2552 | aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2553 | if( pExpr ){ |
| 2554 | rc = valueFromExpr(pParse, pExpr, aff, &pRhs); |
| 2555 | if( rc ) goto whereEqualScanEst_cancel; |
| 2556 | }else{ |
| 2557 | pRhs = sqlite3ValueNew(pParse->db); |
| 2558 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2559 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2560 | rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 2561 | if( rc==SQLITE_OK ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2562 | /*WHERETRACE(("equality scan regions: %d\n", (int)a[1]));*/ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2563 | *pnRow = a[1]; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2564 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2565 | whereEqualScanEst_cancel: |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2566 | sqlite3ValueFree(pRhs); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2567 | return rc; |
| 2568 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2569 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2570 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2571 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2572 | /* |
| 2573 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2574 | ** an IN constraint where the right-hand side of the IN operator |
| 2575 | ** is a list of values. Example: |
| 2576 | ** |
| 2577 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2578 | ** |
| 2579 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2580 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2581 | ** non-zero. |
| 2582 | ** |
| 2583 | ** This routine can fail if it is unable to load a collating sequence |
| 2584 | ** required for string comparison, or if unable to allocate memory |
| 2585 | ** for a UTF conversion required for comparison. The error is stored |
| 2586 | ** in the pParse structure. |
| 2587 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2588 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2589 | Parse *pParse, /* Parsing & code generating context */ |
| 2590 | Index *p, /* The index whose left-most column is pTerm */ |
| 2591 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2592 | WhereCost *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2593 | ){ |
drh | 6825719 | 2011-08-16 17:06:21 +0000 | [diff] [blame] | 2594 | int rc = SQLITE_OK; /* Subfunction return code */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2595 | WhereCost nEst; /* Number of rows for a single term */ |
| 2596 | WhereCost nRowEst = (WhereCost)0; /* New estimate of the number of rows */ |
drh | 6825719 | 2011-08-16 17:06:21 +0000 | [diff] [blame] | 2597 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2598 | |
| 2599 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2600 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 2601 | nEst = p->aiRowEst[0]; |
| 2602 | rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| 2603 | nRowEst += nEst; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2604 | } |
| 2605 | if( rc==SQLITE_OK ){ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2606 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 2607 | *pnRow = nRowEst; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2608 | /*WHERETRACE(("IN row estimate: est=%g\n", nRowEst));*/ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2609 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2610 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2611 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2612 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2613 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2614 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2615 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2616 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2617 | ** or USING clause of that join. |
| 2618 | ** |
| 2619 | ** Consider the term t2.z='ok' in the following queries: |
| 2620 | ** |
| 2621 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2622 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2623 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2624 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2625 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2626 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2627 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2628 | ** |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 2629 | ** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are |
| 2630 | ** completely satisfied by indices. |
| 2631 | ** |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2632 | ** 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] | 2633 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2634 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2635 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2636 | ** but joins might run a little slower. The trick is to disable as much |
| 2637 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2638 | ** the wrong answer. See ticket #813. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2639 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2640 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 2641 | if( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2642 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2643 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 2644 | ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 2645 | pTerm->wtFlags |= TERM_CODED; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 2646 | if( pTerm->iParent>=0 ){ |
| 2647 | WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 2648 | if( (--pOther->nChild)==0 ){ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 2649 | disableTerm(pLevel, pOther); |
| 2650 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2651 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2656 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2657 | ** to the n registers starting at base. |
| 2658 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2659 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2660 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2661 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2662 | ** |
| 2663 | ** This routine makes its own copy of zAff so that the caller is free |
| 2664 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2665 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2666 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2667 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2668 | if( zAff==0 ){ |
| 2669 | assert( pParse->db->mallocFailed ); |
| 2670 | return; |
| 2671 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2672 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2673 | |
| 2674 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2675 | ** and end of the affinity string. |
| 2676 | */ |
| 2677 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2678 | n--; |
| 2679 | base++; |
| 2680 | zAff++; |
| 2681 | } |
| 2682 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2683 | n--; |
| 2684 | } |
| 2685 | |
| 2686 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2687 | if( n>0 ){ |
| 2688 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2689 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2690 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2691 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2694 | |
| 2695 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2696 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2697 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2698 | ** coded. |
| 2699 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2700 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2701 | ** |
| 2702 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2703 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2704 | ** 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] | 2705 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2706 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2707 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2708 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2709 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2710 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2711 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2712 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2713 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2714 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2715 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2716 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2717 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2718 | assert( iTarget>0 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2719 | if( pX->op==TK_EQ ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2720 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2721 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2722 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2723 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2724 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2725 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2726 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2727 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2728 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2729 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2730 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2731 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2732 | && pLoop->u.btree.pIndex!=0 |
| 2733 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2734 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2735 | testcase( iEq==0 ); |
| 2736 | testcase( iEq==pLevel->plan.u.pIdx->nColumn-1 ); |
| 2737 | testcase( iEq>0 && iEq+1<pLevel->plan.u.pIdx->nColumn ); |
| 2738 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2739 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2740 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2741 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2742 | iReg = iTarget; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2743 | eType = sqlite3FindInIndex(pParse, pX, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2744 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2745 | testcase( bRev ); |
| 2746 | bRev = !bRev; |
| 2747 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2748 | iTab = pX->iTable; |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2749 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2750 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2751 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2752 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2753 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2754 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2755 | pLevel->u.in.nIn++; |
| 2756 | pLevel->u.in.aInLoop = |
| 2757 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2758 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2759 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2760 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2761 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2762 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2763 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2764 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2765 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2766 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2767 | } |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2768 | pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2769 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2770 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2771 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2772 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2773 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2774 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2775 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2776 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2779 | /* |
| 2780 | ** Generate code that will evaluate all == and IN constraints for an |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2781 | ** index. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2782 | ** |
| 2783 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2784 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2785 | ** The index has as many as three equality constraints, but in this |
| 2786 | ** example, the third "c" value is an inequality. So only two |
| 2787 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2788 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2789 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2790 | ** |
| 2791 | ** In the example above nEq==2. But this subroutine works for any value |
| 2792 | ** 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] | 2793 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2794 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2795 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2796 | ** This routine always allocates at least one memory cell and returns |
| 2797 | ** the index of that memory cell. The code that |
| 2798 | ** calls this routine will use that memory cell to store the termination |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2799 | ** key value of the loop. If one or more IN operators appear, then |
| 2800 | ** this routine allocates an additional nEq memory cells for internal |
| 2801 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2802 | ** |
| 2803 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2804 | ** copy of the column affinity string of the index allocated using |
| 2805 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2806 | ** with equality constraints that use NONE affinity are set to |
| 2807 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2808 | ** |
| 2809 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2810 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2811 | ** |
| 2812 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2813 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2814 | ** no conversion should be attempted before using a t2.b value as part of |
| 2815 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2816 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2817 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2818 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2819 | Parse *pParse, /* Parsing context */ |
| 2820 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 2821 | WhereClause *pWC, /* The WHERE clause */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2822 | Bitmask notReady, /* Which parts of FROM have not yet been coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2823 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2824 | int nExtraReg, /* Number of extra registers to allocate */ |
| 2825 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2826 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2827 | int nEq; /* The number of == or IN constraints to code */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2828 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 2829 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2830 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2831 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2832 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2833 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2834 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2835 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2836 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2837 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2838 | pLoop = pLevel->pWLoop; |
| 2839 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 2840 | nEq = pLoop->u.btree.nEq; |
| 2841 | pIdx = pLoop->u.btree.pIndex; |
| 2842 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2843 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2844 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2845 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2846 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2847 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2848 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2849 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2850 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 2851 | if( !zAff ){ |
| 2852 | pParse->db->mallocFailed = 1; |
| 2853 | } |
| 2854 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2855 | /* Evaluate the equality constraints |
| 2856 | */ |
drh | c49de5d | 2007-01-19 01:06:01 +0000 | [diff] [blame] | 2857 | assert( pIdx->nColumn>=nEq ); |
| 2858 | for(j=0; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2859 | int r1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2860 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2861 | assert( pTerm!=0 ); |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2862 | /* The following true for indices with redundant columns. |
| 2863 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 2864 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 2865 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2866 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2867 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2868 | if( nReg==1 ){ |
| 2869 | sqlite3ReleaseTempReg(pParse, regBase); |
| 2870 | regBase = r1; |
| 2871 | }else{ |
| 2872 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 2873 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2874 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 2875 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 2876 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2877 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2878 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 2f2855b | 2009-11-18 01:25:26 +0000 | [diff] [blame] | 2879 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2880 | if( zAff ){ |
| 2881 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 2882 | zAff[j] = SQLITE_AFF_NONE; |
| 2883 | } |
| 2884 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 2885 | zAff[j] = SQLITE_AFF_NONE; |
| 2886 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2887 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2888 | } |
| 2889 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2890 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2891 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2894 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2895 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2896 | ** This routine is a helper for explainIndexRange() below |
| 2897 | ** |
| 2898 | ** pStr holds the text of an expression that we are building up one term |
| 2899 | ** at a time. This routine adds a new term to the end of the expression. |
| 2900 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 2901 | ** terms only. |
| 2902 | */ |
| 2903 | static void explainAppendTerm( |
| 2904 | StrAccum *pStr, /* The text expression being built */ |
| 2905 | int iTerm, /* Index of this term. First is zero */ |
| 2906 | const char *zColumn, /* Name of the column */ |
| 2907 | const char *zOp /* Name of the operator */ |
| 2908 | ){ |
| 2909 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 2910 | sqlite3StrAccumAppend(pStr, zColumn, -1); |
| 2911 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 2912 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 2913 | } |
| 2914 | |
| 2915 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2916 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 2917 | ** function returns a pointer to a string buffer containing a description |
| 2918 | ** of the subset of table rows scanned by the strategy in the form of an |
| 2919 | ** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 2920 | ** |
| 2921 | ** For example, if the query: |
| 2922 | ** |
| 2923 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 2924 | ** |
| 2925 | ** is run and there is an index on (a, b), then this function returns a |
| 2926 | ** string similar to: |
| 2927 | ** |
| 2928 | ** "a=? AND b>?" |
| 2929 | ** |
| 2930 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 2931 | ** It is the responsibility of the caller to free the buffer when it is |
| 2932 | ** no longer required. |
| 2933 | */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2934 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 2935 | Index *pIndex = pLoop->u.btree.pIndex; |
| 2936 | int nEq = pLoop->u.btree.nEq; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2937 | int i, j; |
| 2938 | Column *aCol = pTab->aCol; |
| 2939 | int *aiColumn = pIndex->aiColumn; |
| 2940 | StrAccum txt; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2941 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2942 | if( pIndex==0 ) return 0; |
| 2943 | if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2944 | return 0; |
| 2945 | } |
| 2946 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
drh | 03b6df1 | 2010-11-15 16:29:30 +0000 | [diff] [blame] | 2947 | txt.db = db; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2948 | sqlite3StrAccumAppend(&txt, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2949 | for(i=0; i<nEq; i++){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2950 | explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "="); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2953 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2954 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 2955 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 2956 | explainAppendTerm(&txt, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2957 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2958 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 2959 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 2960 | explainAppendTerm(&txt, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2961 | } |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2962 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 2963 | return sqlite3StrAccumFinish(&txt); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2966 | /* |
| 2967 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 2968 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 2969 | ** record is added to the output to describe the table scan strategy in |
| 2970 | ** pLevel. |
| 2971 | */ |
| 2972 | static void explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2973 | Parse *pParse, /* Parse context */ |
| 2974 | SrcList *pTabList, /* Table list this loop refers to */ |
| 2975 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 2976 | int iLevel, /* Value for "level" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2977 | int iFrom, /* Value for "from" column of output */ |
| 2978 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2979 | ){ |
| 2980 | if( pParse->explain==2 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2981 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2982 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 2983 | sqlite3 *db = pParse->db; /* Database handle */ |
| 2984 | char *zMsg; /* Text to add to EQP output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2985 | sqlite3_int64 nRow; /* Expected number of rows visited by scan */ |
| 2986 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2987 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2988 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 2989 | u32 flags; /* Flags that describe this loop */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2990 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2991 | pLoop = pLevel->pWLoop; |
| 2992 | flags = pLoop->wsFlags; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2993 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2994 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2995 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 2996 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 2997 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2998 | |
| 2999 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3000 | if( pItem->pSelect ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3001 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3002 | }else{ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3003 | zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3006 | if( pItem->zAlias ){ |
| 3007 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 3008 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3009 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
| 3010 | && pLoop->u.btree.pIndex!=0 |
| 3011 | ){ |
| 3012 | char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3013 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3014 | ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""), |
| 3015 | ((flags & WHERE_IDX_ONLY)?"COVERING ":""), |
| 3016 | ((flags & WHERE_TEMP_INDEX)?"":" "), |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3017 | ((flags & WHERE_TEMP_INDEX)?"": pLoop->u.btree.pIndex->zName), |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3018 | zWhere |
| 3019 | ); |
| 3020 | sqlite3DbFree(db, zWhere); |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 3021 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3022 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3023 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3024 | if( flags&WHERE_COLUMN_EQ ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3025 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 3026 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3027 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 3028 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 3029 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
| 3030 | }else if( flags&WHERE_TOP_LIMIT ){ |
| 3031 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 3032 | } |
| 3033 | } |
| 3034 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3035 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3036 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3037 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3038 | } |
| 3039 | #endif |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3040 | if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){ |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 3041 | testcase( wctrlFlags & WHERE_ORDERBY_MIN ); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3042 | nRow = 1; |
| 3043 | }else{ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3044 | nRow = (sqlite3_int64)pLoop->nOut; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3045 | } |
| 3046 | zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow); |
| 3047 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | #else |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3051 | # define explainOneScan(u,v,w,x,y,z) |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3052 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 3053 | |
| 3054 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3055 | /* |
| 3056 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 3057 | ** implementation described by pWInfo. |
| 3058 | */ |
| 3059 | static Bitmask codeOneLoopStart( |
| 3060 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 3061 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3062 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3063 | ){ |
| 3064 | int j, k; /* Loop counters */ |
| 3065 | int iCur; /* The VDBE cursor for the table */ |
| 3066 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 3067 | int omitTable; /* True if we use the index only */ |
| 3068 | int bRev; /* True if we need to scan in reverse order */ |
| 3069 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3070 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3071 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 3072 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 3073 | Parse *pParse; /* Parsing context */ |
| 3074 | Vdbe *v; /* The prepared stmt under constructions */ |
| 3075 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3076 | int addrBrk; /* Jump here to break out of the loop */ |
| 3077 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3078 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 3079 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3080 | Bitmask newNotReady; /* Return value */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3081 | |
| 3082 | pParse = pWInfo->pParse; |
| 3083 | v = pParse->pVdbe; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3084 | pWC = &pWInfo->sWC; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3085 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3086 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3087 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 3088 | iCur = pTabItem->iCursor; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3089 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 3090 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3091 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3092 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3093 | |
| 3094 | /* Create labels for the "break" and "continue" instructions |
| 3095 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 3096 | ** Jump to cont to go immediately to the next iteration of the |
| 3097 | ** loop. |
| 3098 | ** |
| 3099 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 3100 | ** means to continue with the next IN value combination. When |
| 3101 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 3102 | ** is the same as "addrBrk". |
| 3103 | */ |
| 3104 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 3105 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 3106 | |
| 3107 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 3108 | ** initialize a memory cell that records if this table matches any |
| 3109 | ** row of the left table of the join. |
| 3110 | */ |
| 3111 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 3112 | pLevel->iLeftJoin = ++pParse->nMem; |
| 3113 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 3114 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 3115 | } |
| 3116 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 3117 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 3118 | if( pTabItem->viaCoroutine ){ |
| 3119 | int regYield = pTabItem->regReturn; |
| 3120 | sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield); |
| 3121 | pLevel->p2 = sqlite3VdbeAddOp1(v, OP_Yield, regYield); |
| 3122 | VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName)); |
| 3123 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 3124 | pLevel->op = OP_Goto; |
| 3125 | }else |
| 3126 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3127 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3128 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 3129 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3130 | ** to access the data. |
| 3131 | */ |
| 3132 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3133 | int addrNotFound; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3134 | int nConstraint = pLoop->nLTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3135 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3136 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3137 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3138 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3139 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 3140 | int iTarget = iReg+j+2; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3141 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3142 | if( pTerm->eOperator & WO_IN ){ |
| 3143 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 3144 | addrNotFound = pLevel->addrNxt; |
| 3145 | }else{ |
| 3146 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 3147 | } |
| 3148 | } |
| 3149 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 3150 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3151 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 3152 | pLoop->u.vtab.idxStr, |
| 3153 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 3154 | pLoop->u.vtab.needFree = 0; |
| 3155 | for(j=0; j<nConstraint && j<16; j++){ |
| 3156 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3157 | disableTerm(pLevel, pLoop->aLTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3158 | } |
| 3159 | } |
| 3160 | pLevel->op = OP_VNext; |
| 3161 | pLevel->p1 = iCur; |
| 3162 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3163 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3164 | sqlite3ExprCachePop(pParse, 1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3165 | }else |
| 3166 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3167 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3168 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3169 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 3170 | ){ |
| 3171 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3172 | ** equality comparison against the ROWID field. Or |
| 3173 | ** we reference multiple rows using a "rowid IN (...)" |
| 3174 | ** construct. |
| 3175 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3176 | assert( pLoop->u.btree.nEq==1 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3177 | iReleaseReg = sqlite3GetTempReg(pParse); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3178 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3179 | assert( pTerm!=0 ); |
| 3180 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3181 | assert( omitTable==0 ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3182 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3183 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3184 | addrNxt = pLevel->addrNxt; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3185 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 3186 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 3187 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3188 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3189 | VdbeComment((v, "pk")); |
| 3190 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3191 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3192 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 3193 | ){ |
| 3194 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3195 | */ |
| 3196 | int testOp = OP_Noop; |
| 3197 | int start; |
| 3198 | int memEndValue = 0; |
| 3199 | WhereTerm *pStart, *pEnd; |
| 3200 | |
| 3201 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3202 | j = 0; |
| 3203 | pStart = pEnd = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3204 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 3205 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3206 | if( bRev ){ |
| 3207 | pTerm = pStart; |
| 3208 | pStart = pEnd; |
| 3209 | pEnd = pTerm; |
| 3210 | } |
| 3211 | if( pStart ){ |
| 3212 | Expr *pX; /* The expression that defines the start bound */ |
| 3213 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 3214 | |
| 3215 | /* The following constant maps TK_xx codes into corresponding |
| 3216 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 3217 | */ |
| 3218 | const u8 aMoveOp[] = { |
| 3219 | /* TK_GT */ OP_SeekGt, |
| 3220 | /* TK_LE */ OP_SeekLe, |
| 3221 | /* TK_LT */ OP_SeekLt, |
| 3222 | /* TK_GE */ OP_SeekGe |
| 3223 | }; |
| 3224 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 3225 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 3226 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 3227 | |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3228 | testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3229 | pX = pStart->pExpr; |
| 3230 | assert( pX!=0 ); |
| 3231 | assert( pStart->leftCursor==iCur ); |
| 3232 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 3233 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| 3234 | VdbeComment((v, "pk")); |
| 3235 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 3236 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 3237 | disableTerm(pLevel, pStart); |
| 3238 | }else{ |
| 3239 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 3240 | } |
| 3241 | if( pEnd ){ |
| 3242 | Expr *pX; |
| 3243 | pX = pEnd->pExpr; |
| 3244 | assert( pX!=0 ); |
| 3245 | assert( pEnd->leftCursor==iCur ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3246 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3247 | memEndValue = ++pParse->nMem; |
| 3248 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 3249 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 3250 | testOp = bRev ? OP_Le : OP_Ge; |
| 3251 | }else{ |
| 3252 | testOp = bRev ? OP_Lt : OP_Gt; |
| 3253 | } |
| 3254 | disableTerm(pLevel, pEnd); |
| 3255 | } |
| 3256 | start = sqlite3VdbeCurrentAddr(v); |
| 3257 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 3258 | pLevel->p1 = iCur; |
| 3259 | pLevel->p2 = start; |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 3260 | if( pStart==0 && pEnd==0 ){ |
| 3261 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3262 | }else{ |
| 3263 | assert( pLevel->p5==0 ); |
| 3264 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3265 | if( testOp!=OP_Noop ){ |
| 3266 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3267 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3268 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3269 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 3270 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3271 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3272 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3273 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3274 | ** |
| 3275 | ** The WHERE clause may contain zero or more equality |
| 3276 | ** terms ("==" or "IN" operators) that refer to the N |
| 3277 | ** left-most columns of the index. It may also contain |
| 3278 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3279 | ** column that immediately follows the N equalities. Only |
| 3280 | ** the right-most column can be an inequality - the rest must |
| 3281 | ** use the "==" and "IN" operators. For example, if the |
| 3282 | ** index is on (x,y,z), then the following clauses are all |
| 3283 | ** optimized: |
| 3284 | ** |
| 3285 | ** x=5 |
| 3286 | ** x=5 AND y=10 |
| 3287 | ** x=5 AND y<10 |
| 3288 | ** x=5 AND y>5 AND y<10 |
| 3289 | ** x=5 AND y=5 AND z<=10 |
| 3290 | ** |
| 3291 | ** The z<10 term of the following cannot be used, only |
| 3292 | ** the x=5 term: |
| 3293 | ** |
| 3294 | ** x=5 AND z<10 |
| 3295 | ** |
| 3296 | ** N may be zero if there are inequality constraints. |
| 3297 | ** If there are no inequality constraints, then N is at |
| 3298 | ** least one. |
| 3299 | ** |
| 3300 | ** This case is also used when there are no WHERE clause |
| 3301 | ** constraints but an index is selected anyway, in order |
| 3302 | ** to force the output order to conform to an ORDER BY. |
| 3303 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3304 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3305 | 0, |
| 3306 | 0, |
| 3307 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3308 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 3309 | OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3310 | OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ |
| 3311 | OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ |
| 3312 | OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ |
| 3313 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3314 | static const u8 aEndOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3315 | OP_Noop, /* 0: (!end_constraints) */ |
| 3316 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 3317 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 3318 | }; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3319 | int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 3320 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3321 | int regBase; /* Base register holding constraint values */ |
| 3322 | int r1; /* Temp register */ |
| 3323 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3324 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3325 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3326 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3327 | int start_constraints; /* Start of range is constrained */ |
| 3328 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3329 | Index *pIdx; /* The index we will be using */ |
| 3330 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3331 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3332 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3333 | char *zStartAff; /* Affinity for start of range constraint */ |
| 3334 | char *zEndAff; /* Affinity for end of range constraint */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3335 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3336 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3337 | iIdxCur = pLevel->iIdxCur; |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3338 | k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3339 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3340 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3341 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3342 | ** query, then the caller will only allow the loop to run for |
| 3343 | ** a single iteration. This means that the first row returned |
| 3344 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3345 | ** the first one after the nEq equality constraints in the index, |
| 3346 | ** this requires some special handling. |
| 3347 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3348 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3349 | && (pWInfo->nOBSat>0) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3350 | && (pIdx->nColumn>nEq) |
| 3351 | ){ |
| 3352 | /* assert( pOrderBy->nExpr==1 ); */ |
| 3353 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 3354 | isMinQuery = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3355 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | /* Find any inequality constraint terms for the start and end |
| 3359 | ** of the range. |
| 3360 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3361 | j = nEq; |
| 3362 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3363 | pRangeStart = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3364 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3365 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3366 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3367 | pRangeEnd = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3368 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3371 | /* Generate code to evaluate all constraint terms using == or IN |
| 3372 | ** and store the values of those terms in an array of registers |
| 3373 | ** starting at regBase. |
| 3374 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3375 | regBase = codeAllEqualityTerms( |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3376 | pParse, pLevel, pWC, notReady, bRev, nExtraReg, &zStartAff |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3377 | ); |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3378 | zEndAff = sqlite3DbStrDup(pParse->db, zStartAff); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3379 | addrNxt = pLevel->addrNxt; |
| 3380 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3381 | /* If we are doing a reverse order scan on an ascending index, or |
| 3382 | ** a forward order scan on a descending index, interchange the |
| 3383 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3384 | */ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3385 | if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3386 | || (bRev && pIdx->nColumn==nEq) |
| 3387 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3388 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 3389 | } |
| 3390 | |
| 3391 | testcase( pRangeStart && pRangeStart->eOperator & WO_LE ); |
| 3392 | testcase( pRangeStart && pRangeStart->eOperator & WO_GE ); |
| 3393 | testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE ); |
| 3394 | testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE ); |
| 3395 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3396 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3397 | start_constraints = pRangeStart || nEq>0; |
| 3398 | |
| 3399 | /* Seek the index cursor to the start of the range. */ |
| 3400 | nConstraint = nEq; |
| 3401 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3402 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3403 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3404 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){ |
| 3405 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3406 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3407 | if( zStartAff ){ |
| 3408 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3409 | /* Since the comparison is to be performed with no conversions |
| 3410 | ** applied to the operands, set the affinity to apply to pRight to |
| 3411 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3412 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3413 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3414 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3415 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3416 | } |
| 3417 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3418 | nConstraint++; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3419 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3420 | }else if( isMinQuery ){ |
| 3421 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3422 | nConstraint++; |
| 3423 | startEq = 0; |
| 3424 | start_constraints = 1; |
| 3425 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3426 | codeApplyAffinity(pParse, regBase, nConstraint, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3427 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3428 | assert( op!=0 ); |
| 3429 | testcase( op==OP_Rewind ); |
| 3430 | testcase( op==OP_Last ); |
| 3431 | testcase( op==OP_SeekGt ); |
| 3432 | testcase( op==OP_SeekGe ); |
| 3433 | testcase( op==OP_SeekLe ); |
| 3434 | testcase( op==OP_SeekLt ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3435 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3436 | |
| 3437 | /* Load the value for the inequality constraint at the end of the |
| 3438 | ** range (if any). |
| 3439 | */ |
| 3440 | nConstraint = nEq; |
| 3441 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3442 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3443 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3444 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3445 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ |
| 3446 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3447 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3448 | if( zEndAff ){ |
| 3449 | if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3450 | /* Since the comparison is to be performed with no conversions |
| 3451 | ** applied to the operands, set the affinity to apply to pRight to |
| 3452 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3453 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3454 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3455 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ |
| 3456 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3457 | } |
| 3458 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3459 | codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3460 | nConstraint++; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3461 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3462 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3463 | sqlite3DbFree(pParse->db, zStartAff); |
| 3464 | sqlite3DbFree(pParse->db, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3465 | |
| 3466 | /* Top of the loop body */ |
| 3467 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3468 | |
| 3469 | /* Check if the index cursor is past the end of the range. */ |
| 3470 | op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)]; |
| 3471 | testcase( op==OP_Noop ); |
| 3472 | testcase( op==OP_IdxGE ); |
| 3473 | testcase( op==OP_IdxLT ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3474 | if( op!=OP_Noop ){ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3475 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3476 | sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0); |
| 3477 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3478 | |
| 3479 | /* If there are inequality constraints, check that the value |
| 3480 | ** of the table column that the inequality contrains is not NULL. |
| 3481 | ** If it is, jump to the next iteration of the loop. |
| 3482 | */ |
| 3483 | r1 = sqlite3GetTempReg(pParse); |
| 3484 | testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ); |
| 3485 | testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3486 | if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3487 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 3488 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 3489 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3490 | sqlite3ReleaseTempReg(pParse, r1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3491 | |
| 3492 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3493 | disableTerm(pLevel, pRangeStart); |
| 3494 | disableTerm(pLevel, pRangeEnd); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3495 | if( !omitTable ){ |
| 3496 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3497 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3498 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3499 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3500 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3501 | |
| 3502 | /* Record the instruction used to terminate the loop. Disable |
| 3503 | ** WHERE clause terms made redundant by the index range scan. |
| 3504 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3505 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3506 | pLevel->op = OP_Noop; |
| 3507 | }else if( bRev ){ |
| 3508 | pLevel->op = OP_Prev; |
| 3509 | }else{ |
| 3510 | pLevel->op = OP_Next; |
| 3511 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3512 | pLevel->p1 = iIdxCur; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3513 | if( (pLoop->wsFlags & (WHERE_COLUMN_EQ | WHERE_COLUMN_RANGE | |
| 3514 | WHERE_COLUMN_NULL | WHERE_COLUMN_IN))==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3515 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3516 | }else{ |
| 3517 | assert( pLevel->p5==0 ); |
| 3518 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3519 | }else |
| 3520 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3521 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3522 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3523 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3524 | ** |
| 3525 | ** Example: |
| 3526 | ** |
| 3527 | ** CREATE TABLE t1(a,b,c,d); |
| 3528 | ** CREATE INDEX i1 ON t1(a); |
| 3529 | ** CREATE INDEX i2 ON t1(b); |
| 3530 | ** CREATE INDEX i3 ON t1(c); |
| 3531 | ** |
| 3532 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3533 | ** |
| 3534 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3535 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3536 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3537 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3538 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3539 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3540 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3541 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3542 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3543 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3544 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3545 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3546 | ** Gosub 2 A |
| 3547 | ** sqlite3WhereEnd() |
| 3548 | ** |
| 3549 | ** Following the above, code to terminate the loop. Label A, the target |
| 3550 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3551 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3552 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3553 | ** Goto B # The loop is finished. |
| 3554 | ** |
| 3555 | ** A: <loop body> # Return data, whatever. |
| 3556 | ** |
| 3557 | ** Return 2 # Jump back to the Gosub |
| 3558 | ** |
| 3559 | ** B: <after the loop> |
| 3560 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3561 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3562 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3563 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3564 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3565 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3566 | |
| 3567 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3568 | int regRowset = 0; /* Register for RowSet object */ |
| 3569 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3570 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3571 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3572 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3573 | int ii; /* Loop counter */ |
| 3574 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3575 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3576 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3577 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3578 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3579 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3580 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3581 | pLevel->op = OP_Return; |
| 3582 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3583 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3584 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3585 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3586 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3587 | */ |
| 3588 | if( pWInfo->nLevel>1 ){ |
| 3589 | int nNotReady; /* The number of notReady tables */ |
| 3590 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3591 | nNotReady = pWInfo->nLevel - iLevel - 1; |
| 3592 | pOrTab = sqlite3StackAllocRaw(pParse->db, |
| 3593 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3594 | if( pOrTab==0 ) return notReady; |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3595 | pOrTab->nAlloc = (i16)(nNotReady + 1); |
| 3596 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3597 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3598 | origSrc = pWInfo->pTabList->a; |
| 3599 | for(k=1; k<=nNotReady; k++){ |
| 3600 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3601 | } |
| 3602 | }else{ |
| 3603 | pOrTab = pWInfo->pTabList; |
| 3604 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3605 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3606 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 3607 | ** equivalent to an empty rowset. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3608 | ** |
| 3609 | ** Also initialize regReturn to contain the address of the instruction |
| 3610 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3611 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3612 | ** over the top of the loop into the body of it. In this case the |
| 3613 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3614 | ** fall through to the next instruction, just as an OP_Next does if |
| 3615 | ** called on an uninitialized cursor. |
| 3616 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3617 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3618 | regRowset = ++pParse->nMem; |
| 3619 | regRowid = ++pParse->nMem; |
| 3620 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3621 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3622 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3623 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3624 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3625 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3626 | ** That way, terms in y that are factored into the disjunction will |
| 3627 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3628 | ** |
| 3629 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3630 | ** the "interesting" terms of z - terms that did not originate in the |
| 3631 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3632 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3633 | ** |
| 3634 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3635 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3636 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3637 | */ |
| 3638 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3639 | int iTerm; |
| 3640 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3641 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3642 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3643 | if( pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_ORINFO) ) continue; |
| 3644 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3645 | pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 3646 | pAndExpr = sqlite3ExprAnd(pParse->db, pAndExpr, pExpr); |
| 3647 | } |
| 3648 | if( pAndExpr ){ |
| 3649 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3650 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3653 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3654 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3655 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3656 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3657 | Expr *pOrExpr = pOrTerm->pExpr; |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3658 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3659 | pAndExpr->pLeft = pOrExpr; |
| 3660 | pOrExpr = pAndExpr; |
| 3661 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3662 | /* Loop through table entries that match term pOrTerm. */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3663 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 3664 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3665 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3666 | assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3667 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3668 | WhereLoop *pSubLoop; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3669 | explainOneScan( |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3670 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3671 | ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3672 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3673 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3674 | int r; |
| 3675 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3676 | regRowid, 0); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3677 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| 3678 | sqlite3VdbeCurrentAddr(v)+2, r, iSet); |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3679 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3680 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 3681 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3682 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 3683 | ** contained one or more AND term from a notReady table. The |
| 3684 | ** terms from the notReady table could not be tested and will |
| 3685 | ** need to be tested later. |
| 3686 | */ |
| 3687 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 3688 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3689 | /* If all of the OR-connected terms are optimized using the same |
| 3690 | ** index, and the index is opened using the same cursor number |
| 3691 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 3692 | ** be possible to use that index as a covering index. |
| 3693 | ** |
| 3694 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 3695 | ** uses an index, and this is either the first OR-connected term |
| 3696 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3697 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 3698 | ** pCov to NULL to indicate that no candidate covering index will |
| 3699 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3700 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3701 | pSubLoop = pSubWInfo->a[0].pWLoop; |
| 3702 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
| 3703 | && (pSubLoop->wsFlags & WHERE_TEMP_INDEX)==0 |
| 3704 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3705 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3706 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
drh | 907717f | 2013-06-04 18:03:22 +0000 | [diff] [blame] | 3707 | pCov = pSubLoop->u.btree.pIndex; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3708 | }else{ |
| 3709 | pCov = 0; |
| 3710 | } |
| 3711 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3712 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 3713 | sqlite3WhereEnd(pSubWInfo); |
| 3714 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3715 | } |
| 3716 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 3717 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 3718 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3719 | if( pAndExpr ){ |
| 3720 | pAndExpr->pLeft = 0; |
| 3721 | sqlite3ExprDelete(pParse->db, pAndExpr); |
| 3722 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3723 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3724 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 3725 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 3726 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3727 | if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab); |
| 3728 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3729 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3730 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3731 | |
| 3732 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3733 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3734 | ** scan of the entire table. |
| 3735 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3736 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 3737 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 3738 | assert( bRev==0 || bRev==1 ); |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3739 | pLevel->op = aStep[bRev]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3740 | pLevel->p1 = iCur; |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3741 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3742 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3743 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3744 | newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3745 | |
| 3746 | /* Insert code to test every subexpression that can be completely |
| 3747 | ** computed using the current set of tables. |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3748 | ** |
| 3749 | ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through |
| 3750 | ** the use of indices become tests that are evaluated against each row of |
| 3751 | ** the relevant input tables. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3752 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3753 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3754 | Expr *pE; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3755 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3756 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3757 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3758 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3759 | testcase( pWInfo->untestedTerms==0 |
| 3760 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 3761 | pWInfo->untestedTerms = 1; |
| 3762 | continue; |
| 3763 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3764 | pE = pTerm->pExpr; |
| 3765 | assert( pE!=0 ); |
| 3766 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 3767 | continue; |
| 3768 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3769 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3770 | pTerm->wtFlags |= TERM_CODED; |
| 3771 | } |
| 3772 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3773 | /* Insert code to test for implied constraints based on transitivity |
| 3774 | ** of the "==" operator. |
| 3775 | ** |
| 3776 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 3777 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 3778 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 3779 | ** the implied "t1.a=123" constraint. |
| 3780 | */ |
| 3781 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3782 | Expr *pE; |
| 3783 | WhereTerm *pAlt; |
| 3784 | Expr sEq; |
| 3785 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 3786 | if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue; |
| 3787 | if( pTerm->leftCursor!=iCur ) continue; |
| 3788 | pE = pTerm->pExpr; |
| 3789 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 3790 | assert( (pTerm->prereqRight & newNotReady)!=0 ); |
| 3791 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 3792 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 3793 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3794 | VdbeNoopComment((v, "begin transitive constraint")); |
| 3795 | sEq = *pAlt->pExpr; |
| 3796 | sEq.pLeft = pE->pLeft; |
| 3797 | sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL); |
| 3798 | } |
| 3799 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3800 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 3801 | ** at least one row of the right table has matched the left table. |
| 3802 | */ |
| 3803 | if( pLevel->iLeftJoin ){ |
| 3804 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 3805 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 3806 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3807 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3808 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3809 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3810 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3811 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3812 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 3813 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3814 | continue; |
| 3815 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3816 | assert( pTerm->pExpr ); |
| 3817 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 3818 | pTerm->wtFlags |= TERM_CODED; |
| 3819 | } |
| 3820 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3821 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3822 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3823 | return newNotReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3826 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3827 | /* |
| 3828 | ** Print a WhereLoop object for debugging purposes |
| 3829 | */ |
| 3830 | static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){ |
| 3831 | int nb = 2*((pTabList->nSrc+15)/16); |
| 3832 | struct SrcList_item *pItem = pTabList->a + p->iTab; |
| 3833 | Table *pTab = pItem->pTab; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3834 | sqlite3DebugPrintf("%c %2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 3835 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
| 3836 | sqlite3DebugPrintf(" %8s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3837 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3838 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 3839 | if( p->u.btree.pIndex ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3840 | const char *zName = p->u.btree.pIndex->zName; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3841 | if( zName==0 ) zName = "ipk"; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3842 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 3843 | int i = sqlite3Strlen30(zName) - 1; |
| 3844 | while( zName[i]!='_' ) i--; |
| 3845 | zName += i; |
| 3846 | } |
| 3847 | sqlite3DebugPrintf(".%-12s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3848 | }else{ |
| 3849 | sqlite3DebugPrintf("%16s",""); |
| 3850 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3851 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3852 | char *z; |
| 3853 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3854 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 3855 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3856 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3857 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3858 | } |
| 3859 | sqlite3DebugPrintf(" %-15s", z); |
| 3860 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3861 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3862 | sqlite3DebugPrintf(" fg %05x N %d", p->wsFlags, p->nLTerm); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3863 | sqlite3DebugPrintf(" cost %.2g,%.2g,%.2g\n", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3864 | p->prereq, p->rSetup, p->rRun, p->nOut); |
| 3865 | } |
| 3866 | #endif |
| 3867 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3868 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3869 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 3870 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3871 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3872 | static void whereLoopInit(WhereLoop *p){ |
| 3873 | p->aLTerm = p->aLTermSpace; |
| 3874 | p->nLTerm = 0; |
| 3875 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 3876 | p->wsFlags = 0; |
| 3877 | } |
| 3878 | |
| 3879 | /* |
| 3880 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 3881 | */ |
| 3882 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3883 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_TEMP_INDEX) ){ |
| 3884 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 3885 | sqlite3_free(p->u.vtab.idxStr); |
| 3886 | p->u.vtab.needFree = 0; |
| 3887 | p->u.vtab.idxStr = 0; |
| 3888 | }else if( (p->wsFlags & WHERE_TEMP_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
| 3889 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 3890 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 3891 | p->u.btree.pIndex = 0; |
| 3892 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3893 | } |
| 3894 | } |
| 3895 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3896 | /* |
| 3897 | ** Deallocate internal memory used by a WhereLoop object |
| 3898 | */ |
| 3899 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 3900 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 3901 | whereLoopClearUnion(db, p); |
| 3902 | whereLoopInit(p); |
| 3903 | } |
| 3904 | |
| 3905 | /* |
| 3906 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 3907 | */ |
| 3908 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 3909 | WhereTerm **paNew; |
| 3910 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 3911 | n = (n+7)&~7; |
| 3912 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 3913 | if( paNew==0 ) return SQLITE_NOMEM; |
| 3914 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 3915 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 3916 | p->aLTerm = paNew; |
| 3917 | p->nLSlot = n; |
| 3918 | return SQLITE_OK; |
| 3919 | } |
| 3920 | |
| 3921 | /* |
| 3922 | ** Transfer content from the second pLoop into the first. |
| 3923 | */ |
| 3924 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
| 3925 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ) return SQLITE_NOMEM; |
| 3926 | whereLoopClearUnion(db, pTo); |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 3927 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 3928 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3929 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 3930 | pFrom->u.vtab.needFree = 0; |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3931 | }else if( (pFrom->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3932 | pFrom->u.btree.pIndex = 0; |
| 3933 | } |
| 3934 | return SQLITE_OK; |
| 3935 | } |
| 3936 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3937 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3938 | ** Delete a WhereLoop object |
| 3939 | */ |
| 3940 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3941 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3942 | sqlite3DbFree(db, p); |
| 3943 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 3944 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 3945 | /* |
| 3946 | ** Free a WhereInfo structure |
| 3947 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 3948 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 3949 | if( ALWAYS(pWInfo) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3950 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3951 | while( pWInfo->pLoops ){ |
| 3952 | WhereLoop *p = pWInfo->pLoops; |
| 3953 | pWInfo->pLoops = p->pNextLoop; |
| 3954 | whereLoopDelete(db, p); |
| 3955 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3956 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 3957 | } |
| 3958 | } |
| 3959 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3960 | /* |
| 3961 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 3962 | ** |
| 3963 | ** An existing WhereLoop entry might be overwritten if the new template |
| 3964 | ** is better and has fewer dependencies. Or the template will be ignored |
| 3965 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 3966 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 3967 | ** added based on the template. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3968 | ** |
| 3969 | ** If pBuilder->pBest is not NULL then we only care about the very |
| 3970 | ** best template and that template should be stored in pBuilder->pBest. |
| 3971 | ** If pBuilder->pBest is NULL then a list of the best templates are stored |
| 3972 | ** in pBuilder->pWInfo->pLoops. |
| 3973 | ** |
| 3974 | ** When accumulating multiple loops (when pBuilder->pBest is NULL) we |
| 3975 | ** still might overwrite similar loops with the new template if the |
| 3976 | ** template is better. Loops may be overwritten if the following |
| 3977 | ** conditions are met: |
| 3978 | ** |
| 3979 | ** (1) They have the same iTab. |
| 3980 | ** (2) They have the same iSortIdx. |
| 3981 | ** (3) The template has same or fewer dependencies than the current loop |
| 3982 | ** (4) The template has the same or lower cost than the current loop |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 3983 | ** (5) The template uses more terms of the same index but has no additional |
| 3984 | ** dependencies |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3985 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3986 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3987 | WhereLoop **ppPrev, *p, *pNext = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3988 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3989 | sqlite3 *db = pWInfo->pParse->db; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3990 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3991 | /* If pBuilder->pBest is defined, then only keep track of the single |
| 3992 | ** best WhereLoop. pBuilder->pBest->maskSelf==0 indicates that no |
| 3993 | ** prior WhereLoops have been evaluated and that the current pTemplate |
| 3994 | ** is therefore the first and hence the best and should be retained. |
| 3995 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3996 | if( (p = pBuilder->pBest)!=0 ){ |
| 3997 | if( p->maskSelf!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3998 | WhereCost rCost = p->rRun + p->rSetup; |
| 3999 | WhereCost rTemplate = pTemplate->rRun + pTemplate->rSetup; |
| 4000 | if( rCost < rTemplate ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4001 | goto whereLoopInsert_noop; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4002 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4003 | if( rCost == rTemplate && p->prereq <= pTemplate->prereq ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4004 | goto whereLoopInsert_noop; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4005 | } |
| 4006 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4007 | whereLoopXfer(db, p, pTemplate); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4008 | #if WHERETRACE_ENABLED |
| 4009 | if( sqlite3WhereTrace & 0x8 ){ |
| 4010 | sqlite3DebugPrintf("ins-best: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4011 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4012 | } |
| 4013 | #endif |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4014 | return SQLITE_OK; |
| 4015 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4016 | |
| 4017 | /* Search for an existing WhereLoop to overwrite, or which takes |
| 4018 | ** priority over pTemplate. |
| 4019 | */ |
| 4020 | for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4021 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ) continue; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4022 | if( (p->prereq & pTemplate->prereq)==p->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4023 | && p->rSetup<=pTemplate->rSetup |
| 4024 | && p->rRun<=pTemplate->rRun |
| 4025 | ){ |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4026 | /* p is equal or better than pTemplate */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4027 | if( p->nLTerm<pTemplate->nLTerm |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4028 | && (p->wsFlags & WHERE_INDEXED)!=0 |
| 4029 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 4030 | && p->u.btree.pIndex==pTemplate->u.btree.pIndex |
| 4031 | && p->prereq==pTemplate->prereq |
| 4032 | ){ |
| 4033 | /* Overwrite an existing WhereLoop with an similar one that uses |
| 4034 | ** more terms of the index */ |
| 4035 | pNext = p->pNextLoop; |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4036 | break; |
| 4037 | }else{ |
| 4038 | /* pTemplate is not helpful. |
| 4039 | ** Return without changing or adding anything */ |
| 4040 | goto whereLoopInsert_noop; |
| 4041 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4042 | } |
| 4043 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4044 | && p->rSetup>=pTemplate->rSetup |
| 4045 | && p->rRun>=pTemplate->rRun |
| 4046 | ){ |
| 4047 | /* Overwrite an existing WhereLoop with a better one */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4048 | pNext = p->pNextLoop; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4049 | break; |
| 4050 | } |
| 4051 | } |
| 4052 | |
| 4053 | /* If we reach this point it means that either p[] should be overwritten |
| 4054 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 4055 | ** WhereLoop and insert it. |
| 4056 | */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4057 | #if WHERETRACE_ENABLED |
| 4058 | if( sqlite3WhereTrace & 0x8 ){ |
| 4059 | if( p!=0 ){ |
| 4060 | sqlite3DebugPrintf("ins-del: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4061 | whereLoopPrint(p, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4062 | } |
| 4063 | sqlite3DebugPrintf("ins-new: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4064 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4065 | } |
| 4066 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4067 | if( p==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4068 | p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4069 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4070 | whereLoopInit(p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4071 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4072 | whereLoopXfer(db, p, pTemplate); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4073 | p->pNextLoop = pNext; |
| 4074 | *ppPrev = p; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4075 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4076 | Index *pIndex = p->u.btree.pIndex; |
| 4077 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4078 | p->u.btree.pIndex = 0; |
| 4079 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4080 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4081 | return SQLITE_OK; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4082 | |
| 4083 | /* Jump here if the insert is a no-op */ |
| 4084 | whereLoopInsert_noop: |
| 4085 | #if WHERETRACE_ENABLED |
| 4086 | if( sqlite3WhereTrace & 0x8 ){ |
| 4087 | sqlite3DebugPrintf("ins-noop: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4088 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4089 | } |
| 4090 | #endif |
| 4091 | return SQLITE_OK; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | /* |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4095 | ** 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] | 4096 | ** Try to match one more. |
| 4097 | ** |
| 4098 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 4099 | ** INTEGER PRIMARY KEY. |
| 4100 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4101 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4102 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 4103 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 4104 | Index *pProbe, /* An index on pSrc */ |
| 4105 | int nInMul /* Number of iterations due to IN */ |
| 4106 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4107 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 4108 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 4109 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4110 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 4111 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4112 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4113 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4114 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 4115 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 4116 | int saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 4117 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 4118 | WhereCost saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4119 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4120 | int rc = SQLITE_OK; /* Return code */ |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4121 | tRowcnt iRowEst; /* Estimated index selectivity */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4122 | WhereCost rLogSize; /* Logarithm of table size */ |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4123 | WhereTerm *pTop, *pBtm; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4124 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4125 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4126 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4127 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4128 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4129 | assert( pNew->u.btree.nEq<=pProbe->nColumn ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4130 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 4131 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 4132 | opMask = WO_LT|WO_LE; |
| 4133 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 4134 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4135 | }else{ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4136 | 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] | 4137 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4138 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4139 | |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4140 | if( pNew->u.btree.nEq < pProbe->nColumn ){ |
| 4141 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 4142 | iRowEst = pProbe->aiRowEst[pNew->u.btree.nEq+1]; |
| 4143 | }else{ |
| 4144 | iCol = -1; |
| 4145 | iRowEst = 1; |
| 4146 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4147 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4148 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4149 | saved_nEq = pNew->u.btree.nEq; |
| 4150 | saved_nLTerm = pNew->nLTerm; |
| 4151 | saved_wsFlags = pNew->wsFlags; |
| 4152 | saved_prereq = pNew->prereq; |
| 4153 | saved_nOut = pNew->nOut; |
| 4154 | pNew->rSetup = (WhereCost)0; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4155 | rLogSize = estLog(pProbe->aiRowEst[0]); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4156 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4157 | int nIn = 1; |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4158 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4159 | pNew->wsFlags = saved_wsFlags; |
| 4160 | pNew->u.btree.nEq = saved_nEq; |
| 4161 | pNew->nLTerm = saved_nLTerm; |
| 4162 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4163 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 4164 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4165 | pNew->rRun = rLogSize; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4166 | if( pTerm->eOperator & WO_IN ){ |
| 4167 | Expr *pExpr = pTerm->pExpr; |
| 4168 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4169 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 4170 | /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */ |
| 4171 | nIn = 25; |
| 4172 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4173 | /* "x IN (value, value, ...)" */ |
| 4174 | nIn = pExpr->x.pList->nExpr; |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4175 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4176 | pNew->rRun *= nIn; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4177 | pNew->u.btree.nEq++; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4178 | pNew->nOut = (WhereCost)iRowEst * nInMul * nIn; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4179 | }else if( pTerm->eOperator & (WO_EQ) ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4180 | assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 |
| 4181 | || nInMul==1 ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4182 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4183 | if( iCol<0 |
drh | ee73b87 | 2013-06-04 13:37:26 +0000 | [diff] [blame] | 4184 | || (pProbe->onError!=OE_None && nInMul==1 |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4185 | && pNew->u.btree.nEq==pProbe->nColumn-1) |
| 4186 | ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4187 | testcase( pNew->wsFlags & WHERE_COLUMN_IN ); |
| 4188 | pNew->wsFlags |= WHERE_ONEROW; |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4189 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4190 | pNew->u.btree.nEq++; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4191 | pNew->nOut = (WhereCost)iRowEst * nInMul; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4192 | }else if( pTerm->eOperator & (WO_ISNULL) ){ |
| 4193 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 4194 | pNew->u.btree.nEq++; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4195 | nIn = 2; /* Assume IS NULL matches two rows */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4196 | pNew->nOut = (WhereCost)iRowEst * nInMul * nIn; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4197 | }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ |
| 4198 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4199 | pBtm = pTerm; |
| 4200 | pTop = 0; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4201 | }else if( pTerm->eOperator & (WO_LT|WO_LE) ){ |
| 4202 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4203 | pTop = pTerm; |
| 4204 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4205 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4206 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4207 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4208 | /* Adjust nOut and rRun for STAT3 range values */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4209 | WhereCost rDiv; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4210 | whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq, |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4211 | pBtm, pTop, &rDiv); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4212 | pNew->nOut = saved_nOut/rDiv; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4213 | } |
| 4214 | #ifdef SQLITE_ENABLE_STAT3 |
| 4215 | if( pNew->u.btree.nEq==1 && pProbe->nSample ){ |
| 4216 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4217 | rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4218 | &pNew->nOut); |
| 4219 | }else if( (pTerm->eOperator & WO_IN) |
| 4220 | && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4221 | rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4222 | &pNew->nOut); |
| 4223 | |
| 4224 | } |
| 4225 | } |
| 4226 | #endif |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4227 | if( pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK) ){ |
| 4228 | pNew->rRun += pNew->nOut; /* Unit step cost to reach each row */ |
| 4229 | }else{ |
| 4230 | /* Each row involves a step of the index, then a binary search of |
| 4231 | ** the main table */ |
| 4232 | pNew->rRun += pNew->nOut*(1 + rLogSize); |
| 4233 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4234 | /* TBD: Adjust nOut for additional constraints */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4235 | rc = whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4236 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4237 | && pNew->u.btree.nEq<=pProbe->nColumn |
| 4238 | && pProbe->zName!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4239 | ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4240 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul*nIn); |
| 4241 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4242 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4243 | pNew->prereq = saved_prereq; |
| 4244 | pNew->u.btree.nEq = saved_nEq; |
| 4245 | pNew->wsFlags = saved_wsFlags; |
| 4246 | pNew->nOut = saved_nOut; |
| 4247 | pNew->nLTerm = saved_nLTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4248 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4252 | ** Return True if it is possible that pIndex might be useful in |
| 4253 | ** implementing the ORDER BY clause in pBuilder. |
| 4254 | ** |
| 4255 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 4256 | ** if there is no way for pIndex to be useful in implementing that |
| 4257 | ** ORDER BY clause. |
| 4258 | */ |
| 4259 | static int indexMightHelpWithOrderBy( |
| 4260 | WhereLoopBuilder *pBuilder, |
| 4261 | Index *pIndex, |
| 4262 | int iCursor |
| 4263 | ){ |
| 4264 | ExprList *pOB; |
| 4265 | int iCol; |
| 4266 | int ii; |
| 4267 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4268 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4269 | iCol = pIndex->aiColumn[0]; |
| 4270 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 4271 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4272 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 4273 | if( pExpr->iTable==iCursor ){ |
| 4274 | if( pExpr->iColumn==iCol ) return 1; |
| 4275 | return 0; |
| 4276 | } |
| 4277 | } |
| 4278 | return 0; |
| 4279 | } |
| 4280 | |
| 4281 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4282 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 4283 | ** the table is used by an index. Only the first 63 columns are considered. |
| 4284 | */ |
| 4285 | static Bitmask columnsUsedByIndex(Index *pIdx){ |
| 4286 | Bitmask m = 0; |
| 4287 | int j; |
| 4288 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 4289 | int x = pIdx->aiColumn[j]; |
| 4290 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 4291 | } |
| 4292 | return m; |
| 4293 | } |
| 4294 | |
| 4295 | |
| 4296 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4297 | ** Add all WhereLoop objects a single table of the join were the table |
| 4298 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 4299 | ** a b-tree table, not a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4300 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4301 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4302 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4303 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4304 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4305 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4306 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4307 | Index sPk; /* A fake index object for the primary key */ |
| 4308 | tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 4309 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4310 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4311 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4312 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4313 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4314 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4315 | int b; /* A boolean value */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4316 | WhereCost rSize; /* number of rows in the table */ |
| 4317 | WhereCost rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4318 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4319 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4320 | pWInfo = pBuilder->pWInfo; |
| 4321 | pTabList = pWInfo->pTabList; |
| 4322 | pSrc = pTabList->a + pNew->iTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4323 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4324 | |
| 4325 | if( pSrc->pIndex ){ |
| 4326 | /* An INDEXED BY clause specifies a particular index to use */ |
| 4327 | pProbe = pSrc->pIndex; |
| 4328 | }else{ |
| 4329 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 4330 | ** variable sPk to represent the rowid primary key index. Make this |
| 4331 | ** fake index the first in a chain of Index objects with all of the real |
| 4332 | ** indices to follow */ |
| 4333 | Index *pFirst; /* First of real indices on the table */ |
| 4334 | memset(&sPk, 0, sizeof(Index)); |
| 4335 | sPk.nColumn = 1; |
| 4336 | sPk.aiColumn = &aiColumnPk; |
| 4337 | sPk.aiRowEst = aiRowEstPk; |
| 4338 | sPk.onError = OE_Replace; |
| 4339 | sPk.pTable = pSrc->pTab; |
| 4340 | aiRowEstPk[0] = pSrc->pTab->nRowEst; |
| 4341 | aiRowEstPk[1] = 1; |
| 4342 | pFirst = pSrc->pTab->pIndex; |
| 4343 | if( pSrc->notIndexed==0 ){ |
| 4344 | /* The real indices of the table are only considered if the |
| 4345 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 4346 | sPk.pNext = pFirst; |
| 4347 | } |
| 4348 | pProbe = &sPk; |
| 4349 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4350 | rSize = (WhereCost)pSrc->pTab->nRowEst; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4351 | rLogSize = estLog(rSize); |
| 4352 | |
| 4353 | /* Automatic indexes */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4354 | if( !pBuilder->pBest |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4355 | && pTabList->nSrc>1 |
| 4356 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4357 | && !pSrc->viaCoroutine |
| 4358 | && !pSrc->notIndexed |
| 4359 | && !pSrc->isCorrelated |
| 4360 | ){ |
| 4361 | /* Generate auto-index WhereLoops */ |
| 4362 | WhereClause *pWC = pBuilder->pWC; |
| 4363 | WhereTerm *pTerm; |
| 4364 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 4365 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4366 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4367 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 4368 | pNew->u.btree.nEq = 1; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4369 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4370 | pNew->nLTerm = 1; |
| 4371 | pNew->aLTerm[0] = pTerm; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4372 | pNew->rSetup = 20*rLogSize*pSrc->pTab->nRowEst; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4373 | pNew->nOut = (WhereCost)10; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4374 | pNew->rRun = rLogSize + pNew->nOut; |
| 4375 | pNew->wsFlags = WHERE_TEMP_INDEX; |
| 4376 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4377 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4378 | } |
| 4379 | } |
| 4380 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4381 | |
| 4382 | /* Loop over all indices |
| 4383 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4384 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4385 | pNew->u.btree.nEq = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4386 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4387 | pNew->iSortIdx = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4388 | pNew->rSetup = (WhereCost)0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4389 | pNew->prereq = mExtra; |
| 4390 | pNew->u.btree.pIndex = pProbe; |
| 4391 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4392 | if( pProbe->tnum<=0 ){ |
| 4393 | /* Integer primary key index */ |
| 4394 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4395 | |
| 4396 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4397 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4398 | pNew->nOut = rSize; |
| 4399 | pNew->rRun = (rSize + rLogSize)*(3+b); /* 4x penalty for a full-scan */ |
| 4400 | rc = whereLoopInsert(pBuilder, pNew); |
| 4401 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4402 | }else{ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4403 | Bitmask m = pSrc->colUsed & ~columnsUsedByIndex(pProbe); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4404 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4405 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4406 | /* Full scan via index */ |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4407 | if( (m==0 || b) |
| 4408 | && pProbe->bUnordered==0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4409 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4410 | && sqlite3GlobalConfig.bUseCis |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4411 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4412 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4413 | pNew->iSortIdx = b ? iSortIdx : 0; |
| 4414 | pNew->nOut = rSize; |
| 4415 | pNew->rRun = (m==0) ? (rSize + rLogSize)*(1+b) : (rSize*rLogSize); |
| 4416 | rc = whereLoopInsert(pBuilder, pNew); |
| 4417 | if( rc ) break; |
| 4418 | } |
| 4419 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4420 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 1); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4421 | |
| 4422 | /* If there was an INDEXED BY clause, then only that one index is |
| 4423 | ** considered. */ |
| 4424 | if( pSrc->pIndex ) break; |
| 4425 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4426 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4430 | ** Add all WhereLoop objects for a table of the join identified by |
| 4431 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4432 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4433 | static int whereLoopAddVirtual( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4434 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4435 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4436 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4437 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4438 | Parse *pParse; /* The parsing context */ |
| 4439 | WhereClause *pWC; /* The WHERE clause */ |
| 4440 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 4441 | Table *pTab; |
| 4442 | sqlite3 *db; |
| 4443 | sqlite3_index_info *pIdxInfo; |
| 4444 | struct sqlite3_index_constraint *pIdxCons; |
| 4445 | struct sqlite3_index_constraint_usage *pUsage; |
| 4446 | WhereTerm *pTerm; |
| 4447 | int i, j; |
| 4448 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4449 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4450 | int seenIn = 0; /* True if an IN operator is seen */ |
| 4451 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 4452 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 4453 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4454 | int rc = SQLITE_OK; |
| 4455 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4456 | pWInfo = pBuilder->pWInfo; |
| 4457 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4458 | db = pParse->db; |
| 4459 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4460 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4461 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4462 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4463 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4464 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4465 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4466 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4467 | pNew->rSetup = 0; |
| 4468 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4469 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4470 | pNew->u.vtab.needFree = 0; |
| 4471 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4472 | nConstraint = pIdxInfo->nConstraint; |
| 4473 | if( whereLoopResize(db, pNew, nConstraint) ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4474 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4475 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4476 | if( !seenIn && (iPhase&1)!=0 ){ |
| 4477 | iPhase++; |
| 4478 | if( iPhase>3 ) break; |
| 4479 | } |
| 4480 | if( !seenVar && iPhase>1 ) break; |
| 4481 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4482 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4483 | j = pIdxCons->iTermOffset; |
| 4484 | pTerm = &pWC->a[j]; |
| 4485 | switch( iPhase ){ |
| 4486 | case 0: /* Constants without IN operator */ |
| 4487 | pIdxCons->usable = 0; |
| 4488 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4489 | seenIn = 1; |
| 4490 | }else if( pTerm->prereqRight!=0 ){ |
| 4491 | seenVar = 1; |
| 4492 | }else{ |
| 4493 | pIdxCons->usable = 1; |
| 4494 | } |
| 4495 | break; |
| 4496 | case 1: /* Constants with IN operators */ |
| 4497 | assert( seenIn ); |
| 4498 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 4499 | break; |
| 4500 | case 2: /* Variables without IN */ |
| 4501 | assert( seenVar ); |
| 4502 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 4503 | break; |
| 4504 | default: /* Variables with IN */ |
| 4505 | assert( seenVar && seenIn ); |
| 4506 | pIdxCons->usable = 1; |
| 4507 | break; |
| 4508 | } |
| 4509 | } |
| 4510 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 4511 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4512 | pIdxInfo->idxStr = 0; |
| 4513 | pIdxInfo->idxNum = 0; |
| 4514 | pIdxInfo->needToFreeIdxStr = 0; |
| 4515 | pIdxInfo->orderByConsumed = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4516 | /* ((WhereCost)2) In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 4517 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((WhereCost)2); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4518 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 4519 | if( rc ) goto whereLoopAddVtab_exit; |
| 4520 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4521 | pNew->prereq = 0; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4522 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4523 | assert( pNew->nLSlot>=nConstraint ); |
| 4524 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4525 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4526 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4527 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 4528 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4529 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4530 | || j<0 |
| 4531 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4532 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4533 | ){ |
| 4534 | rc = SQLITE_ERROR; |
| 4535 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4536 | goto whereLoopAddVtab_exit; |
| 4537 | } |
| 4538 | pTerm = &pWC->a[j]; |
| 4539 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4540 | assert( iTerm<pNew->nLSlot ); |
| 4541 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4542 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4543 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4544 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4545 | if( pUsage[i].omit==0 ){ |
| 4546 | /* Do not attempt to use an IN constraint if the virtual table |
| 4547 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4548 | ** If we do attempt to use such a constraint, some rows might be |
| 4549 | ** repeated in the output. */ |
| 4550 | break; |
| 4551 | } |
| 4552 | /* A virtual table that is constrained by an IN clause may not |
| 4553 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4554 | ** is not necessarily related to the order of output terms and |
| 4555 | ** (2) Multiple outputs from a single IN value will not merge |
| 4556 | ** together. */ |
| 4557 | pIdxInfo->orderByConsumed = 0; |
| 4558 | } |
| 4559 | } |
| 4560 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4561 | if( i>=nConstraint ){ |
| 4562 | pNew->nLTerm = mxTerm+1; |
| 4563 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4564 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4565 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 4566 | pIdxInfo->needToFreeIdxStr = 0; |
| 4567 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 3b1d808 | 2013-06-03 16:56:37 +0000 | [diff] [blame] | 4568 | pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) |
| 4569 | && pIdxInfo->orderByConsumed); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4570 | pNew->rSetup = (WhereCost)0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4571 | pNew->rRun = pIdxInfo->estimatedCost; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4572 | pNew->nOut = (WhereCost)25; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4573 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4574 | if( pNew->u.vtab.needFree ){ |
| 4575 | sqlite3_free(pNew->u.vtab.idxStr); |
| 4576 | pNew->u.vtab.needFree = 0; |
| 4577 | } |
| 4578 | } |
| 4579 | } |
| 4580 | |
| 4581 | whereLoopAddVtab_exit: |
| 4582 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4583 | sqlite3DbFree(db, pIdxInfo); |
| 4584 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4588 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 4589 | ** btrees or virtual tables. |
| 4590 | */ |
| 4591 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4592 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4593 | WhereClause *pWC; |
| 4594 | WhereLoop *pNew; |
| 4595 | WhereTerm *pTerm, *pWCEnd; |
| 4596 | int rc = SQLITE_OK; |
| 4597 | int iCur; |
| 4598 | WhereClause tempWC; |
| 4599 | WhereLoopBuilder sSubBuild; |
| 4600 | WhereLoop sBest; |
| 4601 | struct SrcList_item *pItem; |
| 4602 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4603 | pWC = pBuilder->pWC; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4604 | if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4605 | pWCEnd = pWC->a + pWC->nTerm; |
| 4606 | pNew = pBuilder->pNew; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4607 | |
| 4608 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 4609 | if( (pTerm->eOperator & WO_OR)!=0 |
| 4610 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 4611 | ){ |
| 4612 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 4613 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 4614 | WhereTerm *pOrTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4615 | WhereCost rTotal = 0; |
| 4616 | WhereCost nRow = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4617 | Bitmask prereq = mExtra; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4618 | |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4619 | whereLoopInit(&sBest); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4620 | pItem = pWInfo->pTabList->a + pNew->iTab; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4621 | iCur = pItem->iCursor; |
| 4622 | sSubBuild = *pBuilder; |
| 4623 | sSubBuild.pOrderBy = 0; |
| 4624 | sSubBuild.pBest = &sBest; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4625 | |
| 4626 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4627 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4628 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 4629 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4630 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4631 | tempWC.pOuter = pWC; |
| 4632 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4633 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4634 | tempWC.a = pOrTerm; |
| 4635 | sSubBuild.pWC = &tempWC; |
| 4636 | }else{ |
| 4637 | continue; |
| 4638 | } |
| 4639 | sBest.maskSelf = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4640 | sBest.rSetup = 0; |
| 4641 | sBest.rRun = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4642 | if( IsVirtual(pItem->pTab) ){ |
| 4643 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
| 4644 | }else{ |
| 4645 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 4646 | } |
| 4647 | if( sBest.maskSelf==0 ) break; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4648 | assert( sBest.rSetup==(WhereCost)0 ); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4649 | rTotal += sBest.rRun; |
| 4650 | nRow += sBest.nOut; |
| 4651 | prereq |= sBest.prereq; |
| 4652 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4653 | assert( pNew->nLSlot>=1 ); |
| 4654 | pNew->nLTerm = 1; |
| 4655 | pNew->aLTerm[0] = pTerm; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4656 | pNew->wsFlags = WHERE_MULTI_OR; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4657 | pNew->rSetup = (WhereCost)0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4658 | pNew->rRun = rTotal; |
| 4659 | pNew->nOut = nRow; |
| 4660 | pNew->prereq = prereq; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4661 | memset(&pNew->u, 0, sizeof(pNew->u)); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4662 | rc = whereLoopInsert(pBuilder, pNew); |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4663 | whereLoopClear(pWInfo->pParse->db, &sBest); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4664 | } |
| 4665 | } |
| 4666 | return rc; |
| 4667 | } |
| 4668 | |
| 4669 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4670 | ** Add all WhereLoop objects for all tables |
| 4671 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4672 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4673 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4674 | Bitmask mExtra = 0; |
| 4675 | Bitmask mPrior = 0; |
| 4676 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4677 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4678 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4679 | sqlite3 *db = pWInfo->pParse->db; |
| 4680 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4681 | int rc = SQLITE_OK; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4682 | WhereLoop *pNew, sNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4683 | |
| 4684 | /* Loop over the tables in the join, from left to right */ |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4685 | pBuilder->pNew = pNew = &sNew; |
| 4686 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4687 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4688 | pNew->iTab = iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4689 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4690 | if( (pItem->jointype & (JT_LEFT|JT_CROSS))!=0 ){ |
| 4691 | mExtra = mPrior; |
| 4692 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4693 | if( IsVirtual(pItem->pTab) ){ |
| 4694 | rc = whereLoopAddVirtual(pBuilder, mExtra); |
| 4695 | }else{ |
| 4696 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 4697 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4698 | if( rc==SQLITE_OK ){ |
| 4699 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 4700 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4701 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4702 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4703 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4704 | whereLoopClear(db, pNew); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4705 | pBuilder->pNew = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4706 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4709 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4710 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4711 | ** parameters) to see if it outputs rows in the requested ORDER BY |
| 4712 | ** (or GROUP BY) without requiring a separate source operation. Return: |
| 4713 | ** |
| 4714 | ** 0: ORDER BY is not satisfied. Sorting required |
| 4715 | ** 1: ORDER BY is satisfied. Omit sorting |
| 4716 | ** -1: Unknown at this time |
| 4717 | ** |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4718 | */ |
| 4719 | static int wherePathSatisfiesOrderBy( |
| 4720 | WhereInfo *pWInfo, /* The WHERE clause */ |
| 4721 | WherePath *pPath, /* The WherePath to check */ |
| 4722 | int nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4723 | int isLastLoop, /* True if pLast is the inner-most loop */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4724 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
| 4725 | Bitmask *pRevMask /* Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4726 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4727 | u8 revSet; /* True if rev is known */ |
| 4728 | u8 rev; /* Composite sort order */ |
| 4729 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4730 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 4731 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 4732 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4733 | u16 nColumn; /* Number of columns in pIndex */ |
| 4734 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 4735 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 4736 | int i, j; /* Loop counters */ |
| 4737 | int iCur; /* Cursor number for current WhereLoop */ |
| 4738 | int iColumn; /* A column number within table iCur */ |
| 4739 | WhereLoop *pLoop; /* Current WhereLoop being processed. */ |
| 4740 | ExprList *pOrderBy = pWInfo->pOrderBy; /* the ORDER BY clause */ |
| 4741 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 4742 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 4743 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 4744 | Index *pIndex; /* The index associated with pLoop */ |
| 4745 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 4746 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 4747 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4748 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4749 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4750 | |
| 4751 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4752 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 4753 | ** 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] | 4754 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 4755 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4756 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 4757 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4758 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4759 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 4760 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 4761 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 4762 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 4763 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 4764 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 4765 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 4766 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 4767 | ** |
| 4768 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 4769 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 4770 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4771 | */ |
| 4772 | |
| 4773 | assert( pOrderBy!=0 ); |
| 4774 | |
| 4775 | /* Sortability of virtual tables is determined by the xBestIndex method |
| 4776 | ** of the virtual table itself */ |
| 4777 | if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4778 | testcase( nLoop>0 ); /* True when outer loops are one-row and match |
| 4779 | ** no ORDER BY terms */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4780 | return pLast->u.vtab.isOrdered; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4781 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4782 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4783 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4784 | nOrderBy = pOrderBy->nExpr; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4785 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 4786 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4787 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4788 | orderDistinctMask = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4789 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4790 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4791 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4792 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4793 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 4794 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 4795 | pIndex = 0; |
| 4796 | nColumn = 0; |
| 4797 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 4798 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4799 | }else{ |
| 4800 | nColumn = pIndex->nColumn; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4801 | isOrderDistinct = pIndex->onError!=OE_None; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 4802 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4803 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4804 | /* For every term of the index that is constrained by == or IS NULL, |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4805 | ** mark off corresponding ORDER BY terms wherever they occur |
| 4806 | ** in the ORDER BY clause. |
| 4807 | */ |
| 4808 | for(i=0; i<pLoop->u.btree.nEq; i++){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4809 | pTerm = pLoop->aLTerm[i]; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4810 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))==0 ) continue; |
| 4811 | iColumn = pTerm->u.leftColumn; |
| 4812 | for(j=0; j<nOrderBy; j++){ |
| 4813 | if( MASKBIT(j) & obSat ) continue; |
| 4814 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[j].pExpr); |
| 4815 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 4816 | if( pOBExpr->iTable!=iCur ) continue; |
| 4817 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 4818 | if( iColumn>=0 ){ |
| 4819 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[j].pExpr); |
| 4820 | if( !pColl ) pColl = db->pDfltColl; |
| 4821 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[i])!=0 ) continue; |
| 4822 | } |
| 4823 | obSat |= MASKBIT(j); |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4824 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4825 | if( obSat==obDone ) return 1; |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4826 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4827 | |
| 4828 | /* Loop through all columns of the index and deal with the ones |
| 4829 | ** that are not constrained by == or IN. |
| 4830 | */ |
| 4831 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4832 | distinctColumns = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4833 | for(j=0; j<=nColumn; j++){ |
| 4834 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 4835 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4836 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4837 | if( j<pLoop->u.btree.nEq |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4838 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4839 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4840 | if( i & WO_ISNULL ) isOrderDistinct = 0; |
| 4841 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4844 | /* Get the column number in the table (iColumn) and sort order |
| 4845 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4846 | */ |
| 4847 | if( j<nColumn ){ |
| 4848 | /* Normal index columns */ |
| 4849 | iColumn = pIndex->aiColumn[j]; |
| 4850 | revIdx = pIndex->aSortOrder[j]; |
| 4851 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4852 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4853 | /* The ROWID column at the end */ |
| 4854 | iColumn = -1; |
| 4855 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4856 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4857 | |
| 4858 | /* An unconstrained column that might be NULL means that this |
| 4859 | ** WhereLoop is not well-ordered |
| 4860 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4861 | if( isOrderDistinct |
| 4862 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4863 | && j>=pLoop->u.btree.nEq |
| 4864 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 4865 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4866 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
| 4869 | /* Find the ORDER BY term that corresponds to the j-th column |
| 4870 | ** of the index and and mark that ORDER BY term off |
| 4871 | */ |
| 4872 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4873 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4874 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 4875 | if( MASKBIT(i) & obSat ) continue; |
| 4876 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4877 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4878 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4879 | if( pOBExpr->iTable!=iCur ) continue; |
| 4880 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 4881 | if( iColumn>=0 ){ |
| 4882 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 4883 | if( !pColl ) pColl = db->pDfltColl; |
| 4884 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 4885 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4886 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4887 | break; |
| 4888 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4889 | if( isMatch ){ |
| 4890 | if( iColumn<0 ) distinctColumns = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4891 | obSat |= MASKBIT(i); |
| 4892 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4893 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 4894 | ** Sort order is irrelevant for a GROUP BY clause. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4895 | if( revSet ){ |
| 4896 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; |
| 4897 | }else{ |
| 4898 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 4899 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 4900 | revSet = 1; |
| 4901 | } |
| 4902 | } |
| 4903 | }else{ |
| 4904 | /* No match found */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4905 | if( j==0 || j<nColumn ) isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4906 | break; |
| 4907 | } |
| 4908 | } /* end Loop over all index columns */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4909 | if( distinctColumns ) isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4910 | } /* end-if not one-row */ |
| 4911 | |
| 4912 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4913 | if( isOrderDistinct ){ |
| 4914 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4915 | for(i=0; i<nOrderBy; i++){ |
| 4916 | Expr *p; |
| 4917 | if( MASKBIT(i) & obSat ) continue; |
| 4918 | p = pOrderBy->a[i].pExpr; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4919 | if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4920 | obSat |= MASKBIT(i); |
| 4921 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 4922 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4923 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4924 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4925 | if( obSat==obDone ) return 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4926 | if( !isOrderDistinct ) return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4927 | if( isLastLoop ) return 1; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4928 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4931 | #ifdef WHERETRACE_ENABLED |
| 4932 | /* For debugging use only: */ |
| 4933 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 4934 | static char zName[65]; |
| 4935 | int i; |
| 4936 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 4937 | if( pLast ) zName[i++] = pLast->cId; |
| 4938 | zName[i] = 0; |
| 4939 | return zName; |
| 4940 | } |
| 4941 | #endif |
| 4942 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4943 | |
| 4944 | /* |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4945 | ** Given the list of WhereLoop objects on pWInfo->pLoops, this routine |
| 4946 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 4947 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 4948 | ** |
| 4949 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 4950 | ** error occurs. |
| 4951 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4952 | static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4953 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4954 | int nLoop; /* Number of terms in the join */ |
| 4955 | sqlite3 *db; /* The database connection */ |
| 4956 | int iLoop; /* Loop counter over the terms of the join */ |
| 4957 | int ii, jj; /* Loop counters */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4958 | WhereCost rCost; /* Cost of a path */ |
| 4959 | WhereCost mxCost; /* Maximum cost of a set of paths */ |
| 4960 | WhereCost rSortCost; /* Cost to do a sort */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4961 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 4962 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 4963 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 4964 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 4965 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 4966 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 4967 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 4968 | char *pSpace; /* Temporary memory used by this routine */ |
| 4969 | |
| 4970 | db = pWInfo->pParse->db; |
| 4971 | nLoop = pWInfo->nLevel; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 4972 | mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4973 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4974 | #ifdef WHERETRACE_ENABLED |
| 4975 | if( sqlite3WhereTrace>=2 ) sqlite3DebugPrintf("---- begin solver\n"); |
| 4976 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4977 | |
| 4978 | /* Allocate and initialize space for aTo and aFrom */ |
| 4979 | ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 4980 | pSpace = sqlite3DbMallocRaw(db, ii); |
| 4981 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 4982 | aTo = (WherePath*)pSpace; |
| 4983 | aFrom = aTo+mxChoice; |
| 4984 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 4985 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 4986 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4987 | pFrom->aLoop = pX; |
| 4988 | } |
| 4989 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4990 | /* Seed the search with a single WherePath containing zero WhereLoops */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4991 | aFrom[0].nRow = (WhereCost)1; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4992 | nFrom = 1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4993 | |
| 4994 | /* Precompute the cost of sorting the final result set, if the caller |
| 4995 | ** to sqlite3WhereBegin() was concerned about sorting */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4996 | rSortCost = (WhereCost)0; |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4997 | if( pWInfo->pOrderBy==0 || nRowEst<=0.0 ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4998 | aFrom[0].isOrderedValid = 1; |
| 4999 | }else{ |
| 5000 | /* Compute an estimate on the cost to sort the entire result set */ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5001 | rSortCost = nRowEst*estLog(nRowEst); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5002 | #ifdef WHERETRACE_ENABLED |
| 5003 | if( sqlite3WhereTrace>=2 ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5004 | sqlite3DebugPrintf("---- sort cost=%-7.2g\n", rSortCost); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5005 | } |
| 5006 | #endif |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5007 | } |
| 5008 | |
| 5009 | /* Compute successively longer WherePaths using the previous generation |
| 5010 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 5011 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5012 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 5013 | nTo = 0; |
| 5014 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 5015 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 5016 | Bitmask maskNew; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5017 | Bitmask revMask = 0; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5018 | u8 isOrderedValid = pFrom->isOrderedValid; |
| 5019 | u8 isOrdered = pFrom->isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5020 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 5021 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5022 | /* At this point, pWLoop is a candidate to be the next loop. |
| 5023 | ** Compute its cost */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5024 | rCost = pWLoop->rSetup + pWLoop->rRun*pFrom->nRow + pFrom->rCost; |
| 5025 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5026 | if( !isOrderedValid ){ |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 5027 | switch( wherePathSatisfiesOrderBy(pWInfo, pFrom, iLoop, iLoop==nLoop-1, |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5028 | pWLoop, &revMask) ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5029 | case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ |
| 5030 | isOrdered = 1; |
| 5031 | isOrderedValid = 1; |
| 5032 | break; |
| 5033 | case 0: /* No. pFrom+pWLoop will require a separate sort */ |
| 5034 | isOrdered = 0; |
| 5035 | isOrderedValid = 1; |
| 5036 | rCost += rSortCost; |
| 5037 | break; |
| 5038 | default: /* Cannot tell yet. Try again on the next iteration */ |
| 5039 | break; |
| 5040 | } |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 5041 | }else{ |
| 5042 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5043 | } |
| 5044 | /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 5045 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
| 5046 | if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){ |
| 5047 | break; |
| 5048 | } |
| 5049 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5050 | if( jj>=nTo ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5051 | if( nTo>=mxChoice && rCost>=mxCost ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5052 | #ifdef WHERETRACE_ENABLED |
| 5053 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5054 | sqlite3DebugPrintf("Skip %s cost=%-7.2g order=%c\n", |
| 5055 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5056 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5057 | } |
| 5058 | #endif |
| 5059 | continue; |
| 5060 | } |
| 5061 | /* Add a new Path to the aTo[] set */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5062 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5063 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5064 | jj = nTo++; |
| 5065 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5066 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 5067 | for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5068 | } |
| 5069 | pTo = &aTo[jj]; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5070 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5071 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5072 | sqlite3DebugPrintf("New %s cost=%-7.2g order=%c\n", |
| 5073 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5074 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5075 | } |
| 5076 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5077 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5078 | if( pTo->rCost<=rCost ){ |
| 5079 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5080 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5081 | sqlite3DebugPrintf( |
| 5082 | "Skip %s cost=%-7.2g order=%c", |
| 5083 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5084 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5085 | sqlite3DebugPrintf(" vs %s cost=%-7.2g order=%c\n", |
| 5086 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5087 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5088 | } |
| 5089 | #endif |
| 5090 | continue; |
| 5091 | } |
| 5092 | /* A new and better score for a previously created equivalent path */ |
| 5093 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5094 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5095 | sqlite3DebugPrintf( |
| 5096 | "Update %s cost=%-7.2g order=%c", |
| 5097 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5098 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5099 | sqlite3DebugPrintf(" was %s cost=%-7.2g order=%c\n", |
| 5100 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5101 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5102 | } |
| 5103 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5104 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5105 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5106 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5107 | pTo->revLoop = revMask; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5108 | pTo->nRow = pFrom->nRow * pWLoop->nOut; |
| 5109 | pTo->rCost = rCost; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5110 | pTo->isOrderedValid = isOrderedValid; |
| 5111 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5112 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 5113 | pTo->aLoop[iLoop] = pWLoop; |
| 5114 | if( nTo>=mxChoice ){ |
| 5115 | mxCost = aTo[0].rCost; |
| 5116 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
| 5117 | if( pTo->rCost>mxCost ) mxCost = pTo->rCost; |
| 5118 | } |
| 5119 | } |
| 5120 | } |
| 5121 | } |
| 5122 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5123 | #ifdef WHERETRACE_ENABLED |
| 5124 | if( sqlite3WhereTrace>=2 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5125 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5126 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5127 | sqlite3DebugPrintf(" %s cost=%-7.2g nrow=%-7.2g order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5128 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5129 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5130 | if( pTo->isOrderedValid && pTo->isOrdered ){ |
| 5131 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 5132 | }else{ |
| 5133 | sqlite3DebugPrintf("\n"); |
| 5134 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5135 | } |
| 5136 | } |
| 5137 | #endif |
| 5138 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5139 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5140 | pFrom = aTo; |
| 5141 | aTo = aFrom; |
| 5142 | aFrom = pFrom; |
| 5143 | nFrom = nTo; |
| 5144 | } |
| 5145 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5146 | if( nFrom==0 ){ |
| 5147 | sqlite3ErrorMsg(pWInfo->pParse, "no query solution"); |
| 5148 | sqlite3DbFree(db, pSpace); |
| 5149 | return SQLITE_ERROR; |
| 5150 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5151 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5152 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5153 | pFrom = aFrom; |
| 5154 | for(ii=1; ii<nFrom; ii++){ |
| 5155 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 5156 | } |
| 5157 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5158 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5159 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5160 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 5161 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
| 5162 | pLevel->iFrom = pWLoop->iTab; /* FIXME: Omit the iFrom field */ |
| 5163 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5164 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5165 | if( pFrom->isOrdered ){ |
| 5166 | pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5167 | pWInfo->revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5168 | } |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5169 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5170 | |
| 5171 | /* Free temporary memory and return success */ |
| 5172 | sqlite3DbFree(db, pSpace); |
| 5173 | return SQLITE_OK; |
| 5174 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 5175 | |
| 5176 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5177 | ** Most queries use only a single table (they are not joins) and have |
| 5178 | ** simple == constraints against indexed fields. This routine attempts |
| 5179 | ** to plan those simple cases using much less ceremony than the |
| 5180 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 5181 | ** times for the common case. |
| 5182 | ** |
| 5183 | ** Return non-zero on success, if this query can be handled by this |
| 5184 | ** no-frills query planner. Return zero if this query needs the |
| 5185 | ** general-purpose query planner. |
| 5186 | */ |
| 5187 | static int whereSimpleFastCase(WhereLoopBuilder *pBuilder){ |
| 5188 | WhereInfo *pWInfo; |
| 5189 | struct SrcList_item *pItem; |
| 5190 | WhereClause *pWC; |
| 5191 | WhereTerm *pTerm; |
| 5192 | WhereLoop *pLoop; |
| 5193 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5194 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5195 | int nOrderBy; |
| 5196 | Table *pTab; |
| 5197 | Index *pIdx; |
| 5198 | |
| 5199 | pWInfo = pBuilder->pWInfo; |
| 5200 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 5201 | pItem = pWInfo->pTabList->a; |
| 5202 | pTab = pItem->pTab; |
| 5203 | if( IsVirtual(pTab) ) return 0; |
| 5204 | if( pItem->zIndex ) return 0; |
| 5205 | iCur = pItem->iCursor; |
| 5206 | pWC = &pWInfo->sWC; |
| 5207 | pLoop = pBuilder->pNew; |
| 5208 | pWInfo->a[0].pWLoop = pLoop; |
| 5209 | pLoop->wsFlags = 0; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5210 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 5211 | pWInfo->a[0].iTabCur = iCur; |
| 5212 | #ifdef SQLITE_DEBUG |
| 5213 | pLoop->cId = '0'; |
| 5214 | #endif |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5215 | nOrderBy = pWInfo->pOrderBy ? pWInfo->pOrderBy->nExpr : 0; |
| 5216 | pTerm = findTerm(pWC, iCur, -1, 1, WO_EQ, 0); |
| 5217 | if( pTerm ){ |
| 5218 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 5219 | pLoop->aLTerm[0] = pTerm; |
| 5220 | pLoop->nLTerm = 1; |
| 5221 | pLoop->u.btree.nEq = 1; |
| 5222 | pLoop->rRun = (WhereCost)10; |
| 5223 | pLoop->nOut = (WhereCost)1; |
| 5224 | pWInfo->nRowOut = 1; |
| 5225 | pWInfo->nOBSat = nOrderBy; |
| 5226 | }else{ |
| 5227 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 5228 | if( pIdx->onError==OE_None ) continue; |
| 5229 | for(j=0; j<pIdx->nColumn; j++){ |
| 5230 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 1, WO_EQ, pIdx); |
| 5231 | if( pTerm==0 ) break; |
| 5232 | whereLoopResize(pWInfo->pParse->db, pLoop, j); |
| 5233 | pLoop->aLTerm[j] = pTerm; |
| 5234 | } |
| 5235 | if( j!=pIdx->nColumn ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5236 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
| 5237 | if( (pItem->colUsed & ~columnsUsedByIndex(pIdx))==0 ){ |
| 5238 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 5239 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5240 | pLoop->nLTerm = j; |
| 5241 | pLoop->u.btree.nEq = j; |
| 5242 | pLoop->u.btree.pIndex = pIdx; |
| 5243 | pLoop->rRun = (WhereCost)15; |
| 5244 | pLoop->nOut = (WhereCost)1; |
| 5245 | pWInfo->nRowOut = 1; |
| 5246 | pWInfo->nOBSat = nOrderBy; |
| 5247 | break; |
| 5248 | } |
| 5249 | } |
| 5250 | return pLoop->wsFlags!=0; |
| 5251 | } |
| 5252 | |
| 5253 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5254 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 5255 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5256 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5257 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5258 | ** in order to complete the WHERE clause processing. |
| 5259 | ** |
| 5260 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5261 | ** |
| 5262 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5263 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5264 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5265 | ** example, if the SQL is this: |
| 5266 | ** |
| 5267 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5268 | ** |
| 5269 | ** Then the code generated is conceptually like the following: |
| 5270 | ** |
| 5271 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5272 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5273 | ** foreach row3 in t3 do / |
| 5274 | ** ... |
| 5275 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5276 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5277 | ** end / |
| 5278 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5279 | ** Note that the loops might not be nested in the order in which they |
| 5280 | ** 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] | 5281 | ** use of indices. Note also that when the IN operator appears in |
| 5282 | ** the WHERE clause, it might result in additional nested loops for |
| 5283 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5284 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5285 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5286 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5287 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5288 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5289 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5290 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5291 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5292 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5293 | ** data from the various tables of the loop. |
| 5294 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5295 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5296 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5297 | ** the tables have indices and there are terms in the WHERE clause that |
| 5298 | ** refer to those indices, a complete table scan can be avoided and the |
| 5299 | ** code will run much faster. Most of the work of this routine is checking |
| 5300 | ** to see if there are indices that can be used to speed up the loop. |
| 5301 | ** |
| 5302 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 5303 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 5304 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 5305 | ** loops are evaluated and if false a jump is made around all subsequent |
| 5306 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 5307 | ** most loop) |
| 5308 | ** |
| 5309 | ** OUTER JOINS |
| 5310 | ** |
| 5311 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 5312 | ** |
| 5313 | ** foreach row1 in t1 do |
| 5314 | ** flag = 0 |
| 5315 | ** foreach row2 in t2 do |
| 5316 | ** start: |
| 5317 | ** ... |
| 5318 | ** flag = 1 |
| 5319 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5320 | ** if flag==0 then |
| 5321 | ** move the row2 cursor to a null row |
| 5322 | ** goto start |
| 5323 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5324 | ** end |
| 5325 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5326 | ** ORDER BY CLAUSE PROCESSING |
| 5327 | ** |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5328 | ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5329 | ** 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] | 5330 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5331 | ** |
| 5332 | ** If an index can be used so that the natural output order of the table |
| 5333 | ** scan is correct for the ORDER BY clause, then that index is used and |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5334 | ** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr. This |
| 5335 | ** is an optimization that prevents an unnecessary sort of the result set |
| 5336 | ** if an index appropriate for the ORDER BY clause already exists. |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5337 | ** |
| 5338 | ** If the where clause loops cannot be arranged to provide the correct |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5339 | ** output order, then WhereInfo.nOBSat is 0. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5340 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5341 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5342 | Parse *pParse, /* The parser context */ |
| 5343 | SrcList *pTabList, /* A list of all tables to be scanned */ |
| 5344 | Expr *pWhere, /* The WHERE clause */ |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5345 | ExprList *pOrderBy, /* An ORDER BY clause, or NULL */ |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5346 | ExprList *pDistinct, /* The select-list for DISTINCT queries - or NULL */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 5347 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 5348 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5349 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5350 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5351 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5352 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 5353 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5354 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5355 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5356 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5357 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5358 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5359 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5360 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5361 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5362 | |
| 5363 | /* Variable initialization */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5364 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5365 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5366 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5367 | /* 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] | 5368 | ** bits in a Bitmask |
| 5369 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5370 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5371 | if( pTabList->nSrc>BMS ){ |
| 5372 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5373 | return 0; |
| 5374 | } |
| 5375 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5376 | /* This function normally generates a nested loop for all tables in |
| 5377 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 5378 | ** only generate code for the first table in pTabList and assume that |
| 5379 | ** any cursors associated with subsequent tables are uninitialized. |
| 5380 | */ |
| 5381 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 5382 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5383 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5384 | ** return value. A single allocation is used to store the WhereInfo |
| 5385 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 5386 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 5387 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 5388 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5389 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5390 | db = pParse->db; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5391 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5392 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5393 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5394 | sqlite3DbFree(db, pWInfo); |
| 5395 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5396 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5397 | } |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5398 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5399 | pWInfo->pParse = pParse; |
| 5400 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5401 | pWInfo->pOrderBy = pOrderBy; |
| 5402 | pWInfo->pDistinct = pDistinct; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5403 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5404 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5405 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5406 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5407 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5408 | sWLB.pWC = &pWInfo->sWC; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5409 | sWLB.pNew = (WhereLoop*)&pWInfo->a[nTabList]; |
| 5410 | whereLoopInit(sWLB.pNew); |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5411 | |
drh | a9b1b91 | 2011-07-08 13:07:02 +0000 | [diff] [blame] | 5412 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 5413 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
drh | 7e5418e | 2012-09-27 15:05:54 +0000 | [diff] [blame] | 5414 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0; |
drh | a9b1b91 | 2011-07-08 13:07:02 +0000 | [diff] [blame] | 5415 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5416 | /* Split the WHERE clause into separate subexpressions where each |
| 5417 | ** subexpression is separated by an AND operator. |
| 5418 | */ |
| 5419 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5420 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5421 | sqlite3ExprCodeConstants(pParse, pWhere); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5422 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5423 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5424 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 5425 | ** expression and either jump over all of the code or fall thru. |
| 5426 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5427 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 5428 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 5429 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5430 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5431 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5432 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 5433 | ** |
| 5434 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 5435 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 5436 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 5437 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 5438 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 5439 | ** of the join. Subtracting one from the right table bitmask gives a |
| 5440 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 5441 | ** 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] | 5442 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5443 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 5444 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 5445 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 5446 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5447 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5448 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5449 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5450 | } |
| 5451 | #ifndef NDEBUG |
| 5452 | { |
| 5453 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5454 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5455 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5456 | assert( (m-1)==toTheLeft ); |
| 5457 | toTheLeft |= m; |
| 5458 | } |
| 5459 | } |
| 5460 | #endif |
| 5461 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5462 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 5463 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 5464 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 5465 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5466 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5467 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5468 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5469 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 5470 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5471 | |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5472 | /* Check if the DISTINCT qualifier, if there is one, is redundant. |
| 5473 | ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to |
| 5474 | ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT. |
| 5475 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5476 | if( pDistinct && isDistinctRedundant(pParse,pTabList,&pWInfo->sWC,pDistinct) ){ |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5477 | pDistinct = 0; |
| 5478 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5479 | } |
| 5480 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5481 | /* Construct the WhereLoop objects */ |
| 5482 | WHERETRACE(("*** Optimizer Start ***\n")); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5483 | if( nTabList!=1 || whereSimpleFastCase(&sWLB)==0 ){ |
| 5484 | rc = whereLoopAddAll(&sWLB); |
| 5485 | if( rc ) goto whereBeginError; |
| 5486 | |
| 5487 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5488 | #ifdef WHERETRACE_ENABLED |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5489 | if( sqlite3WhereTrace ){ |
| 5490 | WhereLoop *p; |
| 5491 | int i = 0; |
| 5492 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 5493 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
| 5494 | for(p=pWInfo->pLoops; p; p=p->pNextLoop){ |
| 5495 | p->cId = zLabel[(i++)%sizeof(zLabel)]; |
| 5496 | whereLoopPrint(p, pTabList); |
| 5497 | } |
| 5498 | } |
| 5499 | #endif |
| 5500 | |
| 5501 | wherePathSolver(pWInfo, -1); |
| 5502 | if( db->mallocFailed ) goto whereBeginError; |
| 5503 | if( pWInfo->pOrderBy ){ |
| 5504 | wherePathSolver(pWInfo, pWInfo->nRowOut); |
| 5505 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5506 | } |
| 5507 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5508 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 5509 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5510 | } |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5511 | if( pParse->nErr || db->mallocFailed ){ |
| 5512 | goto whereBeginError; |
| 5513 | } |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5514 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5515 | if( sqlite3WhereTrace ){ |
| 5516 | int ii; |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5517 | sqlite3DebugPrintf("---- Solution"); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5518 | if( pWInfo->nOBSat ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5519 | sqlite3DebugPrintf(" ORDER BY omitted rev=0x%llx\n", pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5520 | }else{ |
| 5521 | sqlite3DebugPrintf("\n"); |
| 5522 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5523 | for(ii=0; ii<nTabList; ii++){ |
| 5524 | whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5525 | } |
| 5526 | } |
| 5527 | #endif |
drh | 0edc94d | 2013-05-31 19:14:56 +0000 | [diff] [blame] | 5528 | WHERETRACE(("*** Optimizer Finished ***\n")); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5529 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5530 | #if 0 /* FIXME: Add this back in? */ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5531 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 5532 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 5533 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 5534 | ** the statement to update a single row. |
| 5535 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 5536 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5537 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5538 | pWInfo->okOnePass = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5539 | pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5540 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5541 | #endif |
| 5542 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5543 | /* Open all tables in the pTabList and any indices selected for |
| 5544 | ** searching those tables. |
| 5545 | */ |
| 5546 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5547 | notReady = ~(Bitmask)0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5548 | pWInfo->nRowOut = (WhereCost)1; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5549 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5550 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5551 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5552 | struct SrcList_item *pTabItem; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5553 | WhereLoop *pLoop; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5554 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5555 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5556 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5557 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5558 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 5559 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 5560 | /* Do nothing */ |
| 5561 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5562 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5563 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5564 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 5565 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5566 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 5567 | }else if( IsVirtual(pTab) ){ |
| 5568 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5569 | }else |
| 5570 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5571 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5572 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5573 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 5574 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5575 | testcase( pTab->nCol==BMS-1 ); |
| 5576 | testcase( pTab->nCol==BMS ); |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 5577 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5578 | Bitmask b = pTabItem->colUsed; |
| 5579 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5580 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5581 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 5582 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5583 | assert( n<=pTab->nCol ); |
| 5584 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5585 | }else{ |
| 5586 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5587 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5588 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5589 | if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5590 | constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel); |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5591 | }else |
| 5592 | #endif |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 5593 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5594 | Index *pIx = pLoop->u.btree.pIndex; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5595 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5596 | /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */ |
| 5597 | int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5598 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 5599 | assert( iIndexCur>=0 ); |
| 5600 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5601 | (char*)pKey, P4_KEYINFO_HANDOFF); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 5602 | VdbeComment((v, "%s", pIx->zName)); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5603 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5604 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5605 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5606 | } |
| 5607 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 5608 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5609 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5610 | /* Generate the code to do the search. Each iteration of the for |
| 5611 | ** loop below generates code for a single nested loop of the VM |
| 5612 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5613 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5614 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5615 | for(ii=0; ii<nTabList; ii++){ |
| 5616 | pLevel = &pWInfo->a[ii]; |
| 5617 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5618 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 5619 | pWInfo->iContinue = pLevel->addrCont; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5620 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 5621 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 5622 | /* Done. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5623 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5624 | |
| 5625 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5626 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5627 | if( pWInfo ){ |
| 5628 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 5629 | whereInfoFree(db, pWInfo); |
| 5630 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5631 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
| 5634 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5635 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5636 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5637 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5638 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5639 | Parse *pParse = pWInfo->pParse; |
| 5640 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5641 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5642 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5643 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 5644 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5645 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5646 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5647 | /* Generate loop termination code. |
| 5648 | */ |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 5649 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5650 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5651 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5652 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5653 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5654 | if( pLevel->op!=OP_Noop ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5655 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 5656 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5657 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5658 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 5659 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5660 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5661 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5662 | 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] | 5663 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 5664 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5665 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5666 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5667 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 5668 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5669 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5670 | if( pLevel->iLeftJoin ){ |
| 5671 | int addr; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5672 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5673 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 5674 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 5675 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 5676 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 5677 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 5678 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5679 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 5680 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 5681 | if( pLevel->op==OP_Return ){ |
| 5682 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 5683 | }else{ |
| 5684 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 5685 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 5686 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5687 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5688 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5689 | |
| 5690 | /* The "break" point is here, just past the end of the outer loop. |
| 5691 | ** Set it. |
| 5692 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5693 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5694 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5695 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5696 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5697 | assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc ); |
| 5698 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 5699 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5700 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5701 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 5702 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5703 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5704 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 5705 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5706 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5707 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5708 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5709 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5710 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 5711 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5712 | if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_TEMP_INDEX))==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5713 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 5714 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5715 | } |
| 5716 | |
danielk1977 | 21de2e7 | 2007-11-29 17:43:27 +0000 | [diff] [blame] | 5717 | /* If this scan uses an index, make code substitutions to read data |
| 5718 | ** from the index in preference to the table. Sometimes, this means |
| 5719 | ** the table need never be read from. This is a performance boost, |
| 5720 | ** as the vdbe level waits until the table is read before actually |
| 5721 | ** seeking the table cursor to the record corresponding to the current |
| 5722 | ** position in the index. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5723 | ** |
| 5724 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 5725 | ** sqlite3WhereEnd will have created code that references the table |
| 5726 | ** directly. This loop scans all that code looking for opcodes |
| 5727 | ** that reference the table and converts them into opcodes that |
| 5728 | ** reference the index. |
| 5729 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5730 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 5731 | pIdx = pLoop->u.btree.pIndex; |
| 5732 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 5733 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 5734 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5735 | if( pIdx && !db->mallocFailed ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 5736 | int k, j, last; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5737 | VdbeOp *pOp; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5738 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5739 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 5740 | last = sqlite3VdbeCurrentAddr(v); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 5741 | for(k=pWInfo->iTop; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5742 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 5743 | if( pOp->opcode==OP_Column ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5744 | for(j=0; j<pIdx->nColumn; j++){ |
| 5745 | if( pOp->p2==pIdx->aiColumn[j] ){ |
| 5746 | pOp->p2 = j; |
danielk1977 | 21de2e7 | 2007-11-29 17:43:27 +0000 | [diff] [blame] | 5747 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5748 | break; |
| 5749 | } |
| 5750 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5751 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5752 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5753 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5754 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5755 | } |
| 5756 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5757 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5758 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5759 | |
| 5760 | /* Final cleanup |
| 5761 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 5762 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 5763 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5764 | return; |
| 5765 | } |