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 | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 42 | typedef struct WhereLoop WhereLoop; |
| 43 | typedef struct WherePath WherePath; |
| 44 | typedef struct WhereTerm WhereTerm; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 45 | typedef struct WhereLoopBuilder WhereLoopBuilder; |
| 46 | typedef struct WhereScan WhereScan; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 47 | typedef struct WhereVtabPlan WhereVtabPlan; |
| 48 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | ** Each instance of this object represents a way of evaluating one |
| 52 | ** term of a join. The WhereClause object holds a table of these |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 53 | ** objects using (maskSelf,prereq,) as the primary key. Note that the |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 54 | ** same join term might have multiple associated WhereLoop objects. |
| 55 | */ |
| 56 | struct WhereLoop { |
| 57 | Bitmask prereq; /* Bitmask of other loops that must run first */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 58 | Bitmask maskSelf; /* Bitmask identifying table iTab */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 59 | #ifdef SQLITE_DEBUG |
| 60 | char cId; /* Symbolic ID of this loop for debugging use */ |
| 61 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 62 | u8 iTab; /* Position in FROM clause of table for this loop */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 63 | u8 iSortIdx; /* Sorting index number. 0==None */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 64 | u16 nTerm; /* Number of entries in aTerm[] */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 65 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 66 | double rSetup; /* One-time setup cost (ex: create transient index) */ |
| 67 | double rRun; /* Cost of running each loop */ |
| 68 | double nOut; /* Estimated number of output rows */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 69 | union { |
| 70 | struct { /* Information for internal btree tables */ |
| 71 | int nEq; /* Number of equality constraints */ |
| 72 | Index *pIndex; /* Index used, or NULL */ |
| 73 | } btree; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 74 | struct { /* Information for virtual tables */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 75 | int idxNum; /* Index number */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 76 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 77 | u8 isOrdered; /* True if satisfies ORDER BY */ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 78 | u16 omitMask; /* Terms that may be omitted */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 79 | char *idxStr; /* Index identifier string */ |
| 80 | } vtab; |
| 81 | } u; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 82 | WhereTerm **aTerm; /* WhereTerms used */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 83 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | ** Each instance of this object holds a sequence of WhereLoop objects |
| 88 | ** that implement some or all of the entire query plan. |
| 89 | */ |
| 90 | struct WherePath { |
| 91 | Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 92 | Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 93 | double nRow; /* Estimated number of rows generated by this path */ |
| 94 | double rCost; /* Total cost of this path */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 95 | u8 isOrdered; /* True if this path satisfies ORDER BY */ |
| 96 | u8 isOrderedValid; /* True if the isOrdered field is valid */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 97 | WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 98 | }; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 99 | |
| 100 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 101 | ** The query generator uses an array of instances of this structure to |
| 102 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 103 | ** clause subexpression is separated from the others by AND operators, |
| 104 | ** usually, or sometimes subexpressions separated by OR. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 105 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 106 | ** All WhereTerms are collected into a single WhereClause structure. |
| 107 | ** The following identity holds: |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 108 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 109 | ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 110 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 111 | ** When a term is of the form: |
| 112 | ** |
| 113 | ** X <op> <expr> |
| 114 | ** |
| 115 | ** where X is a column name and <op> is one of certain operators, |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 116 | ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the |
| 117 | ** cursor number and column number for X. WhereTerm.eOperator records |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 118 | ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 119 | ** use of a bitmask encoding for the operator allows us to search |
| 120 | ** quickly for terms that match any of several different operators. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 121 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 122 | ** A WhereTerm might also be two or more subterms connected by OR: |
| 123 | ** |
| 124 | ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... |
| 125 | ** |
| 126 | ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR |
| 127 | ** and the WhereTerm.u.pOrInfo field points to auxiliary information that |
| 128 | ** is collected about the |
| 129 | ** |
| 130 | ** If a term in the WHERE clause does not match either of the two previous |
| 131 | ** categories, then eOperator==0. The WhereTerm.pExpr field is still set |
| 132 | ** to the original subexpression content and wtFlags is set up appropriately |
| 133 | ** but no other fields in the WhereTerm object are meaningful. |
| 134 | ** |
| 135 | ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 136 | ** but they do so indirectly. A single WhereMaskSet structure translates |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 137 | ** cursor number into bits and the translated bit is stored in the prereq |
| 138 | ** fields. The translation is used in order to maximize the number of |
| 139 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 140 | ** spread out over the non-negative integers. For example, the cursor |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 141 | ** 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] | 142 | ** translates these sparse cursor numbers into consecutive integers |
| 143 | ** beginning with 0 in order to make the best possible use of the available |
| 144 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 145 | ** would be mapped into integers 0 through 7. |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 146 | ** |
| 147 | ** The number of terms in a join is limited by the number of bits |
| 148 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 149 | ** is only able to process joins with 64 or fewer tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 150 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 151 | struct WhereTerm { |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 152 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 153 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 154 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 155 | union { |
| 156 | int leftColumn; /* Column number of X in "X <op> <expr>" */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 157 | WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 158 | WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 159 | } u; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 160 | u16 eOperator; /* A WO_xx value describing <op> */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 161 | u8 wtFlags; /* TERM_xxx bit flags. See below */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 162 | u8 nChild; /* Number of children that must disable us */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 163 | WhereClause *pWC; /* The clause this term is part of */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 164 | Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 165 | Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | /* |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 169 | ** Allowed values of WhereTerm.wtFlags |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 170 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 171 | #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 172 | #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
| 173 | #define TERM_CODED 0x04 /* This term is already coded */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 174 | #define TERM_COPIED 0x08 /* Has a child */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 175 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 176 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 177 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 178 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 179 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 180 | #else |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 181 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 182 | #endif |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 183 | |
| 184 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 185 | ** An instance of the WhereScan object is used as an iterator for locating |
| 186 | ** terms in the WHERE clause that are useful to the query planner. |
| 187 | */ |
| 188 | struct WhereScan { |
| 189 | WhereTerm *pCurrent; /* Most recent match */ |
| 190 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 191 | WhereClause *pWC; /* WhereClause currently being scanned */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 192 | char *zCollName; /* Required collating sequence, if not NULL */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 193 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 194 | unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 195 | unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 196 | u32 opMask; /* Acceptable operators */ |
| 197 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 198 | int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ |
| 199 | }; |
| 200 | |
| 201 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 202 | ** An instance of the following structure holds all information about a |
| 203 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 204 | ** |
| 205 | ** Explanation of pOuter: For a WHERE clause of the form |
| 206 | ** |
| 207 | ** a AND ((b AND c) OR (d AND e)) AND f |
| 208 | ** |
| 209 | ** There are separate WhereClause objects for the whole clause and for |
| 210 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 211 | ** subclauses points to the WhereClause object for the whole clause. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 212 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 213 | struct WhereClause { |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 214 | Parse *pParse; /* The parser context */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 215 | WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 216 | WhereClause *pOuter; /* Outer conjunction */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 217 | u8 op; /* Split operator. TK_AND or TK_OR */ |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 218 | u16 wctrlFlags; /* Might include WHERE_AND_ONLY */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 219 | int nTerm; /* Number of terms */ |
| 220 | int nSlot; /* Number of entries in a[] */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 221 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 222 | #if defined(SQLITE_SMALL_STACK) |
| 223 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| 224 | #else |
| 225 | WhereTerm aStatic[8]; /* Initial static space for a[] */ |
| 226 | #endif |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 230 | ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to |
| 231 | ** a dynamically allocated instance of the following structure. |
| 232 | */ |
| 233 | struct WhereOrInfo { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 234 | WhereClause wc; /* Decomposition into subterms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 235 | Bitmask indexable; /* Bitmask of all indexable tables in the clause */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | /* |
| 239 | ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to |
| 240 | ** a dynamically allocated instance of the following structure. |
| 241 | */ |
| 242 | struct WhereAndInfo { |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 243 | WhereClause wc; /* The subexpression broken out */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 247 | ** An instance of the following structure keeps track of a mapping |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 248 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 249 | ** |
| 250 | ** The VDBE cursor numbers are small integers contained in |
| 251 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 252 | ** clause, the cursor numbers might not begin with 0 and they might |
| 253 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 254 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 255 | ** from the sparse cursor numbers into consecutive integers beginning |
| 256 | ** with 0. |
| 257 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 258 | ** 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] | 259 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 260 | ** |
| 261 | ** For example, if the WHERE clause expression used these VDBE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 262 | ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 263 | ** would map those cursor numbers into bits 0 through 5. |
| 264 | ** |
| 265 | ** Note that the mapping is not necessarily ordered. In the example |
| 266 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 267 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 268 | ** does not really matter. What is important is that sparse cursor |
| 269 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 270 | ** no gaps. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 271 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 272 | struct WhereMaskSet { |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 273 | int n; /* Number of assigned cursor values */ |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 274 | int ix[BMS]; /* Cursor assigned to each bit */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 277 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 278 | ** This object is a factory for WhereLoop objects for a particular query. |
| 279 | */ |
| 280 | struct WhereLoopBuilder { |
| 281 | WhereInfo *pWInfo; /* Information about this WHERE */ |
| 282 | sqlite3 *db; /* Database connection */ |
| 283 | Parse *pParse; /* Parsing context */ |
| 284 | WhereClause *pWC; /* WHERE clause terms */ |
| 285 | SrcList *pTabList; /* FROM clause */ |
| 286 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 287 | WhereLoop *pNew; /* Template WhereLoop */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 288 | WhereLoop *pBest; /* If non-NULL, store single best loop here */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 289 | int mxTerm; /* Maximum number of aTerm[] entries on pNew */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 293 | ** Bitmasks for the operators that indices are able to exploit. An |
| 294 | ** OR-ed combination of these values can be used when searching for |
| 295 | ** terms in the where clause. |
| 296 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 297 | #define WO_IN 0x001 |
| 298 | #define WO_EQ 0x002 |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 299 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 300 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 301 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 302 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 303 | #define WO_MATCH 0x040 |
| 304 | #define WO_ISNULL 0x080 |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 305 | #define WO_OR 0x100 /* Two or more OR-connected terms */ |
| 306 | #define WO_AND 0x200 /* Two or more AND-connected terms */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 307 | #define WO_EQUIV 0x400 /* Of the form A==B, both columns */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 308 | #define WO_NOOP 0x800 /* This term does not restrict search space */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 309 | |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 310 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 311 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 312 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 313 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 314 | ** Value for wsFlags returned by bestIndex() and stored in |
| 315 | ** WhereLevel.wsFlags. These flags determine which search |
| 316 | ** strategies are appropriate. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 317 | */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 318 | #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR or x IN (...) or x IS NULL */ |
| 319 | #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 320 | #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 321 | #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 322 | #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 323 | #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 324 | #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 325 | #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 326 | #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 327 | #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 328 | #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 329 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 330 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 331 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 332 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 333 | #define WHERE_TEMP_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 334 | #define WHERE_COVER_SCAN 0x00008000 /* Full scan of a covering index */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 335 | |
| 336 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 337 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 338 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 339 | static void whereClauseInit( |
| 340 | WhereClause *pWC, /* The WhereClause to be initialized */ |
| 341 | Parse *pParse, /* The parsing context */ |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 342 | WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmasks */ |
| 343 | u16 wctrlFlags /* Might include WHERE_AND_ONLY */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 344 | ){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 345 | pWC->pParse = pParse; |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 346 | pWC->pMaskSet = pMaskSet; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 347 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 348 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 349 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 350 | pWC->a = pWC->aStatic; |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 351 | pWC->wctrlFlags = wctrlFlags; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 352 | } |
| 353 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 354 | /* Forward reference */ |
| 355 | static void whereClauseClear(WhereClause*); |
| 356 | |
| 357 | /* |
| 358 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 359 | */ |
| 360 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 361 | whereClauseClear(&p->wc); |
| 362 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* |
| 366 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 367 | */ |
| 368 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 369 | whereClauseClear(&p->wc); |
| 370 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 371 | } |
| 372 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 373 | /* |
| 374 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 375 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 376 | */ |
| 377 | static void whereClauseClear(WhereClause *pWC){ |
| 378 | int i; |
| 379 | WhereTerm *a; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 380 | sqlite3 *db = pWC->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 381 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 382 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 383 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 384 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 385 | if( a->wtFlags & TERM_ORINFO ){ |
| 386 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 387 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 388 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 389 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 390 | } |
| 391 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 392 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 397 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 398 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 399 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 400 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 401 | ** allocation error. The memory allocation failure will be recorded in |
| 402 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 403 | ** |
| 404 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 405 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 406 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 407 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 408 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 409 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 410 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 411 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 412 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 413 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 414 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 415 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 416 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 417 | int idx; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 418 | testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 419 | if( pWC->nTerm>=pWC->nSlot ){ |
| 420 | WhereTerm *pOld = pWC->a; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 421 | sqlite3 *db = pWC->pParse->db; |
| 422 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 423 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 424 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 425 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 426 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 427 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 428 | return 0; |
| 429 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 430 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 431 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 432 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 433 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 434 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 435 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 436 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 437 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 438 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 439 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 440 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 441 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 442 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 443 | |
| 444 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 445 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 446 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 447 | ** operator specified in the op parameter. The WhereClause structure |
| 448 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 449 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 450 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 451 | ** \________/ \_______________/ \________________/ |
| 452 | ** slot[0] slot[1] slot[2] |
| 453 | ** |
| 454 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 455 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 456 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 457 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 458 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 459 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 460 | */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 461 | static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 462 | pWC->op = (u8)op; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 463 | if( pExpr==0 ) return; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 464 | if( pExpr->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 465 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 466 | }else{ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 467 | whereSplit(pWC, pExpr->pLeft, op); |
| 468 | whereSplit(pWC, pExpr->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 469 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 473 | ** Initialize an expression mask set (a WhereMaskSet object) |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 474 | */ |
| 475 | #define initMaskSet(P) memset(P, 0, sizeof(*P)) |
| 476 | |
| 477 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 478 | ** Return the bitmask for the given cursor number. Return 0 if |
| 479 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 480 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 481 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 482 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 483 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 484 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 485 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 486 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 487 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 488 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 493 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 494 | ** |
| 495 | ** There is one cursor per table in the FROM clause. The number of |
| 496 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 497 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 498 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 499 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 500 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 501 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 502 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 506 | ** This routine walks (recursively) an expression tree and generates |
| 507 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 508 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 509 | ** |
| 510 | ** In order for this routine to work, the calling function must have |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 511 | ** previously invoked sqlite3ResolveExprNames() on the expression. See |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 512 | ** the header comment on that routine for additional information. |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 513 | ** The sqlite3ResolveExprNames() routines looks for column names and |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 514 | ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 515 | ** the VDBE cursor number of the table. This routine just has to |
| 516 | ** translate the cursor numbers into bitmask values and OR all |
| 517 | ** the bitmasks together. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 518 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 519 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 520 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 521 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 522 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 523 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 524 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 525 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 526 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 527 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 528 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 529 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 530 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 531 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 532 | }else{ |
| 533 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 534 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 535 | return mask; |
| 536 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 537 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 538 | int i; |
| 539 | Bitmask mask = 0; |
| 540 | if( pList ){ |
| 541 | for(i=0; i<pList->nExpr; i++){ |
| 542 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 545 | return mask; |
| 546 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 547 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 548 | Bitmask mask = 0; |
| 549 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 550 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 551 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 552 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 553 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 554 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 555 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 556 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 557 | int i; |
| 558 | for(i=0; i<pSrc->nSrc; i++){ |
| 559 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 560 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 561 | } |
| 562 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 563 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 564 | } |
| 565 | return mask; |
| 566 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 567 | |
| 568 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 569 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 570 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 571 | ** "=", "<", ">", "<=", ">=", and "IN". |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 572 | ** |
| 573 | ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be |
| 574 | ** of one of the following forms: column = expression column > expression |
| 575 | ** column >= expression column < expression column <= expression |
| 576 | ** expression = column expression > column expression >= column |
| 577 | ** expression < column expression <= column column IN |
| 578 | ** (expression-list) column IN (subquery) column IS NULL |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 579 | */ |
| 580 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 581 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 582 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 583 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 584 | assert( TK_GE==TK_EQ+4 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 585 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 589 | ** Swap two objects of type TYPE. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 590 | */ |
| 591 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 592 | |
| 593 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 594 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 595 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 596 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 597 | ** If left/right precedence rules come into play when determining the |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 598 | ** collating |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 599 | ** side of the comparison, it remains associated with the same side after |
| 600 | ** the commutation. So "Y collate NOCASE op X" becomes |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 601 | ** "X op Y". This is because any collation sequence on |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 602 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 603 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 604 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 605 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 606 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 607 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 608 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 609 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 610 | if( expRight==expLeft ){ |
| 611 | /* Either X and Y both have COLLATE operator or neither do */ |
| 612 | if( expRight ){ |
| 613 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 614 | ** used by clearing the EP_Collate flag from Y. */ |
| 615 | pExpr->pRight->flags &= ~EP_Collate; |
| 616 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 617 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 618 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 619 | ** it to be searched first. */ |
| 620 | pExpr->pLeft->flags |= EP_Collate; |
| 621 | } |
| 622 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 623 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 624 | if( pExpr->op>=TK_GT ){ |
| 625 | assert( TK_LT==TK_GT+2 ); |
| 626 | assert( TK_GE==TK_LE+2 ); |
| 627 | assert( TK_GT>TK_EQ ); |
| 628 | assert( TK_GT<TK_LE ); |
| 629 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 630 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 631 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 635 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 636 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 637 | static u16 operatorMask(int op){ |
| 638 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 639 | assert( allowedOp(op) ); |
| 640 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 641 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 642 | }else if( op==TK_ISNULL ){ |
| 643 | c = WO_ISNULL; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 644 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 645 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 646 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 647 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 648 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 649 | assert( op!=TK_IN || c==WO_IN ); |
| 650 | assert( op!=TK_EQ || c==WO_EQ ); |
| 651 | assert( op!=TK_LT || c==WO_LT ); |
| 652 | assert( op!=TK_LE || c==WO_LE ); |
| 653 | assert( op!=TK_GT || c==WO_GT ); |
| 654 | assert( op!=TK_GE || c==WO_GE ); |
| 655 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 659 | ** Advance to the next WhereTerm that matches according to the criteria |
| 660 | ** established when the pScan object was initialized by whereScanInit(). |
| 661 | ** Return NULL if there are no more matching WhereTerms. |
| 662 | */ |
| 663 | WhereTerm *whereScanNext(WhereScan *pScan){ |
| 664 | int iCur; /* The cursor on the LHS of the term */ |
| 665 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 666 | Expr *pX; /* An expression being tested */ |
| 667 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 668 | WhereTerm *pTerm; /* The term being tested */ |
| 669 | |
| 670 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 671 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 672 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 673 | while( (pWC = pScan->pWC)!=0 ){ |
| 674 | for(pTerm=pWC->a+pScan->k; pScan->k<pWC->nTerm; pScan->k++, pTerm++){ |
| 675 | if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){ |
| 676 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 677 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 678 | ){ |
| 679 | int j; |
| 680 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 681 | assert( pX->op==TK_COLUMN ); |
| 682 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 683 | if( pScan->aEquiv[j]==pX->iTable |
| 684 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | if( j==pScan->nEquiv ){ |
| 689 | pScan->aEquiv[j] = pX->iTable; |
| 690 | pScan->aEquiv[j+1] = pX->iColumn; |
| 691 | pScan->nEquiv += 2; |
| 692 | } |
| 693 | } |
| 694 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 695 | /* Verify the affinity and collating sequence match */ |
| 696 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 697 | CollSeq *pColl; |
| 698 | pX = pTerm->pExpr; |
| 699 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 700 | continue; |
| 701 | } |
| 702 | assert(pX->pLeft); |
| 703 | pColl = sqlite3BinaryCompareCollSeq(pWC->pParse, |
| 704 | pX->pLeft, pX->pRight); |
| 705 | if( pColl==0 ) pColl = pWC->pParse->db->pDfltColl; |
| 706 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 707 | continue; |
| 708 | } |
| 709 | } |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 710 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 711 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 712 | && pX->iTable==pScan->aEquiv[0] |
| 713 | && pX->iColumn==pScan->aEquiv[1] |
| 714 | ){ |
| 715 | continue; |
| 716 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 717 | pScan->pCurrent = pTerm; |
| 718 | pScan->k++; |
| 719 | return pTerm; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | pWC = pScan->pWC = pScan->pWC->pOuter; |
| 724 | pScan->k = 0; |
| 725 | } |
| 726 | pScan->pWC = pScan->pOrigWC; |
| 727 | pScan->k = 0; |
| 728 | pScan->iEquiv += 2; |
| 729 | } |
| 730 | pScan->pCurrent = 0; |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 736 | ** first match. Return NULL if there are no matches. |
| 737 | ** |
| 738 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 739 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 740 | ** iCur. The <op> must be one of the operators described by opMask. |
| 741 | ** |
| 742 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 743 | ** index pIdx. |
| 744 | */ |
| 745 | WhereTerm *whereScanInit( |
| 746 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 747 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 748 | int iCur, /* Cursor to scan for */ |
| 749 | int iColumn, /* Column to scan for */ |
| 750 | u32 opMask, /* Operator(s) to scan for */ |
| 751 | Index *pIdx /* Must be compatible with this index */ |
| 752 | ){ |
| 753 | int j; |
| 754 | |
| 755 | memset(pScan, 0, sizeof(*pScan)); |
| 756 | pScan->pOrigWC = pWC; |
| 757 | pScan->pWC = pWC; |
| 758 | if( pIdx && iColumn>=0 ){ |
| 759 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 760 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 761 | if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 762 | } |
| 763 | pScan->zCollName = pIdx->azColl[j]; |
| 764 | } |
| 765 | pScan->opMask = opMask; |
| 766 | pScan->aEquiv[0] = iCur; |
| 767 | pScan->aEquiv[1] = iColumn; |
| 768 | pScan->nEquiv = 2; |
| 769 | pScan->iEquiv = 2; |
| 770 | return whereScanNext(pScan); |
| 771 | } |
| 772 | |
| 773 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 774 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 775 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 776 | ** the WO_xx operator codes specified by the op parameter. |
| 777 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 778 | ** |
| 779 | ** The term returned might by Y=<expr> if there is another constraint in |
| 780 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 781 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 782 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 783 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 784 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 785 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 786 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 787 | ** and ... and A9=A10 and A10=<expr>. |
| 788 | ** |
| 789 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 790 | ** then try for the one with no dependencies on <expr> - in other words where |
| 791 | ** <expr> is a constant expression of some kind. Only return entries of |
| 792 | ** 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] | 793 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 794 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 795 | */ |
| 796 | static WhereTerm *findTerm( |
| 797 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 798 | int iCur, /* Cursor number of LHS */ |
| 799 | int iColumn, /* Column number of LHS */ |
| 800 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 801 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 802 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 803 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 804 | WhereTerm *pResult = 0; |
| 805 | WhereTerm *p; |
| 806 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 807 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 808 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 809 | while( p ){ |
| 810 | if( (p->prereqRight & notReady)==0 ){ |
| 811 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 812 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 813 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 814 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 815 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 816 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 817 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 818 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 819 | } |
| 820 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 821 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 822 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 823 | |
| 824 | /* |
| 825 | ** Call exprAnalyze on all terms in a WHERE clause. |
| 826 | ** |
| 827 | ** |
| 828 | */ |
| 829 | static void exprAnalyzeAll( |
| 830 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 831 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 832 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 833 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 834 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 835 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 839 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 840 | /* |
| 841 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 842 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 843 | ** so and false if not. |
| 844 | ** |
| 845 | ** In order for the operator to be optimizible, the RHS must be a string |
| 846 | ** literal that does not begin with a wildcard. |
| 847 | */ |
| 848 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 849 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 850 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 851 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 852 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 853 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 854 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 855 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 856 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 857 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 858 | int c; /* One character in z[] */ |
| 859 | int cnt; /* Number of non-wildcard prefix characters */ |
| 860 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 861 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 862 | sqlite3_value *pVal = 0; |
| 863 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 864 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 865 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 866 | return 0; |
| 867 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 868 | #ifdef SQLITE_EBCDIC |
| 869 | if( *pnoCase ) return 0; |
| 870 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 871 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 872 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 873 | if( pLeft->op!=TK_COLUMN |
| 874 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 875 | || IsVirtual(pLeft->pTab) |
| 876 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 877 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 878 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 879 | return 0; |
| 880 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 881 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 882 | |
| 883 | pRight = pList->a[0].pExpr; |
| 884 | op = pRight->op; |
| 885 | if( op==TK_REGISTER ){ |
| 886 | op = pRight->op2; |
| 887 | } |
| 888 | if( op==TK_VARIABLE ){ |
| 889 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 890 | int iCol = pRight->iColumn; |
| 891 | pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 892 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 893 | z = (char *)sqlite3_value_text(pVal); |
| 894 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 895 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 896 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 897 | }else if( op==TK_STRING ){ |
| 898 | z = pRight->u.zToken; |
| 899 | } |
| 900 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 901 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 902 | 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] | 903 | cnt++; |
| 904 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 905 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 906 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 907 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 908 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 909 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 910 | *ppPrefix = pPrefix; |
| 911 | if( op==TK_VARIABLE ){ |
| 912 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 913 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 914 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 915 | /* If the rhs of the LIKE expression is a variable, and the current |
| 916 | ** value of the variable means there is no need to invoke the LIKE |
| 917 | ** function, then no OP_Variable will be added to the program. |
| 918 | ** This causes problems for the sqlite3_bind_parameter_name() |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 919 | ** API. To workaround them, add a dummy OP_Variable here. |
| 920 | */ |
| 921 | int r1 = sqlite3GetTempReg(pParse); |
| 922 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 923 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 924 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | }else{ |
| 928 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 929 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 930 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 931 | |
| 932 | sqlite3ValueFree(pVal); |
| 933 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 934 | } |
| 935 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 936 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 937 | |
| 938 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 939 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 940 | ** Check to see if the given expression is of the form |
| 941 | ** |
| 942 | ** column MATCH expr |
| 943 | ** |
| 944 | ** If it is then return TRUE. If not, return FALSE. |
| 945 | */ |
| 946 | static int isMatchOfColumn( |
| 947 | Expr *pExpr /* Test this expression */ |
| 948 | ){ |
| 949 | ExprList *pList; |
| 950 | |
| 951 | if( pExpr->op!=TK_FUNCTION ){ |
| 952 | return 0; |
| 953 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 954 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 955 | return 0; |
| 956 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 957 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 958 | if( pList->nExpr!=2 ){ |
| 959 | return 0; |
| 960 | } |
| 961 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 962 | return 0; |
| 963 | } |
| 964 | return 1; |
| 965 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 966 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 967 | |
| 968 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 969 | ** If the pBase expression originated in the ON or USING clause of |
| 970 | ** a join, then transfer the appropriate markings over to derived. |
| 971 | */ |
| 972 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
| 973 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 974 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 975 | } |
| 976 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 977 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 978 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 979 | ** Analyze a term that consists of two or more OR-connected |
| 980 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 981 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 982 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 983 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 984 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 985 | ** This routine analyzes terms such as the middle term in the above example. |
| 986 | ** A WhereOrTerm object is computed and attached to the term under |
| 987 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 988 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 989 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 990 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 991 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 992 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 993 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 994 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 995 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 996 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 997 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 998 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 999 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 1000 | ** (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] | 1001 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1002 | ** CASE 1: |
| 1003 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1004 | ** 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] | 1005 | ** a single table T (as shown in example B above) then create a new virtual |
| 1006 | ** term that is an equivalent IN expression. In other words, if the term |
| 1007 | ** being analyzed is: |
| 1008 | ** |
| 1009 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 1010 | ** |
| 1011 | ** then create a new virtual term like this: |
| 1012 | ** |
| 1013 | ** x IN (expr1,expr2,expr3) |
| 1014 | ** |
| 1015 | ** CASE 2: |
| 1016 | ** |
| 1017 | ** If all subterms are indexable by a single table T, then set |
| 1018 | ** |
| 1019 | ** WhereTerm.eOperator = WO_OR |
| 1020 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 1021 | ** |
| 1022 | ** A subterm is "indexable" if it is of the form |
| 1023 | ** "T.C <op> <expr>" where C is any column of table T and |
| 1024 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 1025 | ** A subterm is also indexable if it is an AND of two or more |
| 1026 | ** subsubterms at least one of which is indexable. Indexable AND |
| 1027 | ** subterms have their eOperator set to WO_AND and they have |
| 1028 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 1029 | ** |
| 1030 | ** From another point of view, "indexable" means that the subterm could |
| 1031 | ** potentially be used with an index if an appropriate index exists. |
| 1032 | ** This analysis does not consider whether or not the index exists; that |
| 1033 | ** is something the bestIndex() routine will determine. This analysis |
| 1034 | ** only looks at whether subterms appropriate for indexing exist. |
| 1035 | ** |
| 1036 | ** All examples A through E above all satisfy case 2. But if a term |
| 1037 | ** also statisfies case 1 (such as B) we know that the optimizer will |
| 1038 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 1039 | ** satisfied. |
| 1040 | ** |
| 1041 | ** It might be the case that multiple tables are indexable. For example, |
| 1042 | ** (E) above is indexable on tables P, Q, and R. |
| 1043 | ** |
| 1044 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 1045 | ** separate indices to find rowids for each subterm and composing |
| 1046 | ** the union of all rowids using a RowSet object. This is similar |
| 1047 | ** to "bitmap indices" in other database engines. |
| 1048 | ** |
| 1049 | ** OTHERWISE: |
| 1050 | ** |
| 1051 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 1052 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1053 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1054 | static void exprAnalyzeOrTerm( |
| 1055 | SrcList *pSrc, /* the FROM clause */ |
| 1056 | WhereClause *pWC, /* the complete WHERE clause */ |
| 1057 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 1058 | ){ |
| 1059 | Parse *pParse = pWC->pParse; /* Parser context */ |
| 1060 | sqlite3 *db = pParse->db; /* Database connection */ |
| 1061 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 1062 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1063 | WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1064 | int i; /* Loop counters */ |
| 1065 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 1066 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 1067 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 1068 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 1069 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1070 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1071 | /* |
| 1072 | ** Break the OR clause into its separate subterms. The subterms are |
| 1073 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 1074 | ** object that is attached to the original OR clause term. |
| 1075 | */ |
| 1076 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 1077 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 1078 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1079 | if( pOrInfo==0 ) return; |
| 1080 | pTerm->wtFlags |= TERM_ORINFO; |
| 1081 | pOrWc = &pOrInfo->wc; |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 1082 | whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1083 | whereSplit(pOrWc, pExpr, TK_OR); |
| 1084 | exprAnalyzeAll(pSrc, pOrWc); |
| 1085 | if( db->mallocFailed ) return; |
| 1086 | assert( pOrWc->nTerm>=2 ); |
| 1087 | |
| 1088 | /* |
| 1089 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 1090 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 1091 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1092 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1093 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 1094 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1095 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1096 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1097 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1098 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 1099 | if( pAndInfo ){ |
| 1100 | WhereClause *pAndWC; |
| 1101 | WhereTerm *pAndTerm; |
| 1102 | int j; |
| 1103 | Bitmask b = 0; |
| 1104 | pOrTerm->u.pAndInfo = pAndInfo; |
| 1105 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 1106 | pOrTerm->eOperator = WO_AND; |
| 1107 | pAndWC = &pAndInfo->wc; |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 1108 | whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1109 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 1110 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 1111 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 1112 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1113 | if( !db->mallocFailed ){ |
| 1114 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 1115 | assert( pAndTerm->pExpr ); |
| 1116 | if( allowedOp(pAndTerm->pExpr->op) ){ |
| 1117 | b |= getMask(pMaskSet, pAndTerm->leftCursor); |
| 1118 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | indexable &= b; |
| 1122 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1123 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 1124 | /* Skip this term for now. We revisit it when we process the |
| 1125 | ** corresponding TERM_VIRTUAL term */ |
| 1126 | }else{ |
| 1127 | Bitmask b; |
| 1128 | b = getMask(pMaskSet, pOrTerm->leftCursor); |
| 1129 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 1130 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
| 1131 | b |= getMask(pMaskSet, pOther->leftCursor); |
| 1132 | } |
| 1133 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1134 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1135 | chngToIN = 0; |
| 1136 | }else{ |
| 1137 | chngToIN &= b; |
| 1138 | } |
| 1139 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1140 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1141 | |
| 1142 | /* |
| 1143 | ** Record the set of tables that satisfy case 2. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1144 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1145 | */ |
| 1146 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1147 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1148 | |
| 1149 | /* |
| 1150 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 1151 | ** we have to do some additional checking to see if case 1 really |
| 1152 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1153 | ** |
| 1154 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 1155 | ** that there is no possibility of transforming the OR clause into an |
| 1156 | ** IN operator because one or more terms in the OR clause contain |
| 1157 | ** something other than == on a column in the single table. The 1-bit |
| 1158 | ** case means that every term of the OR clause is of the form |
| 1159 | ** "table.column=expr" for some single table. The one bit that is set |
| 1160 | ** will correspond to the common table. We still need to check to make |
| 1161 | ** sure the same column is used on all terms. The 2-bit case is when |
| 1162 | ** the all terms are of the form "table1.column=table2.column". It |
| 1163 | ** might be possible to form an IN operator with either table1.column |
| 1164 | ** or table2.column as the LHS if either is common to every term of |
| 1165 | ** the OR clause. |
| 1166 | ** |
| 1167 | ** Note that terms of the form "table.column1=table.column2" (the |
| 1168 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1169 | */ |
| 1170 | if( chngToIN ){ |
| 1171 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 1172 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 1173 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1174 | int j = 0; /* Loop counter */ |
| 1175 | |
| 1176 | /* Search for a table and column that appears on one side or the |
| 1177 | ** other of the == operator in every subterm. That table and column |
| 1178 | ** will be recorded in iCursor and iColumn. There might not be any |
| 1179 | ** such table and column. Set okToChngToIN if an appropriate table |
| 1180 | ** and column is found but leave okToChngToIN false if not found. |
| 1181 | */ |
| 1182 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 1183 | pOrTerm = pOrWc->a; |
| 1184 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1185 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1186 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1187 | if( pOrTerm->leftCursor==iCursor ){ |
| 1188 | /* This is the 2-bit case and we are on the second iteration and |
| 1189 | ** current term is from the first iteration. So skip this term. */ |
| 1190 | assert( j==1 ); |
| 1191 | continue; |
| 1192 | } |
| 1193 | if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){ |
| 1194 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 1195 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 1196 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 1197 | ** and use its inversion. */ |
| 1198 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 1199 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 1200 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 1201 | continue; |
| 1202 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1203 | iColumn = pOrTerm->u.leftColumn; |
| 1204 | iCursor = pOrTerm->leftCursor; |
| 1205 | break; |
| 1206 | } |
| 1207 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1208 | /* No candidate table+column was found. This can only occur |
| 1209 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1210 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1211 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1212 | assert( chngToIN==getMask(pMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1213 | break; |
| 1214 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1215 | testcase( j==1 ); |
| 1216 | |
| 1217 | /* We have found a candidate table and column. Check to see if that |
| 1218 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1219 | okToChngToIN = 1; |
| 1220 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1221 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1222 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1223 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1224 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1225 | okToChngToIN = 0; |
| 1226 | }else{ |
| 1227 | int affLeft, affRight; |
| 1228 | /* If the right-hand side is also a column, then the affinities |
| 1229 | ** of both right and left sides must be such that no type |
| 1230 | ** conversions are required on the right. (Ticket #2249) |
| 1231 | */ |
| 1232 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1233 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1234 | if( affRight!=0 && affRight!=affLeft ){ |
| 1235 | okToChngToIN = 0; |
| 1236 | }else{ |
| 1237 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1244 | ** case 1. In that case, construct a new virtual term that is |
| 1245 | ** pTerm converted into an IN operator. |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 1246 | ** |
| 1247 | ** EV: R-00211-15100 |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1248 | */ |
| 1249 | if( okToChngToIN ){ |
| 1250 | Expr *pDup; /* A transient duplicate expression */ |
| 1251 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1252 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1253 | Expr *pNew; /* The complete IN operator */ |
| 1254 | |
| 1255 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1256 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1257 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1258 | assert( pOrTerm->leftCursor==iCursor ); |
| 1259 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1260 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1261 | pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1262 | pLeft = pOrTerm->pExpr->pLeft; |
| 1263 | } |
| 1264 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1265 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1266 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1267 | if( pNew ){ |
| 1268 | int idxNew; |
| 1269 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1270 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1271 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1272 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1273 | testcase( idxNew==0 ); |
| 1274 | exprAnalyze(pSrc, pWC, idxNew); |
| 1275 | pTerm = &pWC->a[idxTerm]; |
| 1276 | pWC->a[idxNew].iParent = idxTerm; |
| 1277 | pTerm->nChild = 1; |
| 1278 | }else{ |
| 1279 | sqlite3ExprListDelete(db, pList); |
| 1280 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1281 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1282 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1283 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1284 | } |
| 1285 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1286 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1287 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1288 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1289 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1290 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1291 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1292 | ** |
| 1293 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1294 | ** to the standard form of "X <op> <expr>". |
| 1295 | ** |
| 1296 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1297 | ** columns, then the original expression is unchanged and a new virtual |
| 1298 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1299 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1300 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1301 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1302 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1303 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1304 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1305 | static void exprAnalyze( |
| 1306 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1307 | WhereClause *pWC, /* the WHERE clause */ |
| 1308 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1309 | ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1310 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1311 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1312 | Expr *pExpr; /* The expression to be analyzed */ |
| 1313 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1314 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1315 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1316 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1317 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1318 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1319 | int op; /* Top-level operator. pExpr->op */ |
| 1320 | Parse *pParse = pWC->pParse; /* Parsing context */ |
| 1321 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1322 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1323 | if( db->mallocFailed ){ |
| 1324 | return; |
| 1325 | } |
| 1326 | pTerm = &pWC->a[idxTerm]; |
| 1327 | pMaskSet = pWC->pMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1328 | pExpr = pTerm->pExpr; |
| 1329 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1330 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1331 | op = pExpr->op; |
| 1332 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1333 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1334 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1335 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1336 | }else{ |
| 1337 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1338 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1339 | }else if( op==TK_ISNULL ){ |
| 1340 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1341 | }else{ |
| 1342 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1343 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1344 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1345 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1346 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1347 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1348 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1349 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1350 | } |
| 1351 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1352 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1353 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1354 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1355 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1356 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1357 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1358 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1359 | if( pLeft->op==TK_COLUMN ){ |
| 1360 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1361 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1362 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1363 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1364 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1365 | WhereTerm *pNew; |
| 1366 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1367 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1368 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1369 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1370 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1371 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1372 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1373 | return; |
| 1374 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1375 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1376 | if( idxNew==0 ) return; |
| 1377 | pNew = &pWC->a[idxNew]; |
| 1378 | pNew->iParent = idxTerm; |
| 1379 | pTerm = &pWC->a[idxTerm]; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1380 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1381 | pTerm->wtFlags |= TERM_COPIED; |
drh | eb5bc92 | 2013-01-17 16:43:33 +0000 | [diff] [blame] | 1382 | if( pExpr->op==TK_EQ |
| 1383 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1384 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 1385 | ){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1386 | pTerm->eOperator |= WO_EQUIV; |
| 1387 | eExtraOp = WO_EQUIV; |
| 1388 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1389 | }else{ |
| 1390 | pDup = pExpr; |
| 1391 | pNew = pTerm; |
| 1392 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1393 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1394 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1395 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1396 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1397 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1398 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1399 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1400 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1401 | } |
| 1402 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1403 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1404 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1405 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1406 | ** that define the range that the BETWEEN implements. For example: |
| 1407 | ** |
| 1408 | ** a BETWEEN b AND c |
| 1409 | ** |
| 1410 | ** is converted into: |
| 1411 | ** |
| 1412 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1413 | ** |
| 1414 | ** The two new terms are added onto the end of the WhereClause object. |
| 1415 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1416 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1417 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1418 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1419 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1420 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1421 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1422 | int i; |
| 1423 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1424 | assert( pList!=0 ); |
| 1425 | assert( pList->nExpr==2 ); |
| 1426 | for(i=0; i<2; i++){ |
| 1427 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1428 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1429 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1430 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1431 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1432 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1433 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1434 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1435 | pTerm = &pWC->a[idxTerm]; |
| 1436 | pWC->a[idxNew].iParent = idxTerm; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1437 | } |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1438 | pTerm->nChild = 2; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1439 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1440 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1441 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1442 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1443 | /* Analyze a term that is composed of two or more subterms connected by |
| 1444 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1445 | */ |
| 1446 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1447 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1448 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1449 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1450 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1451 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1452 | |
| 1453 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1454 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1455 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1456 | ** |
| 1457 | ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 1458 | ** |
| 1459 | ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 1460 | ** |
| 1461 | ** The last character of the prefix "abc" is incremented to form the |
shane | 7bc71e5 | 2008-05-28 18:01:44 +0000 | [diff] [blame] | 1462 | ** termination condition "abd". |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1463 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1464 | if( pWC->op==TK_AND |
| 1465 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1466 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1467 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1468 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1469 | Expr *pNewExpr1; |
| 1470 | Expr *pNewExpr2; |
| 1471 | int idxNew1; |
| 1472 | int idxNew2; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1473 | Token sCollSeqName; /* Name of collating sequence */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1474 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1475 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1476 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1477 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1478 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1479 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1480 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1481 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1482 | /* The point is to increment the last character before the first |
| 1483 | ** wildcard. But if we increment '@', that will push it into the |
| 1484 | ** alphabetic range where case conversions will mess up the |
| 1485 | ** inequality. To avoid this, make sure to also run the full |
| 1486 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1487 | */ |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 1488 | if( c=='A'-1 ) isComplete = 0; /* EV: R-64339-08207 */ |
| 1489 | |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1490 | |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1491 | c = sqlite3UpperToLower[c]; |
| 1492 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1493 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1494 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1495 | sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 1496 | sCollSeqName.n = 6; |
| 1497 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1498 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1499 | sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1500 | pStr1, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1501 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1502 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1503 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1504 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1505 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1506 | sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1507 | pStr2, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1508 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1509 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1510 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1511 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1512 | if( isComplete ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1513 | pWC->a[idxNew1].iParent = idxTerm; |
| 1514 | pWC->a[idxNew2].iParent = idxTerm; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1515 | pTerm->nChild = 2; |
| 1516 | } |
| 1517 | } |
| 1518 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1519 | |
| 1520 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1521 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1522 | ** current expression is of the form: column MATCH expr. |
| 1523 | ** This information is used by the xBestIndex methods of |
| 1524 | ** virtual tables. The native query optimizer does not attempt |
| 1525 | ** to do anything with MATCH functions. |
| 1526 | */ |
| 1527 | if( isMatchOfColumn(pExpr) ){ |
| 1528 | int idxNew; |
| 1529 | Expr *pRight, *pLeft; |
| 1530 | WhereTerm *pNewTerm; |
| 1531 | Bitmask prereqColumn, prereqExpr; |
| 1532 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1533 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1534 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1535 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1536 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1537 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1538 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1539 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1540 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1541 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1542 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1543 | pNewTerm = &pWC->a[idxNew]; |
| 1544 | pNewTerm->prereqRight = prereqExpr; |
| 1545 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1546 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1547 | pNewTerm->eOperator = WO_MATCH; |
| 1548 | pNewTerm->iParent = idxTerm; |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1549 | pTerm = &pWC->a[idxTerm]; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1550 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1551 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1552 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1553 | } |
| 1554 | } |
| 1555 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1556 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1557 | #ifdef SQLITE_ENABLE_STAT3 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1558 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1559 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1560 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1561 | ** virtual term of that form. |
| 1562 | ** |
| 1563 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 1564 | ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 1565 | ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 1566 | ** the start of the loop will prevent any results from being returned. |
| 1567 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1568 | if( pExpr->op==TK_NOTNULL |
| 1569 | && pExpr->pLeft->op==TK_COLUMN |
| 1570 | && pExpr->pLeft->iColumn>=0 |
| 1571 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1572 | Expr *pNewExpr; |
| 1573 | Expr *pLeft = pExpr->pLeft; |
| 1574 | int idxNew; |
| 1575 | WhereTerm *pNewTerm; |
| 1576 | |
| 1577 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1578 | sqlite3ExprDup(db, pLeft, 0), |
| 1579 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1580 | |
| 1581 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1582 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1583 | if( idxNew ){ |
| 1584 | pNewTerm = &pWC->a[idxNew]; |
| 1585 | pNewTerm->prereqRight = 0; |
| 1586 | pNewTerm->leftCursor = pLeft->iTable; |
| 1587 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1588 | pNewTerm->eOperator = WO_GT; |
| 1589 | pNewTerm->iParent = idxTerm; |
| 1590 | pTerm = &pWC->a[idxTerm]; |
| 1591 | pTerm->nChild = 1; |
| 1592 | pTerm->wtFlags |= TERM_COPIED; |
| 1593 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1594 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1595 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1596 | #endif /* SQLITE_ENABLE_STAT */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1597 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1598 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1599 | ** an index for tables to the left of the join. |
| 1600 | */ |
| 1601 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1604 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1605 | ** This function searches the expression list passed as the second argument |
| 1606 | ** for an expression of type TK_COLUMN that refers to the same column and |
| 1607 | ** uses the same collation sequence as the iCol'th column of index pIdx. |
| 1608 | ** Argument iBase is the cursor number used for the table that pIdx refers |
| 1609 | ** to. |
| 1610 | ** |
| 1611 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1612 | ** no expression is found, -1 is returned. |
| 1613 | */ |
| 1614 | static int findIndexCol( |
| 1615 | Parse *pParse, /* Parse context */ |
| 1616 | ExprList *pList, /* Expression list to search */ |
| 1617 | int iBase, /* Cursor for table associated with pIdx */ |
| 1618 | Index *pIdx, /* Index to match column of */ |
| 1619 | int iCol /* Column of index to match */ |
| 1620 | ){ |
| 1621 | int i; |
| 1622 | const char *zColl = pIdx->azColl[iCol]; |
| 1623 | |
| 1624 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1625 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1626 | if( p->op==TK_COLUMN |
| 1627 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1628 | && p->iTable==iBase |
| 1629 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1630 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1631 | if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1632 | return i; |
| 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | return -1; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1641 | ** Return true if the DISTINCT expression-list passed as the third argument |
| 1642 | ** is redundant. A DISTINCT list is redundant if the database contains a |
| 1643 | ** UNIQUE index that guarantees that the result of the query will be distinct |
| 1644 | ** anyway. |
| 1645 | */ |
| 1646 | static int isDistinctRedundant( |
| 1647 | Parse *pParse, |
| 1648 | SrcList *pTabList, |
| 1649 | WhereClause *pWC, |
| 1650 | ExprList *pDistinct |
| 1651 | ){ |
| 1652 | Table *pTab; |
| 1653 | Index *pIdx; |
| 1654 | int i; |
| 1655 | int iBase; |
| 1656 | |
| 1657 | /* If there is more than one table or sub-select in the FROM clause of |
| 1658 | ** this query, then it will not be possible to show that the DISTINCT |
| 1659 | ** clause is redundant. */ |
| 1660 | if( pTabList->nSrc!=1 ) return 0; |
| 1661 | iBase = pTabList->a[0].iCursor; |
| 1662 | pTab = pTabList->a[0].pTab; |
| 1663 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1664 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1665 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1666 | ** current SELECT is a correlated sub-query. |
| 1667 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1668 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1669 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1670 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | /* Loop through all indices on the table, checking each to see if it makes |
| 1674 | ** the DISTINCT qualifier redundant. It does so if: |
| 1675 | ** |
| 1676 | ** 1. The index is itself UNIQUE, and |
| 1677 | ** |
| 1678 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1679 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1680 | ** where X is a constant value. The collation sequences of the |
| 1681 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1682 | ** |
| 1683 | ** 3. All of those index columns for which the WHERE clause does not |
| 1684 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1685 | */ |
| 1686 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1687 | if( pIdx->onError==OE_None ) continue; |
| 1688 | for(i=0; i<pIdx->nColumn; i++){ |
| 1689 | int iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1690 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1691 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
| 1692 | if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){ |
| 1693 | break; |
| 1694 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | if( i==pIdx->nColumn ){ |
| 1698 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1699 | return 1; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1705 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1706 | /* |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 1707 | ** Prepare a crude estimate of the logarithm of the input value. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1708 | ** The results need not be exact. This is only used for estimating |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 1709 | ** the total cost of performing operations with O(logN) or O(NlogN) |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1710 | ** complexity. Because N is just a guess, it is no great tragedy if |
| 1711 | ** logN is a little off. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1712 | */ |
| 1713 | static double estLog(double N){ |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 1714 | double logN = 1; |
| 1715 | double x = 10; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1716 | while( N>x ){ |
drh | b37df7b | 2005-10-13 02:09:49 +0000 | [diff] [blame] | 1717 | logN += 1; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1718 | x *= 10; |
| 1719 | } |
| 1720 | return logN; |
| 1721 | } |
| 1722 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1723 | /* |
| 1724 | ** Two routines for printing the content of an sqlite3_index_info |
| 1725 | ** structure. Used for testing and debugging only. If neither |
| 1726 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 1727 | ** are no-ops. |
| 1728 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 1729 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1730 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 1731 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1732 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1733 | for(i=0; i<p->nConstraint; i++){ |
| 1734 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 1735 | i, |
| 1736 | p->aConstraint[i].iColumn, |
| 1737 | p->aConstraint[i].iTermOffset, |
| 1738 | p->aConstraint[i].op, |
| 1739 | p->aConstraint[i].usable); |
| 1740 | } |
| 1741 | for(i=0; i<p->nOrderBy; i++){ |
| 1742 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 1743 | i, |
| 1744 | p->aOrderBy[i].iColumn, |
| 1745 | p->aOrderBy[i].desc); |
| 1746 | } |
| 1747 | } |
| 1748 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 1749 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1750 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1751 | for(i=0; i<p->nConstraint; i++){ |
| 1752 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 1753 | i, |
| 1754 | p->aConstraintUsage[i].argvIndex, |
| 1755 | p->aConstraintUsage[i].omit); |
| 1756 | } |
| 1757 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 1758 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 1759 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 1760 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
| 1761 | } |
| 1762 | #else |
| 1763 | #define TRACE_IDX_INPUTS(A) |
| 1764 | #define TRACE_IDX_OUTPUTS(A) |
| 1765 | #endif |
| 1766 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1767 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1768 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1769 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 1770 | ** could be used with an index to access pSrc, assuming an appropriate |
| 1771 | ** index existed. |
| 1772 | */ |
| 1773 | static int termCanDriveIndex( |
| 1774 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 1775 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 1776 | Bitmask notReady /* Tables in outer loops of the join */ |
| 1777 | ){ |
| 1778 | char aff; |
| 1779 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1780 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1781 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 1782 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1783 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 1784 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 1785 | return 1; |
| 1786 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1787 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1788 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1789 | |
| 1790 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1791 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1792 | ** Generate code to construct the Index object for an automatic index |
| 1793 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 1794 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1795 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1796 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1797 | Parse *pParse, /* The parsing context */ |
| 1798 | WhereClause *pWC, /* The WHERE clause */ |
| 1799 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 1800 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 1801 | WhereLevel *pLevel /* Write new index here */ |
| 1802 | ){ |
| 1803 | int nColumn; /* Number of columns in the constructed index */ |
| 1804 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 1805 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 1806 | int nByte; /* Byte of memory needed for pIdx */ |
| 1807 | Index *pIdx; /* Object describing the transient index */ |
| 1808 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1809 | int addrInit; /* Address of the initialization bypass jump */ |
| 1810 | Table *pTable; /* The table being indexed */ |
| 1811 | KeyInfo *pKeyinfo; /* Key information for the index */ |
| 1812 | int addrTop; /* Top of the index fill loop */ |
| 1813 | int regRecord; /* Register holding an index record */ |
| 1814 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1815 | int i; /* Loop counter */ |
| 1816 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1817 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1818 | WhereLoop *pLoop; /* The Loop object */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1819 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 1820 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1821 | const int mxConstraint = 10; /* Maximum number of constraints */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1822 | |
| 1823 | /* Generate code to skip over the creation and initialization of the |
| 1824 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 1825 | v = pParse->pVdbe; |
| 1826 | assert( v!=0 ); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 1827 | addrInit = sqlite3CodeOnce(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1828 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1829 | /* Count the number of columns that will be added to the index |
| 1830 | ** and used to match WHERE clause constraints */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1831 | nColumn = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1832 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1833 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1834 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1835 | idxCols = 0; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1836 | pLoop->aTerm = sqlite3DbRealloc(pParse->db, pLoop->aTerm, |
| 1837 | mxConstraint*sizeof(pLoop->aTerm[0])); |
| 1838 | if( pLoop->aTerm==0 ) return; |
| 1839 | for(pTerm=pWC->a; pTerm<pWCEnd && pLoop->nTerm<mxConstraint; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1840 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 1841 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1842 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1843 | testcase( iCol==BMS ); |
| 1844 | testcase( iCol==BMS-1 ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1845 | if( (idxCols & cMask)==0 ){ |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1846 | pLoop->aTerm[nColumn++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1847 | idxCols |= cMask; |
| 1848 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1849 | } |
| 1850 | } |
| 1851 | assert( nColumn>0 ); |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1852 | pLoop->u.btree.nEq = pLoop->nTerm = nColumn; |
| 1853 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
| 1854 | | WHERE_TEMP_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1855 | |
| 1856 | /* Count the number of additional columns needed to create a |
| 1857 | ** covering index. A "covering index" is an index that contains all |
| 1858 | ** columns that are needed by the query. With a covering index, the |
| 1859 | ** original table never needs to be accessed. Automatic indices must |
| 1860 | ** be a covering index because the index will not be updated if the |
| 1861 | ** original table changes and the index and table cannot both be used |
| 1862 | ** if they go out of sync. |
| 1863 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1864 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1865 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1866 | testcase( pTable->nCol==BMS-1 ); |
| 1867 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1868 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1869 | if( extraCols & MASKBIT(i) ) nColumn++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1870 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1871 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1872 | nColumn += pTable->nCol - BMS + 1; |
| 1873 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1874 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1875 | |
| 1876 | /* Construct the Index object to describe this index */ |
| 1877 | nByte = sizeof(Index); |
| 1878 | nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 1879 | nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 1880 | nByte += nColumn; /* Index.aSortOrder */ |
| 1881 | pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 1882 | if( pIdx==0 ) return; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1883 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1884 | pIdx->azColl = (char**)&pIdx[1]; |
| 1885 | pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 1886 | pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 1887 | pIdx->zName = "auto-index"; |
| 1888 | pIdx->nColumn = nColumn; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1889 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1890 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1891 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1892 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1893 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1894 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1895 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1896 | if( (idxCols & cMask)==0 ){ |
| 1897 | Expr *pX = pTerm->pExpr; |
| 1898 | idxCols |= cMask; |
| 1899 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 1900 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 6f2e6c0 | 2011-02-17 13:33:15 +0000 | [diff] [blame] | 1901 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1902 | n++; |
| 1903 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1904 | } |
| 1905 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1906 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1907 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1908 | /* Add additional columns needed to make the automatic index into |
| 1909 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1910 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1911 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1912 | pIdx->aiColumn[n] = i; |
| 1913 | pIdx->azColl[n] = "BINARY"; |
| 1914 | n++; |
| 1915 | } |
| 1916 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1917 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1918 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 1919 | pIdx->aiColumn[n] = i; |
| 1920 | pIdx->azColl[n] = "BINARY"; |
| 1921 | n++; |
| 1922 | } |
| 1923 | } |
| 1924 | assert( n==nColumn ); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1925 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1926 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1927 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 1928 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 1929 | pLevel->iIdxCur = pParse->nTab++; |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1930 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1931 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1932 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1933 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1934 | /* Fill the automatic index with content */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1935 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); |
| 1936 | regRecord = sqlite3GetTempReg(pParse); |
| 1937 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1); |
| 1938 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 1939 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 1940 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1941 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1942 | sqlite3VdbeJumpHere(v, addrTop); |
| 1943 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 1944 | |
| 1945 | /* Jump here when skipping the initialization */ |
| 1946 | sqlite3VdbeJumpHere(v, addrInit); |
| 1947 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1948 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1949 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1950 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1951 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1952 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 1953 | ** responsibility of the caller to eventually release the structure |
| 1954 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 1955 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 1956 | static sqlite3_index_info *allocateIndexInfo( |
| 1957 | Parse *pParse, |
| 1958 | WhereClause *pWC, |
| 1959 | struct SrcList_item *pSrc, |
| 1960 | ExprList *pOrderBy |
| 1961 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1962 | int i, j; |
| 1963 | int nTerm; |
| 1964 | struct sqlite3_index_constraint *pIdxCons; |
| 1965 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 1966 | struct sqlite3_index_constraint_usage *pUsage; |
| 1967 | WhereTerm *pTerm; |
| 1968 | int nOrderBy; |
| 1969 | sqlite3_index_info *pIdxInfo; |
| 1970 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 1971 | /*WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));*/ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1972 | |
| 1973 | /* Count the number of possible WHERE clause constraints referring |
| 1974 | ** to this virtual table */ |
| 1975 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 1976 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1977 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 1978 | testcase( pTerm->eOperator & WO_IN ); |
| 1979 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 1980 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 1981 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1982 | nTerm++; |
| 1983 | } |
| 1984 | |
| 1985 | /* If the ORDER BY clause contains only columns in the current |
| 1986 | ** virtual table then allocate space for the aOrderBy part of |
| 1987 | ** the sqlite3_index_info structure. |
| 1988 | */ |
| 1989 | nOrderBy = 0; |
| 1990 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 1991 | int n = pOrderBy->nExpr; |
| 1992 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1993 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 1994 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 1995 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 1996 | if( i==n){ |
| 1997 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | /* Allocate the sqlite3_index_info structure |
| 2002 | */ |
| 2003 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 2004 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 2005 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 2006 | if( pIdxInfo==0 ){ |
| 2007 | sqlite3ErrorMsg(pParse, "out of memory"); |
| 2008 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
| 2012 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 2013 | ** many fields that are declared "const" to prevent xBestIndex from |
| 2014 | ** changing them. We have to do some funky casting in order to |
| 2015 | ** initialize those fields. |
| 2016 | */ |
| 2017 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 2018 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 2019 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 2020 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 2021 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 2022 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 2023 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 2024 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 2025 | pUsage; |
| 2026 | |
| 2027 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2028 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2029 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2030 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2031 | testcase( pTerm->eOperator & WO_IN ); |
| 2032 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2033 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2034 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2035 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 2036 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2037 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2038 | if( op==WO_IN ) op = WO_EQ; |
| 2039 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2040 | /* The direct assignment in the previous line is possible only because |
| 2041 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 2042 | ** following asserts verify this fact. */ |
| 2043 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 2044 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 2045 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 2046 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 2047 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 2048 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2049 | 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] | 2050 | j++; |
| 2051 | } |
| 2052 | for(i=0; i<nOrderBy; i++){ |
| 2053 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2054 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 2055 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 2056 | } |
| 2057 | |
| 2058 | return pIdxInfo; |
| 2059 | } |
| 2060 | |
| 2061 | /* |
| 2062 | ** The table object reference passed as the second argument to this function |
| 2063 | ** must represent a virtual table. This function invokes the xBestIndex() |
| 2064 | ** method of the virtual table with the sqlite3_index_info pointer passed |
| 2065 | ** as the argument. |
| 2066 | ** |
| 2067 | ** If an error occurs, pParse is populated with an error message and a |
| 2068 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 2069 | ** part of the sqlite3_index_info structure is left populated. |
| 2070 | ** |
| 2071 | ** Whether or not an error is returned, it is the responsibility of the |
| 2072 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 2073 | ** that this is required. |
| 2074 | */ |
| 2075 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 2076 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2077 | int i; |
| 2078 | int rc; |
| 2079 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2080 | /*WHERETRACE(("xBestIndex for %s\n", pTab->zName));*/ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2081 | TRACE_IDX_INPUTS(p); |
| 2082 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 2083 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2084 | |
| 2085 | if( rc!=SQLITE_OK ){ |
| 2086 | if( rc==SQLITE_NOMEM ){ |
| 2087 | pParse->db->mallocFailed = 1; |
| 2088 | }else if( !pVtab->zErrMsg ){ |
| 2089 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 2090 | }else{ |
| 2091 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 2092 | } |
| 2093 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2094 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2095 | pVtab->zErrMsg = 0; |
| 2096 | |
| 2097 | for(i=0; i<p->nConstraint; i++){ |
| 2098 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 2099 | sqlite3ErrorMsg(pParse, |
| 2100 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | return pParse->nErr; |
| 2105 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2106 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2107 | |
| 2108 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2109 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2110 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2111 | ** Estimate the location of a particular key among all keys in an |
| 2112 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2113 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2114 | ** aStat[0] Est. number of rows less than pVal |
| 2115 | ** aStat[1] Est. number of rows equal to pVal |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2116 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2117 | ** Return SQLITE_OK on success. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2118 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2119 | static int whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2120 | Parse *pParse, /* Database connection */ |
| 2121 | Index *pIdx, /* Index to consider domain of */ |
| 2122 | sqlite3_value *pVal, /* Value to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2123 | int roundUp, /* Round up if true. Round down if false */ |
| 2124 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2125 | ){ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2126 | tRowcnt n; |
| 2127 | IndexSample *aSample; |
| 2128 | int i, eType; |
| 2129 | int isEq = 0; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2130 | i64 v; |
| 2131 | double r, rS; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2132 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2133 | assert( roundUp==0 || roundUp==1 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2134 | assert( pIdx->nSample>0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2135 | if( pVal==0 ) return SQLITE_ERROR; |
| 2136 | n = pIdx->aiRowEst[0]; |
| 2137 | aSample = pIdx->aSample; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2138 | eType = sqlite3_value_type(pVal); |
| 2139 | |
| 2140 | if( eType==SQLITE_INTEGER ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2141 | v = sqlite3_value_int64(pVal); |
| 2142 | r = (i64)v; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2143 | for(i=0; i<pIdx->nSample; i++){ |
| 2144 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2145 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2146 | if( aSample[i].eType==SQLITE_INTEGER ){ |
| 2147 | if( aSample[i].u.i>=v ){ |
| 2148 | isEq = aSample[i].u.i==v; |
| 2149 | break; |
| 2150 | } |
| 2151 | }else{ |
| 2152 | assert( aSample[i].eType==SQLITE_FLOAT ); |
| 2153 | if( aSample[i].u.r>=r ){ |
| 2154 | isEq = aSample[i].u.r==r; |
| 2155 | break; |
| 2156 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2157 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2158 | } |
| 2159 | }else if( eType==SQLITE_FLOAT ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2160 | r = sqlite3_value_double(pVal); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2161 | for(i=0; i<pIdx->nSample; i++){ |
| 2162 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2163 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2164 | if( aSample[i].eType==SQLITE_FLOAT ){ |
| 2165 | rS = aSample[i].u.r; |
| 2166 | }else{ |
| 2167 | rS = aSample[i].u.i; |
| 2168 | } |
| 2169 | if( rS>=r ){ |
| 2170 | isEq = rS==r; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2171 | break; |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2172 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2173 | } |
| 2174 | }else if( eType==SQLITE_NULL ){ |
| 2175 | i = 0; |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2176 | if( aSample[0].eType==SQLITE_NULL ) isEq = 1; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2177 | }else{ |
| 2178 | assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 2179 | for(i=0; i<pIdx->nSample; i++){ |
| 2180 | if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){ |
| 2181 | break; |
| 2182 | } |
| 2183 | } |
| 2184 | if( i<pIdx->nSample ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2185 | sqlite3 *db = pParse->db; |
| 2186 | CollSeq *pColl; |
| 2187 | const u8 *z; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2188 | if( eType==SQLITE_BLOB ){ |
| 2189 | z = (const u8 *)sqlite3_value_blob(pVal); |
| 2190 | pColl = db->pDfltColl; |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2191 | assert( pColl->enc==SQLITE_UTF8 ); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2192 | }else{ |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 2193 | pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl); |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 2194 | if( pColl==0 ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2195 | return SQLITE_ERROR; |
| 2196 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2197 | z = (const u8 *)sqlite3ValueText(pVal, pColl->enc); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2198 | if( !z ){ |
| 2199 | return SQLITE_NOMEM; |
| 2200 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2201 | assert( z && pColl && pColl->xCmp ); |
| 2202 | } |
| 2203 | n = sqlite3ValueBytes(pVal, pColl->enc); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2204 | |
| 2205 | for(; i<pIdx->nSample; i++){ |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2206 | int c; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2207 | int eSampletype = aSample[i].eType; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2208 | if( eSampletype<eType ) continue; |
| 2209 | if( eSampletype!=eType ) break; |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2210 | #ifndef SQLITE_OMIT_UTF16 |
| 2211 | if( pColl->enc!=SQLITE_UTF8 ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2212 | int nSample; |
| 2213 | char *zSample = sqlite3Utf8to16( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2214 | db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample |
| 2215 | ); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2216 | if( !zSample ){ |
| 2217 | assert( db->mallocFailed ); |
| 2218 | return SQLITE_NOMEM; |
| 2219 | } |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2220 | c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2221 | sqlite3DbFree(db, zSample); |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2222 | }else |
| 2223 | #endif |
| 2224 | { |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2225 | 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] | 2226 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2227 | if( c>=0 ){ |
| 2228 | if( c==0 ) isEq = 1; |
| 2229 | break; |
| 2230 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2231 | } |
| 2232 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2233 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2234 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2235 | /* At this point, aSample[i] is the first sample that is greater than |
| 2236 | ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 2237 | ** than pVal. If aSample[i]==pVal, then isEq==1. |
| 2238 | */ |
| 2239 | if( isEq ){ |
| 2240 | assert( i<pIdx->nSample ); |
| 2241 | aStat[0] = aSample[i].nLt; |
| 2242 | aStat[1] = aSample[i].nEq; |
| 2243 | }else{ |
| 2244 | tRowcnt iLower, iUpper, iGap; |
| 2245 | if( i==0 ){ |
| 2246 | iLower = 0; |
| 2247 | iUpper = aSample[0].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2248 | }else{ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2249 | iUpper = i>=pIdx->nSample ? n : aSample[i].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2250 | iLower = aSample[i-1].nEq + aSample[i-1].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2251 | } |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2252 | aStat[1] = pIdx->avgEq; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2253 | if( iLower>=iUpper ){ |
| 2254 | iGap = 0; |
| 2255 | }else{ |
| 2256 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2257 | } |
| 2258 | if( roundUp ){ |
| 2259 | iGap = (iGap*2)/3; |
| 2260 | }else{ |
| 2261 | iGap = iGap/3; |
| 2262 | } |
| 2263 | aStat[0] = iLower + iGap; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2264 | } |
| 2265 | return SQLITE_OK; |
| 2266 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2267 | #endif /* SQLITE_ENABLE_STAT3 */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2268 | |
| 2269 | /* |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2270 | ** If expression pExpr represents a literal value, set *pp to point to |
| 2271 | ** an sqlite3_value structure containing the same value, with affinity |
| 2272 | ** aff applied to it, before returning. It is the responsibility of the |
| 2273 | ** caller to eventually release this structure by passing it to |
| 2274 | ** sqlite3ValueFree(). |
| 2275 | ** |
| 2276 | ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr |
| 2277 | ** is an SQL variable that currently has a non-NULL value bound to it, |
| 2278 | ** create an sqlite3_value structure containing this value, again with |
| 2279 | ** affinity aff applied to it, instead. |
| 2280 | ** |
| 2281 | ** If neither of the above apply, set *pp to NULL. |
| 2282 | ** |
| 2283 | ** If an error occurs, return an error code. Otherwise, SQLITE_OK. |
| 2284 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2285 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2286 | static int valueFromExpr( |
| 2287 | Parse *pParse, |
| 2288 | Expr *pExpr, |
| 2289 | u8 aff, |
| 2290 | sqlite3_value **pp |
| 2291 | ){ |
drh | 4278d53 | 2010-12-16 19:52:52 +0000 | [diff] [blame] | 2292 | if( pExpr->op==TK_VARIABLE |
| 2293 | || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) |
| 2294 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2295 | int iVar = pExpr->iColumn; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 2296 | sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2297 | *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); |
| 2298 | return SQLITE_OK; |
| 2299 | } |
| 2300 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 2301 | } |
dan | f7b0b0a | 2009-10-19 15:52:32 +0000 | [diff] [blame] | 2302 | #endif |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2303 | |
| 2304 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2305 | ** This function is used to estimate the number of rows that will be visited |
| 2306 | ** by scanning an index for a range of values. The range may have an upper |
| 2307 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2308 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2309 | ** example, assuming that index p is on t1(a): |
| 2310 | ** |
| 2311 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2312 | ** |_____| |_____| |
| 2313 | ** | | |
| 2314 | ** pLower pUpper |
| 2315 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2316 | ** 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] | 2317 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2318 | ** |
| 2319 | ** The nEq parameter is passed the index of the index column subject to the |
| 2320 | ** range constraint. Or, equivalently, the number of equality constraints |
| 2321 | ** optimized by the proposed index scan. For example, assuming index p is |
| 2322 | ** on t1(a, b), and the SQL query is: |
| 2323 | ** |
| 2324 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2325 | ** |
| 2326 | ** then nEq should be passed the value 1 (as the range restricted column, |
| 2327 | ** b, is the second left-most column of the index). Or, if the query is: |
| 2328 | ** |
| 2329 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2330 | ** |
| 2331 | ** then nEq should be passed 0. |
| 2332 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2333 | ** The returned value is an integer divisor to reduce the estimated |
| 2334 | ** search space. A return value of 1 means that range constraints are |
| 2335 | ** no help at all. A return value of 2 means range constraints are |
| 2336 | ** expected to reduce the search space by half. And so forth... |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2337 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2338 | ** In the absence of sqlite_stat3 ANALYZE data, each range inequality |
| 2339 | ** reduces the search space by a factor of 4. Hence a single constraint (x>?) |
| 2340 | ** results in a return of 4 and a range constraint (x>? AND x<?) results |
| 2341 | ** in a return of 16. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2342 | */ |
| 2343 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2344 | Parse *pParse, /* Parsing & code generating context */ |
| 2345 | Index *p, /* The index containing the range-compared column; "x" */ |
| 2346 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 2347 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2348 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2349 | double *pRangeDiv /* OUT: Reduce search space by this divisor */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2350 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2351 | int rc = SQLITE_OK; |
| 2352 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2353 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2354 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2355 | if( nEq==0 && p->nSample ){ |
| 2356 | sqlite3_value *pRangeVal; |
| 2357 | tRowcnt iLower = 0; |
| 2358 | tRowcnt iUpper = p->aiRowEst[0]; |
| 2359 | tRowcnt a[2]; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2360 | u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2361 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2362 | if( pLower ){ |
| 2363 | Expr *pExpr = pLower->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2364 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2365 | assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2366 | if( rc==SQLITE_OK |
| 2367 | && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK |
| 2368 | ){ |
| 2369 | iLower = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2370 | if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2371 | } |
| 2372 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2373 | } |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2374 | if( rc==SQLITE_OK && pUpper ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2375 | Expr *pExpr = pUpper->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2376 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2377 | assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2378 | if( rc==SQLITE_OK |
| 2379 | && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK |
| 2380 | ){ |
| 2381 | iUpper = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2382 | if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1]; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2383 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2384 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2385 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2386 | if( rc==SQLITE_OK ){ |
| 2387 | if( iUpper<=iLower ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2388 | *pRangeDiv = (double)p->aiRowEst[0]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2389 | }else{ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2390 | *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2391 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2392 | /*WHERETRACE(("range scan regions: %u..%u div=%g\n", |
| 2393 | (u32)iLower, (u32)iUpper, *pRangeDiv));*/ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2394 | return SQLITE_OK; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2395 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2396 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2397 | #else |
| 2398 | UNUSED_PARAMETER(pParse); |
| 2399 | UNUSED_PARAMETER(p); |
| 2400 | UNUSED_PARAMETER(nEq); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2401 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2402 | assert( pLower || pUpper ); |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2403 | *pRangeDiv = (double)1; |
| 2404 | if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4; |
| 2405 | if( pUpper ) *pRangeDiv *= (double)4; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2406 | return rc; |
| 2407 | } |
| 2408 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2409 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2410 | /* |
| 2411 | ** Estimate the number of rows that will be returned based on |
| 2412 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2413 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2414 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2415 | ** for that index. When pExpr==NULL that means the constraint is |
| 2416 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2417 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2418 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2419 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2420 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2421 | ** |
| 2422 | ** This routine can fail if it is unable to load a collating sequence |
| 2423 | ** required for string comparison, or if unable to allocate memory |
| 2424 | ** for a UTF conversion required for comparison. The error is stored |
| 2425 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2426 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2427 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2428 | Parse *pParse, /* Parsing & code generating context */ |
| 2429 | Index *p, /* The index whose left-most column is pTerm */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2430 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2431 | double *pnRow /* Write the revised row estimate here */ |
| 2432 | ){ |
| 2433 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2434 | u8 aff; /* Column affinity */ |
| 2435 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2436 | tRowcnt a[2]; /* Statistics */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2437 | |
| 2438 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2439 | assert( p->nSample>0 ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2440 | aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2441 | if( pExpr ){ |
| 2442 | rc = valueFromExpr(pParse, pExpr, aff, &pRhs); |
| 2443 | if( rc ) goto whereEqualScanEst_cancel; |
| 2444 | }else{ |
| 2445 | pRhs = sqlite3ValueNew(pParse->db); |
| 2446 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2447 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2448 | rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 2449 | if( rc==SQLITE_OK ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2450 | /*WHERETRACE(("equality scan regions: %d\n", (int)a[1]));*/ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2451 | *pnRow = a[1]; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2452 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2453 | whereEqualScanEst_cancel: |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2454 | sqlite3ValueFree(pRhs); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2455 | return rc; |
| 2456 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2457 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2458 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2459 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2460 | /* |
| 2461 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2462 | ** an IN constraint where the right-hand side of the IN operator |
| 2463 | ** is a list of values. Example: |
| 2464 | ** |
| 2465 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2466 | ** |
| 2467 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2468 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2469 | ** non-zero. |
| 2470 | ** |
| 2471 | ** This routine can fail if it is unable to load a collating sequence |
| 2472 | ** required for string comparison, or if unable to allocate memory |
| 2473 | ** for a UTF conversion required for comparison. The error is stored |
| 2474 | ** in the pParse structure. |
| 2475 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2476 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2477 | Parse *pParse, /* Parsing & code generating context */ |
| 2478 | Index *p, /* The index whose left-most column is pTerm */ |
| 2479 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 2480 | double *pnRow /* Write the revised row estimate here */ |
| 2481 | ){ |
drh | 6825719 | 2011-08-16 17:06:21 +0000 | [diff] [blame] | 2482 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2483 | double nEst; /* Number of rows for a single term */ |
| 2484 | double nRowEst = (double)0; /* New estimate of the number of rows */ |
| 2485 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2486 | |
| 2487 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2488 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 2489 | nEst = p->aiRowEst[0]; |
| 2490 | rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| 2491 | nRowEst += nEst; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2492 | } |
| 2493 | if( rc==SQLITE_OK ){ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2494 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 2495 | *pnRow = nRowEst; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2496 | /*WHERETRACE(("IN row estimate: est=%g\n", nRowEst));*/ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2497 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2498 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2499 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2500 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2501 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2502 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2503 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2504 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2505 | ** or USING clause of that join. |
| 2506 | ** |
| 2507 | ** Consider the term t2.z='ok' in the following queries: |
| 2508 | ** |
| 2509 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2510 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2511 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2512 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2513 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2514 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2515 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2516 | ** |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 2517 | ** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are |
| 2518 | ** completely satisfied by indices. |
| 2519 | ** |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2520 | ** 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] | 2521 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2522 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2523 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2524 | ** but joins might run a little slower. The trick is to disable as much |
| 2525 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2526 | ** the wrong answer. See ticket #813. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2527 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2528 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 2529 | if( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2530 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2531 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 2532 | ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 2533 | pTerm->wtFlags |= TERM_CODED; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 2534 | if( pTerm->iParent>=0 ){ |
| 2535 | WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 2536 | if( (--pOther->nChild)==0 ){ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 2537 | disableTerm(pLevel, pOther); |
| 2538 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2539 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2544 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2545 | ** to the n registers starting at base. |
| 2546 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2547 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2548 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2549 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2550 | ** |
| 2551 | ** This routine makes its own copy of zAff so that the caller is free |
| 2552 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2553 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2554 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2555 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2556 | if( zAff==0 ){ |
| 2557 | assert( pParse->db->mallocFailed ); |
| 2558 | return; |
| 2559 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2560 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2561 | |
| 2562 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2563 | ** and end of the affinity string. |
| 2564 | */ |
| 2565 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2566 | n--; |
| 2567 | base++; |
| 2568 | zAff++; |
| 2569 | } |
| 2570 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2571 | n--; |
| 2572 | } |
| 2573 | |
| 2574 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2575 | if( n>0 ){ |
| 2576 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2577 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2578 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2579 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2582 | |
| 2583 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2584 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2585 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2586 | ** coded. |
| 2587 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2588 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2589 | ** |
| 2590 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2591 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2592 | ** 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] | 2593 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2594 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2595 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2596 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2597 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2598 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2599 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2600 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2601 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2602 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2603 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2604 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2605 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2606 | assert( iTarget>0 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2607 | if( pX->op==TK_EQ ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2608 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2609 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2610 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2611 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2612 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2613 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2614 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2615 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2616 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2617 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2618 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2619 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2620 | && pLoop->u.btree.pIndex!=0 |
| 2621 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2622 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2623 | testcase( iEq==0 ); |
| 2624 | testcase( iEq==pLevel->plan.u.pIdx->nColumn-1 ); |
| 2625 | testcase( iEq>0 && iEq+1<pLevel->plan.u.pIdx->nColumn ); |
| 2626 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2627 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2628 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2629 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2630 | iReg = iTarget; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2631 | eType = sqlite3FindInIndex(pParse, pX, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2632 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2633 | testcase( bRev ); |
| 2634 | bRev = !bRev; |
| 2635 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2636 | iTab = pX->iTable; |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2637 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2638 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2639 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2640 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2641 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2642 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2643 | pLevel->u.in.nIn++; |
| 2644 | pLevel->u.in.aInLoop = |
| 2645 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2646 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2647 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2648 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2649 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2650 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2651 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2652 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2653 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2654 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2655 | } |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2656 | pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2657 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2658 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2659 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2660 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2661 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2662 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2663 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2664 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2667 | /* |
| 2668 | ** Generate code that will evaluate all == and IN constraints for an |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2669 | ** index. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2670 | ** |
| 2671 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2672 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2673 | ** The index has as many as three equality constraints, but in this |
| 2674 | ** example, the third "c" value is an inequality. So only two |
| 2675 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2676 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2677 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2678 | ** |
| 2679 | ** In the example above nEq==2. But this subroutine works for any value |
| 2680 | ** 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] | 2681 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2682 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2683 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2684 | ** This routine always allocates at least one memory cell and returns |
| 2685 | ** the index of that memory cell. The code that |
| 2686 | ** calls this routine will use that memory cell to store the termination |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2687 | ** key value of the loop. If one or more IN operators appear, then |
| 2688 | ** this routine allocates an additional nEq memory cells for internal |
| 2689 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2690 | ** |
| 2691 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2692 | ** copy of the column affinity string of the index allocated using |
| 2693 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2694 | ** with equality constraints that use NONE affinity are set to |
| 2695 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2696 | ** |
| 2697 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2698 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2699 | ** |
| 2700 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2701 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2702 | ** no conversion should be attempted before using a t2.b value as part of |
| 2703 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2704 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2705 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2706 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2707 | Parse *pParse, /* Parsing context */ |
| 2708 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 2709 | WhereClause *pWC, /* The WHERE clause */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2710 | Bitmask notReady, /* Which parts of FROM have not yet been coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2711 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2712 | int nExtraReg, /* Number of extra registers to allocate */ |
| 2713 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2714 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2715 | int nEq; /* The number of == or IN constraints to code */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2716 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 2717 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2718 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2719 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2720 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2721 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2722 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2723 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2724 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2725 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2726 | pLoop = pLevel->pWLoop; |
| 2727 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 2728 | nEq = pLoop->u.btree.nEq; |
| 2729 | pIdx = pLoop->u.btree.pIndex; |
| 2730 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2731 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2732 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2733 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2734 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2735 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2736 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2737 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2738 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 2739 | if( !zAff ){ |
| 2740 | pParse->db->mallocFailed = 1; |
| 2741 | } |
| 2742 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2743 | /* Evaluate the equality constraints |
| 2744 | */ |
drh | c49de5d | 2007-01-19 01:06:01 +0000 | [diff] [blame] | 2745 | assert( pIdx->nColumn>=nEq ); |
| 2746 | for(j=0; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2747 | int r1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2748 | pTerm = pLoop->aTerm[j]; |
| 2749 | assert( pTerm!=0 ); |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2750 | /* The following true for indices with redundant columns. |
| 2751 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 2752 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 2753 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2754 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2755 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2756 | if( nReg==1 ){ |
| 2757 | sqlite3ReleaseTempReg(pParse, regBase); |
| 2758 | regBase = r1; |
| 2759 | }else{ |
| 2760 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 2761 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2762 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 2763 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 2764 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2765 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2766 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 2f2855b | 2009-11-18 01:25:26 +0000 | [diff] [blame] | 2767 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2768 | if( zAff ){ |
| 2769 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 2770 | zAff[j] = SQLITE_AFF_NONE; |
| 2771 | } |
| 2772 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 2773 | zAff[j] = SQLITE_AFF_NONE; |
| 2774 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2775 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2776 | } |
| 2777 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2778 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2779 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2782 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2783 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2784 | ** This routine is a helper for explainIndexRange() below |
| 2785 | ** |
| 2786 | ** pStr holds the text of an expression that we are building up one term |
| 2787 | ** at a time. This routine adds a new term to the end of the expression. |
| 2788 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 2789 | ** terms only. |
| 2790 | */ |
| 2791 | static void explainAppendTerm( |
| 2792 | StrAccum *pStr, /* The text expression being built */ |
| 2793 | int iTerm, /* Index of this term. First is zero */ |
| 2794 | const char *zColumn, /* Name of the column */ |
| 2795 | const char *zOp /* Name of the operator */ |
| 2796 | ){ |
| 2797 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 2798 | sqlite3StrAccumAppend(pStr, zColumn, -1); |
| 2799 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 2800 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 2801 | } |
| 2802 | |
| 2803 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2804 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 2805 | ** function returns a pointer to a string buffer containing a description |
| 2806 | ** of the subset of table rows scanned by the strategy in the form of an |
| 2807 | ** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 2808 | ** |
| 2809 | ** For example, if the query: |
| 2810 | ** |
| 2811 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 2812 | ** |
| 2813 | ** is run and there is an index on (a, b), then this function returns a |
| 2814 | ** string similar to: |
| 2815 | ** |
| 2816 | ** "a=? AND b>?" |
| 2817 | ** |
| 2818 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 2819 | ** It is the responsibility of the caller to free the buffer when it is |
| 2820 | ** no longer required. |
| 2821 | */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2822 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 2823 | Index *pIndex = pLoop->u.btree.pIndex; |
| 2824 | int nEq = pLoop->u.btree.nEq; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2825 | int i, j; |
| 2826 | Column *aCol = pTab->aCol; |
| 2827 | int *aiColumn = pIndex->aiColumn; |
| 2828 | StrAccum txt; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2829 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2830 | if( pIndex==0 ) return 0; |
| 2831 | if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2832 | return 0; |
| 2833 | } |
| 2834 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
drh | 03b6df1 | 2010-11-15 16:29:30 +0000 | [diff] [blame] | 2835 | txt.db = db; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2836 | sqlite3StrAccumAppend(&txt, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2837 | for(i=0; i<nEq; i++){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2838 | explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "="); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2841 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2842 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 2843 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 2844 | explainAppendTerm(&txt, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2845 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2846 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 2847 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 2848 | explainAppendTerm(&txt, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2849 | } |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2850 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 2851 | return sqlite3StrAccumFinish(&txt); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2854 | /* |
| 2855 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 2856 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 2857 | ** record is added to the output to describe the table scan strategy in |
| 2858 | ** pLevel. |
| 2859 | */ |
| 2860 | static void explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2861 | Parse *pParse, /* Parse context */ |
| 2862 | SrcList *pTabList, /* Table list this loop refers to */ |
| 2863 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 2864 | int iLevel, /* Value for "level" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2865 | int iFrom, /* Value for "from" column of output */ |
| 2866 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2867 | ){ |
| 2868 | if( pParse->explain==2 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2869 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2870 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 2871 | sqlite3 *db = pParse->db; /* Database handle */ |
| 2872 | char *zMsg; /* Text to add to EQP output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2873 | sqlite3_int64 nRow; /* Expected number of rows visited by scan */ |
| 2874 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2875 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2876 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 2877 | u32 flags; /* Flags that describe this loop */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2878 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2879 | pLoop = pLevel->pWLoop; |
| 2880 | flags = pLoop->wsFlags; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2881 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2882 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2883 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 2884 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 2885 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2886 | |
| 2887 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2888 | if( pItem->pSelect ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2889 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2890 | }else{ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2891 | zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2894 | if( pItem->zAlias ){ |
| 2895 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 2896 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2897 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
| 2898 | && pLoop->u.btree.pIndex!=0 |
| 2899 | ){ |
| 2900 | char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2901 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2902 | ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""), |
| 2903 | ((flags & WHERE_IDX_ONLY)?"COVERING ":""), |
| 2904 | ((flags & WHERE_TEMP_INDEX)?"":" "), |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2905 | ((flags & WHERE_TEMP_INDEX)?"": pLoop->u.btree.pIndex->zName), |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2906 | zWhere |
| 2907 | ); |
| 2908 | sqlite3DbFree(db, zWhere); |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 2909 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 2910 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2911 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2912 | if( flags&WHERE_COLUMN_EQ ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2913 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 2914 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2915 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 2916 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 2917 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
| 2918 | }else if( flags&WHERE_TOP_LIMIT ){ |
| 2919 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 2920 | } |
| 2921 | } |
| 2922 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2923 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2924 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2925 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2926 | } |
| 2927 | #endif |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2928 | if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){ |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 2929 | testcase( wctrlFlags & WHERE_ORDERBY_MIN ); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2930 | nRow = 1; |
| 2931 | }else{ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 2932 | nRow = (sqlite3_int64)pLoop->nOut; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 2933 | } |
| 2934 | zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow); |
| 2935 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2936 | } |
| 2937 | } |
| 2938 | #else |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2939 | # define explainOneScan(u,v,w,x,y,z) |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2940 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 2941 | |
| 2942 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2943 | /* |
| 2944 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 2945 | ** implementation described by pWInfo. |
| 2946 | */ |
| 2947 | static Bitmask codeOneLoopStart( |
| 2948 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 2949 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 2950 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 2951 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2952 | ){ |
| 2953 | int j, k; /* Loop counters */ |
| 2954 | int iCur; /* The VDBE cursor for the table */ |
| 2955 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 2956 | int omitTable; /* True if we use the index only */ |
| 2957 | int bRev; /* True if we need to scan in reverse order */ |
| 2958 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2959 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2960 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 2961 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 2962 | Parse *pParse; /* Parsing context */ |
| 2963 | Vdbe *v; /* The prepared stmt under constructions */ |
| 2964 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 2965 | int addrBrk; /* Jump here to break out of the loop */ |
| 2966 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 2967 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 2968 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 2969 | Bitmask newNotReady; /* Return value */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2970 | |
| 2971 | pParse = pWInfo->pParse; |
| 2972 | v = pParse->pVdbe; |
| 2973 | pWC = pWInfo->pWC; |
| 2974 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2975 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2976 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 2977 | iCur = pTabItem->iCursor; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2978 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 2979 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 2980 | && (wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 2981 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2982 | |
| 2983 | /* Create labels for the "break" and "continue" instructions |
| 2984 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 2985 | ** Jump to cont to go immediately to the next iteration of the |
| 2986 | ** loop. |
| 2987 | ** |
| 2988 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 2989 | ** means to continue with the next IN value combination. When |
| 2990 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 2991 | ** is the same as "addrBrk". |
| 2992 | */ |
| 2993 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 2994 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 2995 | |
| 2996 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 2997 | ** initialize a memory cell that records if this table matches any |
| 2998 | ** row of the left table of the join. |
| 2999 | */ |
| 3000 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 3001 | pLevel->iLeftJoin = ++pParse->nMem; |
| 3002 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 3003 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 3004 | } |
| 3005 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 3006 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 3007 | if( pTabItem->viaCoroutine ){ |
| 3008 | int regYield = pTabItem->regReturn; |
| 3009 | sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield); |
| 3010 | pLevel->p2 = sqlite3VdbeAddOp1(v, OP_Yield, regYield); |
| 3011 | VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName)); |
| 3012 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 3013 | pLevel->op = OP_Goto; |
| 3014 | }else |
| 3015 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3016 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3017 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 3018 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3019 | ** to access the data. |
| 3020 | */ |
| 3021 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3022 | int addrNotFound; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3023 | int nConstraint = pLoop->nTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3024 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3025 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3026 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3027 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3028 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 3029 | int iTarget = iReg+j+2; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3030 | pTerm = pLoop->aTerm[j]; |
| 3031 | if( pTerm->eOperator & WO_IN ){ |
| 3032 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 3033 | addrNotFound = pLevel->addrNxt; |
| 3034 | }else{ |
| 3035 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 3036 | } |
| 3037 | } |
| 3038 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 3039 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3040 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 3041 | pLoop->u.vtab.idxStr, |
| 3042 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 3043 | pLoop->u.vtab.needFree = 0; |
| 3044 | for(j=0; j<nConstraint && j<16; j++){ |
| 3045 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
| 3046 | disableTerm(pLevel, pLoop->aTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | pLevel->op = OP_VNext; |
| 3050 | pLevel->p1 = iCur; |
| 3051 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3052 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3053 | sqlite3ExprCachePop(pParse, 1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3054 | }else |
| 3055 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3056 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3057 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3058 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 3059 | ){ |
| 3060 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3061 | ** equality comparison against the ROWID field. Or |
| 3062 | ** we reference multiple rows using a "rowid IN (...)" |
| 3063 | ** construct. |
| 3064 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3065 | assert( pLoop->u.btree.nEq==1 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3066 | iReleaseReg = sqlite3GetTempReg(pParse); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3067 | pTerm = pLoop->aTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3068 | assert( pTerm!=0 ); |
| 3069 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3070 | assert( omitTable==0 ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3071 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3072 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3073 | addrNxt = pLevel->addrNxt; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3074 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 3075 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 3076 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3077 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3078 | VdbeComment((v, "pk")); |
| 3079 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3080 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3081 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 3082 | ){ |
| 3083 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3084 | */ |
| 3085 | int testOp = OP_Noop; |
| 3086 | int start; |
| 3087 | int memEndValue = 0; |
| 3088 | WhereTerm *pStart, *pEnd; |
| 3089 | |
| 3090 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3091 | j = 0; |
| 3092 | pStart = pEnd = 0; |
| 3093 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aTerm[j++]; |
| 3094 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aTerm[j++]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3095 | if( bRev ){ |
| 3096 | pTerm = pStart; |
| 3097 | pStart = pEnd; |
| 3098 | pEnd = pTerm; |
| 3099 | } |
| 3100 | if( pStart ){ |
| 3101 | Expr *pX; /* The expression that defines the start bound */ |
| 3102 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 3103 | |
| 3104 | /* The following constant maps TK_xx codes into corresponding |
| 3105 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 3106 | */ |
| 3107 | const u8 aMoveOp[] = { |
| 3108 | /* TK_GT */ OP_SeekGt, |
| 3109 | /* TK_LE */ OP_SeekLe, |
| 3110 | /* TK_LT */ OP_SeekLt, |
| 3111 | /* TK_GE */ OP_SeekGe |
| 3112 | }; |
| 3113 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 3114 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 3115 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 3116 | |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3117 | testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3118 | pX = pStart->pExpr; |
| 3119 | assert( pX!=0 ); |
| 3120 | assert( pStart->leftCursor==iCur ); |
| 3121 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 3122 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| 3123 | VdbeComment((v, "pk")); |
| 3124 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 3125 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 3126 | disableTerm(pLevel, pStart); |
| 3127 | }else{ |
| 3128 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 3129 | } |
| 3130 | if( pEnd ){ |
| 3131 | Expr *pX; |
| 3132 | pX = pEnd->pExpr; |
| 3133 | assert( pX!=0 ); |
| 3134 | assert( pEnd->leftCursor==iCur ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3135 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3136 | memEndValue = ++pParse->nMem; |
| 3137 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 3138 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 3139 | testOp = bRev ? OP_Le : OP_Ge; |
| 3140 | }else{ |
| 3141 | testOp = bRev ? OP_Lt : OP_Gt; |
| 3142 | } |
| 3143 | disableTerm(pLevel, pEnd); |
| 3144 | } |
| 3145 | start = sqlite3VdbeCurrentAddr(v); |
| 3146 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 3147 | pLevel->p1 = iCur; |
| 3148 | pLevel->p2 = start; |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 3149 | if( pStart==0 && pEnd==0 ){ |
| 3150 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3151 | }else{ |
| 3152 | assert( pLevel->p5==0 ); |
| 3153 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3154 | if( testOp!=OP_Noop ){ |
| 3155 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3156 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3157 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3158 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 3159 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3160 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3161 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3162 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3163 | ** |
| 3164 | ** The WHERE clause may contain zero or more equality |
| 3165 | ** terms ("==" or "IN" operators) that refer to the N |
| 3166 | ** left-most columns of the index. It may also contain |
| 3167 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3168 | ** column that immediately follows the N equalities. Only |
| 3169 | ** the right-most column can be an inequality - the rest must |
| 3170 | ** use the "==" and "IN" operators. For example, if the |
| 3171 | ** index is on (x,y,z), then the following clauses are all |
| 3172 | ** optimized: |
| 3173 | ** |
| 3174 | ** x=5 |
| 3175 | ** x=5 AND y=10 |
| 3176 | ** x=5 AND y<10 |
| 3177 | ** x=5 AND y>5 AND y<10 |
| 3178 | ** x=5 AND y=5 AND z<=10 |
| 3179 | ** |
| 3180 | ** The z<10 term of the following cannot be used, only |
| 3181 | ** the x=5 term: |
| 3182 | ** |
| 3183 | ** x=5 AND z<10 |
| 3184 | ** |
| 3185 | ** N may be zero if there are inequality constraints. |
| 3186 | ** If there are no inequality constraints, then N is at |
| 3187 | ** least one. |
| 3188 | ** |
| 3189 | ** This case is also used when there are no WHERE clause |
| 3190 | ** constraints but an index is selected anyway, in order |
| 3191 | ** to force the output order to conform to an ORDER BY. |
| 3192 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3193 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3194 | 0, |
| 3195 | 0, |
| 3196 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3197 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 3198 | OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3199 | OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ |
| 3200 | OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ |
| 3201 | OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ |
| 3202 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3203 | static const u8 aEndOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3204 | OP_Noop, /* 0: (!end_constraints) */ |
| 3205 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 3206 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 3207 | }; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3208 | int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 3209 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3210 | int regBase; /* Base register holding constraint values */ |
| 3211 | int r1; /* Temp register */ |
| 3212 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3213 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3214 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3215 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3216 | int start_constraints; /* Start of range is constrained */ |
| 3217 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3218 | Index *pIdx; /* The index we will be using */ |
| 3219 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3220 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3221 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3222 | char *zStartAff; /* Affinity for start of range constraint */ |
| 3223 | char *zEndAff; /* Affinity for end of range constraint */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3224 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3225 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3226 | iIdxCur = pLevel->iIdxCur; |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3227 | k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3228 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3229 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3230 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3231 | ** query, then the caller will only allow the loop to run for |
| 3232 | ** a single iteration. This means that the first row returned |
| 3233 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3234 | ** the first one after the nEq equality constraints in the index, |
| 3235 | ** this requires some special handling. |
| 3236 | */ |
| 3237 | if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3238 | && (pWInfo->nOBSat>0) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3239 | && (pIdx->nColumn>nEq) |
| 3240 | ){ |
| 3241 | /* assert( pOrderBy->nExpr==1 ); */ |
| 3242 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 3243 | isMinQuery = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3244 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | /* Find any inequality constraint terms for the start and end |
| 3248 | ** of the range. |
| 3249 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3250 | j = nEq; |
| 3251 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
| 3252 | pRangeStart = pLoop->aTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3253 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3254 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3255 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
| 3256 | pRangeEnd = pLoop->aTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3257 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3260 | /* Generate code to evaluate all constraint terms using == or IN |
| 3261 | ** and store the values of those terms in an array of registers |
| 3262 | ** starting at regBase. |
| 3263 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3264 | regBase = codeAllEqualityTerms( |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3265 | pParse, pLevel, pWC, notReady, bRev, nExtraReg, &zStartAff |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3266 | ); |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3267 | zEndAff = sqlite3DbStrDup(pParse->db, zStartAff); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3268 | addrNxt = pLevel->addrNxt; |
| 3269 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3270 | /* If we are doing a reverse order scan on an ascending index, or |
| 3271 | ** a forward order scan on a descending index, interchange the |
| 3272 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3273 | */ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3274 | if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3275 | || (bRev && pIdx->nColumn==nEq) |
| 3276 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3277 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 3278 | } |
| 3279 | |
| 3280 | testcase( pRangeStart && pRangeStart->eOperator & WO_LE ); |
| 3281 | testcase( pRangeStart && pRangeStart->eOperator & WO_GE ); |
| 3282 | testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE ); |
| 3283 | testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE ); |
| 3284 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3285 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3286 | start_constraints = pRangeStart || nEq>0; |
| 3287 | |
| 3288 | /* Seek the index cursor to the start of the range. */ |
| 3289 | nConstraint = nEq; |
| 3290 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3291 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3292 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3293 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){ |
| 3294 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3295 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3296 | if( zStartAff ){ |
| 3297 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3298 | /* Since the comparison is to be performed with no conversions |
| 3299 | ** applied to the operands, set the affinity to apply to pRight to |
| 3300 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3301 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3302 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3303 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3304 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3305 | } |
| 3306 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3307 | nConstraint++; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3308 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3309 | }else if( isMinQuery ){ |
| 3310 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3311 | nConstraint++; |
| 3312 | startEq = 0; |
| 3313 | start_constraints = 1; |
| 3314 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3315 | codeApplyAffinity(pParse, regBase, nConstraint, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3316 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3317 | assert( op!=0 ); |
| 3318 | testcase( op==OP_Rewind ); |
| 3319 | testcase( op==OP_Last ); |
| 3320 | testcase( op==OP_SeekGt ); |
| 3321 | testcase( op==OP_SeekGe ); |
| 3322 | testcase( op==OP_SeekLe ); |
| 3323 | testcase( op==OP_SeekLt ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3324 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3325 | |
| 3326 | /* Load the value for the inequality constraint at the end of the |
| 3327 | ** range (if any). |
| 3328 | */ |
| 3329 | nConstraint = nEq; |
| 3330 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3331 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3332 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3333 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3334 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ |
| 3335 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3336 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3337 | if( zEndAff ){ |
| 3338 | if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3339 | /* Since the comparison is to be performed with no conversions |
| 3340 | ** applied to the operands, set the affinity to apply to pRight to |
| 3341 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3342 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3343 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3344 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ |
| 3345 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3346 | } |
| 3347 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3348 | codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3349 | nConstraint++; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3350 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3351 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3352 | sqlite3DbFree(pParse->db, zStartAff); |
| 3353 | sqlite3DbFree(pParse->db, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3354 | |
| 3355 | /* Top of the loop body */ |
| 3356 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3357 | |
| 3358 | /* Check if the index cursor is past the end of the range. */ |
| 3359 | op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)]; |
| 3360 | testcase( op==OP_Noop ); |
| 3361 | testcase( op==OP_IdxGE ); |
| 3362 | testcase( op==OP_IdxLT ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3363 | if( op!=OP_Noop ){ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3364 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3365 | sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0); |
| 3366 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3367 | |
| 3368 | /* If there are inequality constraints, check that the value |
| 3369 | ** of the table column that the inequality contrains is not NULL. |
| 3370 | ** If it is, jump to the next iteration of the loop. |
| 3371 | */ |
| 3372 | r1 = sqlite3GetTempReg(pParse); |
| 3373 | testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ); |
| 3374 | testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3375 | if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3376 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 3377 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 3378 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3379 | sqlite3ReleaseTempReg(pParse, r1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3380 | |
| 3381 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3382 | disableTerm(pLevel, pRangeStart); |
| 3383 | disableTerm(pLevel, pRangeEnd); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3384 | if( !omitTable ){ |
| 3385 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3386 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3387 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3388 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3389 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3390 | |
| 3391 | /* Record the instruction used to terminate the loop. Disable |
| 3392 | ** WHERE clause terms made redundant by the index range scan. |
| 3393 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3394 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3395 | pLevel->op = OP_Noop; |
| 3396 | }else if( bRev ){ |
| 3397 | pLevel->op = OP_Prev; |
| 3398 | }else{ |
| 3399 | pLevel->op = OP_Next; |
| 3400 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3401 | pLevel->p1 = iIdxCur; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3402 | if( (pLoop->wsFlags & (WHERE_COLUMN_EQ | WHERE_COLUMN_RANGE | |
| 3403 | WHERE_COLUMN_NULL | WHERE_COLUMN_IN))==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3404 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3405 | }else{ |
| 3406 | assert( pLevel->p5==0 ); |
| 3407 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3408 | }else |
| 3409 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3410 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3411 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3412 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3413 | ** |
| 3414 | ** Example: |
| 3415 | ** |
| 3416 | ** CREATE TABLE t1(a,b,c,d); |
| 3417 | ** CREATE INDEX i1 ON t1(a); |
| 3418 | ** CREATE INDEX i2 ON t1(b); |
| 3419 | ** CREATE INDEX i3 ON t1(c); |
| 3420 | ** |
| 3421 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3422 | ** |
| 3423 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3424 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3425 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3426 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3427 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3428 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3429 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3430 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3431 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3432 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3433 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3434 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3435 | ** Gosub 2 A |
| 3436 | ** sqlite3WhereEnd() |
| 3437 | ** |
| 3438 | ** Following the above, code to terminate the loop. Label A, the target |
| 3439 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3440 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3441 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3442 | ** Goto B # The loop is finished. |
| 3443 | ** |
| 3444 | ** A: <loop body> # Return data, whatever. |
| 3445 | ** |
| 3446 | ** Return 2 # Jump back to the Gosub |
| 3447 | ** |
| 3448 | ** B: <after the loop> |
| 3449 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3450 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3451 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3452 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3453 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3454 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3455 | |
| 3456 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3457 | int regRowset = 0; /* Register for RowSet object */ |
| 3458 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3459 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3460 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3461 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3462 | int ii; /* Loop counter */ |
| 3463 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3464 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3465 | pTerm = pLoop->aTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3466 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3467 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3468 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3469 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3470 | pLevel->op = OP_Return; |
| 3471 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3472 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3473 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3474 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3475 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3476 | */ |
| 3477 | if( pWInfo->nLevel>1 ){ |
| 3478 | int nNotReady; /* The number of notReady tables */ |
| 3479 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3480 | nNotReady = pWInfo->nLevel - iLevel - 1; |
| 3481 | pOrTab = sqlite3StackAllocRaw(pParse->db, |
| 3482 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3483 | if( pOrTab==0 ) return notReady; |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3484 | pOrTab->nAlloc = (i16)(nNotReady + 1); |
| 3485 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3486 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3487 | origSrc = pWInfo->pTabList->a; |
| 3488 | for(k=1; k<=nNotReady; k++){ |
| 3489 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3490 | } |
| 3491 | }else{ |
| 3492 | pOrTab = pWInfo->pTabList; |
| 3493 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3494 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3495 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 3496 | ** equivalent to an empty rowset. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3497 | ** |
| 3498 | ** Also initialize regReturn to contain the address of the instruction |
| 3499 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3500 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3501 | ** over the top of the loop into the body of it. In this case the |
| 3502 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3503 | ** fall through to the next instruction, just as an OP_Next does if |
| 3504 | ** called on an uninitialized cursor. |
| 3505 | */ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3506 | if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 3507 | regRowset = ++pParse->nMem; |
| 3508 | regRowid = ++pParse->nMem; |
| 3509 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3510 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3511 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3512 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3513 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3514 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3515 | ** That way, terms in y that are factored into the disjunction will |
| 3516 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3517 | ** |
| 3518 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3519 | ** the "interesting" terms of z - terms that did not originate in the |
| 3520 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3521 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3522 | ** |
| 3523 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3524 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3525 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3526 | */ |
| 3527 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3528 | int iTerm; |
| 3529 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3530 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3531 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3532 | if( pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_ORINFO) ) continue; |
| 3533 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3534 | pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 3535 | pAndExpr = sqlite3ExprAnd(pParse->db, pAndExpr, pExpr); |
| 3536 | } |
| 3537 | if( pAndExpr ){ |
| 3538 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3539 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3542 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3543 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3544 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3545 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3546 | Expr *pOrExpr = pOrTerm->pExpr; |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3547 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3548 | pAndExpr->pLeft = pOrExpr; |
| 3549 | pOrExpr = pAndExpr; |
| 3550 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3551 | /* Loop through table entries that match term pOrTerm. */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3552 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 3553 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3554 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3555 | assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3556 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3557 | WhereLoop *pSubLoop; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3558 | explainOneScan( |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3559 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3560 | ); |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3561 | if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 3562 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3563 | int r; |
| 3564 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3565 | regRowid, 0); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3566 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| 3567 | sqlite3VdbeCurrentAddr(v)+2, r, iSet); |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3568 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3569 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 3570 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3571 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 3572 | ** contained one or more AND term from a notReady table. The |
| 3573 | ** terms from the notReady table could not be tested and will |
| 3574 | ** need to be tested later. |
| 3575 | */ |
| 3576 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 3577 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3578 | /* If all of the OR-connected terms are optimized using the same |
| 3579 | ** index, and the index is opened using the same cursor number |
| 3580 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 3581 | ** be possible to use that index as a covering index. |
| 3582 | ** |
| 3583 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 3584 | ** uses an index, and this is either the first OR-connected term |
| 3585 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3586 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 3587 | ** pCov to NULL to indicate that no candidate covering index will |
| 3588 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3589 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3590 | pSubLoop = pSubWInfo->a[0].pWLoop; |
| 3591 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
| 3592 | && (pSubLoop->wsFlags & WHERE_TEMP_INDEX)==0 |
| 3593 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3594 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3595 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
| 3596 | pCov = pLoop->u.btree.pIndex; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3597 | }else{ |
| 3598 | pCov = 0; |
| 3599 | } |
| 3600 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3601 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 3602 | sqlite3WhereEnd(pSubWInfo); |
| 3603 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3604 | } |
| 3605 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 3606 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 3607 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3608 | if( pAndExpr ){ |
| 3609 | pAndExpr->pLeft = 0; |
| 3610 | sqlite3ExprDelete(pParse->db, pAndExpr); |
| 3611 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3612 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3613 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 3614 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 3615 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3616 | if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab); |
| 3617 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3618 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3619 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3620 | |
| 3621 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3622 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3623 | ** scan of the entire table. |
| 3624 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3625 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 3626 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 3627 | assert( bRev==0 || bRev==1 ); |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3628 | pLevel->op = aStep[bRev]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3629 | pLevel->p1 = iCur; |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3630 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3631 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3632 | } |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3633 | newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3634 | |
| 3635 | /* Insert code to test every subexpression that can be completely |
| 3636 | ** computed using the current set of tables. |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3637 | ** |
| 3638 | ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through |
| 3639 | ** the use of indices become tests that are evaluated against each row of |
| 3640 | ** the relevant input tables. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3641 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3642 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3643 | Expr *pE; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3644 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3645 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3646 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3647 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3648 | testcase( pWInfo->untestedTerms==0 |
| 3649 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 3650 | pWInfo->untestedTerms = 1; |
| 3651 | continue; |
| 3652 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3653 | pE = pTerm->pExpr; |
| 3654 | assert( pE!=0 ); |
| 3655 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 3656 | continue; |
| 3657 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3658 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3659 | pTerm->wtFlags |= TERM_CODED; |
| 3660 | } |
| 3661 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3662 | /* Insert code to test for implied constraints based on transitivity |
| 3663 | ** of the "==" operator. |
| 3664 | ** |
| 3665 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 3666 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 3667 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 3668 | ** the implied "t1.a=123" constraint. |
| 3669 | */ |
| 3670 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3671 | Expr *pE; |
| 3672 | WhereTerm *pAlt; |
| 3673 | Expr sEq; |
| 3674 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 3675 | if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue; |
| 3676 | if( pTerm->leftCursor!=iCur ) continue; |
| 3677 | pE = pTerm->pExpr; |
| 3678 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 3679 | assert( (pTerm->prereqRight & newNotReady)!=0 ); |
| 3680 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 3681 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 3682 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3683 | VdbeNoopComment((v, "begin transitive constraint")); |
| 3684 | sEq = *pAlt->pExpr; |
| 3685 | sEq.pLeft = pE->pLeft; |
| 3686 | sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL); |
| 3687 | } |
| 3688 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3689 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 3690 | ** at least one row of the right table has matched the left table. |
| 3691 | */ |
| 3692 | if( pLevel->iLeftJoin ){ |
| 3693 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 3694 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 3695 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3696 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3697 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3698 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3699 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3700 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3701 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 3702 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3703 | continue; |
| 3704 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3705 | assert( pTerm->pExpr ); |
| 3706 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 3707 | pTerm->wtFlags |= TERM_CODED; |
| 3708 | } |
| 3709 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3710 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3711 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3712 | return newNotReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3715 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3716 | /* |
| 3717 | ** Print a WhereLoop object for debugging purposes |
| 3718 | */ |
| 3719 | static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){ |
| 3720 | int nb = 2*((pTabList->nSrc+15)/16); |
| 3721 | struct SrcList_item *pItem = pTabList->a + p->iTab; |
| 3722 | Table *pTab = pItem->pTab; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3723 | sqlite3DebugPrintf("%c %2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 3724 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
| 3725 | sqlite3DebugPrintf(" %8s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3726 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3727 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 3728 | if( p->u.btree.pIndex ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3729 | const char *zName = p->u.btree.pIndex->zName; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3730 | if( zName==0 ) zName = "ipk"; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3731 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 3732 | int i = sqlite3Strlen30(zName) - 1; |
| 3733 | while( zName[i]!='_' ) i--; |
| 3734 | zName += i; |
| 3735 | } |
| 3736 | sqlite3DebugPrintf(".%-12s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3737 | }else{ |
| 3738 | sqlite3DebugPrintf("%16s",""); |
| 3739 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3740 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3741 | char *z; |
| 3742 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3743 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 3744 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3745 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3746 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3747 | } |
| 3748 | sqlite3DebugPrintf(" %-15s", z); |
| 3749 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3750 | } |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 3751 | sqlite3DebugPrintf(" fg %05x N %d", p->wsFlags, p->nTerm); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3752 | sqlite3DebugPrintf(" cost %.2g,%.2g,%.2g\n", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3753 | p->prereq, p->rSetup, p->rRun, p->nOut); |
| 3754 | } |
| 3755 | #endif |
| 3756 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3757 | /* |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3758 | ** Deallocate internal memory used by a WhereLoop object |
| 3759 | */ |
| 3760 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 3761 | sqlite3DbFree(db, p->aTerm); |
| 3762 | p->aTerm = 0; |
| 3763 | p->nTerm = 0; |
| 3764 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 3765 | if( p->u.vtab.needFree ) sqlite3_free(p->u.vtab.idxStr); |
| 3766 | p->u.vtab.needFree = 0; |
| 3767 | p->u.vtab.idxStr = 0; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 3768 | }else if( (p->wsFlags & WHERE_TEMP_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
| 3769 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 3770 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 3771 | p->u.btree.pIndex = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3772 | } |
| 3773 | } |
| 3774 | |
| 3775 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3776 | ** Delete a WhereLoop object |
| 3777 | */ |
| 3778 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3779 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3780 | sqlite3DbFree(db, p); |
| 3781 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 3782 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 3783 | /* |
| 3784 | ** Free a WhereInfo structure |
| 3785 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 3786 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 3787 | if( ALWAYS(pWInfo) ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3788 | whereClauseClear(pWInfo->pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3789 | while( pWInfo->pLoops ){ |
| 3790 | WhereLoop *p = pWInfo->pLoops; |
| 3791 | pWInfo->pLoops = p->pNextLoop; |
| 3792 | whereLoopDelete(db, p); |
| 3793 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3794 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 3795 | } |
| 3796 | } |
| 3797 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3798 | /* |
| 3799 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 3800 | ** |
| 3801 | ** An existing WhereLoop entry might be overwritten if the new template |
| 3802 | ** is better and has fewer dependencies. Or the template will be ignored |
| 3803 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 3804 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 3805 | ** added based on the template. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3806 | ** |
| 3807 | ** If pBuilder->pBest is not NULL then we only care about the very |
| 3808 | ** best template and that template should be stored in pBuilder->pBest. |
| 3809 | ** If pBuilder->pBest is NULL then a list of the best templates are stored |
| 3810 | ** in pBuilder->pWInfo->pLoops. |
| 3811 | ** |
| 3812 | ** When accumulating multiple loops (when pBuilder->pBest is NULL) we |
| 3813 | ** still might overwrite similar loops with the new template if the |
| 3814 | ** template is better. Loops may be overwritten if the following |
| 3815 | ** conditions are met: |
| 3816 | ** |
| 3817 | ** (1) They have the same iTab. |
| 3818 | ** (2) They have the same iSortIdx. |
| 3819 | ** (3) The template has same or fewer dependencies than the current loop |
| 3820 | ** (4) The template has the same or lower cost than the current loop |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 3821 | ** (5) The template uses more terms of the same index but has no additional |
| 3822 | ** dependencies |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3823 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3824 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3825 | WhereLoop **ppPrev, *p, *pNext = 0, *pToFree = 0; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3826 | WhereTerm **paTerm = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3827 | sqlite3 *db = pBuilder->db; |
| 3828 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 3829 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3830 | /* If pBuilder->pBest is defined, then only keep track of the single |
| 3831 | ** best WhereLoop. pBuilder->pBest->maskSelf==0 indicates that no |
| 3832 | ** prior WhereLoops have been evaluated and that the current pTemplate |
| 3833 | ** is therefore the first and hence the best and should be retained. |
| 3834 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3835 | if( (p = pBuilder->pBest)!=0 ){ |
| 3836 | if( p->maskSelf!=0 ){ |
| 3837 | if( p->rRun+p->rSetup < pTemplate->rRun+pTemplate->rSetup ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3838 | goto whereLoopInsert_noop; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3839 | } |
| 3840 | if( p->rRun+p->rSetup == pTemplate->rRun+pTemplate->rSetup |
| 3841 | && p->prereq <= pTemplate->prereq ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3842 | goto whereLoopInsert_noop; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3843 | } |
| 3844 | } |
| 3845 | *p = *pTemplate; |
| 3846 | p->aTerm = 0; |
| 3847 | p->u.vtab.needFree = 0; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3848 | #if WHERETRACE_ENABLED |
| 3849 | if( sqlite3WhereTrace & 0x8 ){ |
| 3850 | sqlite3DebugPrintf("ins-best: "); |
| 3851 | whereLoopPrint(pTemplate, pBuilder->pTabList); |
| 3852 | } |
| 3853 | #endif |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3854 | return SQLITE_OK; |
| 3855 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3856 | |
| 3857 | /* Search for an existing WhereLoop to overwrite, or which takes |
| 3858 | ** priority over pTemplate. |
| 3859 | */ |
| 3860 | for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 3861 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ) continue; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3862 | if( p->nTerm<pTemplate->nTerm |
| 3863 | && (p->wsFlags & WHERE_INDEXED)!=0 |
| 3864 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 3865 | && p->u.btree.pIndex==pTemplate->u.btree.pIndex |
| 3866 | && p->prereq==pTemplate->prereq |
| 3867 | ){ |
| 3868 | /* Overwrite an existing WhereLoop with an similar one that uses |
| 3869 | ** more terms of the index */ |
| 3870 | pNext = p->pNextLoop; |
| 3871 | whereLoopClear(db, p); |
| 3872 | break; |
| 3873 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3874 | if( (p->prereq & pTemplate->prereq)==p->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3875 | && p->rSetup<=pTemplate->rSetup |
| 3876 | && p->rRun<=pTemplate->rRun |
| 3877 | ){ |
| 3878 | /* Already holding an equal or better WhereLoop. |
| 3879 | ** Return without changing or adding anything */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3880 | goto whereLoopInsert_noop; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3881 | } |
| 3882 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3883 | && p->rSetup>=pTemplate->rSetup |
| 3884 | && p->rRun>=pTemplate->rRun |
| 3885 | ){ |
| 3886 | /* Overwrite an existing WhereLoop with a better one */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3887 | pNext = p->pNextLoop; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3888 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3889 | break; |
| 3890 | } |
| 3891 | } |
| 3892 | |
| 3893 | /* If we reach this point it means that either p[] should be overwritten |
| 3894 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 3895 | ** WhereLoop and insert it. |
| 3896 | */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3897 | #if WHERETRACE_ENABLED |
| 3898 | if( sqlite3WhereTrace & 0x8 ){ |
| 3899 | if( p!=0 ){ |
| 3900 | sqlite3DebugPrintf("ins-del: "); |
| 3901 | whereLoopPrint(p, pBuilder->pTabList); |
| 3902 | } |
| 3903 | sqlite3DebugPrintf("ins-new: "); |
| 3904 | whereLoopPrint(pTemplate, pBuilder->pTabList); |
| 3905 | } |
| 3906 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3907 | if( p==0 ){ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3908 | p = pToFree = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3909 | if( p==0 ) return SQLITE_NOMEM; |
| 3910 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3911 | if( pTemplate->nTerm ){ |
| 3912 | paTerm = sqlite3DbMallocRaw(db, pTemplate->nTerm*sizeof(p->aTerm[0])); |
| 3913 | if( paTerm==0 ){ |
| 3914 | sqlite3DbFree(db, pToFree); |
| 3915 | return SQLITE_NOMEM; |
| 3916 | } |
| 3917 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3918 | *p = *pTemplate; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3919 | p->pNextLoop = pNext; |
| 3920 | *ppPrev = p; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3921 | p->aTerm = paTerm; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3922 | if( p->nTerm ){ |
| 3923 | memcpy(p->aTerm, pTemplate->aTerm, p->nTerm*sizeof(p->aTerm[0])); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3924 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3925 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3926 | Index *pIndex = p->u.btree.pIndex; |
| 3927 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 3928 | p->u.btree.pIndex = 0; |
| 3929 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3930 | }else{ |
| 3931 | pTemplate->u.vtab.needFree = 0; |
| 3932 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3933 | return SQLITE_OK; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3934 | |
| 3935 | /* Jump here if the insert is a no-op */ |
| 3936 | whereLoopInsert_noop: |
| 3937 | #if WHERETRACE_ENABLED |
| 3938 | if( sqlite3WhereTrace & 0x8 ){ |
| 3939 | sqlite3DebugPrintf("ins-noop: "); |
| 3940 | whereLoopPrint(pTemplate, pBuilder->pTabList); |
| 3941 | } |
| 3942 | #endif |
| 3943 | return SQLITE_OK; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | /* |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3947 | ** 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] | 3948 | ** Try to match one more. |
| 3949 | ** |
| 3950 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 3951 | ** INTEGER PRIMARY KEY. |
| 3952 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3953 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3954 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 3955 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 3956 | Index *pProbe, /* An index on pSrc */ |
| 3957 | int nInMul /* Number of iterations due to IN */ |
| 3958 | ){ |
| 3959 | sqlite3 *db; /* Database connection malloc context */ |
| 3960 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 3961 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3962 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3963 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3964 | WhereLoop savedLoop; /* Saved original content of pNew[] */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3965 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3966 | int rc = SQLITE_OK; /* Return code */ |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 3967 | tRowcnt iRowEst; /* Estimated index selectivity */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3968 | double rLogSize; /* Logarithm of table size */ |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 3969 | WhereTerm *pTop, *pBtm; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3970 | |
| 3971 | db = pBuilder->db; |
| 3972 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3973 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3974 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3975 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 3976 | assert( pNew->u.btree.nEq<=pProbe->nColumn ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3977 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 3978 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 3979 | opMask = WO_LT|WO_LE; |
| 3980 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 3981 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3982 | }else{ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 3983 | 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] | 3984 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3985 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 3986 | |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 3987 | if( pNew->u.btree.nEq < pProbe->nColumn ){ |
| 3988 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 3989 | iRowEst = pProbe->aiRowEst[pNew->u.btree.nEq+1]; |
| 3990 | }else{ |
| 3991 | iCol = -1; |
| 3992 | iRowEst = 1; |
| 3993 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3994 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 3995 | opMask, pProbe); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3996 | savedLoop = *pNew; |
| 3997 | pNew->rSetup = (double)0; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 3998 | rLogSize = estLog(pProbe->aiRowEst[0]); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3999 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4000 | int nIn = 1; |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4001 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 4002 | pNew->wsFlags = savedLoop.wsFlags; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4003 | pNew->u.btree.nEq = savedLoop.u.btree.nEq; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4004 | pNew->nTerm = savedLoop.nTerm; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4005 | if( pNew->nTerm>=pBuilder->mxTerm ) break; /* Repeated column in index */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4006 | pNew->aTerm[pNew->nTerm++] = pTerm; |
| 4007 | pNew->prereq = (savedLoop.prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4008 | pNew->rRun = rLogSize; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4009 | if( pTerm->eOperator & WO_IN ){ |
| 4010 | Expr *pExpr = pTerm->pExpr; |
| 4011 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4012 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 4013 | /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */ |
| 4014 | nIn = 25; |
| 4015 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4016 | /* "x IN (value, value, ...)" */ |
| 4017 | nIn = pExpr->x.pList->nExpr; |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4018 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4019 | pNew->rRun *= nIn; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4020 | pNew->u.btree.nEq++; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4021 | pNew->nOut = (double)iRowEst * nInMul * nIn; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4022 | }else if( pTerm->eOperator & (WO_EQ) ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4023 | assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 |
| 4024 | || nInMul==1 ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4025 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4026 | if( iCol<0 |
drh | ee73b87 | 2013-06-04 13:37:26 +0000 | [diff] [blame^] | 4027 | || (pProbe->onError!=OE_None && nInMul==1 |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4028 | && pNew->u.btree.nEq==pProbe->nColumn-1) |
| 4029 | ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4030 | testcase( pNew->wsFlags & WHERE_COLUMN_IN ); |
| 4031 | pNew->wsFlags |= WHERE_ONEROW; |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4032 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4033 | pNew->u.btree.nEq++; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4034 | pNew->nOut = (double)iRowEst * nInMul; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4035 | }else if( pTerm->eOperator & (WO_ISNULL) ){ |
| 4036 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 4037 | pNew->u.btree.nEq++; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4038 | nIn = 2; /* Assume IS NULL matches two rows */ |
| 4039 | pNew->nOut = (double)iRowEst * nInMul * nIn; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4040 | }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ |
| 4041 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4042 | pBtm = pTerm; |
| 4043 | pTop = 0; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4044 | }else if( pTerm->eOperator & (WO_LT|WO_LE) ){ |
| 4045 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4046 | pTop = pTerm; |
| 4047 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
| 4048 | pNew->aTerm[pNew->nTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4049 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4050 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4051 | /* Adjust nOut and rRun for STAT3 range values */ |
| 4052 | double rDiv; |
| 4053 | whereRangeScanEst(pBuilder->pParse, pProbe, pNew->u.btree.nEq, |
| 4054 | pBtm, pTop, &rDiv); |
| 4055 | pNew->nOut = savedLoop.nOut/rDiv; |
| 4056 | } |
| 4057 | #ifdef SQLITE_ENABLE_STAT3 |
| 4058 | if( pNew->u.btree.nEq==1 && pProbe->nSample ){ |
| 4059 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ |
| 4060 | rc = whereEqualScanEst(pBuilder->pParse, pProbe, pTerm->pExpr->pRight, |
| 4061 | &pNew->nOut); |
| 4062 | }else if( (pTerm->eOperator & WO_IN) |
| 4063 | && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){ |
| 4064 | rc = whereInScanEst(pBuilder->pParse, pProbe, pTerm->pExpr->x.pList, |
| 4065 | &pNew->nOut); |
| 4066 | |
| 4067 | } |
| 4068 | } |
| 4069 | #endif |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4070 | if( pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK) ){ |
| 4071 | pNew->rRun += pNew->nOut; /* Unit step cost to reach each row */ |
| 4072 | }else{ |
| 4073 | /* Each row involves a step of the index, then a binary search of |
| 4074 | ** the main table */ |
| 4075 | pNew->rRun += pNew->nOut*(1 + rLogSize); |
| 4076 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4077 | /* TBD: Adjust nOut for additional constraints */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4078 | rc = whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4079 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4080 | && pNew->u.btree.nEq<=pProbe->nColumn |
| 4081 | && pProbe->zName!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4082 | ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4083 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul*nIn); |
| 4084 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4085 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4086 | *pNew = savedLoop; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4087 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4091 | ** Return True if it is possible that pIndex might be useful in |
| 4092 | ** implementing the ORDER BY clause in pBuilder. |
| 4093 | ** |
| 4094 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 4095 | ** if there is no way for pIndex to be useful in implementing that |
| 4096 | ** ORDER BY clause. |
| 4097 | */ |
| 4098 | static int indexMightHelpWithOrderBy( |
| 4099 | WhereLoopBuilder *pBuilder, |
| 4100 | Index *pIndex, |
| 4101 | int iCursor |
| 4102 | ){ |
| 4103 | ExprList *pOB; |
| 4104 | int iCol; |
| 4105 | int ii; |
| 4106 | |
| 4107 | if( (pOB = pBuilder->pOrderBy)==0 ) return 0; |
| 4108 | iCol = pIndex->aiColumn[0]; |
| 4109 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 4110 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4111 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 4112 | if( pExpr->iTable==iCursor ){ |
| 4113 | if( pExpr->iColumn==iCol ) return 1; |
| 4114 | return 0; |
| 4115 | } |
| 4116 | } |
| 4117 | return 0; |
| 4118 | } |
| 4119 | |
| 4120 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4121 | ** Add all WhereLoop objects a single table of the join were the table |
| 4122 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 4123 | ** a b-tree table, not a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4124 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4125 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4126 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4127 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4128 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4129 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4130 | Index sPk; /* A fake index object for the primary key */ |
| 4131 | tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 4132 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 4133 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4134 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4135 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4136 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4137 | int b; /* A boolean value */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4138 | double rSize; /* number of rows in the table */ |
| 4139 | double rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4140 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4141 | pNew = pBuilder->pNew; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4142 | pSrc = pBuilder->pTabList->a + pNew->iTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4143 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4144 | |
| 4145 | if( pSrc->pIndex ){ |
| 4146 | /* An INDEXED BY clause specifies a particular index to use */ |
| 4147 | pProbe = pSrc->pIndex; |
| 4148 | }else{ |
| 4149 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 4150 | ** variable sPk to represent the rowid primary key index. Make this |
| 4151 | ** fake index the first in a chain of Index objects with all of the real |
| 4152 | ** indices to follow */ |
| 4153 | Index *pFirst; /* First of real indices on the table */ |
| 4154 | memset(&sPk, 0, sizeof(Index)); |
| 4155 | sPk.nColumn = 1; |
| 4156 | sPk.aiColumn = &aiColumnPk; |
| 4157 | sPk.aiRowEst = aiRowEstPk; |
| 4158 | sPk.onError = OE_Replace; |
| 4159 | sPk.pTable = pSrc->pTab; |
| 4160 | aiRowEstPk[0] = pSrc->pTab->nRowEst; |
| 4161 | aiRowEstPk[1] = 1; |
| 4162 | pFirst = pSrc->pTab->pIndex; |
| 4163 | if( pSrc->notIndexed==0 ){ |
| 4164 | /* The real indices of the table are only considered if the |
| 4165 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 4166 | sPk.pNext = pFirst; |
| 4167 | } |
| 4168 | pProbe = &sPk; |
| 4169 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4170 | rSize = (double)pSrc->pTab->nRowEst; |
| 4171 | rLogSize = estLog(rSize); |
| 4172 | |
| 4173 | /* Automatic indexes */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4174 | if( !pBuilder->pBest |
| 4175 | && (pBuilder->pParse->db->flags & SQLITE_AutoIndex)!=0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4176 | && !pSrc->viaCoroutine |
| 4177 | && !pSrc->notIndexed |
| 4178 | && !pSrc->isCorrelated |
| 4179 | ){ |
| 4180 | /* Generate auto-index WhereLoops */ |
| 4181 | WhereClause *pWC = pBuilder->pWC; |
| 4182 | WhereTerm *pTerm; |
| 4183 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 4184 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4185 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4186 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 4187 | pNew->u.btree.nEq = 1; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4188 | pNew->u.btree.pIndex = 0; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4189 | pNew->nTerm = 1; |
| 4190 | pNew->aTerm[0] = pTerm; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4191 | pNew->rSetup = 20*rLogSize*pSrc->pTab->nRowEst; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4192 | pNew->nOut = (double)10; |
| 4193 | pNew->rRun = rLogSize + pNew->nOut; |
| 4194 | pNew->wsFlags = WHERE_TEMP_INDEX; |
| 4195 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4196 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4197 | } |
| 4198 | } |
| 4199 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4200 | |
| 4201 | /* Loop over all indices |
| 4202 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4203 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4204 | pNew->u.btree.nEq = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4205 | pNew->nTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4206 | pNew->iSortIdx = 0; |
| 4207 | pNew->rSetup = (double)0; |
| 4208 | pNew->prereq = mExtra; |
| 4209 | pNew->u.btree.pIndex = pProbe; |
| 4210 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4211 | if( pProbe->tnum<=0 ){ |
| 4212 | /* Integer primary key index */ |
| 4213 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4214 | |
| 4215 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4216 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4217 | pNew->nOut = rSize; |
| 4218 | pNew->rRun = (rSize + rLogSize)*(3+b); /* 4x penalty for a full-scan */ |
| 4219 | rc = whereLoopInsert(pBuilder, pNew); |
| 4220 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4221 | }else{ |
| 4222 | Bitmask m = pSrc->colUsed; |
| 4223 | int j; |
| 4224 | for(j=pProbe->nColumn-1; j>=0; j--){ |
| 4225 | int x = pProbe->aiColumn[j]; |
| 4226 | if( x<BMS-1 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4227 | m &= ~MASKBIT(x); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4228 | } |
| 4229 | } |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4230 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4231 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4232 | /* Full scan via index */ |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4233 | if( (m==0 || b) |
| 4234 | && pProbe->bUnordered==0 |
| 4235 | && (pBuilder->pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 4236 | && sqlite3GlobalConfig.bUseCis |
| 4237 | && OptimizationEnabled(pBuilder->pParse->db, SQLITE_CoverIdxScan) |
| 4238 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4239 | pNew->iSortIdx = b ? iSortIdx : 0; |
| 4240 | pNew->nOut = rSize; |
| 4241 | pNew->rRun = (m==0) ? (rSize + rLogSize)*(1+b) : (rSize*rLogSize); |
| 4242 | rc = whereLoopInsert(pBuilder, pNew); |
| 4243 | if( rc ) break; |
| 4244 | } |
| 4245 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4246 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 1); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4247 | |
| 4248 | /* If there was an INDEXED BY clause, then only that one index is |
| 4249 | ** considered. */ |
| 4250 | if( pSrc->pIndex ) break; |
| 4251 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4252 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4256 | ** Add all WhereLoop objects for a table of the join identified by |
| 4257 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4258 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4259 | static int whereLoopAddVirtual( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4260 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4261 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4262 | ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4263 | Parse *pParse; /* The parsing context */ |
| 4264 | WhereClause *pWC; /* The WHERE clause */ |
| 4265 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 4266 | Table *pTab; |
| 4267 | sqlite3 *db; |
| 4268 | sqlite3_index_info *pIdxInfo; |
| 4269 | struct sqlite3_index_constraint *pIdxCons; |
| 4270 | struct sqlite3_index_constraint_usage *pUsage; |
| 4271 | WhereTerm *pTerm; |
| 4272 | int i, j; |
| 4273 | int iTerm, mxTerm; |
| 4274 | int seenIn = 0; /* True if an IN operator is seen */ |
| 4275 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 4276 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 4277 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4278 | int rc = SQLITE_OK; |
| 4279 | |
| 4280 | pParse = pBuilder->pParse; |
| 4281 | db = pParse->db; |
| 4282 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4283 | pNew = pBuilder->pNew; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4284 | pSrc = &pBuilder->pTabList->a[pNew->iTab]; |
| 4285 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4286 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4287 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4288 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4289 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4290 | pNew->rSetup = 0; |
| 4291 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
| 4292 | pNew->nTerm = 0; |
| 4293 | pNew->u.vtab.needFree = 0; |
| 4294 | pUsage = pIdxInfo->aConstraintUsage; |
| 4295 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4296 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4297 | if( !seenIn && (iPhase&1)!=0 ){ |
| 4298 | iPhase++; |
| 4299 | if( iPhase>3 ) break; |
| 4300 | } |
| 4301 | if( !seenVar && iPhase>1 ) break; |
| 4302 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4303 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4304 | j = pIdxCons->iTermOffset; |
| 4305 | pTerm = &pWC->a[j]; |
| 4306 | switch( iPhase ){ |
| 4307 | case 0: /* Constants without IN operator */ |
| 4308 | pIdxCons->usable = 0; |
| 4309 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4310 | seenIn = 1; |
| 4311 | }else if( pTerm->prereqRight!=0 ){ |
| 4312 | seenVar = 1; |
| 4313 | }else{ |
| 4314 | pIdxCons->usable = 1; |
| 4315 | } |
| 4316 | break; |
| 4317 | case 1: /* Constants with IN operators */ |
| 4318 | assert( seenIn ); |
| 4319 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 4320 | break; |
| 4321 | case 2: /* Variables without IN */ |
| 4322 | assert( seenVar ); |
| 4323 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 4324 | break; |
| 4325 | default: /* Variables with IN */ |
| 4326 | assert( seenVar && seenIn ); |
| 4327 | pIdxCons->usable = 1; |
| 4328 | break; |
| 4329 | } |
| 4330 | } |
| 4331 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 4332 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4333 | pIdxInfo->idxStr = 0; |
| 4334 | pIdxInfo->idxNum = 0; |
| 4335 | pIdxInfo->needToFreeIdxStr = 0; |
| 4336 | pIdxInfo->orderByConsumed = 0; |
| 4337 | /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 4338 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2); |
| 4339 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 4340 | if( rc ) goto whereLoopAddVtab_exit; |
| 4341 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4342 | pNew->prereq = 0; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4343 | mxTerm = -1; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4344 | for(i=0; i<pBuilder->mxTerm; i++) pNew->aTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4345 | pNew->u.vtab.omitMask = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4346 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4347 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4348 | if( iTerm>=pBuilder->mxTerm ) break; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4349 | j = pIdxCons->iTermOffset; |
| 4350 | if( iTerm>=pIdxInfo->nConstraint |
| 4351 | || j<0 |
| 4352 | || j>=pWC->nTerm |
| 4353 | || pNew->aTerm[iTerm]!=0 |
| 4354 | ){ |
| 4355 | rc = SQLITE_ERROR; |
| 4356 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4357 | goto whereLoopAddVtab_exit; |
| 4358 | } |
| 4359 | pTerm = &pWC->a[j]; |
| 4360 | pNew->prereq |= pTerm->prereqRight; |
| 4361 | pNew->aTerm[iTerm] = pTerm; |
| 4362 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4363 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4364 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4365 | if( pUsage[i].omit==0 ){ |
| 4366 | /* Do not attempt to use an IN constraint if the virtual table |
| 4367 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4368 | ** If we do attempt to use such a constraint, some rows might be |
| 4369 | ** repeated in the output. */ |
| 4370 | break; |
| 4371 | } |
| 4372 | /* A virtual table that is constrained by an IN clause may not |
| 4373 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4374 | ** is not necessarily related to the order of output terms and |
| 4375 | ** (2) Multiple outputs from a single IN value will not merge |
| 4376 | ** together. */ |
| 4377 | pIdxInfo->orderByConsumed = 0; |
| 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | if( i>=pIdxInfo->nConstraint ){ |
| 4382 | pNew->nTerm = mxTerm+1; |
| 4383 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4384 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 4385 | pIdxInfo->needToFreeIdxStr = 0; |
| 4386 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 3b1d808 | 2013-06-03 16:56:37 +0000 | [diff] [blame] | 4387 | pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) |
| 4388 | && pIdxInfo->orderByConsumed); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4389 | pNew->rSetup = (double)0; |
| 4390 | pNew->rRun = pIdxInfo->estimatedCost; |
| 4391 | pNew->nOut = (double)25; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4392 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4393 | if( pNew->u.vtab.needFree ){ |
| 4394 | sqlite3_free(pNew->u.vtab.idxStr); |
| 4395 | pNew->u.vtab.needFree = 0; |
| 4396 | } |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | whereLoopAddVtab_exit: |
| 4401 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4402 | sqlite3DbFree(db, pIdxInfo); |
| 4403 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4407 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 4408 | ** btrees or virtual tables. |
| 4409 | */ |
| 4410 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
| 4411 | WhereClause *pWC; |
| 4412 | WhereLoop *pNew; |
| 4413 | WhereTerm *pTerm, *pWCEnd; |
| 4414 | int rc = SQLITE_OK; |
| 4415 | int iCur; |
| 4416 | WhereClause tempWC; |
| 4417 | WhereLoopBuilder sSubBuild; |
| 4418 | WhereLoop sBest; |
| 4419 | struct SrcList_item *pItem; |
| 4420 | |
| 4421 | |
| 4422 | pWC = pBuilder->pWC; |
| 4423 | if( pWC->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
| 4424 | pWCEnd = pWC->a + pWC->nTerm; |
| 4425 | pNew = pBuilder->pNew; |
| 4426 | pItem = pBuilder->pTabList->a + pNew->iTab; |
| 4427 | iCur = pItem->iCursor; |
| 4428 | sSubBuild = *pBuilder; |
| 4429 | sSubBuild.pOrderBy = 0; |
| 4430 | sSubBuild.pBest = &sBest; |
| 4431 | tempWC.pParse = pWC->pParse; |
| 4432 | tempWC.pMaskSet = pWC->pMaskSet; |
| 4433 | tempWC.pOuter = pWC; |
| 4434 | tempWC.op = TK_AND; |
| 4435 | tempWC.wctrlFlags = 0; |
| 4436 | tempWC.nTerm = 1; |
| 4437 | |
| 4438 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 4439 | if( (pTerm->eOperator & WO_OR)!=0 |
| 4440 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 4441 | ){ |
| 4442 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 4443 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 4444 | WhereTerm *pOrTerm; |
| 4445 | double rTotal = 0; |
| 4446 | double nRow = 0; |
| 4447 | Bitmask prereq = mExtra; |
| 4448 | |
| 4449 | |
| 4450 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
| 4451 | if( (pOrTerm->eOperator& WO_AND)!=0 ){ |
| 4452 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 4453 | }else if( pOrTerm->leftCursor==iCur ){ |
| 4454 | tempWC.a = pOrTerm; |
| 4455 | sSubBuild.pWC = &tempWC; |
| 4456 | }else{ |
| 4457 | continue; |
| 4458 | } |
| 4459 | sBest.maskSelf = 0; |
| 4460 | if( IsVirtual(pItem->pTab) ){ |
| 4461 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
| 4462 | }else{ |
| 4463 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 4464 | } |
| 4465 | if( sBest.maskSelf==0 ) break; |
| 4466 | assert( sBest.rSetup==(double)0 ); |
| 4467 | rTotal += sBest.rRun; |
| 4468 | nRow += sBest.nOut; |
| 4469 | prereq |= sBest.prereq; |
| 4470 | } |
| 4471 | pNew->nTerm = 1; |
| 4472 | pNew->aTerm[0] = pTerm; |
| 4473 | pNew->wsFlags = WHERE_MULTI_OR; |
| 4474 | pNew->rSetup = (double)0; |
| 4475 | pNew->rRun = rTotal; |
| 4476 | pNew->nOut = nRow; |
| 4477 | pNew->prereq = prereq; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4478 | memset(&pNew->u, 0, sizeof(pNew->u)); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4479 | rc = whereLoopInsert(pBuilder, pNew); |
| 4480 | } |
| 4481 | } |
| 4482 | return rc; |
| 4483 | } |
| 4484 | |
| 4485 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4486 | ** Add all WhereLoop objects for all tables |
| 4487 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4488 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4489 | Bitmask mExtra = 0; |
| 4490 | Bitmask mPrior = 0; |
| 4491 | int iTab; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4492 | SrcList *pTabList = pBuilder->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4493 | struct SrcList_item *pItem; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4494 | WhereClause *pWC = pBuilder->pWC; |
| 4495 | sqlite3 *db = pBuilder->db; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4496 | int nTabList = pBuilder->pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4497 | int rc = SQLITE_OK; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4498 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4499 | |
| 4500 | /* Loop over the tables in the join, from left to right */ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4501 | pBuilder->pNew = pNew = sqlite3DbMallocZero(db, sizeof(WhereLoop)); |
| 4502 | if( pNew==0 ) return SQLITE_NOMEM; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4503 | pBuilder->mxTerm = pWC->nTerm+1; |
| 4504 | while( pWC->pOuter ){ |
| 4505 | pWC = pWC->pOuter; |
| 4506 | pBuilder->mxTerm += pWC->nTerm; |
| 4507 | } |
| 4508 | pWC = pBuilder->pWC; |
| 4509 | pNew->aTerm = sqlite3DbMallocZero(db,pBuilder->mxTerm*sizeof(pNew->aTerm[0])); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4510 | if( pNew->aTerm==0 ){ |
| 4511 | rc = SQLITE_NOMEM; |
| 4512 | goto whereLoopAddAll_end; |
| 4513 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4514 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4515 | pNew->iTab = iTab; |
| 4516 | pNew->maskSelf = getMask(pWC->pMaskSet, pItem->iCursor); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4517 | if( (pItem->jointype & (JT_LEFT|JT_CROSS))!=0 ){ |
| 4518 | mExtra = mPrior; |
| 4519 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4520 | if( IsVirtual(pItem->pTab) ){ |
| 4521 | rc = whereLoopAddVirtual(pBuilder, mExtra); |
| 4522 | }else{ |
| 4523 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 4524 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4525 | if( rc==SQLITE_OK ){ |
| 4526 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 4527 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4528 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4529 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4530 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4531 | whereLoopAddAll_end: |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4532 | whereLoopDelete(db, pBuilder->pNew); |
| 4533 | pBuilder->pNew = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4534 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4537 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4538 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4539 | ** parameters) to see if it outputs rows in the requested ORDER BY |
| 4540 | ** (or GROUP BY) without requiring a separate source operation. Return: |
| 4541 | ** |
| 4542 | ** 0: ORDER BY is not satisfied. Sorting required |
| 4543 | ** 1: ORDER BY is satisfied. Omit sorting |
| 4544 | ** -1: Unknown at this time |
| 4545 | ** |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4546 | */ |
| 4547 | static int wherePathSatisfiesOrderBy( |
| 4548 | WhereInfo *pWInfo, /* The WHERE clause */ |
| 4549 | WherePath *pPath, /* The WherePath to check */ |
| 4550 | int nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4551 | int isLastLoop, /* True if pLast is the inner-most loop */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4552 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
| 4553 | Bitmask *pRevMask /* Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4554 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4555 | u8 revSet; /* True if rev is known */ |
| 4556 | u8 rev; /* Composite sort order */ |
| 4557 | u8 revIdx; /* Index sort order */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4558 | u8 isWellOrdered; /* All WhereLoops are well-ordered so far */ |
| 4559 | u16 nColumn; /* Number of columns in pIndex */ |
| 4560 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 4561 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 4562 | int i, j; /* Loop counters */ |
| 4563 | int iCur; /* Cursor number for current WhereLoop */ |
| 4564 | int iColumn; /* A column number within table iCur */ |
| 4565 | WhereLoop *pLoop; /* Current WhereLoop being processed. */ |
| 4566 | ExprList *pOrderBy = pWInfo->pOrderBy; /* the ORDER BY clause */ |
| 4567 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 4568 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 4569 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 4570 | Index *pIndex; /* The index associated with pLoop */ |
| 4571 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 4572 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 4573 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
| 4574 | Bitmask orderedMask; /* Mask of all well-ordered loops */ |
| 4575 | WhereMaskSet *pMaskSet; /* WhereMaskSet object for this where clause */ |
| 4576 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4577 | |
| 4578 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4579 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 4580 | ** 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] | 4581 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 4582 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4583 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 4584 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4585 | ** |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4586 | ** We say the WhereLoop is "well-ordered" if |
| 4587 | ** (i) it satisfies at least one term of the ORDER BY clause, and |
| 4588 | ** (ii) every row output is distinct over the terms that match the |
| 4589 | ** ORDER BY clause. |
| 4590 | ** Every one-row WhereLoop is automatically well-ordered, even if it |
| 4591 | ** does not match any terms of the ORDER BY clause. |
| 4592 | ** For condition (ii), be mindful that a UNIQUE column can have multiple |
| 4593 | ** rows that are NULL and so it not necessarily distinct. The column |
| 4594 | ** must be UNIQUE and NOT NULL. in order to be well-ordered. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4595 | */ |
| 4596 | |
| 4597 | assert( pOrderBy!=0 ); |
| 4598 | |
| 4599 | /* Sortability of virtual tables is determined by the xBestIndex method |
| 4600 | ** of the virtual table itself */ |
| 4601 | if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4602 | testcase( nLoop>0 ); /* True when outer loops are one-row and match |
| 4603 | ** no ORDER BY terms */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4604 | return pLast->u.vtab.isOrdered; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4605 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4606 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4607 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4608 | nOrderBy = pOrderBy->nExpr; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4609 | if( nOrderBy>60 ) return 0; |
| 4610 | isWellOrdered = 1; |
| 4611 | obDone = MASKBIT(nOrderBy)-1; |
| 4612 | orderedMask = 0; |
| 4613 | pMaskSet = pWInfo->pWC->pMaskSet; |
| 4614 | for(iLoop=0; isWellOrdered && obSat<obDone && iLoop<=nLoop; iLoop++){ |
| 4615 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4616 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4617 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4618 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 4619 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 4620 | pIndex = 0; |
| 4621 | nColumn = 0; |
| 4622 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 4623 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4624 | }else{ |
| 4625 | nColumn = pIndex->nColumn; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 4626 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4627 | |
| 4628 | /* For every term of the index that is constrained by == or IS NULL |
| 4629 | ** mark off corresponding ORDER BY terms wherever they occur |
| 4630 | ** in the ORDER BY clause. |
| 4631 | */ |
| 4632 | for(i=0; i<pLoop->u.btree.nEq; i++){ |
| 4633 | pTerm = pLoop->aTerm[i]; |
| 4634 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))==0 ) continue; |
| 4635 | iColumn = pTerm->u.leftColumn; |
| 4636 | for(j=0; j<nOrderBy; j++){ |
| 4637 | if( MASKBIT(j) & obSat ) continue; |
| 4638 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[j].pExpr); |
| 4639 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 4640 | if( pOBExpr->iTable!=iCur ) continue; |
| 4641 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 4642 | if( iColumn>=0 ){ |
| 4643 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[j].pExpr); |
| 4644 | if( !pColl ) pColl = db->pDfltColl; |
| 4645 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[i])!=0 ) continue; |
| 4646 | } |
| 4647 | obSat |= MASKBIT(j); |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4648 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4649 | if( obSat==obDone ) return 1; |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4650 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4651 | |
| 4652 | /* Loop through all columns of the index and deal with the ones |
| 4653 | ** that are not constrained by == or IN. |
| 4654 | */ |
| 4655 | rev = revSet = 0; |
| 4656 | for(j=0; j<=nColumn; j++){ |
| 4657 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 4658 | |
| 4659 | if( j<pLoop->u.btree.nEq |
| 4660 | && (pLoop->aTerm[j]->eOperator & (WO_EQ|WO_ISNULL))!=0 |
| 4661 | ){ |
| 4662 | continue; /* Skip == and IS NULL terms already processed */ |
| 4663 | } |
| 4664 | |
| 4665 | /* Get the column number in the table and sort order for the |
| 4666 | ** j-th column of the index for this WhereLoop |
| 4667 | */ |
| 4668 | if( j<nColumn ){ |
| 4669 | /* Normal index columns */ |
| 4670 | iColumn = pIndex->aiColumn[j]; |
| 4671 | revIdx = pIndex->aSortOrder[j]; |
| 4672 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4673 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4674 | /* The ROWID column at the end */ |
| 4675 | iColumn = -1; |
| 4676 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4677 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4678 | |
| 4679 | /* An unconstrained column that might be NULL means that this |
| 4680 | ** WhereLoop is not well-ordered |
| 4681 | */ |
| 4682 | if( iColumn>=0 |
| 4683 | && j>=pLoop->u.btree.nEq |
| 4684 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 4685 | ){ |
| 4686 | isWellOrdered = 0; |
| 4687 | } |
| 4688 | |
| 4689 | /* Find the ORDER BY term that corresponds to the j-th column |
| 4690 | ** of the index and and mark that ORDER BY term off |
| 4691 | */ |
| 4692 | bOnce = 1; |
| 4693 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 4694 | if( MASKBIT(i) & obSat ) continue; |
| 4695 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 4696 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 4697 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ) bOnce = 0; |
| 4698 | if( pOBExpr->iTable!=iCur ) continue; |
| 4699 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 4700 | if( iColumn>=0 ){ |
| 4701 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 4702 | if( !pColl ) pColl = db->pDfltColl; |
| 4703 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 4704 | } |
| 4705 | bOnce = 1; |
| 4706 | break; |
| 4707 | } |
| 4708 | if( bOnce && i<nOrderBy ){ |
| 4709 | if( iColumn<0 ) isWellOrdered = 1; |
| 4710 | obSat |= MASKBIT(i); |
| 4711 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 4712 | /* If we have an ORDER BY clause, we must match the next available |
| 4713 | ** column of the ORDER BY */ |
| 4714 | if( revSet ){ |
| 4715 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; |
| 4716 | }else{ |
| 4717 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 4718 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 4719 | revSet = 1; |
| 4720 | } |
| 4721 | } |
| 4722 | }else{ |
| 4723 | /* No match found */ |
drh | ee73b87 | 2013-06-04 13:37:26 +0000 | [diff] [blame^] | 4724 | if( j<nColumn || pIndex==0 || pIndex->onError==OE_None ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4725 | isWellOrdered = 0; |
| 4726 | } |
| 4727 | break; |
| 4728 | } |
| 4729 | } /* end Loop over all index columns */ |
| 4730 | } /* end-if not one-row */ |
| 4731 | |
| 4732 | /* Mark off any other ORDER BY terms that reference pLoop */ |
| 4733 | if( isWellOrdered ){ |
| 4734 | orderedMask |= pLoop->maskSelf; |
| 4735 | for(i=0; i<nOrderBy; i++){ |
| 4736 | Expr *p; |
| 4737 | if( MASKBIT(i) & obSat ) continue; |
| 4738 | p = pOrderBy->a[i].pExpr; |
| 4739 | if( (exprTableUsage(pMaskSet, p)&~orderedMask)==0 ){ |
| 4740 | obSat |= MASKBIT(i); |
| 4741 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 4742 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4743 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4744 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4745 | if( obSat==obDone ) return 1; |
| 4746 | if( !isWellOrdered ) return 0; |
| 4747 | if( isLastLoop ) return 1; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4748 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4751 | #ifdef WHERETRACE_ENABLED |
| 4752 | /* For debugging use only: */ |
| 4753 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 4754 | static char zName[65]; |
| 4755 | int i; |
| 4756 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 4757 | if( pLast ) zName[i++] = pLast->cId; |
| 4758 | zName[i] = 0; |
| 4759 | return zName; |
| 4760 | } |
| 4761 | #endif |
| 4762 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4763 | |
| 4764 | /* |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4765 | ** Given the list of WhereLoop objects on pWInfo->pLoops, this routine |
| 4766 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 4767 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 4768 | ** |
| 4769 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 4770 | ** error occurs. |
| 4771 | */ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4772 | static int wherePathSolver(WhereInfo *pWInfo, double nRowEst){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4773 | const int mxChoice = 10; /* Maximum number of simultaneous paths tracked */ |
| 4774 | int nLoop; /* Number of terms in the join */ |
| 4775 | sqlite3 *db; /* The database connection */ |
| 4776 | int iLoop; /* Loop counter over the terms of the join */ |
| 4777 | int ii, jj; /* Loop counters */ |
| 4778 | double rCost; /* Cost of a path */ |
| 4779 | double mxCost; /* Maximum cost of a set of paths */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4780 | double rSortCost; /* Cost to do a sort */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4781 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 4782 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 4783 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 4784 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 4785 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 4786 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 4787 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 4788 | char *pSpace; /* Temporary memory used by this routine */ |
| 4789 | |
| 4790 | db = pWInfo->pParse->db; |
| 4791 | nLoop = pWInfo->nLevel; |
| 4792 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4793 | #ifdef WHERETRACE_ENABLED |
| 4794 | if( sqlite3WhereTrace>=2 ) sqlite3DebugPrintf("---- begin solver\n"); |
| 4795 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4796 | |
| 4797 | /* Allocate and initialize space for aTo and aFrom */ |
| 4798 | ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 4799 | pSpace = sqlite3DbMallocRaw(db, ii); |
| 4800 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 4801 | aTo = (WherePath*)pSpace; |
| 4802 | aFrom = aTo+mxChoice; |
| 4803 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 4804 | pX = (WhereLoop**)(aFrom+mxChoice); |
| 4805 | for(ii=0, pFrom=aTo; ii<mxChoice*2; ii++, pFrom++, pX += nLoop){ |
| 4806 | pFrom->aLoop = pX; |
| 4807 | } |
| 4808 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4809 | /* Seed the search with a single WherePath containing zero WhereLoops */ |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 4810 | aFrom[0].nRow = (double)1; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4811 | nFrom = 1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4812 | |
| 4813 | /* Precompute the cost of sorting the final result set, if the caller |
| 4814 | ** to sqlite3WhereBegin() was concerned about sorting */ |
| 4815 | rSortCost = (double)0; |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4816 | if( pWInfo->pOrderBy==0 || nRowEst<=0.0 ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4817 | aFrom[0].isOrderedValid = 1; |
| 4818 | }else{ |
| 4819 | /* Compute an estimate on the cost to sort the entire result set */ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4820 | rSortCost = nRowEst*estLog(nRowEst); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4821 | #ifdef WHERETRACE_ENABLED |
| 4822 | if( sqlite3WhereTrace>=2 ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4823 | sqlite3DebugPrintf("---- sort cost=%-7.2g\n", rSortCost); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4824 | } |
| 4825 | #endif |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | /* Compute successively longer WherePaths using the previous generation |
| 4829 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 4830 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4831 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 4832 | nTo = 0; |
| 4833 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 4834 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 4835 | Bitmask maskNew; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4836 | Bitmask revMask = 0; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4837 | u8 isOrderedValid = pFrom->isOrderedValid; |
| 4838 | u8 isOrdered = pFrom->isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4839 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 4840 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4841 | /* At this point, pWLoop is a candidate to be the next loop. |
| 4842 | ** Compute its cost */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4843 | rCost = pWLoop->rSetup + pWLoop->rRun*pFrom->nRow + pFrom->rCost; |
| 4844 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4845 | if( !isOrderedValid ){ |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 4846 | switch( wherePathSatisfiesOrderBy(pWInfo, pFrom, iLoop, iLoop==nLoop-1, |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4847 | pWLoop, &revMask) ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4848 | case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ |
| 4849 | isOrdered = 1; |
| 4850 | isOrderedValid = 1; |
| 4851 | break; |
| 4852 | case 0: /* No. pFrom+pWLoop will require a separate sort */ |
| 4853 | isOrdered = 0; |
| 4854 | isOrderedValid = 1; |
| 4855 | rCost += rSortCost; |
| 4856 | break; |
| 4857 | default: /* Cannot tell yet. Try again on the next iteration */ |
| 4858 | break; |
| 4859 | } |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 4860 | }else{ |
| 4861 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4862 | } |
| 4863 | /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 4864 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
| 4865 | if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){ |
| 4866 | break; |
| 4867 | } |
| 4868 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4869 | if( jj>=nTo ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4870 | if( nTo>=mxChoice && rCost>=mxCost ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4871 | #ifdef WHERETRACE_ENABLED |
| 4872 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4873 | sqlite3DebugPrintf("Skip %s cost=%-7.2g order=%c\n", |
| 4874 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 4875 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 4876 | } |
| 4877 | #endif |
| 4878 | continue; |
| 4879 | } |
| 4880 | /* Add a new Path to the aTo[] set */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4881 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4882 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4883 | jj = nTo++; |
| 4884 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4885 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4886 | for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4887 | } |
| 4888 | pTo = &aTo[jj]; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4889 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4890 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4891 | sqlite3DebugPrintf("New %s cost=%-7.2g order=%c\n", |
| 4892 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 4893 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 4894 | } |
| 4895 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 4896 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4897 | if( pTo->rCost<=rCost ){ |
| 4898 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4899 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4900 | sqlite3DebugPrintf( |
| 4901 | "Skip %s cost=%-7.2g order=%c", |
| 4902 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 4903 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 4904 | sqlite3DebugPrintf(" vs %s cost=%-7.2g order=%c\n", |
| 4905 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 4906 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 4907 | } |
| 4908 | #endif |
| 4909 | continue; |
| 4910 | } |
| 4911 | /* A new and better score for a previously created equivalent path */ |
| 4912 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4913 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4914 | sqlite3DebugPrintf( |
| 4915 | "Update %s cost=%-7.2g order=%c", |
| 4916 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 4917 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 4918 | sqlite3DebugPrintf(" was %s cost=%-7.2g order=%c\n", |
| 4919 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 4920 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 4921 | } |
| 4922 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4923 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4924 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4925 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4926 | pTo->revLoop = revMask; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4927 | pTo->nRow = pFrom->nRow * pWLoop->nOut; |
| 4928 | pTo->rCost = rCost; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4929 | pTo->isOrderedValid = isOrderedValid; |
| 4930 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4931 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 4932 | pTo->aLoop[iLoop] = pWLoop; |
| 4933 | if( nTo>=mxChoice ){ |
| 4934 | mxCost = aTo[0].rCost; |
| 4935 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
| 4936 | if( pTo->rCost>mxCost ) mxCost = pTo->rCost; |
| 4937 | } |
| 4938 | } |
| 4939 | } |
| 4940 | } |
| 4941 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4942 | #ifdef WHERETRACE_ENABLED |
| 4943 | if( sqlite3WhereTrace>=2 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4944 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4945 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4946 | sqlite3DebugPrintf(" %s cost=%-7.2g nrow=%-7.2g order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4947 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4948 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4949 | if( pTo->isOrderedValid && pTo->isOrdered ){ |
| 4950 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 4951 | }else{ |
| 4952 | sqlite3DebugPrintf("\n"); |
| 4953 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 4954 | } |
| 4955 | } |
| 4956 | #endif |
| 4957 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4958 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4959 | pFrom = aTo; |
| 4960 | aTo = aFrom; |
| 4961 | aFrom = pFrom; |
| 4962 | nFrom = nTo; |
| 4963 | } |
| 4964 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 4965 | if( nFrom==0 ){ |
| 4966 | sqlite3ErrorMsg(pWInfo->pParse, "no query solution"); |
| 4967 | sqlite3DbFree(db, pSpace); |
| 4968 | return SQLITE_ERROR; |
| 4969 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4970 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4971 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4972 | pFrom = aFrom; |
| 4973 | for(ii=1; ii<nFrom; ii++){ |
| 4974 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 4975 | } |
| 4976 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4977 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4978 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 4979 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 4980 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
| 4981 | pLevel->iFrom = pWLoop->iTab; /* FIXME: Omit the iFrom field */ |
| 4982 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4983 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4984 | if( pFrom->isOrdered ){ |
| 4985 | pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4986 | pWInfo->revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4987 | } |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 4988 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4989 | |
| 4990 | /* Free temporary memory and return success */ |
| 4991 | sqlite3DbFree(db, pSpace); |
| 4992 | return SQLITE_OK; |
| 4993 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4994 | |
| 4995 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 4996 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 4997 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4998 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4999 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5000 | ** in order to complete the WHERE clause processing. |
| 5001 | ** |
| 5002 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5003 | ** |
| 5004 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5005 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5006 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5007 | ** example, if the SQL is this: |
| 5008 | ** |
| 5009 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5010 | ** |
| 5011 | ** Then the code generated is conceptually like the following: |
| 5012 | ** |
| 5013 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5014 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5015 | ** foreach row3 in t3 do / |
| 5016 | ** ... |
| 5017 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5018 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5019 | ** end / |
| 5020 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5021 | ** Note that the loops might not be nested in the order in which they |
| 5022 | ** 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] | 5023 | ** use of indices. Note also that when the IN operator appears in |
| 5024 | ** the WHERE clause, it might result in additional nested loops for |
| 5025 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5026 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5027 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5028 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5029 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5030 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5031 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5032 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5033 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5034 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5035 | ** data from the various tables of the loop. |
| 5036 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5037 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5038 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5039 | ** the tables have indices and there are terms in the WHERE clause that |
| 5040 | ** refer to those indices, a complete table scan can be avoided and the |
| 5041 | ** code will run much faster. Most of the work of this routine is checking |
| 5042 | ** to see if there are indices that can be used to speed up the loop. |
| 5043 | ** |
| 5044 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 5045 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 5046 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 5047 | ** loops are evaluated and if false a jump is made around all subsequent |
| 5048 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 5049 | ** most loop) |
| 5050 | ** |
| 5051 | ** OUTER JOINS |
| 5052 | ** |
| 5053 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 5054 | ** |
| 5055 | ** foreach row1 in t1 do |
| 5056 | ** flag = 0 |
| 5057 | ** foreach row2 in t2 do |
| 5058 | ** start: |
| 5059 | ** ... |
| 5060 | ** flag = 1 |
| 5061 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5062 | ** if flag==0 then |
| 5063 | ** move the row2 cursor to a null row |
| 5064 | ** goto start |
| 5065 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5066 | ** end |
| 5067 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5068 | ** ORDER BY CLAUSE PROCESSING |
| 5069 | ** |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5070 | ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5071 | ** 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] | 5072 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5073 | ** |
| 5074 | ** If an index can be used so that the natural output order of the table |
| 5075 | ** 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] | 5076 | ** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr. This |
| 5077 | ** is an optimization that prevents an unnecessary sort of the result set |
| 5078 | ** if an index appropriate for the ORDER BY clause already exists. |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5079 | ** |
| 5080 | ** If the where clause loops cannot be arranged to provide the correct |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5081 | ** output order, then WhereInfo.nOBSat is 0. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5082 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5083 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5084 | Parse *pParse, /* The parser context */ |
| 5085 | SrcList *pTabList, /* A list of all tables to be scanned */ |
| 5086 | Expr *pWhere, /* The WHERE clause */ |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5087 | ExprList *pOrderBy, /* An ORDER BY clause, or NULL */ |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5088 | ExprList *pDistinct, /* The select-list for DISTINCT queries - or NULL */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 5089 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 5090 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5091 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5092 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5093 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5094 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 5095 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5096 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5097 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5098 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5099 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5100 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5101 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5102 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5103 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5104 | |
| 5105 | /* Variable initialization */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5106 | memset(&sWLB, 0, sizeof(sWLB)); |
| 5107 | sWLB.pParse = pParse; |
| 5108 | sWLB.db = pParse->db; |
| 5109 | sWLB.pTabList = pTabList; |
| 5110 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5111 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5112 | /* 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] | 5113 | ** bits in a Bitmask |
| 5114 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5115 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5116 | if( pTabList->nSrc>BMS ){ |
| 5117 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5118 | return 0; |
| 5119 | } |
| 5120 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5121 | /* This function normally generates a nested loop for all tables in |
| 5122 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 5123 | ** only generate code for the first table in pTabList and assume that |
| 5124 | ** any cursors associated with subsequent tables are uninitialized. |
| 5125 | */ |
| 5126 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 5127 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5128 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5129 | ** return value. A single allocation is used to store the WhereInfo |
| 5130 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 5131 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 5132 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 5133 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5134 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5135 | db = pParse->db; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5136 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5137 | pWInfo = sqlite3DbMallocZero(db, |
| 5138 | nByteWInfo + |
| 5139 | sizeof(WhereClause) + |
| 5140 | sizeof(WhereMaskSet) |
| 5141 | ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5142 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5143 | sqlite3DbFree(db, pWInfo); |
| 5144 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5145 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5146 | } |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5147 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5148 | pWInfo->pParse = pParse; |
| 5149 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5150 | pWInfo->pOrderBy = pOrderBy; |
| 5151 | pWInfo->pDistinct = pDistinct; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5152 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
drh | 8030dc7 | 2013-05-31 15:50:39 +0000 | [diff] [blame] | 5153 | pWInfo->pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5154 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5155 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 8030dc7 | 2013-05-31 15:50:39 +0000 | [diff] [blame] | 5156 | pMaskSet = (WhereMaskSet*)&pWInfo->pWC[1]; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5157 | sWLB.pWInfo = pWInfo; |
| 5158 | sWLB.pWC = pWInfo->pWC; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5159 | |
drh | a9b1b91 | 2011-07-08 13:07:02 +0000 | [diff] [blame] | 5160 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 5161 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
drh | 7e5418e | 2012-09-27 15:05:54 +0000 | [diff] [blame] | 5162 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0; |
drh | a9b1b91 | 2011-07-08 13:07:02 +0000 | [diff] [blame] | 5163 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5164 | /* Split the WHERE clause into separate subexpressions where each |
| 5165 | ** subexpression is separated by an AND operator. |
| 5166 | */ |
| 5167 | initMaskSet(pMaskSet); |
drh | 8030dc7 | 2013-05-31 15:50:39 +0000 | [diff] [blame] | 5168 | whereClauseInit(pWInfo->pWC, pParse, pMaskSet, wctrlFlags); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5169 | sqlite3ExprCodeConstants(pParse, pWhere); |
drh | 8030dc7 | 2013-05-31 15:50:39 +0000 | [diff] [blame] | 5170 | whereSplit(pWInfo->pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5171 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5172 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 5173 | ** expression and either jump over all of the code or fall thru. |
| 5174 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5175 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 5176 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 5177 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5178 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5179 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5180 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 5181 | ** |
| 5182 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 5183 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 5184 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 5185 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 5186 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 5187 | ** of the join. Subtracting one from the right table bitmask gives a |
| 5188 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 5189 | ** 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] | 5190 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5191 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 5192 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 5193 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 5194 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5195 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5196 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5197 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5198 | } |
| 5199 | #ifndef NDEBUG |
| 5200 | { |
| 5201 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5202 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5203 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5204 | assert( (m-1)==toTheLeft ); |
| 5205 | toTheLeft |= m; |
| 5206 | } |
| 5207 | } |
| 5208 | #endif |
| 5209 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5210 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 5211 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 5212 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 5213 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5214 | */ |
drh | 8030dc7 | 2013-05-31 15:50:39 +0000 | [diff] [blame] | 5215 | exprAnalyzeAll(pTabList, pWInfo->pWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5216 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5217 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 5218 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5219 | |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5220 | /* Check if the DISTINCT qualifier, if there is one, is redundant. |
| 5221 | ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to |
| 5222 | ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT. |
| 5223 | */ |
drh | 8030dc7 | 2013-05-31 15:50:39 +0000 | [diff] [blame] | 5224 | if( pDistinct && isDistinctRedundant(pParse,pTabList,pWInfo->pWC,pDistinct) ){ |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5225 | pDistinct = 0; |
| 5226 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5227 | } |
| 5228 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5229 | /* Construct the WhereLoop objects */ |
| 5230 | WHERETRACE(("*** Optimizer Start ***\n")); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5231 | rc = whereLoopAddAll(&sWLB); |
| 5232 | if( rc ) goto whereBeginError; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5233 | |
| 5234 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5235 | #ifdef WHERETRACE_ENABLED |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5236 | if( sqlite3WhereTrace ){ |
| 5237 | WhereLoop *p; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5238 | int i = 0; |
| 5239 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 5240 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5241 | for(p=pWInfo->pLoops; p; p=p->pNextLoop){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5242 | p->cId = zLabel[(i++)%sizeof(zLabel)]; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5243 | whereLoopPrint(p, pTabList); |
| 5244 | } |
| 5245 | } |
| 5246 | #endif |
| 5247 | |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5248 | wherePathSolver(pWInfo, -1); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5249 | if( db->mallocFailed ) goto whereBeginError; |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5250 | if( pWInfo->pOrderBy ){ |
| 5251 | wherePathSolver(pWInfo, pWInfo->nRowOut); |
| 5252 | if( db->mallocFailed ) goto whereBeginError; |
| 5253 | } |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5254 | if( pParse->nErr || db->mallocFailed ){ |
| 5255 | goto whereBeginError; |
| 5256 | } |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5257 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5258 | if( sqlite3WhereTrace ){ |
| 5259 | int ii; |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5260 | sqlite3DebugPrintf("---- Solution"); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5261 | if( pWInfo->nOBSat ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5262 | sqlite3DebugPrintf(" ORDER BY omitted rev=0x%llx\n", pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5263 | }else{ |
| 5264 | sqlite3DebugPrintf("\n"); |
| 5265 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5266 | for(ii=0; ii<nTabList; ii++){ |
| 5267 | whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5268 | } |
| 5269 | } |
| 5270 | #endif |
drh | 0edc94d | 2013-05-31 19:14:56 +0000 | [diff] [blame] | 5271 | WHERETRACE(("*** Optimizer Finished ***\n")); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5272 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5273 | #if 0 /* FIXME: Add this back in? */ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5274 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 5275 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 5276 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 5277 | ** the statement to update a single row. |
| 5278 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 5279 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5280 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5281 | pWInfo->okOnePass = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5282 | pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5283 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5284 | #endif |
| 5285 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5286 | /* Open all tables in the pTabList and any indices selected for |
| 5287 | ** searching those tables. |
| 5288 | */ |
| 5289 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5290 | notReady = ~(Bitmask)0; |
drh | 95aa47b | 2010-11-16 02:49:15 +0000 | [diff] [blame] | 5291 | pWInfo->nRowOut = (double)1; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5292 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5293 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5294 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5295 | struct SrcList_item *pTabItem; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5296 | WhereLoop *pLoop; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5297 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5298 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5299 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5300 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5301 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 5302 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 5303 | /* Do nothing */ |
| 5304 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5305 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5306 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5307 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 5308 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5309 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 5310 | }else if( IsVirtual(pTab) ){ |
| 5311 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5312 | }else |
| 5313 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5314 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5315 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5316 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 5317 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5318 | testcase( pTab->nCol==BMS-1 ); |
| 5319 | testcase( pTab->nCol==BMS ); |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 5320 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5321 | Bitmask b = pTabItem->colUsed; |
| 5322 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5323 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5324 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 5325 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5326 | assert( n<=pTab->nCol ); |
| 5327 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5328 | }else{ |
| 5329 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5330 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5331 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5332 | if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
drh | 8030dc7 | 2013-05-31 15:50:39 +0000 | [diff] [blame] | 5333 | constructAutomaticIndex(pParse, pWInfo->pWC, pTabItem, notReady, pLevel); |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5334 | }else |
| 5335 | #endif |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 5336 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5337 | Index *pIx = pLoop->u.btree.pIndex; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5338 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5339 | /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */ |
| 5340 | int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5341 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 5342 | assert( iIndexCur>=0 ); |
| 5343 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5344 | (char*)pKey, P4_KEYINFO_HANDOFF); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 5345 | VdbeComment((v, "%s", pIx->zName)); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5346 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5347 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 8030dc7 | 2013-05-31 15:50:39 +0000 | [diff] [blame] | 5348 | notReady &= ~getMask(pWInfo->pWC->pMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5349 | } |
| 5350 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 5351 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5352 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5353 | /* Generate the code to do the search. Each iteration of the for |
| 5354 | ** loop below generates code for a single nested loop of the VM |
| 5355 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5356 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5357 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5358 | for(ii=0; ii<nTabList; ii++){ |
| 5359 | pLevel = &pWInfo->a[ii]; |
| 5360 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
| 5361 | notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 5362 | pWInfo->iContinue = pLevel->addrCont; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5363 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 5364 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 5365 | /* Done. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5366 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5367 | |
| 5368 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5369 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5370 | if( pWInfo ){ |
| 5371 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 5372 | whereInfoFree(db, pWInfo); |
| 5373 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5374 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5378 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5379 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5380 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5381 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5382 | Parse *pParse = pWInfo->pParse; |
| 5383 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5384 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5385 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5386 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 5387 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5388 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5389 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5390 | /* Generate loop termination code. |
| 5391 | */ |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 5392 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5393 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5394 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5395 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5396 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5397 | if( pLevel->op!=OP_Noop ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5398 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 5399 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5400 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5401 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 5402 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5403 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5404 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5405 | 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] | 5406 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 5407 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5408 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5409 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5410 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 5411 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5412 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5413 | if( pLevel->iLeftJoin ){ |
| 5414 | int addr; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5415 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5416 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 5417 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 5418 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 5419 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 5420 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 5421 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5422 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 5423 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 5424 | if( pLevel->op==OP_Return ){ |
| 5425 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 5426 | }else{ |
| 5427 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 5428 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 5429 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5430 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5431 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5432 | |
| 5433 | /* The "break" point is here, just past the end of the outer loop. |
| 5434 | ** Set it. |
| 5435 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5436 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5437 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5438 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5439 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5440 | assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc ); |
| 5441 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 5442 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5443 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5444 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 5445 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5446 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5447 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 5448 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5449 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5450 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5451 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5452 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5453 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 5454 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5455 | if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_TEMP_INDEX))==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5456 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 5457 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
danielk1977 | 21de2e7 | 2007-11-29 17:43:27 +0000 | [diff] [blame] | 5460 | /* If this scan uses an index, make code substitutions to read data |
| 5461 | ** from the index in preference to the table. Sometimes, this means |
| 5462 | ** the table need never be read from. This is a performance boost, |
| 5463 | ** as the vdbe level waits until the table is read before actually |
| 5464 | ** seeking the table cursor to the record corresponding to the current |
| 5465 | ** position in the index. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5466 | ** |
| 5467 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 5468 | ** sqlite3WhereEnd will have created code that references the table |
| 5469 | ** directly. This loop scans all that code looking for opcodes |
| 5470 | ** that reference the table and converts them into opcodes that |
| 5471 | ** reference the index. |
| 5472 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5473 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 5474 | pIdx = pLoop->u.btree.pIndex; |
| 5475 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 5476 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 5477 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5478 | if( pIdx && !db->mallocFailed ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 5479 | int k, j, last; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5480 | VdbeOp *pOp; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5481 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5482 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 5483 | last = sqlite3VdbeCurrentAddr(v); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 5484 | for(k=pWInfo->iTop; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5485 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 5486 | if( pOp->opcode==OP_Column ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5487 | for(j=0; j<pIdx->nColumn; j++){ |
| 5488 | if( pOp->p2==pIdx->aiColumn[j] ){ |
| 5489 | pOp->p2 = j; |
danielk1977 | 21de2e7 | 2007-11-29 17:43:27 +0000 | [diff] [blame] | 5490 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5491 | break; |
| 5492 | } |
| 5493 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5494 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5495 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5496 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5497 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5498 | } |
| 5499 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5500 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5501 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5502 | |
| 5503 | /* Final cleanup |
| 5504 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 5505 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 5506 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5507 | return; |
| 5508 | } |